diff --git a/src/field/crypto_bigint_const_monty.rs b/src/field/crypto_bigint_const_monty.rs index 4e1c25f..4f9526d 100644 --- a/src/field/crypto_bigint_const_monty.rs +++ b/src/field/crypto_bigint_const_monty.rs @@ -187,8 +187,12 @@ impl, const LIMBS: usize> Neg for ConstMontyField type Output = Self; #[inline(always)] - fn neg(self) -> Self::Output { - Self(self.0.neg()) + fn neg(mut self) -> Self::Output { + *self.0.as_montgomery_mut() = self + .0 + .as_montgomery() + .neg_mod(Mod::PARAMS.modulus().as_nz_ref()); + self } } @@ -291,14 +295,20 @@ impl_op_assign_boilerplate!(DivAssign, div_assign); impl, const LIMBS: usize> AddAssign<&Self> for ConstMontyField { #[inline(always)] fn add_assign(&mut self, rhs: &Self) { - self.0.add_assign(&rhs.0); + *self.0.as_montgomery_mut() = self + .0 + .as_montgomery() + .add_mod(rhs.0.as_montgomery(), Mod::PARAMS.modulus().as_nz_ref()); } } impl, const LIMBS: usize> SubAssign<&Self> for ConstMontyField { #[inline(always)] fn sub_assign(&mut self, rhs: &Self) { - self.0.sub_assign(&rhs.0); + *self.0.as_montgomery_mut() = self + .0 + .as_montgomery() + .sub_mod(rhs.0.as_montgomery(), Mod::PARAMS.modulus().as_nz_ref()); } } diff --git a/src/field/crypto_bigint_monty.rs b/src/field/crypto_bigint_monty.rs index c2287f7..7f05e10 100644 --- a/src/field/crypto_bigint_monty.rs +++ b/src/field/crypto_bigint_monty.rs @@ -133,8 +133,12 @@ impl Hash for MontyField { impl Neg for MontyField { type Output = Self; - fn neg(self) -> Self::Output { - Self(self.0.neg()) + fn neg(mut self) -> Self::Output { + *self.0.as_montgomery_mut() = self + .0 + .as_montgomery() + .neg_mod(self.0.params().modulus().as_nz_ref()); + self } } @@ -243,14 +247,20 @@ impl_op_assign_boilerplate!(DivAssign, div_assign); impl AddAssign<&Self> for MontyField { #[inline(always)] fn add_assign(&mut self, rhs: &Self) { - self.0.add_assign(&rhs.0); + *self.0.as_montgomery_mut() = self + .0 + .as_montgomery() + .add_mod(rhs.0.as_montgomery(), self.0.params().modulus().as_nz_ref()); } } impl SubAssign<&Self> for MontyField { #[inline(always)] fn sub_assign(&mut self, rhs: &Self) { - self.0.sub_assign(&rhs.0); + *self.0.as_montgomery_mut() = self + .0 + .as_montgomery() + .sub_mod(rhs.0.as_montgomery(), self.0.params().modulus().as_nz_ref()); } }