use crate::{FieldParameters, MontyParameters};
#[inline]
pub(crate) const fn to_monty<MP: MontyParameters>(x: u32) -> u32 {
(((x as u64) << MP::MONTY_BITS) % MP::PRIME as u64) as u32
}
#[inline]
pub(crate) const fn to_monty_64<MP: MontyParameters>(x: u64) -> u32 {
(((x as u128) << MP::MONTY_BITS) % MP::PRIME as u128) as u32
}
#[inline]
#[must_use]
pub(crate) const fn from_monty<MP: MontyParameters>(x: u32) -> u32 {
monty_reduce::<MP>(x as u64)
}
#[inline]
pub(crate) const fn halve_u32<FP: FieldParameters>(input: u32) -> u32 {
let shr = input >> 1;
let lo_bit = input & 1;
let shr_corr = shr + FP::HALF_P_PLUS_1;
if lo_bit == 0 {
shr
} else {
shr_corr
}
}
#[inline]
#[must_use]
pub(crate) const fn monty_reduce<MP: MontyParameters>(x: u64) -> u32 {
let t = x.wrapping_mul(MP::MONTY_MU as u64) & (MP::MONTY_MASK as u64);
let u = t * (MP::PRIME as u64);
let (x_sub_u, over) = x.overflowing_sub(u);
let x_sub_u_hi = (x_sub_u >> MP::MONTY_BITS) as u32;
let corr = if over { MP::PRIME } else { 0 };
x_sub_u_hi.wrapping_add(corr)
}