rand impl

This commit is contained in:
Dmitry Vagner 2023-02-01 19:15:56 -08:00
parent a950a26266
commit 962754be99
3 changed files with 49 additions and 39 deletions

View File

@ -1,7 +1,8 @@
use std::ops::{Add, Div, Mul, Neg, Sub};
use ethereum_types::U256;
use rand::{thread_rng, Rng};
use rand::distributions::{Distribution, Standard};
use rand::Rng;
pub const BN_BASE: U256 = U256([
0x3c208c16d87cfd47,
@ -23,6 +24,15 @@ impl Fp {
}
}
impl Distribution<Fp> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Fp {
let (x0, x1, x2, x3) = rng.gen::<(u64, u64, u64, u64)>();
Fp {
val: U256([x0, x1, x2, x3]) % BN_BASE,
}
}
}
impl Add for Fp {
type Output = Self;
@ -113,6 +123,13 @@ pub const UNIT_FP2: Fp2 = Fp2 {
im: ZERO_FP,
};
impl Distribution<Fp2> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Fp2 {
let (re, im) = rng.gen::<(Fp, Fp)>();
Fp2 { re, im }
}
}
impl Add for Fp2 {
type Output = Self;
@ -229,6 +246,13 @@ pub const UNIT_FP6: Fp6 = Fp6 {
t2: ZERO_FP2,
};
impl Distribution<Fp6> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Fp6 {
let (t0, t1, t2) = rng.gen::<(Fp2, Fp2, Fp2)>();
Fp6 { t0, t1, t2 }
}
}
impl Add for Fp6 {
type Output = Self;
@ -372,6 +396,13 @@ pub const UNIT_FP12: Fp12 = Fp12 {
z1: ZERO_FP6,
};
impl Distribution<Fp12> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Fp12 {
let (z0, z1) = rng.gen::<(Fp6, Fp6)>();
Fp12 { z0, z1 }
}
}
impl Mul for Fp12 {
type Output = Self;
@ -833,32 +864,3 @@ const FROB_Z: [Fp2; 12] = [
},
},
];
pub fn gen_fp() -> Fp {
let mut rng = thread_rng();
let x64 = rng.gen::<u64>();
let x256 = U256([x64, x64, x64, x64]) % BN_BASE;
Fp { val: x256 }
}
pub fn gen_fp2() -> Fp2 {
Fp2 {
re: gen_fp(),
im: gen_fp(),
}
}
pub fn gen_fp6() -> Fp6 {
Fp6 {
t0: gen_fp2(),
t1: gen_fp2(),
t2: gen_fp2(),
}
}
pub fn gen_fp12() -> Fp12 {
Fp12 {
z0: gen_fp6(),
z1: gen_fp6(),
}
}

View File

@ -1,6 +1,8 @@
use std::ops::Add;
use crate::bn254_arithmetic::{gen_fp, gen_fp2, Fp, Fp12, Fp2, Fp6, UNIT_FP12, ZERO_FP, ZERO_FP2};
use rand::Rng;
use crate::bn254_arithmetic::{Fp, Fp12, Fp2, Fp6, UNIT_FP12, ZERO_FP, ZERO_FP2};
// The curve consists of pairs (x, y): (Fp, Fp) | y^2 = x^3 + 2
#[derive(Debug, Copy, Clone, PartialEq)]
@ -98,8 +100,8 @@ pub fn sparse_embed(g000: Fp, g01: Fp2, g11: Fp2) -> Fp12 {
Fp12 { z0: g0, z1: g1 }
}
pub fn gen_fp12_sparse() -> Fp12 {
sparse_embed(gen_fp(), gen_fp2(), gen_fp2())
pub fn gen_fp12_sparse<R: Rng + ?Sized>(rng: &mut R) -> Fp12 {
sparse_embed(rng.gen::<Fp>(), rng.gen::<Fp2>(), rng.gen::<Fp2>())
}
/// The output y of the miller loop is not an invariant,

View File

@ -3,8 +3,9 @@ use std::ops::Range;
use anyhow::Result;
use ethereum_types::U256;
use rand::Rng;
use crate::bn254_arithmetic::{gen_fp12, Fp, Fp12, Fp2};
use crate::bn254_arithmetic::{Fp, Fp12, Fp2};
use crate::bn254_pairing::{gen_fp12_sparse, tate, Curve, TwistedCurve};
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::interpreter::Interpreter;
@ -78,9 +79,10 @@ fn test_mul_fp12() -> Result<()> {
let in1: usize = 76;
let out: usize = 88;
let f: Fp12 = gen_fp12();
let g: Fp12 = gen_fp12();
let h: Fp12 = gen_fp12_sparse();
let mut rng = rand::thread_rng();
let f: Fp12 = rng.gen::<Fp12>();
let g: Fp12 = rng.gen::<Fp12>();
let h: Fp12 = gen_fp12_sparse(&mut rng);
let setup_normal: InterpreterSetup = setup_mul_test(in0, in1, out, f, g, "mul_fp12");
let setup_sparse: InterpreterSetup = setup_mul_test(in0, in1, out, f, h, "mul_fp12_sparse");
@ -116,7 +118,9 @@ fn setup_frob_test(ptr: usize, f: Fp12, label: &str) -> InterpreterSetup {
#[test]
fn test_frob_fp12() -> Result<()> {
let ptr: usize = 100;
let f: Fp12 = gen_fp12();
let mut rng = rand::thread_rng();
let f: Fp12 = rng.gen::<Fp12>();
let setup_frob_1 = setup_frob_test(ptr, f, "test_frob_fp12_1");
let setup_frob_2 = setup_frob_test(ptr, f, "test_frob_fp12_2");
@ -150,8 +154,10 @@ fn test_frob_fp12() -> Result<()> {
fn test_inv_fp254_12() -> Result<()> {
let ptr: usize = 100;
let inv: usize = 112;
let f: Fp12 = gen_fp12();
let mut rng = rand::thread_rng();
let f: Fp12 = rng.gen::<Fp12>();
let setup = InterpreterSetup {
label: "inv_fp254_12".to_string(),
stack: vec![U256::from(ptr), U256::from(inv), U256::from(0xdeadbeefu32)],