crypto_bigint/uint/modular/runtime_mod/
runtime_inv.rs

1use subtle::CtOption;
2
3use crate::{modular::inv::inv_montgomery_form, traits::Invert, CtChoice};
4
5use super::DynResidue;
6
7impl<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).
12    pub const fn invert(&self) -> (Self, CtChoice) {
13        let (montgomery_form, is_some) = inv_montgomery_form(
14            &self.montgomery_form,
15            &self.residue_params.modulus,
16            &self.residue_params.r3,
17            self.residue_params.mod_neg_inv,
18        );
19
20        let value = Self {
21            montgomery_form,
22            residue_params: self.residue_params,
23        };
24
25        (value, is_some)
26    }
27}
28
29impl<const LIMBS: usize> Invert for DynResidue<LIMBS> {
30    type Output = CtOption<Self>;
31    fn invert(&self) -> Self::Output {
32        let (value, is_some) = self.invert();
33        CtOption::new(value, is_some.into())
34    }
35}