arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/math/tests/automorphism.rs
math/tests/automorphism.rsRaw
 1use itertools::izip;
 2use math::poly::Poly;
 3use math::ring::Ring;
 4
 5#[test]
 6fn automorphism_u64() {
 7    let n: usize = 1 << 4;
 8    let nth_root: usize = n << 1;
 9    let q_base: u64 = 65537u64;
10    let q_power: usize = 1usize;
11    let ring: Ring<u64> = Ring::new(n, q_base, q_power);
12
13    sub_test("test_automorphism_u64::<NTT:false>", || {
14        test_automorphism_u64::<false>(&ring, nth_root)
15    });
16    sub_test("test_automorphism_u64::<NTT:true>", || {
17        test_automorphism_u64::<true>(&ring, nth_root)
18    });
19}
20
21fn sub_test<F: FnOnce()>(name: &str, f: F) {
22    println!("Running {}", name);
23    f();
24}
25
26fn test_automorphism_u64<const NTT: bool>(ring: &Ring<u64>, nth_root: usize) {
27    let n: usize = ring.n();
28    let q: u64 = ring.modulus.q;
29
30    let mut p0: Poly<u64> = ring.new_poly();
31    let mut p1: Poly<u64> = ring.new_poly();
32
33    for i in 0..p0.n() {
34        p0.0[i] = i as u64
35    }
36
37    if NTT {
38        ring.ntt_inplace::<false>(&mut p0);
39    }
40
41    ring.a_apply_automorphism_into_b::<NTT>(&p0, 2 * n - 1, nth_root, &mut p1);
42
43    if NTT {
44        ring.intt_inplace::<false>(&mut p1);
45    }
46
47    p0.0[0] = 0;
48    for i in 1..p0.n() {
49        p0.0[i] = q - (n - i) as u64
50    }
51
52    izip!(p0.0, p1.0).for_each(|(a, b)| assert_eq!(a, b));
53}