1use subtle::CtOption;
23use crate::{modular::inv::inv_montgomery_form, traits::Invert, CtChoice};
45use super::DynResidue;
67impl<const LIMBS: usize> DynResidue<LIMBS> {
8/// Computes the residue `self^-1` representing the multiplicative inverse of `self`.
9 /// I.e. `self * self^-1 = 1`.
10 /// If the number was invertible, the second element of the tuple is the truthy value,
11 /// otherwise it is the falsy value (in which case the first element's value is unspecified).
12pub const fn invert(&self) -> (Self, CtChoice) {
13let (montgomery_form, is_some) = inv_montgomery_form(
14&self.montgomery_form,
15&self.residue_params.modulus,
16&self.residue_params.r3,
17self.residue_params.mod_neg_inv,
18 );
1920let value = Self {
21 montgomery_form,
22 residue_params: self.residue_params,
23 };
2425 (value, is_some)
26 }
27}
2829impl<const LIMBS: usize> Invert for DynResidue<LIMBS> {
30type Output = CtOption<Self>;
31fn invert(&self) -> Self::Output {
32let (value, is_some) = self.invert();
33 CtOption::new(value, is_some.into())
34 }
35}