Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
reduction::{montgomery_reduction_boxed, montgomery_reduction_boxed_mut},
Retrieve,
};
use crate::{BoxedUint, ConstantTimeSelect, Integer, Limb, NonZero, Word};
use crate::{BoxedUint, ConstantTimeSelect, Integer, Limb, MontyFormLike, NonZero, Word};
use subtle::CtOption;

#[cfg(feature = "std")]
Expand Down Expand Up @@ -251,6 +251,27 @@ impl Retrieve for BoxedMontyForm {
}
}

impl MontyFormLike for BoxedMontyForm {
type Integer = BoxedUint;
type Params = BoxedMontyParams;

fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
BoxedMontyParams::new(modulus)
}

fn new(value: Self::Integer, params: Self::Params) -> Self {
BoxedMontyForm::new(value, params)
}

fn zero(params: Self::Params) -> Self {
BoxedMontyForm::zero(params)
}

fn one(params: Self::Params) -> Self {
BoxedMontyForm::one(params)
}
}

/// Convert the given integer into the Montgomery domain.
#[inline]
fn convert_to_montgomery(integer: &mut BoxedUint, params: &BoxedMontyParams) {
Expand Down
23 changes: 22 additions & 1 deletion src/modular/monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
reduction::montgomery_reduction,
Retrieve,
};
use crate::{Limb, NonZero, Uint, Word, Zero};
use crate::{Limb, MontyFormLike, NonZero, Uint, Word, Zero};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

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

impl<const LIMBS: usize> MontyFormLike for MontyForm<LIMBS> {
type Integer = Uint<LIMBS>;
type Params = MontyParams<LIMBS>;

fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
MontyParams::new(&modulus)
}

fn new(value: Self::Integer, params: Self::Params) -> Self {
MontyForm::new(&value, params)
}

fn zero(params: Self::Params) -> Self {
MontyForm::zero(params)
}

fn one(params: Self::Params) -> Self {
MontyForm::one(params)
}
}

impl<const LIMBS: usize, P: ConstMontyParams<LIMBS>> From<&ConstMontyForm<P, LIMBS>>
for MontyForm<LIMBS>
{
Expand Down
28 changes: 28 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ pub trait Integer:
+ WrappingShr
+ Zero
{
/// The corresponding Montgomery representation,
/// optimized for the performance of modular operations at the price of a conversion overhead.
type MontyForm: MontyFormLike<Integer = Self>;

/// The value `1`.
fn one() -> Self;

Expand Down Expand Up @@ -513,3 +517,27 @@ pub trait WideningMul<Rhs = Self>: Sized {
/// Perform widening multiplication.
fn widening_mul(&self, rhs: Rhs) -> Self::Output;
}

/// A representation of an integer optimized for the performance of modular operations.
pub trait MontyFormLike {
/// The original integer type.
type Integer: Integer<MontyForm = Self>;

/// The precomputed data needed for this representation.
type Params: Clone;

/// Create the precomputed data.
///
/// Can return `None` if `modulus` is not valid for the representation;
/// see the documentation of the specific type for the requirements.
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params>;

/// Convert the value into the representation using precomputed data.
fn new(value: Self::Integer, params: Self::Params) -> Self;

/// Returns zero in this representation.
fn zero(params: Self::Params) -> Self;

/// Returns one in this representation.
fn one(params: Self::Params) -> Self;
}
7 changes: 5 additions & 2 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ pub(crate) mod boxed;
mod rand;

use crate::{
modular::BernsteinYangInverter, Bounded, ConstCtOption, Constants, Encoding, FixedInteger,
Integer, Limb, NonZero, PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
modular::{BernsteinYangInverter, MontyForm},
Bounded, ConstCtOption, Constants, Encoding, FixedInteger, Integer, Limb, NonZero,
PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
};
use core::fmt;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
Expand Down Expand Up @@ -235,6 +236,8 @@ impl<const LIMBS: usize> FixedInteger for Uint<LIMBS> {
}

impl<const LIMBS: usize> Integer for Uint<LIMBS> {
type MontyForm = MontyForm<LIMBS>;

fn one() -> Self {
Self::ONE
}
Expand Down
4 changes: 3 additions & 1 deletion src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod sub_mod;
#[cfg(feature = "rand_core")]
mod rand;

use crate::{Integer, Limb, NonZero, Word, Zero};
use crate::{modular::BoxedMontyForm, Integer, Limb, NonZero, Word, Zero};
use alloc::{boxed::Box, vec, vec::Vec};
use core::fmt;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
Expand Down Expand Up @@ -284,6 +284,8 @@ impl Default for BoxedUint {
}

impl Integer for BoxedUint {
type MontyForm = BoxedMontyForm;

fn one() -> Self {
Self::one()
}
Expand Down