This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathsmallntt.cpp
More file actions
137 lines (121 loc) · 5.42 KB
/
Copy pathsmallntt.cpp
File metadata and controls
137 lines (121 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "CppUnitTest.h"
#include "seal/util/mempool.h"
#include "seal/util/uintcore.h"
#include "seal/util/polycore.h"
#include "seal/util/smallntt.h"
#include "seal/defaultparams.h"
#include "seal/util/numth.h"
#include <random>
#include <cstdint>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace seal;
using namespace seal::util;
using namespace std;
namespace SEALTest
{
namespace util
{
TEST_CLASS(SmallNTTTablesTest)
{
public:
TEST_METHOD(SmallNTTBasics)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
SmallNTTTables tables(pool);
int coeff_count_power = 1;
int coeff_count = 1 << coeff_count_power;
SmallModulus modulus(small_mods_60bit(0));
tables.generate(coeff_count_power, modulus);
Assert::AreEqual(2, tables.coeff_count());
Assert::IsTrue(tables.is_generated());
Assert::AreEqual(1, tables.coeff_count_power());
coeff_count_power = 2;
coeff_count = 1 << coeff_count_power;
modulus = small_mods_50bit(0);
tables.generate(coeff_count_power, modulus);
Assert::AreEqual(4, tables.coeff_count());
Assert::IsTrue(tables.is_generated());
Assert::AreEqual(2, tables.coeff_count_power());
coeff_count_power = 10;
coeff_count = 1 << coeff_count_power;
modulus = small_mods_40bit(0);
tables.generate(coeff_count_power, modulus);
Assert::AreEqual(1024, tables.coeff_count());
Assert::IsTrue(tables.is_generated());
Assert::AreEqual(10, tables.coeff_count_power());
}
TEST_METHOD(SmallNTTPrimitiveRootsTest)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
SmallNTTTables tables(pool);
int coeff_count_power = 1;
SmallModulus modulus(0xffffffffffc0001ULL);
tables.generate(coeff_count_power, modulus);
Assert::AreEqual(1ULL, tables.get_from_root_powers(0));
Assert::AreEqual(288794978602139552ULL, tables.get_from_root_powers(1));
uint64_t inv;
try_mod_inverse(tables.get_from_root_powers(1), modulus.value(), inv);
Assert::AreEqual(inv, tables.get_from_inv_root_powers(1));
coeff_count_power = 2;
tables.generate(coeff_count_power, modulus);
Assert::AreEqual(1ULL, tables.get_from_root_powers(0));
Assert::AreEqual(288794978602139552ULL, tables.get_from_root_powers(1));
Assert::AreEqual(178930308976060547ULL, tables.get_from_root_powers(2));
Assert::AreEqual(748001537669050592ULL, tables.get_from_root_powers(3));
}
TEST_METHOD(NegacyclicSmallNTTTest)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
SmallNTTTables tables(pool);
int coeff_count_power = 1;
int coeff_count = 1 << coeff_count_power;
SmallModulus modulus(0xffffffffffc0001ULL);
tables.generate(coeff_count_power, modulus);
Pointer poly(allocate_poly(2, 1, pool));
poly[0] = 0;
poly[1] = 0;
ntt_negacyclic_harvey(poly.get(), tables);
Assert::AreEqual(0ULL, poly[0]);
Assert::AreEqual(0ULL, poly[1]);
poly[0] = 1;
poly[1] = 0;
ntt_negacyclic_harvey(poly.get(), tables);
Assert::AreEqual(1ULL, poly[0]);
Assert::AreEqual(1ULL, poly[1]);
poly[0] = 1;
poly[1] = 1;
ntt_negacyclic_harvey(poly.get(), tables);
Assert::AreEqual(288794978602139553ULL, poly[0]);
Assert::AreEqual(864126526004445282ULL, poly[1]);
}
TEST_METHOD(InverseNegacyclicSmallNTTTest)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
SmallNTTTables tables(pool);
int coeff_count_power = 3;
int coeff_count = 1 << coeff_count_power;
SmallModulus modulus(0xffffffffffc0001ULL);
tables.generate(coeff_count_power, modulus);
Pointer poly(allocate_zero_poly(800, 1, pool));
Pointer temp(allocate_zero_poly(800, 1, pool));
inverse_ntt_negacyclic_harvey(poly.get(), tables);
for (int i = 0; i < 800; i++)
{
Assert::AreEqual(0ULL, poly[i]);
}
random_device rd;
for (int i = 0; i < 800; i++)
{
poly[i] = static_cast<uint64_t>(rd()) % modulus.value();
temp[i] = poly[i];
}
ntt_negacyclic_harvey(poly.get(), tables);
inverse_ntt_negacyclic_harvey(poly.get(), tables);
for (int i = 0; i < 800; i++)
{
Assert::AreEqual(temp[i], poly[i]);
}
}
};
}
}