24 #ifndef BINFHE_RINGCORE_H 25 #define BINFHE_RINGCORE_H 32 #include "lattice/backend.h" 34 #include "math/backend.h" 35 #include "math/discretegaussiangenerator.h" 36 #include "math/nbtheory.h" 37 #include "math/transfrm.h" 38 #include "utils/serializable.h" 43 enum BINGATE { OR, AND, NOR, NAND, XOR_FAST, XNOR_FAST, XOR, XNOR };
48 enum BINFHEMETHOD { AP, GINX };
57 : m_baseG(0), m_digitsG(0), m_digitsG2(0), m_baseR(0), m_method(GINX) {}
68 uint32_t baseG, uint32_t baseR,
70 : m_LWEParams(lweparams),
75 PALISADE_THROW(
config_error,
"Gadget base should be a power of two.");
85 const shared_ptr<LWECryptoParams> lweparams = m_LWEParams;
89 uint32_t N = lweparams->GetN();
90 NativeInteger rootOfUnity = RootOfUnity<NativeInteger>(2 * N, Q);
97 m_polyParams = std::make_shared<ILNativeParams>(2 * N, Q, rootOfUnity);
99 m_digitsG = (uint32_t)std::ceil(log(Q.ConvertToDouble()) /
100 log(static_cast<double>(m_baseG)));
101 m_digitsG2 = m_digitsG * 2;
104 if (m_method == AP) {
105 uint32_t digitCountR =
106 (uint32_t)std::ceil(log(static_cast<double>(q.
ConvertToInt())) /
107 log(static_cast<double>(m_baseR)));
110 for (uint32_t i = 0; i < digitCountR; i++) {
111 m_digitsR.push_back(value);
118 for (uint32_t i = 0; i < m_digitsG; i++) {
119 m_Gpower.push_back(vTemp);
135 if (m_method == GINX) {
137 for (uint32_t i = 0; i < N; i++) {
142 m_monomials.push_back(aPoly);
146 for (uint32_t i = 0; i < N; i++) {
151 m_monomials.push_back(aPoly);
154 #if defined(BINFHE_DEBUG) 155 std::cerr <<
"base_g = " << m_baseG << std::endl;
156 std::cerr <<
"m_digitsG = " << m_digitsG << std::endl;
157 std::cerr <<
"m_digitsG2 = " << m_digitsG2 << std::endl;
158 std::cerr <<
"m_baseR = " << m_baseR << std::endl;
159 std::cerr <<
"m_digitsR = " << m_digitsR << std::endl;
160 std::cerr <<
"m_Gpower = " << m_Gpower << std::endl;
161 std::cerr <<
"n = " << m_LWEParams->Getn() << std::endl;
162 std::cerr <<
"N = " << m_LWEParams->GetN() << std::endl;
163 std::cerr <<
"q = " << m_LWEParams->Getq() << std::endl;
164 std::cerr <<
"Q = " << m_LWEParams->GetQ() << std::endl;
165 std::cerr <<
"baseKS = " << m_LWEParams->GetBaseKS() << std::endl;
166 std::cerr <<
"digitsKS = " << m_LWEParams->GetDigitsKS() << std::endl;
170 const std::shared_ptr<LWECryptoParams> GetLWEParams()
const {
174 uint32_t GetBaseG()
const {
return m_baseG; }
176 uint32_t GetDigitsG()
const {
return m_digitsG; }
178 uint32_t GetDigitsG2()
const {
return m_digitsG2; }
180 uint32_t GetBaseR()
const {
return m_baseR; }
182 const std::vector<NativeInteger>& GetDigitsR()
const {
return m_digitsR; }
184 const shared_ptr<ILNativeParams> GetPolyParams()
const {
188 const std::vector<NativeInteger>& GetGPower()
const {
return m_Gpower; }
190 const std::vector<NativeInteger>& GetGateConst()
const {
return m_gateConst; }
192 const NativePoly& GetMonomial(uint32_t i)
const {
return m_monomials[i]; }
194 BINFHEMETHOD GetMethod()
const {
return m_method; }
197 return *m_LWEParams == *other.m_LWEParams && m_baseR == other.m_baseR &&
198 m_baseG == other.m_baseG && m_method == other.m_method;
202 return !(*
this == other);
205 template <
class Archive>
206 void save(Archive& ar, std::uint32_t
const version)
const {
207 ar(::cereal::make_nvp(
"params", m_LWEParams));
208 ar(::cereal::make_nvp(
"bR", m_baseR));
209 ar(::cereal::make_nvp(
"bG", m_baseG));
210 ar(::cereal::make_nvp(
"method", m_method));
213 template <
class Archive>
214 void load(Archive& ar, std::uint32_t
const version) {
215 if (version > SerializedVersion()) {
217 "serialized object version " + std::to_string(version) +
218 " is from a later version of the library");
220 ar(::cereal::make_nvp(
"params", m_LWEParams));
221 ar(::cereal::make_nvp(
"bR", m_baseR));
222 ar(::cereal::make_nvp(
"bG", m_baseG));
223 ar(::cereal::make_nvp(
"method", m_method));
228 std::string SerializedObjectName()
const {
return "RingGSWCryptoParams"; }
229 static uint32_t SerializedVersion() {
return 1; }
233 std::shared_ptr<LWECryptoParams> m_LWEParams;
248 std::vector<NativeInteger> m_digitsR;
251 std::vector<NativeInteger> m_Gpower;
254 shared_ptr<ILNativeParams> m_polyParams;
257 std::vector<NativeInteger> m_gateConst;
261 std::vector<NativePoly> m_monomials;
264 BINFHEMETHOD m_method;
276 m_elements.resize(rowSize);
277 for (uint32_t i = 0; i < rowSize; i++) m_elements[i].resize(colSize);
281 const std::vector<std::vector<NativePoly>>& elements)
282 : m_elements(elements) {}
285 this->m_elements = rhs.m_elements;
289 this->m_elements = std::move(rhs.m_elements);
293 this->m_elements = rhs.m_elements;
298 this->m_elements = rhs.m_elements;
302 const std::vector<std::vector<NativePoly>>& GetElements()
const {
306 void SetElements(
const std::vector<std::vector<NativePoly>>& elements) {
307 m_elements = elements;
315 for (uint32_t i = 0; i < m_elements.size(); i++)
317 for (uint32_t j = 0; j < m_elements[0].size(); j++)
318 m_elements[i][j].SetFormat(format);
321 std::vector<NativePoly>& operator[](uint32_t i) {
return m_elements[i]; }
323 const std::vector<NativePoly>& operator[](usint i)
const {
324 return m_elements[i];
328 return m_elements == other.m_elements;
332 return !(*
this == other);
335 template <
class Archive>
336 void save(Archive& ar, std::uint32_t
const version)
const {
337 ar(::cereal::make_nvp(
"elements", m_elements));
340 template <
class Archive>
341 void load(Archive& ar, std::uint32_t
const version) {
342 if (version > SerializedVersion()) {
344 "serialized object version " + std::to_string(version) +
345 " is from a later version of the library");
347 ar(::cereal::make_nvp(
"elements", m_elements));
350 std::string SerializedObjectName()
const {
return "RingGSWCiphertext"; }
351 static uint32_t SerializedVersion() {
return 1; }
354 std::vector<std::vector<NativePoly>> m_elements;
365 explicit RingGSWBTKey(uint32_t dim1, uint32_t dim2, uint32_t dim3) {
367 for (uint32_t i = 0; i < dim1; i++) {
368 m_key[i].resize(dim2);
369 for (uint32_t j = 0; j < dim2; j++) m_key[i][j].resize(dim3);
374 const std::vector<std::vector<std::vector<RingGSWCiphertext>>>& key)
380 this->m_key = std::move(rhs.m_key);
384 this->m_key = rhs.m_key;
389 this->m_key = std::move(rhs.m_key);
393 const std::vector<std::vector<std::vector<RingGSWCiphertext>>>& GetElements()
399 const std::vector<std::vector<std::vector<RingGSWCiphertext>>>& key) {
403 std::vector<std::vector<RingGSWCiphertext>>& operator[](uint32_t i) {
407 const std::vector<std::vector<RingGSWCiphertext>>& operator[](usint i)
const {
412 return m_key == other.m_key;
415 bool operator!=(
const RingGSWBTKey& other)
const {
return !(*
this == other); }
417 template <
class Archive>
418 void save(Archive& ar, std::uint32_t
const version)
const {
419 ar(::cereal::make_nvp(
"key", m_key));
422 template <
class Archive>
423 void load(Archive& ar, std::uint32_t
const version) {
424 if (version > SerializedVersion()) {
426 "serialized object version " + std::to_string(version) +
427 " is from a later version of the library");
429 ar(::cereal::make_nvp(
"key", m_key));
432 std::string SerializedObjectName()
const {
return "RingGSWBTKey"; }
433 static uint32_t SerializedVersion() {
return 1; }
436 std::vector<std::vector<std::vector<RingGSWCiphertext>>> m_key;
442 std::shared_ptr<RingGSWBTKey> BSkey;
444 std::shared_ptr<LWESwitchingKey> KSkey;
Base class for PALISADE serialization.
Definition: serializable.h:76
Class that stores the refreshing key (used in bootstrapping) A three-dimensional vector of RingGSW ci...
Definition: ringcore.h:361
void SetFormat(const Format format)
Sets the format/representation of the element.
Definition: ilelement.h:451
bool IsPowerOfTwo(usint Input)
Definition: utilities.h:74
void PreCompute()
Definition: ringcore.h:84
Definition: exception.h:147
NativeIntegerT ModMul(const NativeIntegerT &b, const NativeIntegerT &modulus, typename std::enable_if<!std::is_same< T, DNativeInt >::value, bool >::type=true) const
Definition: ubintnat.h:1195
static void PreCompute(const IntType &rootOfUnity, const usint CycloOrder, const IntType &modulus)
Definition: transfrm.cpp:849
Class that stores a RingGSW ciphertext; a two-dimensional vector of ring elements.
Definition: ringcore.h:271
void SetFormat(const Format format)
Definition: ringcore.h:314
OutputType ConvertToInt() const
Definition: ubintnat.h:1886
RingGSWCryptoParams(const std::shared_ptr< LWECryptoParams > lweparams, uint32_t baseG, uint32_t baseR, BINFHEMETHOD method)
Definition: ringcore.h:67
Definition: ringcore.h:440
Definition: binfhecontext.h:36
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60
Class that stores all parameters for the RingGSW scheme used in bootstrapping.
Definition: ringcore.h:54
Definition: exception.h:107