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) } }