Skip to content

Commit

Permalink
make Monty::new_params() take an Odd-wrapped modulus (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri authored Dec 21, 2023
1 parent 747f0ca commit 871b645
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
11 changes: 3 additions & 8 deletions src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use super::{
reduction::{montgomery_reduction_boxed, montgomery_reduction_boxed_mut},
Retrieve,
};
use crate::{BoxedUint, Integer, Limb, Monty, Odd, Word};
use subtle::CtOption;
use crate::{BoxedUint, Limb, Monty, Odd, Word};

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

fn new_params(modulus: Self::Integer) -> CtOption<Self::Params> {
let is_odd = modulus.is_odd();

// Note: instantiates a potentially invalid `Odd`, but guards with `CtOption`.
let params = BoxedMontyParams::new(Odd(modulus));
CtOption::new(params, is_odd)
fn new_params(modulus: Odd<Self::Integer>) -> Self::Params {
BoxedMontyParams::new(modulus)
}

fn new(value: Self::Integer, params: Self::Params) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions src/modular/monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::{
Retrieve,
};
use crate::{Limb, Monty, Odd, Uint, Word};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

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

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

fn new(value: Self::Integer, params: Self::Params) -> Self {
Expand Down
6 changes: 2 additions & 4 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use num_traits::{

pub(crate) use sealed::PrecomputeInverterWithAdjuster;

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

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

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

0 comments on commit 871b645

Please sign in to comment.