From 0650d2636c337abece38cadb317c6fdee706f2b4 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 22 Mar 2023 17:26:14 -0700 Subject: [PATCH] remove .scale --- evm/src/bn254_pairing.rs | 4 +-- evm/src/extension_tower.rs | 69 ++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/evm/src/bn254_pairing.rs b/evm/src/bn254_pairing.rs index 08eb614c..7277c2a8 100644 --- a/evm/src/bn254_pairing.rs +++ b/evm/src/bn254_pairing.rs @@ -69,14 +69,14 @@ pub fn miller_loop(p: Curve, q: TwistedCurve) -> Fp12 { pub fn tangent(p: Curve, q: TwistedCurve) -> Fp12 { let cx = -BN254::new(3) * p.x * p.x; let cy = BN254::new(2) * p.y; - sparse_embed(p.y * p.y - BN254::new(9), q.x.scale(cx), q.y.scale(cy)) + sparse_embed(p.y * p.y - BN254::new(9), q.x * cx, q.y * cy) } /// The sloped line function for adding two points 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)) + sparse_embed(p1.y * p2.x - p2.y * p1.x, q.x * cx, q.y * cy) } /// The tangent and cord functions output sparse Fp12 elements. diff --git a/evm/src/extension_tower.rs b/evm/src/extension_tower.rs index 23c130a0..35bbefdd 100644 --- a/evm/src/extension_tower.rs +++ b/evm/src/extension_tower.rs @@ -6,8 +6,7 @@ use rand::distributions::{Distribution, Standard}; use rand::Rng; pub trait FieldExt: - Sized - + Copy + Copy + std::ops::Add + std::ops::Neg + std::ops::Sub @@ -317,15 +316,19 @@ impl Mul for Fp2 { } } -impl Fp2 { - /// This function scalar multiplies an Fp2 by an BN254 - pub fn scale(self, x: T) -> Self { +/// This function scalar multiplies an Fp2 by an Fp +impl Mul for Fp2 { + type Output = Fp2; + + fn mul(self, other: T) -> Self { Fp2 { - re: x * self.re, - im: x * self.im, + re: other * self.re, + im: other * self.im, } } +} +impl Fp2 { /// Return the complex conjugate z' of z: Fp2 /// This also happens to be the frobenius map /// z -> z^p @@ -357,7 +360,7 @@ impl FieldExt for Fp2 { /// The inverse of z is given by z'/||z||^2 since ||z||^2 = zz' fn inv(self) -> Fp2 { let norm_sq = self.norm_sq(); - self.conj().scale(norm_sq.inv()) + self.conj() * norm_sq.inv() } } @@ -880,17 +883,19 @@ where } } -impl Fp6 +/// This function scalar multiplies an Fp6 by an Fp2 +impl Mul> for Fp6 where T: FieldExt, Fp2: Adj, { - // This function scalar multiplies an Fp6 by an Fp2 - fn scale(self, x: Fp2) -> Fp6 { + type Output = Fp6; + + fn mul(self, other: Fp2) -> Self { Fp6 { - t0: x * self.t0, - t1: x * self.t1, - t2: x * self.t2, + t0: other * self.t0, + t1: other * self.t1, + t2: other * self.t2, } } } @@ -981,9 +986,9 @@ where let prod_13 = self.frob(1) * self.frob(3); let prod_135 = (prod_13 * self.frob(5)).t0; let phi = prod_135.norm_sq(); - let prod_odds_over_phi = prod_135.scale(phi.inv()); + let prod_odds_over_phi = prod_135 * phi.inv(); let prod_24 = prod_13.frob(1); - prod_24.scale(prod_odds_over_phi) + prod_24 * prod_odds_over_phi } } @@ -1044,10 +1049,10 @@ where let prod_1379 = prod_17 * prod_17.frob(2); let prod_odds = (prod_1379 * prod_17.frob(4)).t0; let phi = prod_odds.norm_sq(); - let prod_odds_over_phi = prod_odds.scale(phi.inv()); + let prod_odds_over_phi = prod_odds * phi.inv(); let prod_evens_except_six = prod_1379.frob(1); - let prod_except_six = prod_evens_except_six.scale(prod_odds_over_phi); - self.conj().scale(prod_except_six) + let prod_except_six = prod_evens_except_six * prod_odds_over_phi; + self.conj() * prod_except_six } } @@ -1126,19 +1131,27 @@ where } } +/// This function scalar multiplies an Fp12 by an Fp6 +impl Mul> for Fp12 +where + T: FieldExt, + Fp2: Adj, +{ + type Output = Fp12; + + fn mul(self, other: Fp6) -> Self { + Fp12 { + z0: other * self.z0, + z1: other * self.z1, + } + } +} + impl Fp12 where T: FieldExt, Fp2: Adj, { - // This function scalar multiplies an Fp12 by an Fp6 - fn scale(self, x: Fp6) -> Fp12 { - Fp12 { - z0: x * self.z0, - z1: x * self.z1, - } - } - fn conj(self) -> Fp12 { Fp12 { z0: self.z0, @@ -1161,7 +1174,7 @@ where let n = n % 12; Fp12 { z0: self.z0.frob(n), - z1: self.z1.frob(n).scale(Fp2::::FROB_Z[n]), + z1: self.z1.frob(n) * (Fp2::::FROB_Z[n]), } } }