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::BarrettRNS;
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::<false>(a.at(level), buf.at_mut(0));
15 let rescaling_constants: BarrettRNS<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::<false>(&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.0[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: BarrettRNS<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.0[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:crate::modulus::barrett::BarrettRNS<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.0[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: BarrettRNS<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.0[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 num_bigint::BigInt;
80 use crate::ring::Ring;
81 use crate::ring::impl_u64::ring_rns::new_rings;
82 use super::*;
83
84 #[test]
85 fn test_div_floor_by_last_modulus_ntt() {
86 let n = 1<<10;
87 let moduli: Vec<u64> = vec![0x1fffffffffc80001u64, 0x1fffffffffe00001u64];
88 let rings: Vec<Ring<u64>> = new_rings(n, moduli);
89 let ring_rns = RingRNS::new(&rings);
90
91 let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
92 let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
93 let mut c: PolyRNS<u64> = ring_rns.at_level(ring_rns.level()-1).new_polyrns();
94
95 // Allocates an rns poly with values [0..n]
96 let mut coeffs_a: Vec<BigInt> = (0..n).map(|i|{BigInt::from(i)}).collect();
97 ring_rns.from_bigint_inplace(&coeffs_a, 1, &mut a);
98
99 // Scales by q_level both a and coeffs_a
100 let scalar: u64 = ring_rns.0[ring_rns.level()].modulus.q;
101 ring_rns.mul_scalar_inplace::<ONCE>(&scalar, &mut a);
102 let scalar_big = BigInt::from(scalar);
103 coeffs_a.iter_mut().for_each(|a|{*a *= &scalar_big});
104
105 // Performs c = intt(ntt(a) / q_level)
106 ring_rns.ntt_inplace::<false>(&mut a);
107 ring_rns.div_floor_by_last_modulus_ntt(&a, &mut b, &mut c);
108 ring_rns.at_level(c.level()).intt_inplace::<false>(&mut c);
109
110 // Exports c to coeffs_c
111 let mut coeffs_c = vec![BigInt::from(0);c.n()];
112 ring_rns.at_level(c.level()).to_bigint_inplace(&c, 1, &mut coeffs_c);
113
114 // Performs floor division on a
115 coeffs_a.iter_mut().for_each(|a|{*a /= &scalar_big});
116
117 assert!(coeffs_a == coeffs_c);
118 }
119}