Skip to content

Commit 6336108

Browse files
committed
Make the Montgomery form type an associated type of Integer
1 parent 8b550fb commit 6336108

File tree

5 files changed

+20
-26
lines changed

5 files changed

+20
-26
lines changed

src/modular/boxed_monty_form.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ impl Retrieve for BoxedMontyForm {
252252
}
253253

254254
impl MontyFormLike for BoxedMontyForm {
255-
type Raw = BoxedUint;
255+
type Integer = BoxedUint;
256256
type Params = BoxedMontyParams;
257257

258-
fn new_params(modulus: Self::Raw) -> CtOption<Self::Params> {
258+
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
259259
BoxedMontyParams::new(modulus)
260260
}
261261

262-
fn new(value: Self::Raw, params: Self::Params) -> Self {
262+
fn new(value: Self::Integer, params: Self::Params) -> Self {
263263
BoxedMontyForm::new(value, params)
264264
}
265265

src/modular/monty_form.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ impl<const LIMBS: usize> Retrieve for MontyForm<LIMBS> {
209209
}
210210

211211
impl<const LIMBS: usize> MontyFormLike for MontyForm<LIMBS> {
212-
type Raw = Uint<LIMBS>;
212+
type Integer = Uint<LIMBS>;
213213
type Params = MontyParams<LIMBS>;
214214

215-
fn new_params(modulus: Self::Raw) -> CtOption<Self::Params> {
215+
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
216216
MontyParams::new(&modulus)
217217
}
218218

219-
fn new(value: Self::Raw, params: Self::Params) -> Self {
219+
fn new(value: Self::Integer, params: Self::Params) -> Self {
220220
MontyForm::new(&value, params)
221221
}
222222

src/traits.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ pub trait Integer:
141141
+ WrappingShr
142142
+ Zero
143143
{
144+
/// The corresponding Montgomery representation,
145+
/// optimized for the performance of modular operations at the price of a conversion overhead.
146+
type MontyForm: MontyFormLike<Integer = Self>;
147+
144148
/// The value `1`.
145149
fn one() -> Self;
146150

@@ -514,16 +518,10 @@ pub trait WideningMul<Rhs = Self>: Sized {
514518
fn widening_mul(&self, rhs: Rhs) -> Self::Output;
515519
}
516520

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-
523521
/// A representation of an integer optimized for the performance of modular operations.
524522
pub trait MontyFormLike {
525523
/// The original integer type.
526-
type Raw: HasMontyForm<MontyForm = Self>;
524+
type Integer: Integer<MontyForm = Self>;
527525

528526
/// The precomputed data needed for this representation.
529527
type Params: Clone;
@@ -532,10 +530,10 @@ pub trait MontyFormLike {
532530
///
533531
/// Can return `None` if `modulus` is not valid for the representation;
534532
/// see the documentation of the specific type for the requirements.
535-
fn new_params(modulus: Self::Raw) -> CtOption<Self::Params>;
533+
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params>;
536534

537535
/// Convert the value into the representation using precomputed data.
538-
fn new(value: Self::Raw, params: Self::Params) -> Self;
536+
fn new(value: Self::Integer, params: Self::Params) -> Self;
539537

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

src/uint.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ mod rand;
4141

4242
use crate::{
4343
modular::{BernsteinYangInverter, MontyForm},
44-
Bounded, ConstCtOption, Constants, Encoding, FixedInteger, HasMontyForm, Integer, Limb,
45-
NonZero, PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
44+
Bounded, ConstCtOption, Constants, Encoding, FixedInteger, Integer, Limb, NonZero,
45+
PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
4646
};
4747
use core::fmt;
4848
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
@@ -236,6 +236,8 @@ impl<const LIMBS: usize> FixedInteger for Uint<LIMBS> {
236236
}
237237

238238
impl<const LIMBS: usize> Integer for Uint<LIMBS> {
239+
type MontyForm = MontyForm<LIMBS>;
240+
239241
fn one() -> Self {
240242
Self::ONE
241243
}
@@ -261,10 +263,6 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
261263
}
262264
}
263265

264-
impl<const LIMBS: usize> HasMontyForm for Uint<LIMBS> {
265-
type MontyForm = MontyForm<LIMBS>;
266-
}
267-
268266
impl<const LIMBS: usize> ZeroConstant for Uint<LIMBS> {
269267
const ZERO: Self = Self::ZERO;
270268
}

src/uint/boxed.rs

Lines changed: 3 additions & 5 deletions
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::{modular::BoxedMontyForm, HasMontyForm, Integer, Limb, NonZero, Word, Zero};
28+
use crate::{modular::BoxedMontyForm, Integer, Limb, NonZero, Word, Zero};
2929
use alloc::{boxed::Box, vec, vec::Vec};
3030
use core::fmt;
3131
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
@@ -284,6 +284,8 @@ impl Default for BoxedUint {
284284
}
285285

286286
impl Integer for BoxedUint {
287+
type MontyForm = BoxedMontyForm;
288+
287289
fn one() -> Self {
288290
Self::one()
289291
}
@@ -343,10 +345,6 @@ impl num_traits::One for BoxedUint {
343345
}
344346
}
345347

346-
impl HasMontyForm for BoxedUint {
347-
type MontyForm = BoxedMontyForm;
348-
}
349-
350348
#[cfg(feature = "zeroize")]
351349
impl Zeroize for BoxedUint {
352350
fn zeroize(&mut self) {

0 commit comments

Comments
 (0)