mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
fixed automorphism for ring and added test
This commit is contained in:
53
math/tests/automorphism.rs
Normal file
53
math/tests/automorphism.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use itertools::izip;
|
||||
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_u64::<NTT:false>", || {
|
||||
test_automorphism_u64::<false>(&ring, nth_root)
|
||||
});
|
||||
sub_test("test_automorphism_u64::<NTT:true>", || {
|
||||
test_automorphism_u64::<true>(&ring, nth_root)
|
||||
});
|
||||
}
|
||||
|
||||
fn sub_test<F: FnOnce()>(name: &str, f: F) {
|
||||
println!("Running {}", name);
|
||||
f();
|
||||
}
|
||||
|
||||
fn test_automorphism_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);
|
||||
}
|
||||
|
||||
ring.automorphism::<NTT>(&p0, 2 * n - 1, 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));
|
||||
}
|
||||
Reference in New Issue
Block a user