-
Notifications
You must be signed in to change notification settings - Fork 709
/
galois.cpp
219 lines (196 loc) · 8.04 KB
/
galois.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/util/galois.h"
#include "seal/util/numth.h"
#include "seal/util/uintcore.h"
using namespace std;
namespace seal
{
namespace util
{
// Required for C++14 compliance: static constexpr member variables are not necessarily inlined so need to
// ensure symbol is created.
constexpr uint32_t GaloisTool::generator_;
void GaloisTool::generate_table_ntt(uint32_t galois_elt, Pointer<uint32_t> &result)
{
#ifdef SEAL_DEBUG
if (!(galois_elt & 1) || (galois_elt >= 2 * (uint64_t(1) << coeff_count_power_)))
{
throw std::invalid_argument("Galois element is not valid");
}
#endif
ReaderLock reader_lock(permutation_tables_locker_.acquire_read());
if (result)
{
return;
}
reader_lock.unlock();
WriterLock writer_lock(permutation_tables_locker_.acquire_write());
result = allocate<uint32_t>(coeff_count_, pool_);
auto result_ptr = result.get();
uint32_t coeff_count_minus_one = safe_cast<uint32_t>(coeff_count_) - 1;
for (size_t i = coeff_count_; i < coeff_count_ << 1; i++)
{
uint32_t reversed = reverse_bits<uint32_t>(safe_cast<uint32_t>(i), coeff_count_power_ + 1);
uint64_t index_raw = (static_cast<uint64_t>(galois_elt) * static_cast<uint64_t>(reversed)) >> 1;
index_raw &= static_cast<uint64_t>(coeff_count_minus_one);
*result_ptr++ = reverse_bits<uint32_t>(static_cast<uint32_t>(index_raw), coeff_count_power_);
}
writer_lock.unlock();
return;
}
uint32_t GaloisTool::get_elt_from_step(int step) const
{
uint32_t n = safe_cast<uint32_t>(coeff_count_);
uint32_t m32 = mul_safe(n, uint32_t(2));
uint64_t m = static_cast<uint64_t>(m32);
if (step == 0)
{
return static_cast<uint32_t>(m - 1);
}
else
{
// Extract sign of steps. When steps is positive, the rotation
// is to the left; when steps is negative, it is to the right.
bool sign = step < 0;
uint32_t pos_step = safe_cast<uint32_t>(abs(step));
if (pos_step >= (n >> 1))
{
throw invalid_argument("step count too large");
}
pos_step &= m32 - 1;
if (sign)
{
step = safe_cast<int>(n >> 1) - safe_cast<int>(pos_step);
}
else
{
step = safe_cast<int>(pos_step);
}
// Construct Galois element for row rotation
uint64_t gen = static_cast<uint64_t>(generator_);
uint64_t galois_elt = 1;
while (step--)
{
galois_elt *= gen;
galois_elt &= m - 1;
}
return static_cast<uint32_t>(galois_elt);
}
}
vector<uint32_t> GaloisTool::get_elts_from_steps(const vector<int> &steps) const
{
vector<uint32_t> galois_elts;
transform(steps.begin(), steps.end(), back_inserter(galois_elts), [&](auto s) {
return this->get_elt_from_step(s);
});
return galois_elts;
}
vector<uint32_t> GaloisTool::get_elts_all() const noexcept
{
uint32_t m = safe_cast<uint32_t>(static_cast<uint64_t>(coeff_count_) << 1);
vector<uint32_t> galois_elts{};
// Generate Galois keys for m - 1 (X -> X^{m-1})
galois_elts.push_back(m - 1);
// Generate Galois key for power of generator_ mod m (X -> X^{3^k}) and
// for negative power of generator_ mod m (X -> X^{-3^k})
uint64_t pos_power = generator_;
uint64_t neg_power = 0;
try_invert_uint_mod(generator_, m, neg_power);
for (int i = 0; i < coeff_count_power_ - 1; i++)
{
galois_elts.push_back(static_cast<uint32_t>(pos_power));
pos_power *= pos_power;
pos_power &= (m - 1);
galois_elts.push_back(static_cast<uint32_t>(neg_power));
neg_power *= neg_power;
neg_power &= (m - 1);
}
return galois_elts;
}
void GaloisTool::initialize(int coeff_count_power)
{
if ((coeff_count_power < get_power_of_two(SEAL_POLY_MOD_DEGREE_MIN)) ||
coeff_count_power > get_power_of_two(SEAL_POLY_MOD_DEGREE_MAX))
{
throw invalid_argument("coeff_count_power out of range");
}
coeff_count_power_ = coeff_count_power;
coeff_count_ = size_t(1) << coeff_count_power_;
// Capacity for coeff_count_ number of tables
permutation_tables_ = allocate<Pointer<uint32_t>>(coeff_count_, pool_);
}
void GaloisTool::apply_galois(
const uint64_t *operand, uint32_t galois_elt, const Modulus &modulus, uint64_t *result) const
{
#ifdef SEAL_DEBUG
if (operand == nullptr)
{
throw std::invalid_argument("operand");
}
if (result == nullptr)
{
throw std::invalid_argument("result");
}
if (operand == result)
{
throw std::invalid_argument("result cannot point to the same value as operand");
}
// Verify coprime conditions.
if (!(galois_elt & 1) || (galois_elt >= 2 * (uint64_t(1) << coeff_count_power_)))
{
throw std::invalid_argument("Galois element is not valid");
}
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
#endif
const uint64_t modulus_value = modulus.value();
uint64_t coeff_count_minus_one = (uint64_t(1) << coeff_count_power_) - 1;
for (uint64_t i = 0; i <= coeff_count_minus_one; i++)
{
uint64_t index_raw = i * galois_elt;
uint64_t index = index_raw & coeff_count_minus_one;
uint64_t result_value = *operand++;
if ((index_raw >> coeff_count_power_) & 1)
{
// Explicit inline
// result[index] = negate_uint_mod(result[index], modulus);
int64_t non_zero = (result_value != 0);
result_value = (modulus_value - result_value) & static_cast<uint64_t>(-non_zero);
}
result[index] = result_value;
}
}
void GaloisTool::apply_galois_ntt(const uint64_t *operand, uint32_t galois_elt, uint64_t *result)
{
#ifdef SEAL_DEBUG
if (operand == nullptr)
{
throw std::invalid_argument("operand");
}
if (result == nullptr)
{
throw std::invalid_argument("result");
}
if (operand == result)
{
throw std::invalid_argument("result cannot point to the same value as operand");
}
// Verify coprime conditions.
if (!(galois_elt & 1) || (galois_elt >= 2 * (uint64_t(1) << coeff_count_power_)))
{
throw std::invalid_argument("Galois element is not valid");
}
#endif
generate_table_ntt(galois_elt, permutation_tables_[GetIndexFromElt(galois_elt)]);
auto &table = permutation_tables_[GetIndexFromElt(galois_elt)];
// Perform permutation.
for (size_t i = 0; i < coeff_count_; i++)
{
result[i] = operand[table[i]];
}
}
} // namespace util
} // namespace seal