From d928a70b6f34cc1bf12ac34ddd374bf68c7e7505 Mon Sep 17 00:00:00 2001 From: Dmitry Vagner Date: Wed, 26 Apr 2023 09:54:46 -0700 Subject: [PATCH] clean --- evm/src/bn254_pairing.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/evm/src/bn254_pairing.rs b/evm/src/bn254_pairing.rs index 93d0d1e9..138f23e7 100644 --- a/evm/src/bn254_pairing.rs +++ b/evm/src/bn254_pairing.rs @@ -1,4 +1,4 @@ -use std::ops::Add; +use std::ops::{Add, Mul, Neg}; use ethereum_types::U256; use rand::Rng; @@ -42,6 +42,38 @@ impl Add for Curve { } } +impl Neg for Curve { + type Output = Curve; + + fn neg(self) -> Self { + Curve { + x: self.x, + y: -self.y, + } + } +} + +impl Mul for Curve { + type Output = Curve; + + fn mul(self, other: i32) -> Self { + let mut result: Curve = self; + if other.is_negative() { + result = -result; + } + let mut multiplier = result; + let mut exp = other.abs() as usize; + while exp > 0 { + if exp % 2 == 1 { + result = result + multiplier; + } + exp >>= 1; + multiplier = multiplier + multiplier; + } + result + } +} + // The twisted curve consists of pairs (x, y): (Fp2, Fp2) | y^2 = x^3 + 3/(9 + i) #[derive(Debug, Copy, Clone, PartialEq)] pub struct TwistedCurve {