From 30c5148b9a78c47914fca042a97b44768e9c7b4d Mon Sep 17 00:00:00 2001 From: "Mario J. Rugiero" Date: Mon, 9 Jan 2023 14:59:51 -0300 Subject: [PATCH] Use Euclid to compute the inverse faster --- felt/src/bigint_felt.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/felt/src/bigint_felt.rs b/felt/src/bigint_felt.rs index b15b7ba852..af3acb1e6f 100644 --- a/felt/src/bigint_felt.rs +++ b/felt/src/bigint_felt.rs @@ -423,27 +423,39 @@ impl<'a> Pow for &'a FeltBigInt { impl Div for FeltBigInt { type Output = Self; fn div(self, rhs: Self) -> Self::Output { - let mut x = rhs.0.modpow(&(&*CAIRO_PRIME - 2usize), &CAIRO_PRIME); - x *= self.0; - FeltBigInt::from(x) + let x = rhs + .0 + .to_bigint() + .unwrap() + .extended_gcd(&CAIRO_SIGNED_PRIME) + .x; + self * &FeltBigInt::from(x) } } impl<'a> Div for &'a FeltBigInt { type Output = FeltBigInt; fn div(self, rhs: Self) -> Self::Output { - let mut x = rhs.0.modpow(&(&*CAIRO_PRIME - 2usize), &CAIRO_PRIME); - x *= &self.0; - FeltBigInt::from(x) + let x = rhs + .0 + .to_bigint() + .unwrap() + .extended_gcd(&CAIRO_SIGNED_PRIME) + .x; + self * &FeltBigInt::from(x) } } impl<'a> Div for &'a FeltBigInt { type Output = FeltBigInt; fn div(self, rhs: FeltBigInt) -> Self::Output { - let mut x = rhs.0.modpow(&(&*CAIRO_PRIME - 2usize), &CAIRO_PRIME); - x *= &self.0; - FeltBigInt::from(x) + let x = rhs + .0 + .to_bigint() + .unwrap() + .extended_gcd(&CAIRO_SIGNED_PRIME) + .x; + self * &FeltBigInt::from(x) } }