diff --git a/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/pairing.asm b/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/pairing.asm index 56f5f243..829ceff2 100644 --- a/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/pairing.asm +++ b/evm/src/cpu/kernel/asm/curve/bn254/curve_arithmetic/pairing.asm @@ -4,7 +4,7 @@ /// out *= miller_loop(P, Q) /// return bn254_final_exponent(out) -global bn254_tate: +global bn254_pairing: // stack: k , inp, out, retdest DUP1 ISZERO @@ -14,9 +14,9 @@ global bn254_tate: %sub_const(1) // stack: k=k-1, inp, out, retdest - %stack (k, inp, out) -> (k, inp, 200, mul_fp254_12, 200, out, out, bn254_tate, k, inp, out) - // stack: k, inp, 200, mul_fp254_12, 200, out, out, bn254_tate, k, inp, out retdest + %stack (k, inp, out) -> (k, inp, 200, mul_fp254_12, 200, out, out, bn254_pairing, k, inp, out) + // stack: k, inp, 200, mul_fp254_12, 200, out, out, bn254_pairing, k, inp, out retdest %mul_const(6) ADD - // stack: inp_k, 200, mul_fp254_12, 200, out, out, bn254_tate, k, inp, out retdest + // stack: inp_k, 200, mul_fp254_12, 200, out, out, bn254_pairing, k, inp, out retdest %jump(bn254_miller) diff --git a/evm/src/cpu/kernel/tests/bn254.rs b/evm/src/cpu/kernel/tests/bn254.rs index 1ac239ad..38574ca1 100644 --- a/evm/src/cpu/kernel/tests/bn254.rs +++ b/evm/src/cpu/kernel/tests/bn254.rs @@ -20,7 +20,7 @@ fn extract_stack(interpreter: Interpreter<'static>) -> Vec { .collect::>() } -fn run_mul_fp6(f: Fp6, g: Fp6, label: &str) -> Vec { +fn run_bn_mul_fp6(f: Fp6, g: Fp6, label: &str) -> Vec { let mut stack = f.on_stack(); if label == "mul_fp254_6" { stack.extend(g.on_stack()); @@ -38,13 +38,13 @@ fn run_mul_fp6(f: Fp6, g: Fp6, label: &str) -> Vec { } #[test] -fn test_mul_fp6() -> Result<()> { +fn test_bn_mul_fp6() -> Result<()> { let mut rng = rand::thread_rng(); let f: Fp6 = rng.gen::>(); let g: Fp6 = rng.gen::>(); - let out_normal: Vec = run_mul_fp6(f, g, "mul_fp254_6"); - let out_square: Vec = run_mul_fp6(f, f, "square_fp254_6"); + let out_normal: Vec = run_bn_mul_fp6(f, g, "mul_fp254_6"); + let out_square: Vec = run_bn_mul_fp6(f, f, "square_fp254_6"); let exp_normal: Vec = (f * g).on_stack(); let exp_square: Vec = (f * f).on_stack(); @@ -55,7 +55,7 @@ fn test_mul_fp6() -> Result<()> { Ok(()) } -fn run_mul_fp12(f: Fp12, g: Fp12, label: &str) -> Vec { +fn run_bn_mul_fp12(f: Fp12, g: Fp12, label: &str) -> Vec { let in0: usize = 200; let in1: usize = 212; let out: usize = 224; @@ -81,15 +81,15 @@ fn run_mul_fp12(f: Fp12, g: Fp12, label: &str) -> Vec { } #[test] -fn test_mul_fp12() -> Result<()> { +fn test_bn_mul_fp12() -> Result<()> { let mut rng = rand::thread_rng(); let f: Fp12 = rng.gen::>(); let g: Fp12 = rng.gen::>(); let h: Fp12 = gen_fp12_sparse(&mut rng); - let out_normal: Vec = run_mul_fp12(f, g, "mul_fp254_12"); - let out_sparse: Vec = run_mul_fp12(f, h, "mul_fp254_12_sparse"); - let out_square: Vec = run_mul_fp12(f, f, "square_fp254_12"); + let out_normal: Vec = run_bn_mul_fp12(f, g, "mul_fp254_12"); + let out_sparse: Vec = run_bn_mul_fp12(f, h, "mul_fp254_12_sparse"); + let out_square: Vec = run_bn_mul_fp12(f, f, "square_fp254_12"); let exp_normal: Vec = (f * g).on_stack(); let exp_sparse: Vec = (f * h).on_stack(); @@ -102,7 +102,7 @@ fn test_mul_fp12() -> Result<()> { Ok(()) } -fn run_frob_fp6(f: Fp6, n: usize) -> Vec { +fn run_bn_frob_fp6(f: Fp6, n: usize) -> Vec { let setup = InterpreterMemoryInitialization { label: format!("test_frob_fp254_6_{}", n), stack: f.on_stack(), @@ -114,18 +114,18 @@ fn run_frob_fp6(f: Fp6, n: usize) -> Vec { } #[test] -fn test_frob_fp6() -> Result<()> { +fn test_bn_frob_fp6() -> Result<()> { let mut rng = rand::thread_rng(); let f: Fp6 = rng.gen::>(); for n in 1..4 { - let output: Vec = run_frob_fp6(f, n); + let output: Vec = run_bn_frob_fp6(f, n); let expected: Vec = f.frob(n).on_stack(); assert_eq!(output, expected); } Ok(()) } -fn run_frob_fp12(f: Fp12, n: usize) -> Vec { +fn run_bn_frob_fp12(f: Fp12, n: usize) -> Vec { let ptr: usize = 200; let setup = InterpreterMemoryInitialization { label: format!("test_frob_fp254_12_{}", n), @@ -138,12 +138,12 @@ fn run_frob_fp12(f: Fp12, n: usize) -> Vec { } #[test] -fn test_frob_fp12() -> Result<()> { +fn test_bn_frob_fp12() -> Result<()> { let mut rng = rand::thread_rng(); let f: Fp12 = rng.gen::>(); for n in [1, 2, 3, 6] { - let output = run_frob_fp12(f, n); + let output = run_bn_frob_fp12(f, n); let expected: Vec = f.frob(n).on_stack(); assert_eq!(output, expected); } @@ -151,7 +151,7 @@ fn test_frob_fp12() -> Result<()> { } #[test] -fn test_inv_fp12() -> Result<()> { +fn test_bn_inv_fp12() -> Result<()> { let ptr: usize = 200; let inv: usize = 212; let mut rng = rand::thread_rng(); @@ -173,7 +173,7 @@ fn test_inv_fp12() -> Result<()> { } #[test] -fn test_final_exponent() -> Result<()> { +fn test_bn_final_exponent() -> Result<()> { let ptr: usize = 200; let mut rng = rand::thread_rng(); @@ -281,9 +281,11 @@ fn test_miller() -> Result<()> { } #[test] -fn test_pairing() -> Result<()> { - let ptr: usize = 224; +fn test_bn_pairing() -> Result<()> { + let acc: usize = 200; let out: usize = 212; + let ptr: usize = 224; + let inputs: Vec = vec![ CURVE_GENERATOR.x.val, CURVE_GENERATOR.y.val, @@ -302,7 +304,7 @@ fn test_pairing() -> Result<()> { U256::from(0xdeadbeefu32), ], segment: BnPairing, - memory: vec![(ptr, inputs), (out, vec![U256::one()])], + memory: vec![(ptr, inputs), (out, vec![U256::from(acc)])], }; let interpreter = run_interpreter_with_memory(setup).unwrap(); let output: Vec = interpreter.extract_kernel_memory(BnPairing, out..out + 12);