poulpy/math/src/ring/impl_u64/rescaling_rns.rs
math/src/ring/impl_u64/rescaling_rns.rsRaw
1use crate::ring::RingRNS;
2use crate::poly::PolyRNS;
3use crate::modulus::barrett::Barrett;
4use crate::modulus::ONCE;
5extern crate test;
6
7impl RingRNS<'_, u64>{
8
9 /// Updates b to floor(b / q[b.level()]).
10 /// Expects a and b to be in the NTT domain.
11 pub fn div_floor_by_last_modulus_ntt(&self, a: &PolyRNS<u64>, buf: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>){
12 assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1);
13 let level = self.level();
14 self.0[level].intt::<true>(a.at(level), buf.at_mut(0));
15 let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
16 let (buf_ntt_q_scaling, buf_ntt_qi_scaling) = buf.0.split_at_mut(1);
17 for (i, r) in self.0[0..level].iter().enumerate(){
18 r.ntt::<true>(&buf_ntt_q_scaling[0], &mut buf_ntt_qi_scaling[0]);
19 r.sum_aqqmb_prod_c_scalar_barrett::<ONCE>(&buf_ntt_qi_scaling[0], a.at(i), &rescaling_constants[i], b.at_mut(i));
20 }
21 }
22
23 /// Updates b to floor(b / q[b.level()]).
24 pub fn div_floor_by_last_modulus(&self, a: &PolyRNS<u64>, b: &mut PolyRNS<u64>){
25 assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1);
26 let level = self.level();
27 let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
28 for (i, r) in self.0[0..level].iter().enumerate(){
29 r.sum_aqqmb_prod_c_scalar_barrett::<ONCE>(a.at(level), a.at(i), &rescaling_constants[i], b.at_mut(i));
30 }
31 }
32}
33
34
35#[cfg(test)]
36mod tests {
37 use crate::ring::Ring;
38 use crate::ring::impl_u64::ring_rns::new_rings;
39 use super::*;
40
41 #[test]
42 fn test_div_floor_by_last_modulus_ntt() {
43 let n = 1<<10;
44 let moduli: Vec<u64> = vec![0x1fffffffffe00001u64, 0x1fffffffffc80001u64];
45 let rings: Vec<Ring<u64>> = new_rings(n, moduli);
46 let ring_rns = RingRNS::new(&rings);
47
48 let a: PolyRNS<u64> = ring_rns.new_polyrns();
49 let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
50 let mut c: PolyRNS<u64> = ring_rns.new_polyrns();
51
52 ring_rns.div_floor_by_last_modulus_ntt(&a, &mut b, &mut c);
53
54 //assert!(m_precomp.mul_external::<ONCE>(y_mont, x) == (x as u128 * y as u128 % q as u128) as u64);
55 }
56}