diff --git a/evm/src/bls381_arithmetic.rs b/evm/src/bls381_arithmetic.rs index f3217639..f47467c7 100644 --- a/evm/src/bls381_arithmetic.rs +++ b/evm/src/bls381_arithmetic.rs @@ -1,9 +1,8 @@ use std::ops::{Add, Div, Mul, Neg, Sub}; use ethereum_types::U512; - -// use rand::distributions::{Distribution, Standard}; -// use rand::Rng; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; pub trait FieldExt: Sized @@ -43,14 +42,14 @@ impl Fp { } } -// impl Distribution for Standard { -// fn sample(&self, rng: &mut R) -> Fp { -// let xs = rng.gen::<[u64; 8]>(); -// Fp { -// val: U512(xs) % BLS_BASE, -// } -// } -// } +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Fp { + let xs = rng.gen::<[u64; 8]>(); + Fp { + val: U512(xs) % BLS_BASE, + } + } +} impl Add for Fp { type Output = Self; @@ -105,11 +104,11 @@ impl Mul for Fp { type Output = Self; fn mul(self, other: Self) -> Self { - // x1, y1 are at most (q-1) // 2^256 < 2^125 - let x1 = U512(self.val.0[..4].try_into().unwrap()); - let x0 = U512(self.val.0[4..].try_into().unwrap()); - let y1 = U512(other.val.0[..4].try_into().unwrap()); - let y0 = U512(other.val.0[4..].try_into().unwrap()); + // x1, y1 are at most ((q-1) // 2^256) < 2^125 + let x0 = U512(self.val.0[..4].try_into().unwrap()); + let x1 = U512(self.val.0[4..].try_into().unwrap()); + let y0 = U512(other.val.0[..4].try_into().unwrap()); + let y1 = U512(other.val.0[4..].try_into().unwrap()); let z00 = Fp { val: x0.saturating_mul(y0) % BLS_BASE, @@ -169,12 +168,15 @@ where pub im: T, } -// impl> Distribution> for Standard { -// fn sample(&self, rng: &mut R) -> Fp2 { -// let (re, im) = rng.gen::<(T, T)>(); -// Fp2 { re, im } -// } -// } +impl + FieldExt> Distribution> for Standard +where + Standard: Distribution, +{ + fn sample(&self, rng: &mut R) -> Fp2 { + let (re, im) = rng.gen::<(T, T)>(); + Fp2 { re, im } + } +} impl Add for Fp2 { type Output = Self; @@ -300,12 +302,15 @@ where pub t2: Fp2, } -// impl> Distribution> for Standard { -// fn sample(&self, rng: &mut R) -> Fp6 { -// let (t0, t1, t2) = rng.gen::<(Fp2, Fp2, Fp2)>(); -// Fp6 { t0, t1, t2 } -// } -// } +impl + FieldExt> Distribution> for Standard +where + Standard: Distribution, +{ + fn sample(&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; @@ -506,12 +511,15 @@ impl FieldExt for Fp12 { } } -// impl> Distribution> for Standard { -// fn sample(&self, rng: &mut R) -> Fp12 { -// let (z0, z1) = rng.gen::<(Fp6, Fp6)>(); -// Fp12 { z0, z1 } -// } -// } +impl + FieldExt> Distribution> for Standard +where + Standard: Distribution, +{ + fn sample(&self, rng: &mut R) -> Fp12 { + let (z0, z1) = rng.gen::<(Fp6, Fp6)>(); + Fp12 { z0, z1 } + } +} impl Add for Fp12 { type Output = Self;