This commit is contained in:
Nicholas Ward 2023-03-15 23:05:44 -07:00
parent 534395ee4d
commit 4e736b63b8
2 changed files with 14 additions and 12 deletions

View File

@ -29,25 +29,22 @@ add_loop:
// stack: (a[cur] + b[cur] + carry) // 2^128, a[cur] + b[cur] + carry, i, a_cur_loc, b_cur_loc, retdest
SWAP1
// stack: a[cur] + b[cur] + carry, (a[cur] + b[cur] + carry) // 2^128, i, a_cur_loc, b_cur_loc, retdest
%shl_const(128)
%shr_const(128)
%mod_const(0x100000000000000000000000000000000)
// stack: c[cur] = (a[cur] + b[cur] + carry) % 2^128, carry_new = (a[cur] + b[cur] + carry) // 2^128, i, a_cur_loc, b_cur_loc, retdest
DUP4
// stack: a_cur_loc, c[cur], carry_new, i, a_cur_loc, b_cur_loc, retdest
%mstore_kernel_general
// stack: carry_new, i, a_cur_loc, b_cur_loc, retdest
%stack (c, i, a, b) -> (a, b, c, i)
// stack: a_cur_loc, b_cur_loc, carry_new, i, retdest
SWAP2
%increment
// stack: a_cur_loc + 1, b_cur_loc, carry_new, i, retdest
SWAP2
// stack: carry_new, i, a_cur_loc + 1, b_cur_loc, retdest
SWAP3
%increment
SWAP3
// stack: carry_new, i, a_cur_loc + 1, b_cur_loc + 1, retdest
SWAP1
// stack: b_cur_loc, a_cur_loc + 1, carry_new, i, retdest
%increment
// stack: b_cur_loc + 1, a_cur_loc + 1, carry_new, i, retdest
%stack (b, a, c, i) -> (i, c, a, b)
// stack: i, carry_new, a_cur_loc + 1, b_cur_loc + 1, retdest
%decrement
// stack: i - 1, carry_new, a_cur_loc + 1, b_cur_loc + 1, retdest
SWAP1
// stack: carry_new, i - 1, a_cur_loc + 1, b_cur_loc + 1, retdest
DUP2

View File

@ -20,6 +20,8 @@ fn test_data() -> Vec<BigUint> {
0u128.into(),
1u128.into(),
u128::MAX.into(),
// Generated by GMP's mpz_rrandomb for bit lengths 5, 10, 100, 255, 256, 257, 500, 511, 512, 513, 1000, and 1500.
21u128.into(),
908u128.into(),
1267650597867046177654064545792u128.into(),
@ -266,6 +268,7 @@ where
F: Fn(usize) -> (BigUint, BigUint, U256, Vec<U256>),
{
let (a, b, length, memory) = prepare_two_bignums_fn(1000);
let len: usize = length.try_into().unwrap();
// Determine expected sum.
let sum = a + b;
@ -288,7 +291,9 @@ where
interpreter.run()?;
// Determine actual sum.
let new_memory = interpreter.get_kernel_general_memory();
let carry_limb = interpreter.stack()[0];
let mut new_memory = interpreter.get_kernel_general_memory();
new_memory[len] = carry_limb;
let actual_sum: Vec<_> = new_memory[..expected_sum.len()].into();
// Compare.