fix modexp test

This commit is contained in:
Nicholas Ward 2023-03-28 11:15:01 -07:00
parent 7028b6ba74
commit 91fb4fc02e
5 changed files with 31 additions and 28 deletions

View File

@ -3,8 +3,8 @@
// Stores a * b % m in output_loc, leaving a, b, and m unchanged. // Stores a * b % m in output_loc, leaving a, b, and m unchanged.
// a, b, and m must have the same length. // a, b, and m must have the same length.
// Both output_loc and scratch_1 must have size length. // output_loc must have size length; scratch_2 must have size 2*length.
// Both scratch_2 and scratch_3 have size 2 * length and be initialized with zeroes. // Both scratch_2 and scratch_3 have size 2*length and be initialized with zeroes.
global modmul_bignum: global modmul_bignum:
// stack: len, a_loc, b_loc, m_loc, out_loc, s1 (=scratch_1), s2, s3, retdest // stack: len, a_loc, b_loc, m_loc, out_loc, s1 (=scratch_1), s2, s3, retdest
DUP1 DUP1
@ -39,28 +39,32 @@ modmul_remainder_loop:
// The prover provides k := (a * b) / m, which we store in scratch_1. // The prover provides k := (a * b) / m, which we store in scratch_1.
// stack: len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
DUP1
// stack: len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
%mul_const(2)
// stack: 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
PUSH 0 PUSH 0
// stack: i=0, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: i=0, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
modmul_quotient_loop: modmul_quotient_loop:
// stack: i, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: i, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
PROVER_INPUT(bignum_modmul) PROVER_INPUT(bignum_modmul)
// stack: PI, i, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: PI, i, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
DUP8 DUP9
DUP3 DUP3
ADD ADD
// stack: s1[i], PI, i, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: s1[i], PI, i, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
%mstore_kernel_general %mstore_kernel_general
// stack: i, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: i, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
%increment %increment
DUP2 DUP2
DUP2 DUP2
// stack: i+1, len, i+1, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: i+1, 2*len, i+1, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
SUB // functions as NEQ SUB // functions as NEQ
// stack: i+1!=len, i+1, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: i+1!=2*len, i+1, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
%jumpi(modmul_quotient_loop) %jumpi(modmul_quotient_loop)
// end of modmul_quotient_loop // end of modmul_quotient_loop
// stack: i, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: i, 2*len, len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
POP %pop2
// stack: len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest // stack: len, a_loc, b_loc, m_loc, out_loc, s1, s2, s3, retdest
// Verification step 1: calculate x + k * m. // Verification step 1: calculate x + k * m.

View File

@ -375,7 +375,11 @@ impl<'a> Interpreter<'a> {
0xa2 => todo!(), // "LOG2", 0xa2 => todo!(), // "LOG2",
0xa3 => todo!(), // "LOG3", 0xa3 => todo!(), // "LOG3",
0xa4 => todo!(), // "LOG4", 0xa4 => todo!(), // "LOG4",
0xa5 => bail!("Executed PANIC, stack={:?}", self.stack()), // "PANIC", 0xa5 => bail!(
"Executed PANIC, stack={:?}, memory={:?}",
self.stack(),
self.get_kernel_general_memory()
), // "PANIC",
0xf0 => todo!(), // "CREATE", 0xf0 => todo!(), // "CREATE",
0xf1 => todo!(), // "CALL", 0xf1 => todo!(), // "CALL",
0xf2 => todo!(), // "CALLCODE", 0xf2 => todo!(), // "CALLCODE",

View File

