diff --git a/evm/src/bn254_pairing.rs b/evm/src/bn254_pairing.rs index 828865a3..fc470e9b 100644 --- a/evm/src/bn254_pairing.rs +++ b/evm/src/bn254_pairing.rs @@ -7,8 +7,8 @@ use crate::bn254_arithmetic::{gen_fp, gen_fp2, Fp, Fp12, Fp2, Fp6, UNIT_FP12, ZE // The curve consists of pairs (x, y): (Fp, Fp) | y^2 = x^3 + 2 #[derive(Debug, Copy, Clone, PartialEq)] pub struct Curve { - x: Fp, - y: Fp, + pub x: Fp, + pub y: Fp, } /// Standard addition formula for elliptic curves, restricted to the cases @@ -34,8 +34,8 @@ impl Add for Curve { // The twisted curve consists of pairs (x, y): (Fp2, Fp2) | y^2 = x^3 + 3/(9 + i) #[derive(Debug, Copy, Clone, PartialEq)] pub struct TwistedCurve { - x: Fp2, - y: Fp2, + pub x: Fp2, + pub y: Fp2, } // The tate pairing takes a point each from the curve and its twist and outputs an Fp12 element @@ -75,8 +75,16 @@ pub fn miller_loop(p: Curve, q: TwistedCurve) -> Fp12 { acc } -pub fn gen_fp12_sparse() -> Fp12 { - sparse_embed(gen_fp(), gen_fp2(), gen_fp2()) +pub fn tangent(p: Curve, q: TwistedCurve) -> Fp12 { + let cx = -Fp::new(3) * p.x * p.x; + let cy = Fp::new(2) * p.y; + sparse_embed(p.y * p.y - Fp::new(9), q.x.scale(cx), q.y.scale(cy)) +} + +pub fn cord(p1: Curve, p2: Curve, q: TwistedCurve) -> Fp12 { + let cx = p2.y - p1.y; + let cy = p1.x - p2.x; + sparse_embed(p1.y * p2.x - p2.y * p1.x, q.x.scale(cx), q.y.scale(cy)) } pub fn sparse_embed(g000: Fp, g01: Fp2, g11: Fp2) -> Fp12 { @@ -98,16 +106,8 @@ pub fn sparse_embed(g000: Fp, g01: Fp2, g11: Fp2) -> Fp12 { Fp12 { z0: g0, z1: g1 } } -pub fn tangent(p: Curve, q: TwistedCurve) -> Fp12 { - let cx = -Fp::new(3) * p.x * p.x; - let cy = Fp::new(2) * p.y; - sparse_embed(p.y * p.y - Fp::new(9), q.x.scale(cx), q.y.scale(cy)) -} - -pub fn cord(p1: Curve, p2: Curve, q: TwistedCurve) -> Fp12 { - let cx = p2.y - p1.y; - let cy = p1.x - p2.x; - sparse_embed(p1.y * p2.x - p2.y * p1.x, q.x.scale(cx), q.y.scale(cy)) +pub fn gen_fp12_sparse() -> Fp12 { + sparse_embed(gen_fp(), gen_fp2(), gen_fp2()) } /// The output T of the miller loop is not an invariant, @@ -308,7 +308,7 @@ fn get_powers(f: Fp12) -> (Fp12, Fp12, Fp12) { } y0 = y0 * sq; - (y2, y4 * y2 * y2 * y0, y0.inv()) + (y2, y4 * y2 * y2 / y0, y0.inv()) } // The curve is cyclic with generator (1, 2) diff --git a/evm/src/cpu/kernel/tests/bn254.rs b/evm/src/cpu/kernel/tests/bn254.rs index 62a2a8d3..761dca0a 100644 --- a/evm/src/cpu/kernel/tests/bn254.rs +++ b/evm/src/cpu/kernel/tests/bn254.rs @@ -5,7 +5,9 @@ use anyhow::Result; use ethereum_types::U256; use crate::bn254_arithmetic::{gen_fp12, Fp12}; -use crate::bn254_pairing::gen_fp12_sparse; +use crate::bn254_pairing::{ + gen_fp12_sparse, tate, CURVE_GENERATOR, TWISTED_GENERATOR, +}; use crate::cpu::kernel::aggregator::KERNEL; use crate::cpu::kernel::interpreter::Interpreter; use crate::memory::segments::Segment; @@ -190,25 +192,6 @@ fn test_inv_fp12() -> Result<()> { // Ok(()) // } -// fn make_tate_stack(p: Curve, q: TwistedCurve) -> Vec { -// let ptr = U256::from(300); -// let out = U256::from(400); - -// let p_: Vec = p.into_iter().collect(); -// let q_: Vec = q.into_iter().flatten().collect(); - -// let mut stack = vec![ptr]; -// stack.extend(p_); -// stack.extend(q_); -// stack.extend(vec![ -// ptr, -// out, -// get_address_from_label("return_fp12_on_stack"), -// out, -// ]); -// stack -// } - // #[test] // fn test_miller() -> Result<()> { // let p: Curve = curve_generator(); @@ -223,16 +206,31 @@ fn test_inv_fp12() -> Result<()> { // Ok(()) // } -// #[test] -// fn test_tate() -> Result<()> { -// let p: Curve = curve_generator(); -// let q: TwistedCurve = twisted_curve_generator(); +#[test] +fn test_tate() -> Result<()> { + let ptr: usize = 300; + let out: usize = 400; -// let stack = make_tate_stack(p, q); -// let output = run_setup_interpreter("test_tate", stack); -// let expected = fp12_on_stack(tate(p, q)); + let setup = InterpreterSetup { + label: "tate".to_string(), + stack: vec![U256::from(ptr), U256::from(out), U256::from(0xdeadbeefu32)], + memory: vec![( + ptr, + vec![ + CURVE_GENERATOR.x.val, + CURVE_GENERATOR.y.val, + TWISTED_GENERATOR.x.re.val, + TWISTED_GENERATOR.x.im.val, + TWISTED_GENERATOR.y.re.val, + TWISTED_GENERATOR.y.im.val, + ], + )], + }; + let interpreter = run_setup_interpreter(setup).unwrap(); + let output: Vec = extract_kernel_output(out..out + 12, interpreter); + let expected = fp12_on_stack(tate(CURVE_GENERATOR, TWISTED_GENERATOR)); -// assert_eq!(output, expected); + assert_eq!(output, expected); -// Ok(()) -// } + Ok(()) +}