From f1a0d40a9d4e2a76ef6bd725374a8f8b9b937c33 Mon Sep 17 00:00:00 2001 From: Sebastian Hasler Date: Tue, 17 Jan 2023 11:58:56 +0100 Subject: [PATCH] Add const versions of `Residue::square` and `DynResidue::square` --- src/uint/modular/constant_mod/const_mul.rs | 21 +++++++++++++-------- src/uint/modular/runtime_mod/runtime_mul.rs | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/uint/modular/constant_mod/const_mul.rs b/src/uint/modular/constant_mod/const_mul.rs index c3ea45588..3bce1848a 100644 --- a/src/uint/modular/constant_mod/const_mul.rs +++ b/src/uint/modular/constant_mod/const_mul.rs @@ -23,6 +23,18 @@ impl, const LIMBS: usize> Residue { phantom: PhantomData, } } + + /// Computes the (reduced) square of a residue. + pub const fn square(&self) -> Self { + Self { + montgomery_form: square_montgomery_form( + &self.montgomery_form, + &MOD::MODULUS, + MOD::MOD_NEG_INV, + ), + phantom: PhantomData, + } + } } impl, const LIMBS: usize> Mul<&Residue> @@ -77,13 +89,6 @@ impl, const LIMBS: usize> MulAssign for Residue< impl, const LIMBS: usize> Square for Residue { fn square(&self) -> Self { - Self { - montgomery_form: square_montgomery_form( - &self.montgomery_form, - &MOD::MODULUS, - MOD::MOD_NEG_INV, - ), - phantom: PhantomData, - } + Residue::square(self) } } diff --git a/src/uint/modular/runtime_mod/runtime_mul.rs b/src/uint/modular/runtime_mod/runtime_mul.rs index b260461c4..30c4b9c04 100644 --- a/src/uint/modular/runtime_mod/runtime_mul.rs +++ b/src/uint/modular/runtime_mod/runtime_mul.rs @@ -20,6 +20,18 @@ impl DynResidue { residue_params: self.residue_params, } } + + /// Computes the (reduced) square of a residue. + pub const fn square(&self) -> Self { + Self { + montgomery_form: square_montgomery_form( + &self.montgomery_form, + &self.residue_params.modulus, + self.residue_params.mod_neg_inv, + ), + residue_params: self.residue_params, + } + } } impl Mul<&DynResidue> for &DynResidue { @@ -67,13 +79,6 @@ impl MulAssign> for DynResidue { impl Square for DynResidue { fn square(&self) -> Self { - Self { - montgomery_form: square_montgomery_form( - &self.montgomery_form, - &self.residue_params.modulus, - self.residue_params.mod_neg_inv, - ), - residue_params: self.residue_params, - } + DynResidue::square(self) } }