@ -28,7 +28,7 @@ const TEST_DATA_ADD_OUTPUTS: &str = "add_outputs";
const TEST_DATA_ADDMUL_OUTPUTS: &str = "addmul_outputs"; const TEST_DATA_ADDMUL_OUTPUTS: &str = "addmul_outputs";
const TEST_DATA_MUL_OUTPUTS: &str = "mul_outputs"; const TEST_DATA_MUL_OUTPUTS: &str = "mul_outputs";
const TEST_DATA_MODMUL_OUTPUTS: &str = "modmul_outputs"; const TEST_DATA_MODMUL_OUTPUTS: &str = "modmul_outputs";
const TEST_DATA_MODEXP_OUTPUTS: &str = "modexp_outputs"; // const TEST_DATA_MODEXP_OUTPUTS: &str = "modexp_outputs";
const BIT_SIZES_TO_TEST: [usize; 15] = [ const BIT_SIZES_TO_TEST: [usize; 15] = [
0, 1, 2, 127, 128, 129, 255, 256, 257, 512, 1000, 1023, 1024, 1025, 31415, 0, 1, 2, 127, 128, 129, 255, 256, 257, 512, 1000, 1023, 1024, 1025, 31415,
@ -244,9 +244,9 @@ fn test_modmul_bignum(a: BigUint, b: BigUint, m: BigUint, expected_output: BigUi
let b_start_loc = len; let b_start_loc = len;
let m_start_loc = 2 * len; let m_start_loc = 2 * len;
let output_start_loc = 3 * len; let output_start_loc = 3 * len;
let scratch_1 = 4 * len; let scratch_1 = 4 * len; // size 2*len
let scratch_2 = 5 * len; // size 2*len let scratch_2 = 6 * len; // size 2*len
let scratch_3 = 7 * len; // size 2*len let scratch_3 = 8 * len; // size 2*len
let (new_memory, _new_stack) = run_test( let (new_memory, _new_stack) = run_test(
"modmul_bignum", "modmul_bignum",
memory, memory,
@ -509,14 +509,13 @@ fn test_modexp_bignum_all() -> Result<()> {
// Only test smaller values for exponent. // Only test smaller values for exponent.
let exp_bit_sizes = vec![2, 100, 127, 128, 129]; let exp_bit_sizes = vec![2, 100, 127, 128, 129];
for bit_size in &BIT_SIZES_TO_TEST[3..] { for bit_size in &BIT_SIZES_TO_TEST[3..14] {
for exp_bit_size in &exp_bit_sizes { for exp_bit_size in &exp_bit_sizes {
let b = gen_bignum(*bit_size); let b = gen_bignum(*bit_size);
let e = gen_bignum(*exp_bit_size); let e = gen_bignum(*exp_bit_size);
let m = gen_bignum(*bit_size); let m = gen_bignum(*bit_size);
if !m.is_zero() { if !m.is_zero() {
let output = b.clone().modpow(&e, &m); let output = b.clone().modpow(&e, &m);
dbg!(b.clone(), e.clone(), m.clone(), output.clone());
test_modexp_bignum(b, e, m, output)?; test_modexp_bignum(b, e, m, output)?;
} }
@ -525,7 +524,6 @@ fn test_modexp_bignum_all() -> Result<()> {
let m = max_bignum(*bit_size); let m = max_bignum(*bit_size);
if !m.is_zero() { if !m.is_zero() {
let output = b.clone().modpow(&e, &m); let output = b.clone().modpow(&e, &m);
dbg!(b.clone(), e.clone(), m.clone(), output.clone());
test_modexp_bignum(b, e, m, output)?; test_modexp_bignum(b, e, m, output)?;
} }
} }

View File

@ -154,15 +154,12 @@ impl<F: Field> GenerationState<F> {
let (remainder, quotient) = let (remainder, quotient) =
self.bignum_modmul(len, a_start_loc, b_start_loc, m_start_loc); self.bignum_modmul(len, a_start_loc, b_start_loc, m_start_loc);
dbg!(remainder.clone(), quotient.clone());
self.bignum_modmul_result_limbs = remainder self.bignum_modmul_result_limbs = remainder
.iter() .iter()
.cloned() .cloned()
.pad_using(len, |_| 0.into()) .pad_using(len, |_| 0.into())
.chain(quotient.iter().cloned().pad_using(len, |_| 0.into())) .chain(quotient.iter().cloned().pad_using(2 * len, |_| 0.into()))
.collect(); .collect();
dbg!(self.bignum_modmul_result_limbs.clone());
self.bignum_modmul_result_limbs.reverse(); self.bignum_modmul_result_limbs.reverse();
} }
@ -187,11 +184,10 @@ impl<F: Field> GenerationState<F> {
let b_biguint = mem_vec_to_biguint(b); let b_biguint = mem_vec_to_biguint(b);
let m_biguint = mem_vec_to_biguint(m); let m_biguint = mem_vec_to_biguint(m);
dbg!(a_biguint.clone(), b_biguint.clone(), m_biguint.clone());
let prod = a_biguint * b_biguint; let prod = a_biguint * b_biguint;
let quo = prod.clone() / m_biguint.clone(); let quo = prod.clone() / m_biguint.clone();
let rem = prod - quo.clone() * m_biguint; let rem = prod - quo.clone() * m_biguint;
(biguint_to_mem_vec(rem), biguint_to_mem_vec(quo)) (biguint_to_mem_vec(rem), biguint_to_mem_vec(quo))
} }
} }

View File

@ -309,10 +309,11 @@ pub(crate) fn transition<F: Field>(state: &mut GenerationState<F>) -> anyhow::Re
if state.registers.is_kernel { if state.registers.is_kernel {
let offset_name = KERNEL.offset_name(state.registers.program_counter); let offset_name = KERNEL.offset_name(state.registers.program_counter);
bail!( bail!(
"{:?} in kernel at pc={}, stack={:?}", "{:?} in kernel at pc={}, stack={:?}, memory={:?}",
e, e,
offset_name, offset_name,
state.stack() state.stack(),
state.memory.contexts[0].segments[Segment::KernelGeneral as usize].content,
); );
} }
state.rollback(checkpoint); state.rollback(checkpoint);