Skip to content

Commit 8b550fb

Browse files
committed
Add HasResidueRepr and ResidueRepr traits to connect integers and residue representations
1 parent 1ef8172 commit 8b550fb

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

src/modular/boxed_monty_form.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::{
1212
reduction::{montgomery_reduction_boxed, montgomery_reduction_boxed_mut},
1313
Retrieve,
1414
};
15-
use crate::{BoxedUint, ConstantTimeSelect, Integer, Limb, NonZero, Word};
15+
use crate::{BoxedUint, ConstantTimeSelect, Integer, Limb, MontyFormLike, NonZero, Word};
1616
use subtle::CtOption;
1717

1818
#[cfg(feature = "std")]
@@ -251,6 +251,27 @@ impl Retrieve for BoxedMontyForm {
251251
}
252252
}
253253

254+
impl MontyFormLike for BoxedMontyForm {
255+
type Raw = BoxedUint;
256+
type Params = BoxedMontyParams;
257+
258+
fn new_params(modulus: Self::Raw) -> CtOption<Self::Params> {
259+
BoxedMontyParams::new(modulus)
260+
}
261+
262+
fn new(value: Self::Raw, params: Self::Params) -> Self {
263+
BoxedMontyForm::new(value, params)
264+
}
265+
266+
fn zero(params: Self::Params) -> Self {
267+
BoxedMontyForm::zero(params)
268+
}
269+
270+
fn one(params: Self::Params) -> Self {
271+
BoxedMontyForm::one(params)
272+
}
273+
}
274+
254275
/// Convert the given integer into the Montgomery domain.
255276
#[inline]
256277
fn convert_to_montgomery(integer: &mut BoxedUint, params: &BoxedMontyParams) {

src/modular/monty_form.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::{
1313
reduction::montgomery_reduction,
1414
Retrieve,
1515
};
16-
use crate::{Limb, NonZero, Uint, Word, Zero};
16+
use crate::{Limb, MontyFormLike, NonZero, Uint, Word, Zero};
1717
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1818

1919
/// Parameters to efficiently go to/from the Montgomery form for an odd modulus provided at runtime.
@@ -208,6 +208,27 @@ impl<const LIMBS: usize> Retrieve for MontyForm<LIMBS> {
208208
}
209209
}
210210

211+
impl<const LIMBS: usize> MontyFormLike for MontyForm<LIMBS> {
212+
type Raw = Uint<LIMBS>;
213+
type Params = MontyParams<LIMBS>;
214+
215+
fn new_params(modulus: Self::Raw) -> CtOption<Self::Params> {
216+
MontyParams::new(&modulus)
217+
}
218+
219+
fn new(value: Self::Raw, params: Self::Params) -> Self {
220+
MontyForm::new(&value, params)
221+
}
222+
223+
fn zero(params: Self::Params) -> Self {
224+
MontyForm::zero(params)
225+
}
226+
227+
fn one(params: Self::Params) -> Self {
228+
MontyForm::one(params)
229+
}
230+
}
231+
211232
impl<const LIMBS: usize, P: ConstMontyParams<LIMBS>> From<&ConstMontyForm<P, LIMBS>>
212233
for MontyForm<LIMBS>
213234
{

src/traits.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,33 @@ pub trait WideningMul<Rhs = Self>: Sized {
513513
/// Perform widening multiplication.
514514
fn widening_mul(&self, rhs: Rhs) -> Self::Output;
515515
}
516+
517+
/// This integer has a representation optimized for the performance of modular operations.
518+
pub trait HasMontyForm {
519+
/// The representation type.
520+
type MontyForm: MontyFormLike<Raw = Self>;
521+
}
522+
523+
/// A representation of an integer optimized for the performance of modular operations.
524+
pub trait MontyFormLike {
525+
/// The original integer type.
526+
type Raw: HasMontyForm<MontyForm = Self>;
527+
528+
/// The precomputed data needed for this representation.
529+
type Params: Clone;
530+
531+
/// Create the precomputed data.
532+
///
533+
/// Can return `None` if `modulus` is not valid for the representation;
534+
/// see the documentation of the specific type for the requirements.
535+
fn new_params(modulus: Self::Raw) -> CtOption<Self::Params>;
536+
537+
/// Convert the value into the representation using precomputed data.
538+
fn new(value: Self::Raw, params: Self::Params) -> Self;
539+
540+
/// Returns zero in this representation.
541+
fn zero(params: Self::Params) -> Self;
542+
543+
/// Returns one in this representation.
544+
fn one(params: Self::Params) -> Self;
545+
}

src/uint.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ pub(crate) mod boxed;
4040
mod rand;
4141

4242
use crate::{
43-
modular::BernsteinYangInverter, Bounded, ConstCtOption, Constants, Encoding, FixedInteger,
44-
Integer, Limb, NonZero, PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
43+
modular::{BernsteinYangInverter, MontyForm},
44+
Bounded, ConstCtOption, Constants, Encoding, FixedInteger, HasMontyForm, Integer, Limb,
45+
NonZero, PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
4546
};
4647
use core::fmt;
4748
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
@@ -260,6 +261,10 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
260261
}
261262
}
262263

264+
impl<const LIMBS: usize> HasMontyForm for Uint<LIMBS> {
265+
type MontyForm = MontyForm<LIMBS>;
266+
}
267+
263268
impl<const LIMBS: usize> ZeroConstant for Uint<LIMBS> {
264269
const ZERO: Self = Self::ZERO;
265270
}

src/uint/boxed.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod sub_mod;
2525
#[cfg(feature = "rand_core")]
2626
mod rand;
2727

28-
use crate::{Integer, Limb, NonZero, Word, Zero};
28+
use crate::{modular::BoxedMontyForm, HasMontyForm, Integer, Limb, NonZero, Word, Zero};
2929
use alloc::{boxed::Box, vec, vec::Vec};
3030
use core::fmt;
3131
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
@@ -343,6 +343,10 @@ impl num_traits::One for BoxedUint {
343343
}
344344
}
345345

346+
impl HasMontyForm for BoxedUint {
347+
type MontyForm = BoxedMontyForm;
348+
}
349+
346350
#[cfg(feature = "zeroize")]
347351
impl Zeroize for BoxedUint {
348352
fn zeroize(&mut self) {

0 commit comments

Comments
 (0)