mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
fixed rescaling & added all tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use itertools::izip;
|
||||
use math::num_bigint::Div;
|
||||
use math::poly::PolyRNS;
|
||||
use math::poly::{Poly, PolyRNS};
|
||||
use math::ring::RingRNS;
|
||||
use num_bigint::BigInt;
|
||||
use sampling::source::Source;
|
||||
@@ -44,8 +44,34 @@ fn rescaling_rns_u64() {
|
||||
"test_div_by_last_modulus_inplace::<ROUND:true, NTT:false>",
|
||||
|| test_div_by_last_modulus_inplace::<true, false>(&ring_rns),
|
||||
);
|
||||
|
||||
//sub_test("test_div_by_last_moduli::<ROUND:false, NTT:false>", ||{test_div_by_last_moduli::<false, false>(&ring_rns)});
|
||||
sub_test("test_div_by_last_moduli::<ROUND:false, NTT:false>", || {
|
||||
test_div_by_last_moduli::<false, false>(&ring_rns)
|
||||
});
|
||||
sub_test("test_div_by_last_moduli::<ROUND:false, NTT:true>", || {
|
||||
test_div_by_last_moduli::<false, true>(&ring_rns)
|
||||
});
|
||||
sub_test("test_div_by_last_moduli::<ROUND:true, NTT:false>", || {
|
||||
test_div_by_last_moduli::<true, false>(&ring_rns)
|
||||
});
|
||||
sub_test("test_div_by_last_moduli::<ROUND:true, NTT:true>", || {
|
||||
test_div_by_last_moduli::<true, true>(&ring_rns)
|
||||
});
|
||||
sub_test(
|
||||
"test_div_by_last_moduli_inplace::<ROUND:false, NTT:false>",
|
||||
|| test_div_by_last_moduli_inplace::<false, false>(&ring_rns),
|
||||
);
|
||||
sub_test(
|
||||
"test_div_by_last_moduli_inplace::<ROUND:false, NTT:true>",
|
||||
|| test_div_by_last_moduli_inplace::<false, true>(&ring_rns),
|
||||
);
|
||||
sub_test(
|
||||
"test_div_by_last_moduli_inplace::<ROUND:true, NTT:false>",
|
||||
|| test_div_by_last_moduli_inplace::<true, false>(&ring_rns),
|
||||
);
|
||||
sub_test(
|
||||
"test_div_by_last_moduli_inplace::<ROUND:true, NTT:true>",
|
||||
|| test_div_by_last_moduli_inplace::<true, true>(&ring_rns),
|
||||
);
|
||||
}
|
||||
|
||||
fn sub_test<F: FnOnce()>(name: &str, f: F) {
|
||||
@@ -58,7 +84,7 @@ fn test_div_by_last_modulus<const ROUND: bool, const NTT: bool>(ring_rns: &RingR
|
||||
let mut source: Source = Source::new(seed);
|
||||
|
||||
let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut buf: [Poly<u64>; 2] = [ring_rns.new_poly(), ring_rns.new_poly()];
|
||||
let mut c: PolyRNS<u64> = ring_rns.at_level(ring_rns.level() - 1).new_polyrns();
|
||||
|
||||
// Allocates a random PolyRNS
|
||||
@@ -75,7 +101,7 @@ fn test_div_by_last_modulus<const ROUND: bool, const NTT: bool>(ring_rns: &RingR
|
||||
ring_rns.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
ring_rns.div_by_last_modulus::<ROUND, NTT>(&a, &mut b, &mut c);
|
||||
ring_rns.div_by_last_modulus::<ROUND, NTT>(&a, &mut buf, &mut c);
|
||||
|
||||
if NTT {
|
||||
ring_rns.at_level(c.level()).intt_inplace::<false>(&mut c);
|
||||
@@ -105,7 +131,7 @@ fn test_div_by_last_modulus_inplace<const ROUND: bool, const NTT: bool>(ring_rns
|
||||
let mut source: Source = Source::new(seed);
|
||||
|
||||
let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut buf: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut buf: [Poly<u64>; 2] = [ring_rns.new_poly(), ring_rns.new_poly()];
|
||||
|
||||
// Allocates a random PolyRNS
|
||||
ring_rns.fill_uniform(&mut source, &mut a);
|
||||
@@ -152,12 +178,13 @@ fn test_div_by_last_moduli<const ROUND: bool, const NTT: bool>(ring_rns: &RingRN
|
||||
let seed: [u8; 32] = [0; 32];
|
||||
let mut source: Source = Source::new(seed);
|
||||
|
||||
let nb_moduli: usize = ring_rns.level();
|
||||
let nb_moduli_dropped: usize = ring_rns.level();
|
||||
|
||||
let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut buf: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut buf0: [Poly<u64>; 2] = [ring_rns.new_poly(), ring_rns.new_poly()];
|
||||
let mut buf1: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut c: PolyRNS<u64> = ring_rns
|
||||
.at_level(ring_rns.level() - nb_moduli)
|
||||
.at_level(ring_rns.level() - nb_moduli_dropped)
|
||||
.new_polyrns();
|
||||
|
||||
// Allocates a random PolyRNS
|
||||
@@ -174,7 +201,7 @@ fn test_div_by_last_moduli<const ROUND: bool, const NTT: bool>(ring_rns: &RingRN
|
||||
ring_rns.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
ring_rns.div_by_last_moduli::<ROUND, NTT>(nb_moduli, &a, &mut buf, &mut c);
|
||||
ring_rns.div_by_last_moduli::<ROUND, NTT>(nb_moduli_dropped, &a, &mut buf0, &mut buf1, &mut c);
|
||||
|
||||
if NTT {
|
||||
ring_rns.at_level(c.level()).intt_inplace::<false>(&mut c);
|
||||
@@ -188,7 +215,7 @@ fn test_div_by_last_moduli<const ROUND: bool, const NTT: bool>(ring_rns: &RingRN
|
||||
|
||||
// Performs floor division on a
|
||||
let mut scalar_big = BigInt::from(1);
|
||||
(0..nb_moduli)
|
||||
(0..nb_moduli_dropped)
|
||||
.for_each(|i| scalar_big *= BigInt::from(ring_rns.0[ring_rns.level() - i].modulus.q));
|
||||
coeffs_a.iter_mut().for_each(|a| {
|
||||
if ROUND {
|
||||
@@ -201,15 +228,15 @@ fn test_div_by_last_moduli<const ROUND: bool, const NTT: bool>(ring_rns: &RingRN
|
||||
izip!(coeffs_a, coeffs_c).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
|
||||
/*
|
||||
fn test_div_floor_by_last_moduli_inplace<const NTT: bool>(ring_rns: &RingRNS<u64>) {
|
||||
fn test_div_by_last_moduli_inplace<const ROUND: bool, const NTT: bool>(ring_rns: &RingRNS<u64>) {
|
||||
let seed: [u8; 32] = [0; 32];
|
||||
let mut source: Source = Source::new(seed);
|
||||
|
||||
let nb_moduli: usize = ring_rns.level();
|
||||
let nb_moduli_dropped: usize = ring_rns.level();
|
||||
|
||||
let mut a: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut b: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
let mut buf0: [Poly<u64>; 2] = [ring_rns.new_poly(), ring_rns.new_poly()];
|
||||
let mut buf1: PolyRNS<u64> = ring_rns.new_polyrns();
|
||||
|
||||
// Allocates a random PolyRNS
|
||||
ring_rns.fill_uniform(&mut source, &mut a);
|
||||
@@ -225,23 +252,36 @@ fn test_div_floor_by_last_moduli_inplace<const NTT: bool>(ring_rns: &RingRNS<u64
|
||||
ring_rns.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
ring_rns.div_floor_by_last_moduli_inplace::<NTT>(nb_moduli, &mut b, &mut a);
|
||||
ring_rns.div_by_last_moduli_inplace::<ROUND, NTT>(
|
||||
nb_moduli_dropped,
|
||||
&mut buf0,
|
||||
&mut buf1,
|
||||
&mut a,
|
||||
);
|
||||
|
||||
if NTT {
|
||||
ring_rns.at_level(a.level()-nb_moduli).intt_inplace::<false>(&mut a);
|
||||
ring_rns
|
||||
.at_level(a.level() - nb_moduli_dropped)
|
||||
.intt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
// Exports c to coeffs_c
|
||||
let mut coeffs_c = vec![BigInt::from(0); a.n()];
|
||||
ring_rns
|
||||
.at_level(a.level()-nb_moduli)
|
||||
.at_level(a.level() - nb_moduli_dropped)
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_c);
|
||||
|
||||
// Performs floor division on a
|
||||
let mut scalar_big = BigInt::from(1);
|
||||
(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| {a.div_floor(&scalar_big)});
|
||||
(0..nb_moduli_dropped)
|
||||
.for_each(|i| scalar_big *= BigInt::from(ring_rns.0[ring_rns.level() - i].modulus.q));
|
||||
coeffs_a.iter_mut().for_each(|a| {
|
||||
if ROUND {
|
||||
*a = a.div_round(&scalar_big);
|
||||
} else {
|
||||
*a = a.div_floor(&scalar_big);
|
||||
}
|
||||
});
|
||||
|
||||
assert!(coeffs_a == coeffs_c, "test_div_floor_by_last_moduli_inplace");
|
||||
izip!(coeffs_a, coeffs_c).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user