fixed div_floor rns scaling

This commit is contained in:
Jean-Philippe Bossuat
2025-01-06 23:47:17 +01:00
parent 7e4ca491c7
commit 3db800f4ce
2 changed files with 50 additions and 37 deletions

View File

@@ -14,6 +14,7 @@ impl RingRNS<u64> {
buf: &mut PolyRNS<u64>, buf: &mut PolyRNS<u64>,
b: &mut PolyRNS<u64>, b: &mut PolyRNS<u64>,
) { ) {
debug_assert!(self.level() != 0, "invalid call: self.level()=0");
debug_assert!( debug_assert!(
self.level() <= a.level(), self.level() <= a.level(),
"invalid input a: self.level()={} > a.level()={}", "invalid input a: self.level()={} > a.level()={}",
@@ -21,10 +22,10 @@ impl RingRNS<u64> {
a.level() a.level()
); );
debug_assert!( debug_assert!(
b.level() >= a.level() - 1, b.level() >= self.level() - 1,
"invalid input b: b.level()={} < a.level()-1={}", "invalid input b: b.level()={} < self.level()-1={}",
b.level(), b.level(),
a.level() - 1 self.level() - 1
); );
let level = self.level(); let level = self.level();
@@ -102,26 +103,29 @@ impl RingRNS<u64> {
buf: &mut PolyRNS<u64>, buf: &mut PolyRNS<u64>,
c: &mut PolyRNS<u64>, c: &mut PolyRNS<u64>,
) { ) {
println!("{:?}", buf);
debug_assert!( debug_assert!(
self.level() <= a.level(), nb_moduli <= self.level(),
"invalid input nb_moduli: nb_moduli={} > a.level()={}",
nb_moduli,
a.level()
);
debug_assert!(
a.level() <= self.level(),
"invalid input a: self.level()={} > a.level()={}", "invalid input a: self.level()={} > a.level()={}",
self.level(), self.level(),
a.level() a.level()
); );
debug_assert!( debug_assert!(
c.level() >= a.level() - 1, buf.level() >= self.level() - 1,
"invalid input b: b.level()={} < a.level()-1={}", "invalid input buf: buf.level()={} < a.level()-1={}",
c.level(), buf.level(),
a.level() - 1 a.level() - 1
); );
debug_assert!( debug_assert!(
nb_moduli <= a.level(), c.level() >= self.level() - nb_moduli,
"invalid input nb_moduli: nb_moduli={} > a.level()={}", "invalid input c: c.level()={} < c.level()-nb_moduli={}",
nb_moduli, c.level(),
a.level() a.level() - nb_moduli
); );
if nb_moduli == 0 { if nb_moduli == 0 {
@@ -140,11 +144,21 @@ impl RingRNS<u64> {
}); });
self.at_level(self.level() - nb_moduli).ntt::<false>(buf, c); self.at_level(self.level() - nb_moduli).ntt::<false>(buf, c);
} else { } else {
self.div_floor_by_last_modulus::<false>(a, buf, c);
(1..nb_moduli).for_each(|i| { let empty_buf: &mut PolyRNS<u64> = &mut PolyRNS::<u64>::default();
if nb_moduli == 1{
self.div_floor_by_last_modulus::<false>(a, empty_buf, c);
}else{
self.div_floor_by_last_modulus::<false>(a, empty_buf, buf);
}
(1..nb_moduli-1).for_each(|i| {
self.at_level(self.level() - i) self.at_level(self.level() - i)
.div_floor_by_last_modulus_inplace::<false>(buf, c) .div_floor_by_last_modulus_inplace::<false>(empty_buf, buf);
}); });
self.at_level(self.level()-nb_moduli+1).div_floor_by_last_modulus::<false>(buf, empty_buf, c);
} }
} }
} }
@@ -168,17 +182,20 @@ impl RingRNS<u64> {
nb_moduli, nb_moduli,
a.level() a.level()
); );
if nb_moduli == 0{
return
}
if NTT { if NTT {
self.intt::<false>(a, buf); self.intt::<false>(a, buf);
(0..nb_moduli).for_each(|i| { (0..nb_moduli).for_each(|i| {
self.at_level(self.level() - i) self.at_level(self.level() - i)
.div_floor_by_last_modulus_inplace::<false>(&mut PolyRNS::<u64>::default(), buf) .div_floor_by_last_modulus_inplace::<false>(&mut PolyRNS::<u64>::default(), buf)
}); });
self.at_level(self.level() - nb_moduli).ntt::<false>(buf, a); self.at_level(self.level() - nb_moduli+1).ntt::<false>(buf, a);
} else { } else {
(0..nb_moduli).for_each(|i| { (0..nb_moduli).for_each(|i| {
self.at_level(self.level() - i) self.at_level(self.level() - i)
.div_floor_by_last_modulus_inplace::<false>(buf, a) .div_floor_by_last_modulus_inplace::<false>(buf, a);
}); });
} }
} }

