Added Barrett & fixed Montgomery, added tests

This commit is contained in:
Jean-Philippe Bossuat
2024-12-04 17:19:30 +01:00
parent 2f37ed24e3
commit ddee3d34f7
6 changed files with 97 additions and 17 deletions

View File

@@ -1,19 +1,28 @@
use num_bigint::BigUint;
use num_traits::cast::ToPrimitive;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BarrettPrecomp<O>(O, O);
impl<O> BarrettPrecomp<O>{
#[inline(always)]
pub fn new(a:O, b: O) -> Self{
Self(a, b)
}
#[inline(always)]
pub fn value_hi(&self) -> &O{
&self.0
&self.1
}
#[inline(always)]
pub fn value_lo(&self) -> &O{
&self.1
&self.0
}
}
impl BarrettPrecomp<u64>{
pub fn new(q: u64) -> BarrettPrecomp<u64> {
let mut big_r = BigUint::parse_bytes(b"100000000000000000000000000000000", 16).unwrap();
big_r = big_r / BigUint::from(q);
let lo = (&big_r & BigUint::from(u64::MAX)).to_u64().unwrap();
let hi = (big_r >> 64u64).to_u64().unwrap();
Self(lo, hi)
}
}