mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
added spqlios as submodule
This commit is contained in:
97
rns/tests/automorphism.rs
Normal file
97
rns/tests/automorphism.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use itertools::izip;
|
||||
use math::automorphism::AutoPerm;
|
||||
use math::poly::Poly;
|
||||
use math::ring::Ring;
|
||||
|
||||
#[test]
|
||||
fn automorphism_u64() {
|
||||
let n: usize = 1 << 4;
|
||||
let nth_root: usize = n << 1;
|
||||
let q_base: u64 = 65537u64;
|
||||
let q_power: usize = 1usize;
|
||||
let ring: Ring<u64> = Ring::new(n, q_base, q_power);
|
||||
|
||||
sub_test("test_automorphism_native_u64::<NTT:false>", || {
|
||||
test_automorphism_native_u64::<false>(&ring, nth_root)
|
||||
});
|
||||
sub_test("test_automorphism_native_u64::<NTT:true>", || {
|
||||
test_automorphism_native_u64::<true>(&ring, nth_root)
|
||||
});
|
||||
|
||||
sub_test("test_automorphism_from_perm_u64::<NTT:false>", || {
|
||||
test_automorphism_from_perm_u64::<false>(&ring)
|
||||
});
|
||||
sub_test("test_automorphism_from_perm_u64::<NTT:true>", || {
|
||||
test_automorphism_from_perm_u64::<true>(&ring)
|
||||
});
|
||||
}
|
||||
|
||||
fn sub_test<F: FnOnce()>(name: &str, f: F) {
|
||||
println!("Running {}", name);
|
||||
f();
|
||||
}
|
||||
|
||||
fn test_automorphism_native_u64<const NTT: bool>(ring: &Ring<u64>, nth_root: usize) {
|
||||
let n: usize = ring.n();
|
||||
let q: u64 = ring.modulus.q;
|
||||
|
||||
let mut p0: Poly<u64> = ring.new_poly();
|
||||
let mut p1: Poly<u64> = ring.new_poly();
|
||||
|
||||
for i in 0..p0.n() {
|
||||
p0.0[i] = i as u64
|
||||
}
|
||||
|
||||
if NTT {
|
||||
ring.ntt_inplace::<false>(&mut p0);
|
||||
}
|
||||
|
||||
let gal_el: usize = 2 * nth_root - 1;
|
||||
|
||||
ring.a_apply_automorphism_native_into_b::<NTT>(&p0, gal_el, nth_root, &mut p1);
|
||||
|
||||
if NTT {
|
||||
ring.intt_inplace::<false>(&mut p1);
|
||||
}
|
||||
|
||||
p0.0[0] = 0;
|
||||
for i in 1..p0.n() {
|
||||
p0.0[i] = q - (n - i) as u64
|
||||
}
|
||||
|
||||
izip!(p0.0, p1.0).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
|
||||
fn test_automorphism_from_perm_u64<const NTT: bool>(ring: &Ring<u64>) {
|
||||
let n: usize = ring.n();
|
||||
let q: u64 = ring.modulus.q;
|
||||
|
||||
let mut p0: Poly<u64> = ring.new_poly();
|
||||
let mut p1: Poly<u64> = ring.new_poly();
|
||||
|
||||
for i in 0..p0.n() {
|
||||
p0.0[i] = i as u64
|
||||
}
|
||||
|
||||
if NTT {
|
||||
ring.ntt_inplace::<false>(&mut p0);
|
||||
}
|
||||
|
||||
let gen_1 = 0;
|
||||
let gen_2 = true;
|
||||
|
||||
let auto_perm = AutoPerm::new::<_, NTT>(&ring, gen_1, gen_2);
|
||||
|
||||
ring.a_apply_automorphism_from_perm_into_b::<NTT>(&p0, &auto_perm, &mut p1);
|
||||
|
||||
if NTT {
|
||||
ring.intt_inplace::<false>(&mut p1);
|
||||
}
|
||||
|
||||
p0.0[0] = 0;
|
||||
for i in 1..p0.n() {
|
||||
p0.0[i] = q - (n - i) as u64
|
||||
}
|
||||
|
||||
izip!(p0.0, p1.0).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
75
rns/tests/digit_decomposition.rs
Normal file
75
rns/tests/digit_decomposition.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use itertools::izip;
|
||||
use math::modulus::{WordOps, ONCE};
|
||||
use math::poly::Poly;
|
||||
use math::ring::Ring;
|
||||
use sampling::source::Source;
|
||||
|
||||
#[test]
|
||||
fn digit_decomposition() {
|
||||
let n: usize = 1 << 4;
|
||||
let q_base: u64 = 65537u64;
|
||||
let q_power: usize = 1usize;
|
||||
let ring: Ring<u64> = Ring::new(n, q_base, q_power);
|
||||
|
||||
sub_test("test_unsigned_digit_decomposition", || {
|
||||
test_unsigned_digit_decomposition(&ring)
|
||||
});
|
||||
|
||||
sub_test("test_signed_digit_decomposition::<BALANCED=false>", || {
|
||||
test_signed_digit_decomposition::<false>(&ring)
|
||||
});
|
||||
|
||||
sub_test("test_signed_digit_decomposition::<BALANCED=true>", || {
|
||||
test_signed_digit_decomposition::<true>(&ring)
|
||||
});
|
||||
}
|
||||
|
||||
fn sub_test<F: FnOnce()>(name: &str, f: F) {
|
||||
println!("Running {}", name);
|
||||
f();
|
||||
}
|
||||
|
||||
fn test_unsigned_digit_decomposition(ring: &Ring<u64>) {
|
||||
let mut a: Poly<u64> = ring.new_poly();
|
||||
let mut b: Poly<u64> = ring.new_poly();
|
||||
let mut c: Poly<u64> = ring.new_poly();
|
||||
|
||||
let seed: [u8; 32] = [0; 32];
|
||||
let mut source: Source = Source::new(seed);
|
||||
ring.fill_uniform(&mut source, &mut a);
|
||||
|
||||
let base: usize = 8;
|
||||
let log_q: usize = ring.modulus.q.log2();
|
||||
let d: usize = ((log_q + base - 1) / base) as _;
|
||||
|
||||
(0..d).for_each(|i| {
|
||||
ring.a_ith_digit_unsigned_base_scalar_b_into_c(i, &a, &base, &mut b);
|
||||
ring.a_mul_b_scalar_into_a::<ONCE>(&(1 << (i * base)), &mut b);
|
||||
ring.a_add_b_into_b::<ONCE>(&b, &mut c);
|
||||
});
|
||||
|
||||
izip!(a.0, c.0).for_each(|(a, c)| assert_eq!(a, c));
|
||||
}
|
||||
|
||||
fn test_signed_digit_decomposition<const BALANCED: bool>(ring: &Ring<u64>) {
|
||||
let mut a: Poly<u64> = ring.new_poly();
|
||||
let mut b: Poly<u64> = ring.new_poly();
|
||||
let mut carry: Poly<u64> = ring.new_poly();
|
||||
let mut c: Poly<u64> = ring.new_poly();
|
||||
|
||||
let seed: [u8; 32] = [0; 32];
|
||||
let mut source: Source = Source::new(seed);
|
||||
ring.fill_uniform(&mut source, &mut a);
|
||||
|
||||
let base: usize = 8;
|
||||
let log_q: usize = ring.modulus.q.log2();
|
||||
let d: usize = ((log_q + base - 1) / base) as _;
|
||||
|
||||
(0..d).for_each(|i| {
|
||||
ring.a_ith_digit_signed_base_scalar_b_into_c::<BALANCED>(i, &a, &base, &mut carry, &mut b);
|
||||
ring.a_mul_b_scalar_into_a::<ONCE>(&(1 << (i * base)), &mut b);
|
||||
ring.a_add_b_into_b::<ONCE>(&b, &mut c);
|
||||
});
|
||||
|
||||
izip!(a.0, c.0).for_each(|(a, c)| assert_eq!(a, c));
|
||||
}
|
||||
287
rns/tests/rescaling_rns.rs
Normal file
287
rns/tests/rescaling_rns.rs
Normal file
@@ -0,0 +1,287 @@
|
||||
use itertools::izip;
|
||||
use math::num_bigint::Div;
|
||||
use math::poly::{Poly, PolyRNS};
|
||||
use math::ring::RingRNS;
|
||||
use num_bigint::BigInt;
|
||||
use sampling::source::Source;
|
||||
|
||||
#[test]
|
||||
fn rescaling_rns_u64() {
|
||||
let n = 1 << 10;
|
||||
let moduli: Vec<u64> = vec![
|
||||
0x1fffffffffc80001u64,
|
||||
0x1fffffffffe00001u64,
|
||||
0x1fffffffffb40001,
|
||||
0x1fffffffff500001,
|
||||
];
|
||||
let ring_rns: RingRNS<u64> = RingRNS::new(n, moduli);
|
||||
|
||||
sub_test("test_div_by_last_modulus::<ROUND:false, NTT:false>", || {
|
||||
test_div_by_last_modulus::<false, false>(&ring_rns)
|
||||
});
|
||||
sub_test("test_div_by_last_modulus::<ROUND:false, NTT:true>", || {
|
||||
test_div_by_last_modulus::<false, true>(&ring_rns)
|
||||
});
|
||||
sub_test("test_div_by_last_modulus::<ROUND:true, NTT:false>", || {
|
||||
test_div_by_last_modulus::<true, false>(&ring_rns)
|
||||
});
|
||||
sub_test("test_div_by_last_modulus::<ROUND:true, NTT:true>", || {
|
||||
test_div_by_last_modulus::<true, true>(&ring_rns)
|
||||
});
|
||||
sub_test(
|
||||
"test_div_by_last_modulus_inplace::<ROUND:false, NTT:false>",
|
||||
|| test_div_by_last_modulus_inplace::<false, false>(&ring_rns),
|
||||
);
|
||||
sub_test(
|
||||
"test_div_by_last_modulus_inplace::<ROUND:false, NTT:true>",
|
||||
|| test_div_by_last_modulus_inplace::<false, true>(&ring_rns),
|
||||
);
|
||||
sub_test(
|
||||
"test_div_by_last_modulus_inplace::<ROUND:true, NTT:true>",
|
||||
|| test_div_by_last_modulus_inplace::<true, true>(&ring_rns),
|
||||
);
|
||||
sub_test(
|
||||
"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: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) {
|
||||
println!("Running {}", name);
|
||||
f();
|
||||
}
|
||||
|
||||
fn test_div_by_last_modulus<const ROUND: bool, const NTT: bool>(ring_rns: &RingRNS<u64>) {
|
||||
let seed: [u8; 32] = [0; 32];
|
||||
let mut source: Source = Source::new(seed);
|
||||
|
||||
let mut a: 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
|
||||
ring_rns.fill_uniform(&mut source, &mut a);
|
||||
|
||||
// Maps PolyRNS to [BigInt]
|
||||
let mut coeffs_a: Vec<BigInt> = (0..a.n()).map(|i| BigInt::from(i)).collect();
|
||||
ring_rns
|
||||
.at_level(a.level())
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_a);
|
||||
|
||||
// Performs c = intt(ntt(a) / q_level)
|
||||
if NTT {
|
||||
ring_rns.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Exports c to coeffs_c
|
||||
let mut coeffs_c = vec![BigInt::from(0); c.n()];
|
||||
ring_rns
|
||||
.at_level(c.level())
|
||||
.to_bigint_inplace(&c, 1, &mut coeffs_c);
|
||||
|
||||
// Performs floor division on a
|
||||
let scalar_big = BigInt::from(ring_rns.0[ring_rns.level()].modulus.q);
|
||||
coeffs_a.iter_mut().for_each(|a| {
|
||||
if ROUND {
|
||||
*a = a.div_round(&scalar_big);
|
||||
} else {
|
||||
*a = a.div_floor(&scalar_big);
|
||||
}
|
||||
});
|
||||
|
||||
izip!(coeffs_a, coeffs_c).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
|
||||
fn test_div_by_last_modulus_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 mut a: 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);
|
||||
|
||||
// Maps PolyRNS to [BigInt]
|
||||
let mut coeffs_a: Vec<BigInt> = (0..a.n()).map(|i| BigInt::from(i)).collect();
|
||||
ring_rns
|
||||
.at_level(a.level())
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_a);
|
||||
|
||||
// Performs c = intt(ntt(a) / q_level)
|
||||
if NTT {
|
||||
ring_rns.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
ring_rns.div_by_last_modulus_inplace::<ROUND, NTT>(&mut buf, &mut a);
|
||||
|
||||
if NTT {
|
||||
ring_rns
|
||||
.at_level(a.level() - 1)
|
||||
.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() - 1)
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_c);
|
||||
|
||||
// Performs floor division on a
|
||||
let scalar_big = BigInt::from(ring_rns.0[ring_rns.level()].modulus.q);
|
||||
coeffs_a.iter_mut().for_each(|a| {
|
||||
if ROUND {
|
||||
*a = a.div_round(&scalar_big);
|
||||
} else {
|
||||
*a = a.div_floor(&scalar_big);
|
||||
}
|
||||
});
|
||||
|
||||
izip!(coeffs_a, coeffs_c).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
|
||||
fn test_div_by_last_moduli<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_dropped: usize = ring_rns.level();
|
||||
|
||||
let mut a: 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_dropped)
|
||||
.new_polyrns();
|
||||
|
||||
// Allocates a random PolyRNS
|
||||
ring_rns.fill_uniform(&mut source, &mut a);
|
||||
|
||||
// Maps PolyRNS to [BigInt]
|
||||
let mut coeffs_a: Vec<BigInt> = (0..a.n()).map(|i| BigInt::from(i)).collect();
|
||||
ring_rns
|
||||
.at_level(a.level())
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_a);
|
||||
|
||||
// Performs c = intt(ntt(a) / q_level)
|
||||
if NTT {
|
||||
ring_rns.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// Exports c to coeffs_c
|
||||
let mut coeffs_c = vec![BigInt::from(0); a.n()];
|
||||
ring_rns
|
||||
.at_level(c.level())
|
||||
.to_bigint_inplace(&c, 1, &mut coeffs_c);
|
||||
|
||||
// Performs floor division on a
|
||||
let mut scalar_big = BigInt::from(1);
|
||||
(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);
|
||||
}
|
||||
});
|
||||
|
||||
izip!(coeffs_a, coeffs_c).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
|
||||
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_dropped: usize = ring_rns.level();
|
||||
|
||||
let mut a: 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);
|
||||
|
||||
// Maps PolyRNS to [BigInt]
|
||||
let mut coeffs_a: Vec<BigInt> = (0..a.n()).map(|i| BigInt::from(i)).collect();
|
||||
ring_rns
|
||||
.at_level(a.level())
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_a);
|
||||
|
||||
// Performs c = intt(ntt(a) / q_level)
|
||||
if NTT {
|
||||
ring_rns.ntt_inplace::<false>(&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_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_dropped)
|
||||
.to_bigint_inplace(&a, 1, &mut coeffs_c);
|
||||
|
||||
// Performs floor division on a
|
||||
let mut scalar_big = BigInt::from(1);
|
||||
(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);
|
||||
}
|
||||
});
|
||||
|
||||
izip!(coeffs_a, coeffs_c).for_each(|(a, b)| assert_eq!(a, b));
|
||||
}
|
||||
87
rns/tests/ring_switch.rs
Normal file
87
rns/tests/ring_switch.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use math::poly::Poly;
|
||||
use math::ring::Ring;
|
||||
|
||||
#[test]
|
||||
fn ring_switch_u64() {
|
||||
let n: usize = 1 << 4;
|
||||
let q_base: u64 = 65537u64;
|
||||
let q_power: usize = 1usize;
|
||||
let ring_small: Ring<u64> = Ring::new(n, q_base, q_power);
|
||||
let ring_large = Ring::new(2 * n, q_base, q_power);
|
||||
|
||||
sub_test("test_ring_switch_small_to_large_u64::<NTT:false>", || {
|
||||
test_ring_switch_small_to_large_u64::<false>(&ring_small, &ring_large)
|
||||
});
|
||||
sub_test("test_ring_switch_small_to_large_u64::<NTT:true>", || {
|
||||
test_ring_switch_small_to_large_u64::<true>(&ring_small, &ring_large)
|
||||
});
|
||||
sub_test("test_ring_switch_large_to_small_u64::<NTT:false>", || {
|
||||
test_ring_switch_large_to_small_u64::<false>(&ring_small, &ring_large)
|
||||
});
|
||||
sub_test("test_ring_switch_large_to_small_u64::<NTT:true>", || {
|
||||
test_ring_switch_large_to_small_u64::<true>(&ring_small, &ring_large)
|
||||
});
|
||||
}
|
||||
|
||||
fn sub_test<F: FnOnce()>(name: &str, f: F) {
|
||||
println!("Running {}", name);
|
||||
f();
|
||||
}
|
||||
|
||||
fn test_ring_switch_small_to_large_u64<const NTT: bool>(
|
||||
ring_small: &Ring<u64>,
|
||||
ring_large: &Ring<u64>,
|
||||
) {
|
||||
let mut a: Poly<u64> = ring_small.new_poly();
|
||||
let mut buf: Poly<u64> = ring_small.new_poly();
|
||||
let mut b: Poly<u64> = ring_large.new_poly();
|
||||
|
||||
a.0.iter_mut().enumerate().for_each(|(i, x)| *x = i as u64);
|
||||
|
||||
if NTT {
|
||||
ring_small.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
ring_large.switch_degree::<NTT>(&a, &mut buf, &mut b);
|
||||
|
||||
if NTT {
|
||||
ring_small.intt_inplace::<false>(&mut a);
|
||||
ring_large.intt_inplace::<false>(&mut b);
|
||||
}
|
||||
|
||||
let gap: usize = ring_large.n() / ring_small.n();
|
||||
|
||||
b.0.iter()
|
||||
.step_by(gap)
|
||||
.zip(a.0.iter())
|
||||
.for_each(|(x_out, x_in)| assert_eq!(x_out, x_in));
|
||||
}
|
||||
|
||||
fn test_ring_switch_large_to_small_u64<const NTT: bool>(
|
||||
ring_small: &Ring<u64>,
|
||||
ring_large: &Ring<u64>,
|
||||
) {
|
||||
let mut a: Poly<u64> = ring_large.new_poly();
|
||||
let mut buf: Poly<u64> = ring_large.new_poly();
|
||||
let mut b: Poly<u64> = ring_small.new_poly();
|
||||
|
||||
a.0.iter_mut().enumerate().for_each(|(i, x)| *x = i as u64);
|
||||
|
||||
if NTT {
|
||||
ring_large.ntt_inplace::<false>(&mut a);
|
||||
}
|
||||
|
||||
ring_large.switch_degree::<NTT>(&a, &mut buf, &mut b);
|
||||
|
||||
if NTT {
|
||||
ring_large.intt_inplace::<false>(&mut a);
|
||||
ring_small.intt_inplace::<false>(&mut b);
|
||||
}
|
||||
|
||||
let gap: usize = ring_large.n() / ring_small.n();
|
||||
|
||||
a.0.iter()
|
||||
.step_by(gap)
|
||||
.zip(b.0.iter())
|
||||
.for_each(|(x_out, x_in)| assert_eq!(x_out, x_in));
|
||||
}
|
||||
Reference in New Issue
Block a user