From 1bf142db51c5d14ecc58fcc95b29a1d0a69698a9 Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Thu, 18 May 2023 15:52:40 -0700 Subject: [PATCH 1/2] add montgomery form access methods for Residue, DynResidue Signed-off-by: Andrew Whitehead --- src/uint/modular/constant_mod.rs | 24 ++++++++++++++++++++++++ src/uint/modular/runtime_mod.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/uint/modular/constant_mod.rs b/src/uint/modular/constant_mod.rs index 3e9b522ef..92b6ce761 100644 --- a/src/uint/modular/constant_mod.rs +++ b/src/uint/modular/constant_mod.rs @@ -59,6 +59,7 @@ pub trait ResidueParams: #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// A residue mod `MOD`, represented using `LIMBS` limbs. The modulus of this residue is constant, so it cannot be set at runtime. +/// Internally, the value is stored in Montgomery form (multiplied by MOD::R) until it is retrieved. pub struct Residue where MOD: ResidueParams, @@ -107,6 +108,29 @@ impl, const LIMBS: usize> Residue { ) } + /// Access the `Residue` value in Montgomery form. + pub const fn as_montgomery(&self) -> &Uint { + &self.montgomery_form + } + + /// Mutably access the `Residue` value in Montgomery form. + pub fn as_montgomery_mut(&mut self) -> &mut Uint { + &mut self.montgomery_form + } + + /// Create a `Residue` from a value in Montgomery form. + pub const fn from_montgomery(integer: Uint) -> Self { + Self { + montgomery_form: integer, + phantom: PhantomData, + } + } + + /// Extract the value from the `Residue` in Montgomery form. + pub const fn to_montgomery(&self) -> Uint { + self.montgomery_form + } + /// Performs the modular division by 2, that is for given `x` returns `y` /// such that `y * 2 = x mod p`. This means: /// - if `x` is even, returns `x / 2`, diff --git a/src/uint/modular/runtime_mod.rs b/src/uint/modular/runtime_mod.rs index 84622d167..4ca9d8121 100644 --- a/src/uint/modular/runtime_mod.rs +++ b/src/uint/modular/runtime_mod.rs @@ -113,6 +113,32 @@ impl DynResidue { &self.residue_params } + /// Access the `DynResidue` value in Montgomery form. + pub const fn as_montgomery(&self) -> &Uint { + &self.montgomery_form + } + + /// Mutably access the `DynResidue` value in Montgomery form. + pub fn as_montgomery_mut(&mut self) -> &mut Uint { + &mut self.montgomery_form + } + + /// Create a `DynResidue` from a value in Montgomery form. + pub const fn from_montgomery( + integer: Uint, + residue_params: DynResidueParams, + ) -> Self { + Self { + montgomery_form: integer, + residue_params, + } + } + + /// Extract the value from the `DynResidue` in Montgomery form. + pub const fn to_montgomery(&self) -> Uint { + self.montgomery_form + } + /// Performs the modular division by 2, that is for given `x` returns `y` /// such that `y * 2 = x mod p`. This means: /// - if `x` is even, returns `x / 2`, From 65f876078a86816634d55816dd41a048654b1d1a Mon Sep 17 00:00:00 2001 From: Andrew Whitehead Date: Thu, 18 May 2023 16:00:34 -0700 Subject: [PATCH 2/2] create DynResidue from Residue Signed-off-by: Andrew Whitehead --- src/uint/modular/runtime_mod.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/uint/modular/runtime_mod.rs b/src/uint/modular/runtime_mod.rs index 4ca9d8121..f0e993d09 100644 --- a/src/uint/modular/runtime_mod.rs +++ b/src/uint/modular/runtime_mod.rs @@ -1,6 +1,11 @@ use crate::{Limb, Uint, Word}; -use super::{div_by_2::div_by_2, reduction::montgomery_reduction, Retrieve}; +use super::{ + constant_mod::{Residue, ResidueParams}, + div_by_2::div_by_2, + reduction::montgomery_reduction, + Retrieve, +}; /// Additions between residues with a modulus set at runtime mod runtime_add; @@ -58,6 +63,20 @@ impl DynResidueParams { pub const fn modulus(&self) -> &Uint { &self.modulus } + + /// Create `DynResidueParams` corresponding to a `ResidueParams`. + pub const fn from_residue_params

() -> Self + where + P: ResidueParams, + { + Self { + modulus: P::MODULUS, + r: P::R, + r2: P::R2, + r3: P::R3, + mod_neg_inv: P::MOD_NEG_INV, + } + } } /// A residue represented using `LIMBS` limbs. The odd modulus of this residue is set at runtime. @@ -158,3 +177,12 @@ impl Retrieve for DynResidue { self.retrieve() } } + +impl> From<&Residue> for DynResidue { + fn from(residue: &Residue) -> Self { + Self { + montgomery_form: residue.to_montgomery(), + residue_params: DynResidueParams::from_residue_params::

(), + } + } +}