1 #include <iostream>
2 #include <nfl/poly_p.hpp>
3 #include "tools.h"
4
5 template <size_t degree, size_t modulus, class T>
run()6 bool run() {
7 using poly_p = nfl::poly_p_from_modulus<T, degree, modulus>;
8 using poly_t = nfl::poly_from_modulus<T, degree, modulus>;
9
10 bool ret = true;
11
12 // Define polynomials
13 poly_p a{nfl::uniform()};
14 poly_p b{nfl::uniform()};
15
16 poly_t& A = *alloc_aligned<poly_t, 32>(1, a.poly_obj());
17 poly_t& B = *alloc_aligned<poly_t, 32>(1, b.poly_obj());
18
19 // Test addition
20 poly_t& add = *alloc_aligned<poly_t, 32>(1, A + B);
21 poly_p add_p{a + b};
22 ret &= (add_p == add);
23
24 // Test substraction
25 poly_t& sub = *alloc_aligned<poly_t, 32>(1, A - B);
26 poly_p sub_p{a - b};
27 ret &= (sub_p == sub);
28
29 // Test multiplication
30 poly_t& mul = *alloc_aligned<poly_t, 32>(1, A * B);
31 poly_p mul_p{a * b};
32 ret &= (mul_p == mul);
33
34 // Test detach
35 poly_p c{b};
36 ret &= (c == b);
37
38 // Test assign
39 c = {1};
40 ret &= (c != b);
41
42 // Shoup
43 poly_p bshoup = nfl::compute_shoup(b);
44 poly_t& Bshoup = *alloc_aligned<poly_t, 32>(1, nfl::compute_shoup(B));
45 ret &= (bshoup == Bshoup);
46
47 poly_p mul2_p = nfl::shoup(a * b, bshoup);
48 poly_t& mul2 = *alloc_aligned<poly_t, 32>(1, nfl::shoup(A * B, Bshoup));
49 ret &= (mul2_p == mul2);
50
51 // NTT and invNTT
52 a.ntt_pow_phi();
53 A.ntt_pow_phi();
54 ret &= (a == A);
55
56 b.invntt_pow_invphi();
57 B.invntt_pow_invphi();
58 ret &= (b == B);
59
60 // Operators overloads with expression templates
61 // (test == operator in both directions: poly_p == poly_t
62 // and poly_t == poly_p)
63 poly_p tmp_p = a + b * add_p;
64 ret &= (tmp_p == A + B * add);
65 poly_t& tmp = *alloc_aligned<poly_t, 32>(1, A + B * add);
66 ret &= (tmp == a + b * add_p);
67
68 // Cleaning
69 free_aligned(1, &add);
70 free_aligned(1, &sub);
71 free_aligned(1, &mul);
72 free_aligned(1, &mul2);
73 free_aligned(1, &Bshoup);
74 free_aligned(1, &tmp);
75
76 return ret;
77 }
78
main(int argc,char const * argv[])79 int main(int argc, char const* argv[]) {
80 return not run<CONFIG>();
81 }
82