use super::{BoxedLimbs, Elem, PublicModulus, Unencoded, N0};
use crate::{
bits::BitLength,
cpu, error,
limb::{self, Limb, LimbMask, LIMB_BITS},
polyfill::LeadingZerosStripped,
};
use core::marker::PhantomData;
pub const MODULUS_MIN_LIMBS: usize = 4;
pub const MODULUS_MAX_LIMBS: usize = super::super::BIGINT_MODULUS_MAX_LIMBS;
pub struct OwnedModulus<M> {
limbs: BoxedLimbs<M>,
n0: N0,
len_bits: BitLength,
}
impl<M: PublicModulus> Clone for OwnedModulus<M> {
fn clone(&self) -> Self {
Self {
limbs: self.limbs.clone(),
n0: self.n0,
len_bits: self.len_bits,
}
}
}
impl<M> OwnedModulus<M> {
pub(crate) fn from_be_bytes(input: untrusted::Input) -> Result<Self, error::KeyRejected> {
let n = BoxedLimbs::positive_minimal_width_from_be_bytes(input)?;
if n.len() > MODULUS_MAX_LIMBS {
return Err(error::KeyRejected::too_large());
}
if n.len() < MODULUS_MIN_LIMBS {
return Err(error::KeyRejected::unexpected_error());
}
if limb::limbs_are_even_constant_time(&n) != LimbMask::False {
return Err(error::KeyRejected::invalid_component());
}
if limb::limbs_less_than_limb_constant_time(&n, 3) != LimbMask::False {
return Err(error::KeyRejected::unexpected_error());
}
#[allow(clippy::useless_conversion)]
let n0 = {
prefixed_extern! {
fn bn_neg_inv_mod_r_u64(n: u64) -> u64;
}
let mut n_mod_r: u64 = u64::from(n[0]);
if N0::LIMBS_USED == 2 {
debug_assert_eq!(LIMB_BITS, 32);
n_mod_r |= u64::from(n[1]) << 32;
}
N0::precalculated(unsafe { bn_neg_inv_mod_r_u64(n_mod_r) })
};
let len_bits = limb::limbs_minimal_bits(&n);
Ok(Self {
limbs: n,
n0,
len_bits,
})
}
pub fn verify_less_than<L>(&self, l: &Modulus<L>) -> Result<(), error::Unspecified> {
if self.len_bits() > l.len_bits()
|| (self.limbs.len() == l.limbs().len()
&& limb::limbs_less_than_limbs_consttime(&self.limbs, l.limbs()) != LimbMask::True)
{
return Err(error::Unspecified);
}
Ok(())
}
pub fn to_elem<L>(&self, l: &Modulus<L>) -> Result<Elem<L, Unencoded>, error::Unspecified> {
self.verify_less_than(l)?;
let mut limbs = BoxedLimbs::zero(l.limbs.len());
limbs[..self.limbs.len()].copy_from_slice(&self.limbs);
Ok(Elem {
limbs,
encoding: PhantomData,
})
}
pub(crate) fn modulus(&self, cpu_features: cpu::Features) -> Modulus<M> {
Modulus {
limbs: &self.limbs,
n0: self.n0,
len_bits: self.len_bits,
m: PhantomData,
cpu_features,
}
}
pub fn len_bits(&self) -> BitLength {
self.len_bits
}
}
impl<M: PublicModulus> OwnedModulus<M> {
pub fn be_bytes(&self) -> LeadingZerosStripped<impl ExactSizeIterator<Item = u8> + Clone + '_> {
LeadingZerosStripped::new(limb::unstripped_be_bytes(&self.limbs))
}
}
pub struct Modulus<'a, M> {
limbs: &'a [Limb],
n0: N0,
len_bits: BitLength,
m: PhantomData<M>,
cpu_features: cpu::Features,
}
impl<M> Modulus<'_, M> {
pub(super) fn oneR(&self, out: &mut [Limb]) {
assert_eq!(self.limbs.len(), out.len());
let r = self.limbs.len() * LIMB_BITS;
limb::limbs_negative_odd(out, self.limbs);
let lg_m = self.len_bits().as_bits();
let leading_zero_bits_in_m = r - lg_m;
if leading_zero_bits_in_m != 0 {
debug_assert!(leading_zero_bits_in_m < LIMB_BITS);
*out.last_mut().unwrap() &= (!0) >> leading_zero_bits_in_m;
for _ in 0..leading_zero_bits_in_m {
limb::limbs_double_mod(out, self.limbs)
}
}
}
pub(super) fn zero<E>(&self) -> Elem<M, E> {
Elem {
limbs: BoxedLimbs::zero(self.limbs.len()),
encoding: PhantomData,
}
}
#[inline]
pub(super) fn limbs(&self) -> &[Limb] {
self.limbs
}
#[inline]
pub(super) fn n0(&self) -> &N0 {
&self.n0
}
pub fn len_bits(&self) -> BitLength {
self.len_bits
}
#[inline]
pub(crate) fn cpu_features(&self) -> cpu::Features {
self.cpu_features
}
}