forked from microsoft/SEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modulus.cpp
174 lines (153 loc) · 5.43 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
// 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)
{
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);
}
}
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];
}
for (const auto &table_elt : count_table)
{
prime_table[table_elt.first] = get_primes(poly_modulus_degree, 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