diff --git a/evm/src/bn254_pairing.rs b/evm/src/bn254_pairing.rs index 44e22189..5f3fc7fa 100644 --- a/evm/src/bn254_pairing.rs +++ b/evm/src/bn254_pairing.rs @@ -1,6 +1,8 @@ use std::ops::{Add, Mul, Neg}; use ethereum_types::U256; +use rand::distributions::Standard; +use rand::prelude::Distribution; use rand::Rng; use crate::extension_tower::{FieldExt, Fp12, Fp2, Fp6, BN254}; @@ -23,6 +25,26 @@ impl Curve { } } +impl Curve +where + T: FieldExt, + Curve: CyclicGroup, +{ + pub fn int(z: i32) -> Self { + Curve::::GENERATOR * z + } +} + +impl Distribution> for Standard +where + T: FieldExt, + Curve: CyclicGroup, +{ + fn sample(&self, rng: &mut R) -> Curve { + Curve::::GENERATOR * rng.gen::() + } +} + /// Standard addition formula for elliptic curves, restricted to the cases /// https://en.wikipedia.org/wiki/Elliptic_curve#Algebraic_interpretation impl Add for Curve { @@ -62,14 +84,14 @@ impl Neg for Curve { } } -pub trait CurveGroup { +pub trait CyclicGroup { const GENERATOR: Self; } /// The BN curve consists of pairs /// (x, y): (BN254, BN254) | y^2 = x^3 + 2 // with generator given by (1, 2) -impl CurveGroup for Curve { +impl CyclicGroup for Curve { const GENERATOR: Curve = Curve { x: BN254 { val: U256::one() }, y: BN254 { @@ -81,7 +103,7 @@ impl CurveGroup for Curve { impl Mul for Curve where T: FieldExt, - Curve: CurveGroup, + Curve: CyclicGroup, { type Output = Curve; @@ -107,7 +129,6 @@ where exp >>= 1; x = x + x; } - println!("result: {:?}", result); result } } @@ -115,7 +136,7 @@ where /// The twisted curve consists of pairs /// (x, y): (Fp2, Fp2) | y^2 = x^3 + 3/(9 + i) /// with generator given as follows -impl CurveGroup for Curve> { +impl CyclicGroup for Curve> { const GENERATOR: Curve> = Curve { x: Fp2 { re: BN254 { diff --git a/evm/src/cpu/kernel/tests/bn254.rs b/evm/src/cpu/kernel/tests/bn254.rs index 25ec6429..63796a4e 100644 --- a/evm/src/cpu/kernel/tests/bn254.rs +++ b/evm/src/cpu/kernel/tests/bn254.rs @@ -4,7 +4,7 @@ use anyhow::Result; use ethereum_types::U256; use rand::Rng; -use crate::bn254_pairing::{final_exponent, gen_fp12_sparse, miller_loop, Curve, CurveGroup}; +use crate::bn254_pairing::{final_exponent, gen_fp12_sparse, miller_loop, Curve, CyclicGroup}; use crate::cpu::kernel::interpreter::{ run_interpreter_with_memory, Interpreter, InterpreterMemoryInitialization, }; @@ -213,7 +213,15 @@ fn pairing_input() -> Vec { fn test_bn_miller() -> Result<()> { let ptr: usize = 100; let out: usize = 106; - let input = pairing_input(); + + let mut rng = rand::thread_rng(); + let p: Curve = rng.gen::>(); + let q: Curve> = rng.gen::>>(); + + let p_stack: [U256; 2] = unsafe { transmute(p) }; + let q_stack: [U256; 4] = unsafe { transmute(q) }; + let mut input = p_stack.to_vec(); + input.extend(q_stack); let setup = InterpreterMemoryInitialization { label: "bn254_miller".to_string(), @@ -223,8 +231,7 @@ fn test_bn_miller() -> Result<()> { }; let interpreter = run_interpreter_with_memory(setup).unwrap(); let output: Vec = interpreter.extract_kernel_memory(BnPairing, out..out + 12); - let expected = - miller_loop(Curve::::GENERATOR, Curve::>::GENERATOR).on_stack(); + let expected = miller_loop(p, q).on_stack(); assert_eq!(output, expected);