View File

@@ -10,14 +10,14 @@ fn rescaling_rns_u64() {
let moduli: Vec<u64> = vec![0x1fffffffffc80001u64, 0x1fffffffffe00001u64, 0x1fffffffffb40001, 0x1fffffffff500001]; let moduli: Vec<u64> = vec![0x1fffffffffc80001u64, 0x1fffffffffe00001u64, 0x1fffffffffb40001, 0x1fffffffff500001];
let ring_rns: RingRNS<u64> = RingRNS::new(n, moduli); let ring_rns: RingRNS<u64> = RingRNS::new(n, moduli);
//test_div_floor_by_last_modulus::<false>(&ring_rns); test_div_floor_by_last_modulus::<false>(&ring_rns);
//test_div_floor_by_last_modulus::<true>(&ring_rns); test_div_floor_by_last_modulus::<true>(&ring_rns);
test_div_floor_by_last_modulus_inplace::<false>(&ring_rns); test_div_floor_by_last_modulus_inplace::<false>(&ring_rns);
//test_div_floor_by_last_modulus_inplace::<true>(&ring_rns); test_div_floor_by_last_modulus_inplace::<true>(&ring_rns);
//test_div_floor_by_last_moduli::<false>(&ring_rns); test_div_floor_by_last_moduli::<false>(&ring_rns);
//test_div_floor_by_last_moduli::<true>(&ring_rns); test_div_floor_by_last_moduli::<true>(&ring_rns);
//test_div_floor_by_last_moduli_inplace::<false>(&ring_rns); test_div_floor_by_last_moduli_inplace::<false>(&ring_rns);
//test_div_floor_by_last_moduli_inplace::<true>(&ring_rns); test_div_floor_by_last_moduli_inplace::<true>(&ring_rns);
} }
fn test_div_floor_by_last_modulus<const NTT: bool>(ring_rns: &RingRNS<u64>) { fn test_div_floor_by_last_modulus<const NTT: bool>(ring_rns: &RingRNS<u64>) {
@@ -83,8 +83,6 @@ fn test_div_floor_by_last_modulus_inplace<const NTT: bool>(ring_rns: &RingRNS<u6
.at_level(a.level()) .at_level(a.level())
.to_bigint_inplace(&a, 1, &mut coeffs_a); .to_bigint_inplace(&a, 1, &mut coeffs_a);
println!("{:?}", &coeffs_a[..8]);
// Performs c = intt(ntt(a) / q_level) // Performs c = intt(ntt(a) / q_level)
if NTT { if NTT {
ring_rns.ntt_inplace::<false>(&mut a); ring_rns.ntt_inplace::<false>(&mut a);
@@ -112,9 +110,6 @@ fn test_div_floor_by_last_modulus_inplace<const NTT: bool>(ring_rns: &RingRNS<u6
} }
}); });
println!("{:?}", &coeffs_a[..8]);
println!("{:?}", &coeffs_c[..8]);
assert!(coeffs_a == coeffs_c, "test_div_floor_by_last_modulus_inplace"); assert!(coeffs_a == coeffs_c, "test_div_floor_by_last_modulus_inplace");
} }
@@ -122,11 +117,11 @@ fn test_div_floor_by_last_moduli<const NTT: bool>(ring_rns: &RingRNS<u64>) {
let seed: [u8; 32] = [0; 32]; let seed: [u8; 32] = [0; 32];
let mut source: Source = Source::new(seed); let mut source: Source = Source::new(seed);
let nb_moduli: usize = ring_rns.level()-1; let nb_moduli: usize = ring_rns.level();
let mut a: PolyRNS<u64> = ring_rns.new_polyrns(); let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
let mut b: PolyRNS<u64> = ring_rns.new_polyrns(); let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
let mut c: PolyRNS<u64> = ring_rns.at_level(ring_rns.level() - 1).new_polyrns(); let mut c: PolyRNS<u64> = ring_rns.at_level(ring_rns.level() - nb_moduli).new_polyrns();
// Allocates a random PolyRNS // Allocates a random PolyRNS
ring_rns.fill_uniform(&mut source, &mut a); ring_rns.fill_uniform(&mut source, &mut a);
@@ -156,7 +151,8 @@ fn test_div_floor_by_last_moduli<const NTT: bool>(ring_rns: &RingRNS<u64>) {
// Performs floor division on a // Performs floor division on a
let mut scalar_big = BigInt::from(1); let mut scalar_big = BigInt::from(1);
(0..nb_moduli).for_each(|i|{scalar_big *= BigInt::from(ring_rns.0[ring_rns.level()].modulus.q)}); (0..nb_moduli).for_each(|i|{scalar_big *= BigInt::from(ring_rns.0[ring_rns.level()-i].modulus.q)});
coeffs_a.iter_mut().for_each(|a| { coeffs_a.iter_mut().for_each(|a| {
// Emulates floor division in [0, q-1] and maps to [-(q-1)/2, (q-1)/2-1] // Emulates floor division in [0, q-1] and maps to [-(q-1)/2, (q-1)/2-1]
*a /= &scalar_big; *a /= &scalar_big;
@@ -172,7 +168,7 @@ fn test_div_floor_by_last_moduli_inplace<const NTT: bool>(ring_rns: &RingRNS<u64
let seed: [u8; 32] = [0; 32]; let seed: [u8; 32] = [0; 32];
let mut source: Source = Source::new(seed); let mut source: Source = Source::new(seed);
let nb_moduli: usize = ring_rns.level()-1; let nb_moduli: usize = ring_rns.level();
let mut a: PolyRNS<u64> = ring_rns.new_polyrns(); let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
let mut b: PolyRNS<u64> = ring_rns.new_polyrns(); let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
@@ -205,7 +201,7 @@ fn test_div_floor_by_last_moduli_inplace<const NTT: bool>(ring_rns: &RingRNS<u64
// Performs floor division on a // Performs floor division on a
let mut scalar_big = BigInt::from(1); let mut scalar_big = BigInt::from(1);
(0..nb_moduli).for_each(|i|{scalar_big *= BigInt::from(ring_rns.0[ring_rns.level()].modulus.q)}); (0..nb_moduli).for_each(|i|{scalar_big *= BigInt::from(ring_rns.0[ring_rns.level()-i].modulus.q)});
coeffs_a.iter_mut().for_each(|a| { coeffs_a.iter_mut().for_each(|a| {
// Emulates floor division in [0, q-1] and maps to [-(q-1)/2, (q-1)/2-1] // Emulates floor division in [0, q-1] and maps to [-(q-1)/2, (q-1)/2-1]
*a /= &scalar_big; *a /= &scalar_big;