refactoring for specific implementations

This commit is contained in:
Jean-Philippe Bossuat
2024-12-20 13:22:35 +01:00
parent a24ad55adc
commit 5dd371f6b0
23 changed files with 1671 additions and 527 deletions

View File

@@ -1,13 +1,8 @@
use crate::modulus::ReduceOnce;
use num_bigint::BigUint;
use num_traits::cast::ToPrimitive;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BarrettPrecomp<O>{
q: O,
lo:O,
hi:O,
pub q: O,
pub lo:O,
pub hi:O,
}
impl<O> BarrettPrecomp<O>{
@@ -23,26 +18,3 @@ impl<O> BarrettPrecomp<O>{
}
}
impl BarrettPrecomp<u64>{
pub fn new(q: u64) -> BarrettPrecomp<u64> {
let big_r: BigUint = (BigUint::from(1 as usize)<<((u64::BITS<<1) as usize)) / BigUint::from(q);
let lo: u64 = (&big_r & BigUint::from(u64::MAX)).to_u64().unwrap();
let hi: u64 = (big_r >> u64::BITS).to_u64().unwrap();
Self{q, lo, hi}
}
/// Returns lhs mod q.
#[inline(always)]
pub fn reduce(&self, lhs: u64) -> u64{
let mut r: u64 = self.reduce_lazy(lhs);
r.reduce_once_assign(self.q);
r
}
/// Returns lhs mod q in range [0, 2q-1].
#[inline(always)]
pub fn reduce_lazy(&self, lhs: u64) -> u64{
let (_, mhi) = lhs.widening_mul(self.hi);
lhs - mhi.wrapping_mul(self.q)
}
}