-
Notifications
You must be signed in to change notification settings - Fork 709
/
modulus.cpp
230 lines (203 loc) · 7.51 KB
/
modulus.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
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/modulus.h"
#include "seal/util/common.h"
#include "seal/util/numth.h"
#include "seal/util/uintarith.h"
#include "seal/util/uintarithsmallmod.h"
#include <numeric>
#include <stdexcept>
#include <unordered_map>
using namespace std;
using namespace seal::util;
namespace seal
{
void Modulus::save_members(ostream &stream) const
{
auto old_except_mask = stream.exceptions();
try
{
// Throw exceptions on std::ios_base::badbit and std::ios_base::failbit
stream.exceptions(ios_base::badbit | ios_base::failbit);
stream.write(reinterpret_cast<const char *>(&value_), sizeof(uint64_t));
}
catch (const ios_base::failure &)
{
stream.exceptions(old_except_mask);
throw runtime_error("I/O error");
}
catch (...)
{
stream.exceptions(old_except_mask);
throw;
}
stream.exceptions(old_except_mask);
}
void Modulus::load_members(istream &stream, SEAL_MAYBE_UNUSED SEALVersion version)
{
auto old_except_mask = stream.exceptions();
try
{
// Throw exceptions on std::ios_base::badbit and std::ios_base::failbit
stream.exceptions(ios_base::badbit | ios_base::failbit);
uint64_t value;
stream.read(reinterpret_cast<char *>(&value), sizeof(uint64_t));
set_value(value);
}
catch (const ios_base::failure &)
{
stream.exceptions(old_except_mask);
throw runtime_error("I/O error");
}
catch (...)
{
stream.exceptions(old_except_mask);
throw;
}
stream.exceptions(old_except_mask);
}
void Modulus::set_value(uint64_t value)
{
if (value == 0)
{
// Zero settings
bit_count_ = 0;
uint64_count_ = 1;
value_ = 0;
const_ratio_ = { { 0, 0, 0 } };
is_prime_ = false;
}
else if ((value >> SEAL_MOD_BIT_COUNT_MAX != 0) || (value == 1))
{
throw invalid_argument("value can be at most 61-bit and cannot be 1");
}
else
{
// All normal, compute const_ratio and set everything
value_ = value;
bit_count_ = get_significant_bit_count(value_);
// Compute Barrett ratios for 64-bit words (barrett_reduce_128)
uint64_t numerator[3]{ 0, 0, 1 };
uint64_t quotient[3]{ 0, 0, 0 };
// Use a special method to avoid using memory pool
divide_uint192_inplace(numerator, value_, quotient);
const_ratio_[0] = quotient[0];
const_ratio_[1] = quotient[1];
// We store also the remainder
const_ratio_[2] = numerator[0];
uint64_count_ = 1;
// Set the primality flag
is_prime_ = util::is_prime(*this);
}
}
uint64_t Modulus::reduce(uint64_t value) const
{
if (value_ == 0)
{
throw logic_error("cannot reduce modulo a zero modulus");
}
return barrett_reduce_64(value, *this);
}
vector<Modulus> CoeffModulus::BFVDefault(size_t poly_modulus_degree, sec_level_type sec_level)
{
if (!MaxBitCount(poly_modulus_degree, sec_level))
{
throw invalid_argument("non-standard poly_modulus_degree");
}
if (sec_level == sec_level_type::none)
{
throw invalid_argument("invalid security level");
}
switch (sec_level)
{
case sec_level_type::tc128:
return global_variables::GetDefaultCoeffModulus128().at(poly_modulus_degree);
case sec_level_type::tc192:
return global_variables::GetDefaultCoeffModulus192().at(poly_modulus_degree);
case sec_level_type::tc256:
return global_variables::GetDefaultCoeffModulus256().at(poly_modulus_degree);
default:
throw runtime_error("invalid security level");
}
}
vector<Modulus> CoeffModulus::Create(size_t poly_modulus_degree, vector<int> bit_sizes)
{
if (poly_modulus_degree > SEAL_POLY_MOD_DEGREE_MAX || poly_modulus_degree < SEAL_POLY_MOD_DEGREE_MIN ||
get_power_of_two(static_cast<uint64_t>(poly_modulus_degree)) < 0)
{
throw invalid_argument("poly_modulus_degree is invalid");
}
if (bit_sizes.size() > SEAL_COEFF_MOD_COUNT_MAX)
{
throw invalid_argument("bit_sizes is invalid");
}
if (accumulate(
bit_sizes.cbegin(), bit_sizes.cend(), SEAL_USER_MOD_BIT_COUNT_MIN,
[](int a, int b) { return max(a, b); }) > SEAL_USER_MOD_BIT_COUNT_MAX ||
accumulate(bit_sizes.cbegin(), bit_sizes.cend(), SEAL_USER_MOD_BIT_COUNT_MAX, [](int a, int b) {
return min(a, b);
}) < SEAL_USER_MOD_BIT_COUNT_MIN)
{
throw invalid_argument("bit_sizes is invalid");
}
unordered_map<int, size_t> count_table;
unordered_map<int, vector<Modulus>> prime_table;
for (int size : bit_sizes)
{
++count_table[size];
}
uint64_t factor = mul_safe(uint64_t(2), safe_cast<uint64_t>(poly_modulus_degree));
for (const auto &table_elt : count_table)
{
prime_table[table_elt.first] = get_primes(factor, table_elt.first, table_elt.second);
}
vector<Modulus> result;
for (int size : bit_sizes)
{
result.emplace_back(prime_table[size].back());
prime_table[size].pop_back();
}
return result;
}
vector<Modulus> CoeffModulus::Create(
size_t poly_modulus_degree, const Modulus &plain_modulus, vector<int> bit_sizes)
{
if (poly_modulus_degree > SEAL_POLY_MOD_DEGREE_MAX || poly_modulus_degree < SEAL_POLY_MOD_DEGREE_MIN ||
get_power_of_two(static_cast<uint64_t>(poly_modulus_degree)) < 0)
{
throw invalid_argument("poly_modulus_degree is invalid");
}
if (bit_sizes.size() > SEAL_COEFF_MOD_COUNT_MAX)
{
throw invalid_argument("bit_sizes is invalid");
}
if (accumulate(
bit_sizes.cbegin(), bit_sizes.cend(), SEAL_USER_MOD_BIT_COUNT_MIN,
[](int a, int b) { return max(a, b); }) > SEAL_USER_MOD_BIT_COUNT_MAX ||
accumulate(bit_sizes.cbegin(), bit_sizes.cend(), SEAL_USER_MOD_BIT_COUNT_MAX, [](int a, int b) {
return min(a, b);
}) < SEAL_USER_MOD_BIT_COUNT_MIN)
{
throw invalid_argument("bit_sizes is invalid");
}
unordered_map<int, size_t> count_table;
unordered_map<int, vector<Modulus>> prime_table;
for (int size : bit_sizes)
{
++count_table[size];
}
uint64_t factor = mul_safe(uint64_t(2), safe_cast<uint64_t>(poly_modulus_degree));
factor = mul_safe(factor, plain_modulus.value() / gcd(plain_modulus.value(), factor));
for (const auto &table_elt : count_table)
{
prime_table[table_elt.first] = get_primes(factor, table_elt.first, table_elt.second);
}
vector<Modulus> result;
for (int size : bit_sizes)
{
result.emplace_back(prime_table[size].back());
prime_table[size].pop_back();
}
return result;
}
} // namespace seal