1pub mod ring;
2pub mod ntt;
3pub mod polys;
4pub mod modular;
5pub mod sampling;
6pub mod encoding;
7pub mod bitwriting;
8pub use ring::RingParams as RingParams;
9pub use polys::Polynomial as Polynomial;
10pub use polys::ToPoly;
11pub use modular::{find_valid_omega, is_q_valid, mod_pow, is_prime};
12pub use sampling::{generate_small_sample, generate_cbd_sample, generate_uniform_polynomial, generate_then_shake, shake_128, Sample, SeedType};
13pub use ntt::{forward_ntt, inverse_ntt, precalculate};
14pub use bitwriting::BitWriter;
15
16#[cfg(test)]
17#[test]
18fn test_polynomials() {
19 let params = RingParams::new(4, 17, find_valid_omega(4, 17)); let ntt_tables = &precalculate(¶ms);
21 let mut coeffs = vec![0u64; 4]; coeffs[3] = 8;
23 let poly1 = Polynomial::new(coeffs.clone()); let poly2 = Polynomial::zeros(4); let sum = poly1.sum(¶ms, &poly2); let mul = poly1.mul(¶ms, &poly2);
27 let mul_ntt = poly1.mul_ntt(¶ms, ntt_tables, &poly2);
28 let scaled = poly1.scale(¶ms, 10);
29 let divided = poly1.divide_by_constant(2);
30 let reduced = poly1.reduce(2);
31 let opposite = poly1.opposite(¶ms);
32 println!();
33 assert_eq!(poly1.coeffs, coeffs.into_boxed_slice()); println!("First polynomial : {:?}", poly1);
35 println!();
36 println!("Second polynomial : {:?}", poly2);
37 assert_eq!(poly2.coeffs, vec![0u64; 4].into_boxed_slice());
38 println!();
39 println!("Sum is : {:?}", sum);
40 assert_eq!(poly1.coeffs, sum.coeffs);
41 println!();
42 println!("Product is : {:?}", mul);
43 assert_eq!(poly2.coeffs, mul.coeffs);
44 println!();
45 println!("Product with NTT is : {:?}", mul_ntt);
46 assert_eq!(poly2.coeffs, mul.coeffs);
47 println!();
48 println!("Scaled first polynomial is : {:?}", scaled);
49 let mut coeffs = vec![0u64; 4];
50 coeffs[3] = (8 * 10) % params.q; assert_eq!(coeffs.into_boxed_slice(), scaled.coeffs);
52 println!();
53 println!("Divided first polynomial is : {:?}", divided);
54 let mut coeffs = vec![0u64; 4];
55 coeffs[3] = 8 / 2;
56 assert_eq!(coeffs.into_boxed_slice(), divided.coeffs);
57 println!();
58 println!("Reduced first polynomial is : {:?}", reduced);
59 let mut coeffs = vec![0u64; 4];
60 coeffs[3] = 8 % 2;
61 assert_eq!(coeffs.into_boxed_slice(), reduced.coeffs);
62 println!();
63 println!("Opposite poly1 is : {:?}", opposite);
64 let mut coeffs = vec![0u64; 4];
65 coeffs[3] = (-8 as i64).rem_euclid(params.q as i64) as u64;assert_eq!(coeffs.into_boxed_slice(), opposite.coeffs);
67 println!()
68}