From 5e98a5f90722d5594357b818daac5ba0ef3a18ef Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Mon, 20 Mar 2023 12:25:23 -0700 Subject: [PATCH] adj trait --- evm/src/extension_tower.rs | 183 ++++++++++++++++++++++++++----------- 1 file changed, 131 insertions(+), 52 deletions(-) diff --git a/evm/src/extension_tower.rs b/evm/src/extension_tower.rs index f47467c7..3a8d7443 100644 --- a/evm/src/extension_tower.rs +++ b/evm/src/extension_tower.rs @@ -30,77 +30,77 @@ pub const BLS_BASE: U512 = U512([ ]); #[derive(Debug, Copy, Clone, PartialEq)] -pub struct Fp { +pub struct Fp381 { pub val: U512, } -impl Fp { - pub fn new(val: usize) -> Fp { - Fp { +impl Fp381 { + pub fn new(val: usize) -> Fp381 { + Fp381 { val: U512::from(val), } } } -impl Distribution for Standard { - fn sample(&self, rng: &mut R) -> Fp { +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Fp381 { let xs = rng.gen::<[u64; 8]>(); - Fp { + Fp381 { val: U512(xs) % BLS_BASE, } } } -impl Add for Fp { +impl Add for Fp381 { type Output = Self; fn add(self, other: Self) -> Self { - Fp { + Fp381 { val: (self.val + other.val) % BLS_BASE, } } } -impl Neg for Fp { +impl Neg for Fp381 { type Output = Self; fn neg(self) -> Self::Output { - Fp { + Fp381 { val: (BLS_BASE - self.val) % BLS_BASE, } } } -impl Sub for Fp { +impl Sub for Fp381 { type Output = Self; fn sub(self, other: Self) -> Self { - Fp { + Fp381 { val: (BLS_BASE + self.val - other.val) % BLS_BASE, } } } -impl Fp { - fn lsh_128(self) -> Fp { +impl Fp381 { + fn lsh_128(self) -> Fp381 { let b128: U512 = U512([0, 0, 1, 0, 0, 0, 0, 0]); // since BLS_BASE < 2^384, multiplying by 2^128 doesn't overflow the U512 - Fp { + Fp381 { val: self.val.saturating_mul(b128) % BLS_BASE, } } - fn lsh_256(self) -> Fp { + fn lsh_256(self) -> Fp381 { self.lsh_128().lsh_128() } - fn lsh_512(self) -> Fp { + fn lsh_512(self) -> Fp381 { self.lsh_256().lsh_256() } } #[allow(clippy::suspicious_arithmetic_impl)] -impl Mul for Fp { +impl Mul for Fp381 { type Output = Self; fn mul(self, other: Self) -> Self { @@ -110,16 +110,16 @@ impl Mul for Fp { let y0 = U512(other.val.0[..4].try_into().unwrap()); let y1 = U512(other.val.0[4..].try_into().unwrap()); - let z00 = Fp { + let z00 = Fp381 { val: x0.saturating_mul(y0) % BLS_BASE, }; - let z01 = Fp { + let z01 = Fp381 { val: x0.saturating_mul(y1), }; - let z10 = Fp { + let z10 = Fp381 { val: x1.saturating_mul(y0), }; - let z11 = Fp { + let z11 = Fp381 { val: x1.saturating_mul(y1), }; @@ -127,16 +127,16 @@ impl Mul for Fp { } } -impl FieldExt for Fp { - const ZERO: Self = Fp { val: U512::zero() }; - const UNIT: Self = Fp { val: U512::one() }; - fn inv(self) -> Fp { +impl FieldExt for Fp381 { + const ZERO: Self = Fp381 { val: U512::zero() }; + const UNIT: Self = Fp381 { val: U512::one() }; + fn inv(self) -> Fp381 { exp_fp(self, BLS_BASE - 2) } } #[allow(clippy::suspicious_arithmetic_impl)] -impl Div for Fp { +impl Div for Fp381 { type Output = Self; fn div(self, rhs: Self) -> Self::Output { @@ -144,9 +144,9 @@ impl Div for Fp { } } -fn exp_fp(x: Fp, e: U512) -> Fp { +fn exp_fp(x: Fp381, e: U512) -> Fp381 { let mut current = x; - let mut product = Fp { val: U512::one() }; + let mut product = Fp381 { val: U512::one() }; for j in 0..512 { if e.bit(j) { @@ -168,8 +168,9 @@ where pub im: T, } -impl + FieldExt> Distribution> for Standard +impl Distribution> for Standard where + T: Distribution + FieldExt, Standard: Distribution, { fn sample(&self, rng: &mut R) -> Fp2 { @@ -281,7 +282,7 @@ pub trait Adj { /// Helper function which multiplies by the Fp2 element /// whose cube root we will adjoin in the next extension -impl Adj for Fp2 { +impl Adj for Fp2 { fn mul_adj(self) -> Self { Fp2 { re: self.re - self.im, @@ -296,14 +297,17 @@ impl Adj for Fp2 { pub struct Fp6 where T: FieldExt, + Fp2: Adj, { pub t0: Fp2, pub t1: Fp2, pub t2: Fp2, } -impl + FieldExt> Distribution> for Standard +impl Distribution> for Standard where + T: Distribution + FieldExt, + Fp2: Adj, Standard: Distribution, { fn sample(&self, rng: &mut R) -> Fp6 { @@ -312,7 +316,11 @@ where } } -impl Add for Fp6 { +impl Add for Fp6 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn add(self, other: Self) -> Self { @@ -324,7 +332,11 @@ impl Add for Fp6 { } } -impl Neg for Fp6 { +impl Neg for Fp6 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn neg(self) -> Self::Output { @@ -336,7 +348,11 @@ impl Neg for Fp6 { } } -impl Sub for Fp6 { +impl Sub for Fp6 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn sub(self, other: Self) -> Self { @@ -348,7 +364,11 @@ impl Sub for Fp6 { } } -impl Mul for Fp6 { +impl Mul for Fp6 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn mul(self, other: Self) -> Self { @@ -360,7 +380,11 @@ impl Mul for Fp6 { } } -impl Fp6 { +impl Fp6 +where + T: FieldExt, + Fp2: Adj, +{ // This function scalar multiplies an Fp6 by an Fp2 fn scale(self, x: Fp2) -> Fp6 { Fp6 { @@ -371,7 +395,11 @@ impl Fp6 { } } -impl Fp6 { +impl Fp6 +where + T: FieldExt, + Fp2: Adj, +{ /// This function multiplies an Fp6 element by t, and hence shifts the bases, /// where the t^2 coefficient picks up a factor of 1+i as the 1 coefficient of the output fn sh(self) -> Fp6 { @@ -383,12 +411,20 @@ impl Fp6 { } } -impl Fp6 { +impl Fp6 +where + T: FieldExt, + Fp2: Adj, +{ const FROB_T: [[Fp2; 6]; 2] = [[Fp2::::ZERO; 6]; 2]; const FROB_Z: [Fp2; 12] = [Fp2::::ZERO; 12]; } -impl Fp6 { +impl Fp6 +where + T: FieldExt, + Fp2: Adj, +{ /// The nth frobenius endomorphism of a p^q field is given by mapping /// x to x^(p^n) /// which sends a + bt + ct^2: Fp6 to @@ -418,7 +454,11 @@ impl Fp6 { } } -impl FieldExt for Fp6 { +impl FieldExt for Fp6 +where + T: FieldExt, + Fp2: Adj, +{ const ZERO: Fp6 = Fp6 { t0: Fp2::::ZERO, t1: Fp2::::ZERO, @@ -457,7 +497,11 @@ impl FieldExt for Fp6 { } #[allow(clippy::suspicious_arithmetic_impl)] -impl Div for Fp6 { +impl Div for Fp6 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn div(self, rhs: Self) -> Self::Output { @@ -471,12 +515,17 @@ impl Div for Fp6 { pub struct Fp12 where T: FieldExt, + Fp2: Adj, { pub z0: Fp6, pub z1: Fp6, } -impl FieldExt for Fp12 { +impl FieldExt for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ const ZERO: Fp12 = Fp12 { z0: Fp6::::ZERO, z1: Fp6::::ZERO, @@ -511,8 +560,10 @@ impl FieldExt for Fp12 { } } -impl + FieldExt> Distribution> for Standard +impl Distribution> for Standard where + T: Distribution + FieldExt, + Fp2: Adj, Standard: Distribution, { fn sample(&self, rng: &mut R) -> Fp12 { @@ -521,7 +572,11 @@ where } } -impl Add for Fp12 { +impl Add for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn add(self, other: Self) -> Self { @@ -532,7 +587,11 @@ impl Add for Fp12 { } } -impl Neg for Fp12 { +impl Neg for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn neg(self) -> Self::Output { @@ -543,7 +602,11 @@ impl Neg for Fp12 { } } -impl Sub for Fp12 { +impl Sub for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn sub(self, other: Self) -> Self { @@ -554,7 +617,11 @@ impl Sub for Fp12 { } } -impl Mul for Fp12 { +impl Mul for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn mul(self, other: Self) -> Self { @@ -568,7 +635,11 @@ impl Mul for Fp12 { } } -impl Fp12 { +impl Fp12 +where + T: FieldExt, + Fp2: Adj, +{ // This function scalar multiplies an Fp12 by an Fp6 fn scale(self, x: Fp6) -> Fp12 { Fp12 { @@ -585,7 +656,11 @@ impl Fp12 { } } -impl Fp12 { +impl Fp12 +where + T: FieldExt, + Fp2: Adj, +{ /// The nth frobenius endomorphism of a p^q field is given by mapping /// x to x^(p^n) /// which sends a + bz: Fp12 to @@ -601,7 +676,11 @@ impl Fp12 { } #[allow(clippy::suspicious_arithmetic_impl)] -impl Div for Fp12 { +impl Div for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ type Output = Self; fn div(self, rhs: Self) -> Self::Output {