This commit is contained in:
Jean-Philippe Bossuat
2025-01-04 15:39:15 +01:00
parent 66a7513987
commit affb0b47ef
8 changed files with 217 additions and 17 deletions

View File

@@ -1,12 +1,12 @@
use crate::ring::RingRNS;
use crate::poly::PolyRNS;
use crate::poly::{Poly, PolyRNS};
use crate::modulus::barrett::Barrett;
use crate::modulus::ONCE;
extern crate test;
impl RingRNS<'_, u64>{
/// Updates b to floor(b / q[b.level()]).
/// Updates b to floor(a / q[b.level()]).
/// Expects a and b to be in the NTT domain.
pub fn div_floor_by_last_modulus_ntt(&self, a: &PolyRNS<u64>, buf: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>){
assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1);
@@ -21,6 +21,19 @@ impl RingRNS<'_, u64>{
}
/// Updates b to floor(b / q[b.level()]).
/// Expects b to be in the NTT domain.
pub fn div_floor_by_last_modulus_ntt_inplace(&self, buf: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>){
let level = self.level();
self.0[level].intt::<true>(b.at(level), buf.at_mut(0));
let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
let (buf_ntt_q_scaling, buf_ntt_qi_scaling) = buf.0.split_at_mut(1);
for (i, r) in self.0[0..level].iter().enumerate(){
r.ntt::<true>(&buf_ntt_q_scaling[0], &mut buf_ntt_qi_scaling[0]);
r.sum_aqqmb_prod_c_scalar_barrett_inplace::<ONCE>(&buf_ntt_qi_scaling[0], &rescaling_constants[i], b.at_mut(i));
}
}
/// Updates b to floor(a / q[b.level()]).
pub fn div_floor_by_last_modulus(&self, a: &PolyRNS<u64>, b: &mut PolyRNS<u64>){
assert!(b.level() >= a.level()-1, "invalid input b: b.level()={} < a.level()-1={}", b.level(), a.level()-1);
let level = self.level();
@@ -29,6 +42,35 @@ impl RingRNS<'_, u64>{
r.sum_aqqmb_prod_c_scalar_barrett::<ONCE>(a.at(level), a.at(i), &rescaling_constants[i], b.at_mut(i));
}
}
/// Updates a to floor(b / q[b.level()]).
pub fn div_floor_by_last_modulus_inplace(&self, a: &mut PolyRNS<u64>){
let level = self.level();
let rescaling_constants: Vec<Barrett<u64>> = self.rescaling_constant();
let (a_i, a_level) = a.split_at_mut(level);
for (i, r) in self.0[0..level].iter().enumerate(){
r.sum_aqqmb_prod_c_scalar_barrett_inplace::<ONCE>(&a_level[0], &rescaling_constants[i], &mut a_i[i]);
}
}
pub fn div_floor_by_last_moduli(&self, nb_moduli:usize, a: &PolyRNS<u64>, b: &mut PolyRNS<u64>){
if nb_moduli == 0{
if a != b{
b.copy(a);
}
}else{
self.div_floor_by_last_modulus(a, b);
(1..nb_moduli).for_each(|i|{self.at_level(self.level()-i).div_floor_by_last_modulus_inplace(b)});
}
}
pub fn div_floor_by_last_moduli_inplace(&self, nb_moduli:usize, a: &mut PolyRNS<u64>){
(0..nb_moduli).for_each(|i|{self.at_level(self.level()-i).div_floor_by_last_modulus_inplace(a)});
}
pub fn div_round_by_last_modulus_ntt(&self, a: &PolyRNS<u64>, buf: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>){
let level = self.level();
}
}