Skip to content

Commit acb69c4

Browse files
authored
Modular inversion cleanups (#740)
DRYs out some safegcd init by extracting a `const fn`
1 parent 682f17a commit acb69c4

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/modular/boxed_monty_form/inv.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ impl BoxedMontyForm {
1313
/// Computes `self^-1` representing the multiplicative inverse of `self`,
1414
/// i.e. `self * self^-1 = 1`.
1515
pub fn invert(&self) -> CtOption<Self> {
16-
let inverter = self.params.precompute_inverter();
17-
inverter.invert(self)
16+
self.params.precompute_inverter().invert(self)
1817
}
1918

2019
/// Computes `self^-1` representing the multiplicative inverse of `self`,
@@ -23,8 +22,7 @@ impl BoxedMontyForm {
2322
/// This version is variable-time with respect to the value of `self`, but constant-time with
2423
/// respect to `self`'s `params`.
2524
pub fn invert_vartime(&self) -> CtOption<Self> {
26-
let inverter = self.params.precompute_inverter();
27-
inverter.invert_vartime(self)
25+
self.params.precompute_inverter().invert_vartime(self)
2826
}
2927
}
3028

src/modular/monty_form/inv.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ where
2121
/// If the number was invertible, the second element of the tuple is the truthy value,
2222
/// otherwise it is the falsy value (in which case the first element's value is unspecified).
2323
pub const fn inv(&self) -> ConstCtOption<Self> {
24-
let inverter = <Odd<Uint<SAT_LIMBS>> as PrecomputeInverter>::Inverter::new(
25-
&self.params.modulus,
26-
&self.params.r2,
27-
);
28-
24+
let inverter = self.params.inverter();
2925
let maybe_inverse = inverter.inv(&self.montgomery_form);
3026
let (inverse, inverse_is_some) = maybe_inverse.components_ref();
3127

@@ -46,11 +42,7 @@ where
4642
/// This version is variable-time with respect to the value of `self`, but constant-time with
4743
/// respect to `self`'s `params`.
4844
pub const fn inv_vartime(&self) -> ConstCtOption<Self> {
49-
let inverter = <Odd<Uint<SAT_LIMBS>> as PrecomputeInverter>::Inverter::new(
50-
&self.params.modulus,
51-
&self.params.r2,
52-
);
53-
45+
let inverter = self.params.inverter();
5446
let maybe_inverse = inverter.inv_vartime(&self.montgomery_form);
5547
let (inverse, inverse_is_some) = maybe_inverse.components_ref();
5648

@@ -81,6 +73,20 @@ where
8173
}
8274
}
8375

76+
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> MontyParams<SAT_LIMBS>
77+
where
78+
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
79+
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
80+
Output = Uint<SAT_LIMBS>,
81+
>,
82+
{
83+
/// Create a modular inverter for the modulus of these params.
84+
// TODO(tarcieri): make `pub`?
85+
const fn inverter(&self) -> SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS> {
86+
SafeGcdInverter::new(&self.modulus, &self.r2)
87+
}
88+
}
89+
8490
impl<const LIMBS: usize> PrecomputeInverter for MontyParams<LIMBS>
8591
where
8692
Odd<Uint<LIMBS>>:

0 commit comments

Comments
 (0)