poulpy/math/src/ring/impl_u64/rescaling_rns.rs
math/src/ring/impl_u64/rescaling_rns.rsRaw
1use crate::ring::RingRNS;
2use crate::poly::{Poly, PolyRNS};
3use crate::modulus::barrett::Barrett;
4use crate::modulus::ONCE;
5extern crate test;
6
7impl RingRNS<'_, u64>{
8
9 /// Updates b to floor(a / 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 /// Expects b to be in the NTT domain.
25 pub fn div_floor_by_last_modulus_ntt_inplace(&self, buf: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>){
26 let level = self.level();
27 self.0[level].intt::<true>(b.at(level), buf.at_mut(0));
28 let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
29 let (buf_ntt_q_scaling, buf_ntt_qi_scaling) = buf.0.split_at_mut(1);
30 for (i, r) in self.0[0..level].iter().enumerate(){
31 r.ntt::<true>(&buf_ntt_q_scaling[0], &mut buf_ntt_qi_scaling[0]);
32 r.sum_aqqmb_prod_c_scalar_barrett_inplace::<ONCE>(&buf_ntt_qi_scaling[0], &rescaling_constants[i], b.at_mut(i));
33 }
34 }
35
36 /// Updates b to floor(a / q[b.level()]).
37 pub fn div_floor_by_last_modulus(&self, a: &PolyRNS<u64>, b: &mut PolyRNS<u64>){
38 assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1);
39 let level = self.level();
40 let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
41 for (i, r) in self.0[0..level].iter().enumerate(){
42 r.sum_aqqmb_prod_c_scalar_barrett::<ONCE>(a.at(level), a.at(i), &rescaling_constants[i], b.at_mut(i));
43 }
44 }
45
46 /// Updates a to floor(b / q[b.level()]).
47 pub fn div_floor_by_last_modulus_inplace(&self, a: &mut PolyRNS<u64>){
48 let level = self.level();
49 let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
50 let (a_i, a_level) = a.split_at_mut(level);
51 for (i, r) in self.0[0..level].iter().enumerate(){
52 r.sum_aqqmb_prod_c_scalar_barrett_inplace::<ONCE>(&a_level[0], &rescaling_constants[i], &mut a_i[i]);
53 }
54 }
55
56 pub fn div_floor_by_last_moduli(&self, nb_moduli:usize, a: &PolyRNS<u64>, b: &mut PolyRNS<u64>){
57 if nb_moduli == 0{
58 if a != b{
59 b.copy(a);
60 }
61 }else{
62 self.div_floor_by_last_modulus(a, b);
63 (1..nb_moduli).for_each(|i|{self.at_level(self.level()-i).div_floor_by_last_modulus_inplace(b)});
64 }
65 }
66
67 pub fn div_floor_by_last_moduli_inplace(&self, nb_moduli:usize, a: &mut PolyRNS<u64>){
68 (0..nb_moduli).for_each(|i|{self.at_level(self.level()-i).div_floor_by_last_modulus_inplace(a)});
69 }
70
71 pub fn div_round_by_last_modulus_ntt(&self, a: &PolyRNS<u64>, buf: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>){
72 let level = self.level();
73 }
74}
75
76
77#[cfg(test)]
78mod tests {
79 use crate::ring::Ring;
80 use crate::ring::impl_u64::ring_rns::new_rings;
81 use super::*;
82
83 #[test]
84 fn test_div_floor_by_last_modulus_ntt() {
85 let n = 1<<10;
86 let moduli: Vec<u64> = vec![0x1fffffffffe00001u64, 0x1fffffffffc80001u64];
87 let rings: Vec<Ring<u64>> = new_rings(n, moduli);
88 let ring_rns = RingRNS::new(&rings);
89
90 let a: PolyRNS<u64> = ring_rns.new_polyrns();
91 let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
92 let mut c: PolyRNS<u64> = ring_rns.new_polyrns();
93
94 ring_rns.div_floor_by_last_modulus_ntt(&a, &mut b, &mut c);
95
96 //assert!(m_precomp.mul_external::<ONCE>(y_mont, x) == (x as u128 * y as u128 % q as u128) as u64);
97 }
98}