-
Notifications
You must be signed in to change notification settings - Fork 0
/
modulus.h
185 lines (160 loc) · 6.99 KB
/
modulus.h
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include <cstddef>
#include <vector>
#include <numeric>
#include "seal/smallmodulus.h"
#include "seal/util/hestdparms.h"
namespace seal
{
/**
Represents a standard security level according to the HomomorphicEncryption.org
security standard. The value sec_level_type::none signals that no standard
security level should be imposed. The value sec_level_type::tc128 provides
a very high level of security and is the default security level enforced by
Microsoft SEAL when constructing a SEALContext object. Normal users should not
have to specify the security level explicitly anywhere.
*/
enum class sec_level_type : int
{
/**
No security level specified.
*/
none = 0,
/**
128-bit security level according to HomomorphicEncryption.org standard.
*/
tc128 = 128,
/**
192-bit security level according to HomomorphicEncryption.org standard.
*/
tc192 = 192,
/**
256-bit security level according to HomomorphicEncryption.org standard.
*/
tc256 = 256
};
/**
This class contains static methods for creating a coefficient modulus easily.
Note that while these functions take a sec_level_type argument, all security
guarantees are lost if the output is used with encryption parameters with
a mismatching value for the poly_modulus_degree.
The default value sec_level_type::tc128 provides a very high level of security
and is the default security level enforced by Microsoft SEAL when constructing
a SEALContext object. Normal users should not have to specify the security
level explicitly anywhere.
*/
class CoeffModulus
{
public:
CoeffModulus() = delete;
/**
Returns the largest bit-length of the coefficient modulus, i.e., bit-length
of the product of the primes in the coefficient modulus, that guarantees
a given security level when using a given poly_modulus_degree, according
to the HomomorphicEncryption.org security standard.
@param[in] poly_modulus_degree The value of the poly_modulus_degree
encryption parameter
@param[in] sec_level The desired standard security level
*/
SEAL_NODISCARD static constexpr int MaxBitCount(
std::size_t poly_modulus_degree,
sec_level_type sec_level = sec_level_type::tc128) noexcept
{
switch (sec_level)
{
case sec_level_type::tc128:
return util::SEAL_HE_STD_PARMS_128_TC(poly_modulus_degree);
case sec_level_type::tc192:
return util::SEAL_HE_STD_PARMS_192_TC(poly_modulus_degree);
case sec_level_type::tc256:
return util::SEAL_HE_STD_PARMS_256_TC(poly_modulus_degree);
case sec_level_type::none:
return std::numeric_limits<int>::max();
default:
return 0;
}
}
/**
Returns a default coefficient modulus for the BFV scheme that guarantees
a given security level when using a given poly_modulus_degree, according
to the HomomorphicEncryption.org security standard. Note that all security
guarantees are lost if the output is used with encryption parameters with
a mismatching value for the poly_modulus_degree.
The coefficient modulus returned by this function will not perform well
if used with the CKKS scheme.
@param[in] poly_modulus_degree The value of the poly_modulus_degree
encryption parameter
@param[in] sec_level The desired standard security level
@throws std::invalid_argument if poly_modulus_degree is not a power-of-two
or is too large
@throws std::invalid_argument if sec_level is sec_level_type::none
*/
SEAL_NODISCARD static std::vector<SmallModulus> BFVDefault(
std::size_t poly_modulus_degree,
sec_level_type sec_level = sec_level_type::tc128);
/**
Returns a custom coefficient modulus suitable for use with the specified
poly_modulus_degree. The return value will be a vector consisting of
SmallModulus elements representing distinct prime numbers of bit-lengths
as given in the bit_sizes parameter. The bit sizes of the prime numbers
can be at most 60 bits.
@param[in] poly_modulus_degree The value of the poly_modulus_degree
encryption parameter
@param[in] bit_sizes The bit-lengths of the primes to be generated
@throws std::invalid_argument if poly_modulus_degree is not a power-of-two
or is too large
@throws std::invalid_argument if bit_sizes is too large or if its elements
are out of bounds
@throws std::logic_error if not enough suitable primes could be found
*/
SEAL_NODISCARD static std::vector<SmallModulus> Create(
std::size_t poly_modulus_degree,
std::vector<int> bit_sizes);
};
/**
This class contains static methods for creating a plaintext modulus easily.
*/
class PlainModulus
{
public:
PlainModulus() = delete;
/**
Creates a prime number SmallModulus for use as plain_modulus encryption
parameter that supports batching with a given poly_modulus_degree.
@param[in] poly_modulus_degree The value of the poly_modulus_degree
encryption parameter
@param[in] bit_size The bit-length of the prime to be generated
@throws std::invalid_argument if poly_modulus_degree is not a power-of-two
or is too large
@throws std::invalid_argument if bit_size is out of bounds
@throws std::logic_error if a suitable prime could not be found
*/
SEAL_NODISCARD static inline SmallModulus Batching(
std::size_t poly_modulus_degree,
int bit_size)
{
return CoeffModulus::Create(poly_modulus_degree, { bit_size })[0];
}
/**
Creates several prime number SmallModulus elements that can be used as
plain_modulus encryption parameters, each supporting batching with a given
poly_modulus_degree.
@param[in] poly_modulus_degree The value of the poly_modulus_degree
encryption parameter
@param[in] bit_sizes The bit-lengths of the primes to be generated
@throws std::invalid_argument if poly_modulus_degree is not a power-of-two
or is too large
@throws std::invalid_argument if bit_sizes is too large or if its elements
are out of bounds
@throws std::logic_error if not enough suitable primes could be found
*/
SEAL_NODISCARD static inline std::vector<SmallModulus> Batching(
std::size_t poly_modulus_degree,
std::vector<int> bit_sizes)
{
return CoeffModulus::Create(poly_modulus_degree, bit_sizes);
}
};
}