Skip to content

Commit 871b645

Browse files
authored
make Monty::new_params() take an Odd-wrapped modulus (#488)
1 parent 747f0ca commit 871b645

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

src/modular/boxed_monty_form.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use super::{
1212
reduction::{montgomery_reduction_boxed, montgomery_reduction_boxed_mut},
1313
Retrieve,
1414
};
15-
use crate::{BoxedUint, Integer, Limb, Monty, Odd, Word};
16-
use subtle::CtOption;
15+
use crate::{BoxedUint, Limb, Monty, Odd, Word};
1716

1817
#[cfg(feature = "std")]
1918
use std::sync::Arc;
@@ -233,12 +232,8 @@ impl Monty for BoxedMontyForm {
233232
type Integer = BoxedUint;
234233
type Params = BoxedMontyParams;
235234

236-
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
237-
let is_odd = modulus.is_odd();
238-
239-
// Note: instantiates a potentially invalid `Odd`, but guards with `CtOption`.
240-
let params = BoxedMontyParams::new(Odd(modulus));
241-
CtOption::new(params, is_odd)
235+
fn new_params(modulus: Odd<Self::Integer>) -> Self::Params {
236+
BoxedMontyParams::new(modulus)
242237
}
243238

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

src/modular/monty_form.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::{
1414
Retrieve,
1515
};
1616
use crate::{Limb, Monty, Odd, Uint, Word};
17-
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
17+
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
1818

1919
/// Parameters to efficiently go to/from the Montgomery form for an odd modulus provided at runtime.
2020
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -204,8 +204,8 @@ impl<const LIMBS: usize> Monty for MontyForm<LIMBS> {
204204
type Integer = Uint<LIMBS>;
205205
type Params = MontyParams<LIMBS>;
206206

207-
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
208-
Odd::new(modulus).map(MontyParams::new)
207+
fn new_params(modulus: Odd<Self::Integer>) -> Self::Params {
208+
MontyParams::new(modulus)
209209
}
210210

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

src/traits.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use num_traits::{
88

99
pub(crate) use sealed::PrecomputeInverterWithAdjuster;
1010

11-
use crate::{Limb, NonZero};
11+
use crate::{Limb, NonZero, Odd};
1212
use core::fmt::Debug;
1313
use core::ops::{
1414
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
@@ -553,9 +553,7 @@ pub trait Monty:
553553
type Params: Clone;
554554

555555
/// Create the precomputed data for Montgomery representation of integers modulo `modulus`.
556-
///
557-
/// `modulus` must be odd, otherwise returns `None`.
558-
fn new_params(modulus: Self::Integer) -> CtOption<Self::Params>;
556+
fn new_params(modulus: Odd<Self::Integer>) -> Self::Params;
559557

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

0 commit comments

Comments
 (0)