arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/src/modulus/barrett.rs
src/modulus/barrett.rsRaw
 1use num_bigint::BigUint;
 2use num_traits::cast::ToPrimitive;
 3
 4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
 5pub struct BarrettPrecomp<O>(O, O);
 6
 7impl<O> BarrettPrecomp<O>{
 8
 9    #[inline(always)]
10    pub fn value_hi(&self) -> &O{
11        &self.1
12    }
13
14    #[inline(always)]
15    pub fn value_lo(&self) -> &O{
16        &self.0
17    }
18}
19
20impl BarrettPrecomp<u64>{
21    pub fn new(q: u64) -> BarrettPrecomp<u64> {
22        let mut big_r = BigUint::parse_bytes(b"100000000000000000000000000000000", 16).unwrap();
23        big_r = big_r / BigUint::from(q);
24        let lo = (&big_r & BigUint::from(u64::MAX)).to_u64().unwrap();
25        let hi = (big_r >> 64u64).to_u64().unwrap();
26        Self(lo, hi)
27    }
28}