-
Notifications
You must be signed in to change notification settings - Fork 711
/
ntt.cpp
137 lines (119 loc) · 5.13 KB
/
ntt.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/modulus.h"
#include "seal/util/mempool.h"
#include "seal/util/ntt.h"
#include "seal/util/numth.h"
#include "seal/util/polycore.h"
#include "seal/util/uintcore.h"
#include <cstddef>
#include <cstdint>
#include <random>
#include "gtest/gtest.h"
using namespace seal;
using namespace seal::util;
using namespace std;
namespace sealtest
{
namespace util
{
TEST(NTTTablesTest, NTTBasics)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
Pointer<NTTTables> tables;
int coeff_count_power = 1;
Modulus modulus(get_prime(uint64_t(1) << coeff_count_power, 60));
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
ASSERT_EQ(2ULL, tables->coeff_count());
ASSERT_EQ(1, tables->coeff_count_power());
coeff_count_power = 2;
modulus = get_prime(uint64_t(1) << coeff_count_power, 50);
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
ASSERT_EQ(4ULL, tables->coeff_count());
ASSERT_EQ(2, tables->coeff_count_power());
coeff_count_power = 10;
modulus = get_prime(uint64_t(1) << coeff_count_power, 40);
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
ASSERT_EQ(1024ULL, tables->coeff_count());
ASSERT_EQ(10, tables->coeff_count_power());
ASSERT_NO_THROW(CreateNTTTables(
coeff_count_power, CoeffModulus::Create(uint64_t(1) << coeff_count_power, { 20, 20, 20, 20, 20 }),
tables, pool));
for (size_t i = 0; i < 5; i++)
{
ASSERT_EQ(1024ULL, tables[i].coeff_count());
ASSERT_EQ(10, tables[i].coeff_count_power());
}
}
TEST(NTTTablesTest, NTTPrimitiveRootsTest)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
Pointer<NTTTables> tables;
int coeff_count_power = 1;
Modulus modulus(0xffffffffffc0001ULL);
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
ASSERT_EQ(1ULL, tables->get_from_root_powers(0));
ASSERT_EQ(288794978602139552ULL, tables->get_from_root_powers(1));
uint64_t inv;
try_invert_uint_mod(tables->get_from_root_powers(1), modulus.value(), inv);
ASSERT_EQ(inv, tables->get_from_inv_root_powers(1));
coeff_count_power = 2;
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
ASSERT_EQ(1ULL, tables->get_from_root_powers(0));
ASSERT_EQ(288794978602139552ULL, tables->get_from_root_powers(1));
ASSERT_EQ(178930308976060547ULL, tables->get_from_root_powers(2));
ASSERT_EQ(748001537669050592ULL, tables->get_from_root_powers(3));
}
TEST(NTTTablesTest, NegacyclicNTTTest)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
Pointer<NTTTables> tables;
int coeff_count_power = 1;
Modulus modulus(0xffffffffffc0001ULL);
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
auto poly(allocate_poly(2, 1, pool));
poly[0] = 0;
poly[1] = 0;
ntt_negacyclic_harvey(poly.get(), *tables);
ASSERT_EQ(0ULL, poly[0]);
ASSERT_EQ(0ULL, poly[1]);
poly[0] = 1;
poly[1] = 0;
ntt_negacyclic_harvey(poly.get(), *tables);
ASSERT_EQ(1ULL, poly[0]);
ASSERT_EQ(1ULL, poly[1]);
poly[0] = 1;
poly[1] = 1;
ntt_negacyclic_harvey(poly.get(), *tables);
ASSERT_EQ(288794978602139553ULL, poly[0]);
ASSERT_EQ(864126526004445282ULL, poly[1]);
}
TEST(NTTTablesTest, InverseNegacyclicNTTTest)
{
MemoryPoolHandle pool = MemoryPoolHandle::Global();
Pointer<NTTTables> tables;
int coeff_count_power = 3;
Modulus modulus(0xffffffffffc0001ULL);
ASSERT_NO_THROW(tables = allocate<NTTTables>(pool, coeff_count_power, modulus, pool));
auto poly(allocate_zero_poly(800, 1, pool));
auto temp(allocate_zero_poly(800, 1, pool));
inverse_ntt_negacyclic_harvey(poly.get(), *tables);
for (size_t i = 0; i < 800; i++)
{
ASSERT_EQ(0ULL, poly[i]);
}
random_device rd;
for (size_t 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 (size_t i = 0; i < 800; i++)
{
ASSERT_EQ(temp[i], poly[i]);
}
}
} // namespace util
} // namespace sealtest