Skip to content

Commit

Permalink
Modular inversion cleanups (#740)
Browse files Browse the repository at this point in the history
DRYs out some safegcd init by extracting a `const fn`
  • Loading branch information
tarcieri authored Jan 18, 2025
1 parent 682f17a commit acb69c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
6 changes: 2 additions & 4 deletions src/modular/boxed_monty_form/inv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ impl BoxedMontyForm {
/// Computes `self^-1` representing the multiplicative inverse of `self`,
/// i.e. `self * self^-1 = 1`.
pub fn invert(&self) -> CtOption<Self> {
let inverter = self.params.precompute_inverter();
inverter.invert(self)
self.params.precompute_inverter().invert(self)
}

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

Expand Down
26 changes: 16 additions & 10 deletions src/modular/monty_form/inv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ where
/// If the number was invertible, the second element of the tuple is the truthy value,
/// otherwise it is the falsy value (in which case the first element's value is unspecified).
pub const fn inv(&self) -> ConstCtOption<Self> {
let inverter = <Odd<Uint<SAT_LIMBS>> as PrecomputeInverter>::Inverter::new(
&self.params.modulus,
&self.params.r2,
);

let inverter = self.params.inverter();
let maybe_inverse = inverter.inv(&self.montgomery_form);
let (inverse, inverse_is_some) = maybe_inverse.components_ref();

Expand All @@ -46,11 +42,7 @@ where
/// This version is variable-time with respect to the value of `self`, but constant-time with
/// respect to `self`'s `params`.
pub const fn inv_vartime(&self) -> ConstCtOption<Self> {
let inverter = <Odd<Uint<SAT_LIMBS>> as PrecomputeInverter>::Inverter::new(
&self.params.modulus,
&self.params.r2,
);

let inverter = self.params.inverter();
let maybe_inverse = inverter.inv_vartime(&self.montgomery_form);
let (inverse, inverse_is_some) = maybe_inverse.components_ref();

Expand Down Expand Up @@ -81,6 +73,20 @@ where
}
}

impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> MontyParams<SAT_LIMBS>
where
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<
Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>,
Output = Uint<SAT_LIMBS>,
>,
{
/// Create a modular inverter for the modulus of these params.
// TODO(tarcieri): make `pub`?
const fn inverter(&self) -> SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS> {
SafeGcdInverter::new(&self.modulus, &self.r2)
}
}

impl<const LIMBS: usize> PrecomputeInverter for MontyParams<LIMBS>
where
Odd<Uint<LIMBS>>:
Expand Down

0 comments on commit acb69c4

Please sign in to comment.