deal with and test zero-len case

This commit is contained in:
Nicholas Ward 2023-03-14 15:33:36 -07:00
parent 4b6a51469f
commit d23e4e20b6
5 changed files with 48 additions and 2 deletions

View File

@ -4,6 +4,11 @@
// Adds two bignums of the same given length. Assumes that len > 0. // Adds two bignums of the same given length. Assumes that len > 0.
// Replaces a with a + b, leaving b unchanged, and returns the final carry. // Replaces a with a + b, leaving b unchanged, and returns the final carry.
global add_bignum: global add_bignum:
// stack: len, a_start_loc, b_start_loc, retdest
DUP1
// stack: len, len, a_start_loc, b_start_loc, retdest
ISZERO
%jumpi(len_zero)
// stack: len, a_start_loc, b_start_loc, retdest // stack: len, a_start_loc, b_start_loc, retdest
PUSH 0 PUSH 0
// stack: carry=0, i=len, a_cur_loc=a_start_loc, b_cur_loc=b_start_loc, retdest // stack: carry=0, i=len, a_cur_loc=a_start_loc, b_cur_loc=b_start_loc, retdest
@ -55,3 +60,11 @@ add_end:
SWAP1 SWAP1
// stack: retdest, carry_new // stack: retdest, carry_new
JUMP JUMP
len_zero:
// stack: len, a_start_loc, b_start_loc, retdest
%pop3
// stack: retdest
PUSH 0
// stack: carry=0, retdest
SWAP1
JUMP

View File

@ -4,6 +4,10 @@
// Sets a[0:len] += b[0:len] * val, and returns the carry. // Sets a[0:len] += b[0:len] * val, and returns the carry.
global addmul_bignum: global addmul_bignum:
// stack: len, a_start_loc, b_start_loc, val, retdest // stack: len, a_start_loc, b_start_loc, val, retdest
DUP1
// stack: len, len, a_start_loc, b_start_loc, val, retdest
ISZERO
%jumpi(len_zero)
PUSH 0 PUSH 0
// stack: carry=0, i=len, a_cur_loc=a_start_loc, b_cur_loc=b_start_loc, val, retdest // stack: carry=0, i=len, a_cur_loc=a_start_loc, b_cur_loc=b_start_loc, val, retdest
addmul_loop: addmul_loop:
@ -97,3 +101,11 @@ addmul_end:
SWAP1 SWAP1
// stack: retdest, carry_new // stack: retdest, carry_new
JUMP JUMP
len_zero:
// stack: len, a_start_loc, b_start_loc, val, retdest
%pop4
// stack: retdest
PUSH 0
// stack: carry=0, retdest
SWAP1
JUMP

View File

@ -7,6 +7,10 @@
global mul_bignum: global mul_bignum:
// stack: len, a_start_loc, b_start_loc, output_loc, retdest // stack: len, a_start_loc, b_start_loc, output_loc, retdest
DUP1 DUP1
// stack: len, len, a_start_loc, b_start_loc, output_loc, retdest
ISZERO
%jumpi(len_zero)
DUP1
// stack: n=len, len, a_start_loc, bi=b_start_loc, output_cur=output_loc, retdest // stack: n=len, len, a_start_loc, bi=b_start_loc, output_cur=output_loc, retdest
mul_loop: mul_loop:
// stack: n, len, a_start_loc, bi, output_cur, retdest // stack: n, len, a_start_loc, bi, output_cur, retdest
@ -51,3 +55,8 @@ mul_end:
%pop5 %pop5
// stack: retdest // stack: retdest
JUMP JUMP
len_zero:
// stack: len, a_start_loc, b_start_loc, output_loc, retdest
%pop4
// stack: retdest
JUMP

View File

@ -8,7 +8,7 @@ global shr_bignum:
DUP1 DUP1
// stack: len, len, start_loc, retdest // stack: len, len, start_loc, retdest
ISZERO ISZERO
%jumpi(shr_end_len_zero) %jumpi(len_zero)
// stack: len, start_loc, retdest // stack: len, start_loc, retdest
DUP2 DUP2
// stack: start_loc, len, start_loc, retdest // stack: start_loc, len, start_loc, retdest
@ -60,7 +60,7 @@ shr_end:
%pop3 %pop3
// stack: retdest // stack: retdest
JUMP JUMP
shr_end_len_zero: len_zero:
// stack: len, start_loc, retdest // stack: len, start_loc, retdest
%pop2 %pop2
// stack: retdest // stack: retdest

View File

@ -104,6 +104,15 @@ fn prepare_two_bignums_diff(bit_size: usize) -> (BigUint, BigUint, U256, Vec<U25
(a, b, length, memory) (a, b, length, memory)
} }
fn prepare_two_bignums_zero(_bit_size: usize) -> (BigUint, BigUint, U256, Vec<U256>) {
let a = BigUint::zero();
let b = BigUint::zero();
let length: U256 = bignum_len(&a).into();
let memory = pad_bignums(&[a.clone(), b.clone()], length.try_into().unwrap());
(a, b, length, memory)
}
fn test_shr_bignum<F>(prepare_bignum_fn: &F) -> Result<()> fn test_shr_bignum<F>(prepare_bignum_fn: &F) -> Result<()>
where where
F: Fn(usize) -> (BigUint, U256, Vec<U256>), F: Fn(usize) -> (BigUint, U256, Vec<U256>),
@ -356,6 +365,7 @@ fn test_add_bignum_all() -> Result<()> {
test_add_bignum(&prepare_two_bignums_max)?; test_add_bignum(&prepare_two_bignums_max)?;
test_add_bignum(&prepare_two_bignums_min)?; test_add_bignum(&prepare_two_bignums_min)?;
test_add_bignum(&prepare_two_bignums_diff)?; test_add_bignum(&prepare_two_bignums_diff)?;
test_add_bignum(&prepare_two_bignums_zero)?;
Ok(()) Ok(())
} }
@ -366,6 +376,7 @@ fn test_addmul_bignum_all() -> Result<()> {
test_addmul_bignum(&prepare_two_bignums_max)?; test_addmul_bignum(&prepare_two_bignums_max)?;
test_addmul_bignum(&prepare_two_bignums_min)?; test_addmul_bignum(&prepare_two_bignums_min)?;
test_addmul_bignum(&prepare_two_bignums_diff)?; test_addmul_bignum(&prepare_two_bignums_diff)?;
test_addmul_bignum(&prepare_two_bignums_zero)?;
Ok(()) Ok(())
} }
@ -376,6 +387,7 @@ fn test_mul_bignum_all() -> Result<()> {
test_mul_bignum(&prepare_two_bignums_max)?; test_mul_bignum(&prepare_two_bignums_max)?;
test_mul_bignum(&prepare_two_bignums_min)?; test_mul_bignum(&prepare_two_bignums_min)?;
test_mul_bignum(&prepare_two_bignums_diff)?; test_mul_bignum(&prepare_two_bignums_diff)?;
test_mul_bignum(&prepare_two_bignums_zero)?;
Ok(()) Ok(())
} }