Skip to content

Commit 62e19cf

Browse files
authored
Extract sealed PrecomputeInverterWithAdjuster trait (#449)
This trait is needed for the Montgomery form types to instantiate a Bernstein-Yang inverter which operates in the Montgomery domain, by passing R^2 as the adjuster. It's sealed for now, but could be made public if needed (which might be the case for writing generic trait bounds). It's extracted from the `PrecomputeInverter` trait, where previously the impl for `DynResidueParams` had a `todo!()` about what to do with this method. Splitting it out into a separate trait resolves this issue.
1 parent 71058cc commit 62e19cf

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

src/modular/dyn_residue/inv.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{DynResidue, DynResidueParams};
44
use crate::{
55
modular::{inv::inv_montgomery_form, BernsteinYangInverter},
66
traits::Invert,
7-
ConstChoice, Inverter, PrecomputeInverter, Uint,
7+
ConstChoice, Inverter, PrecomputeInverter, PrecomputeInverterWithAdjuster, Uint,
88
};
99
use core::fmt;
1010
use subtle::CtOption;
@@ -41,7 +41,7 @@ impl<const LIMBS: usize> Invert for DynResidue<LIMBS> {
4141

4242
impl<const LIMBS: usize> PrecomputeInverter for DynResidueParams<LIMBS>
4343
where
44-
Uint<LIMBS>: PrecomputeInverter<Output = Uint<LIMBS>>,
44+
Uint<LIMBS>: PrecomputeInverter<Output = Uint<LIMBS>> + PrecomputeInverterWithAdjuster,
4545
{
4646
type Inverter = DynResidueInverter<LIMBS>;
4747
type Output = DynResidue<LIMBS>;
@@ -52,10 +52,6 @@ where
5252
residue_params: *self,
5353
}
5454
}
55-
56-
fn precompute_inverter_with_adjuster(&self, _adjuster: &Self) -> Self::Inverter {
57-
todo!()
58-
}
5955
}
6056

6157
/// Bernstein-Yang inverter which inverts [`DynResidue`] types.

src/traits.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
//! Traits provided by this crate
22
3+
mod sealed;
4+
35
pub use num_traits::{
46
WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub,
57
};
68

9+
pub(crate) use sealed::PrecomputeInverterWithAdjuster;
10+
711
use crate::{Limb, NonZero};
812
use core::fmt::Debug;
913
use core::ops::{
@@ -156,10 +160,6 @@ pub trait PrecomputeInverter {
156160
///
157161
/// Returns `None` if `self` is even.
158162
fn precompute_inverter(&self) -> Self::Inverter;
159-
160-
/// Obtain a precomputed inverter for `&self` as the modulus, supplying a custom adjusting parameter (e.g. R^2 for
161-
/// when computing inversions in Montgomery form).
162-
fn precompute_inverter_with_adjuster(&self, adjuster: &Self) -> Self::Inverter;
163163
}
164164

165165
/// Trait impl'd by precomputed modular inverters.

src/traits/sealed.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Sealed traits.
2+
3+
use super::PrecomputeInverter;
4+
5+
/// Obtain a precomputed inverter which applies the given adjustment factor, i.e. for Montgomery form.
6+
pub trait PrecomputeInverterWithAdjuster: PrecomputeInverter {
7+
/// Obtain a precomputed inverter for `&self` as the modulus, supplying a custom adjusting parameter (e.g. R^2 for
8+
/// when computing inversions in Montgomery form).
9+
fn precompute_inverter_with_adjuster(&self, adjuster: &Self) -> Self::Inverter;
10+
}

src/uint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ mod rand;
4040

4141
use crate::{
4242
modular::BernsteinYangInverter, Bounded, Constants, Encoding, FixedInteger, Integer, Limb,
43-
PrecomputeInverter, Word, ZeroConstant,
43+
PrecomputeInverter, PrecomputeInverterWithAdjuster, Word, ZeroConstant,
4444
};
4545
use core::fmt;
4646
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};

src/uint/macros.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Macros used to define traits on aliases of `Uint`.
22
33
/// Impl the `Inverter` trait, where we need to compute the number of unsaturated limbs for a given number of bits.
4-
macro_rules! impl_inverter_trait {
4+
macro_rules! impl_precompute_inverter_trait {
55
($name:ident, $bits:expr) => {
66
/// Precompute a Bernstein-Yang inverter using `self` as the modulus. Panics if called on an even number!
77
impl PrecomputeInverter for $name {
@@ -16,7 +16,9 @@ macro_rules! impl_inverter_trait {
1616
fn precompute_inverter(&self) -> Self::Inverter {
1717
Self::precompute_inverter_with_adjuster(self, &Self::ONE)
1818
}
19+
}
1920

21+
impl PrecomputeInverterWithAdjuster for $name {
2022
fn precompute_inverter_with_adjuster(&self, adjuster: &Self) -> Self::Inverter {
2123
let (ret, is_some) = Self::Inverter::new(self, adjuster);
2224
assert!(bool::from(is_some), "modulus must be odd");
@@ -62,7 +64,7 @@ macro_rules! impl_uint_aliases {
6264
}
6365
}
6466

65-
impl_inverter_trait!($name, $bits);
67+
impl_precompute_inverter_trait!($name, $bits);
6668
)+
6769
};
6870
}

0 commit comments

Comments
 (0)