From dbd22fa68f21aea168e13b6d8278646e4e781b2b Mon Sep 17 00:00:00 2001 From: Alexander Abdugafarov Date: Thu, 5 Feb 2026 22:14:42 +0500 Subject: [PATCH 1/2] Do addition and subtraction in-place without cloning parameters --- src/field/crypto_bigint_const_monty.rs | 10 ++++++++-- src/field/crypto_bigint_monty.rs | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/field/crypto_bigint_const_monty.rs b/src/field/crypto_bigint_const_monty.rs index 4e1c25f..a4ed6d6 100644 --- a/src/field/crypto_bigint_const_monty.rs +++ b/src/field/crypto_bigint_const_monty.rs @@ -291,14 +291,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..d25ae5f 100644 --- a/src/field/crypto_bigint_monty.rs +++ b/src/field/crypto_bigint_monty.rs @@ -243,14 +243,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()); } } From 91faf6a8700eaf437f893352fdbd8a8466a70c18 Mon Sep 17 00:00:00 2001 From: Alexander Abdugafarov Date: Thu, 5 Feb 2026 22:42:33 +0500 Subject: [PATCH 2/2] Do the same with negation --- src/field/crypto_bigint_const_monty.rs | 8 ++++++-- src/field/crypto_bigint_monty.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/field/crypto_bigint_const_monty.rs b/src/field/crypto_bigint_const_monty.rs index a4ed6d6..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 } } diff --git a/src/field/crypto_bigint_monty.rs b/src/field/crypto_bigint_monty.rs index d25ae5f..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 } }