25 #ifndef LBCRYPTO_LATTICE_ILDCRTELEMENT_H 26 #define LBCRYPTO_LATTICE_ILDCRTELEMENT_H 34 #include "math/backend.h" 35 #include "math/nbtheory.h" 36 #include "utils/inttypes.h" 52 template <
typename IntType>
53 class ILDCRTParams :
public ElemParams<IntType> {
55 static const usint DEFAULT_NBITS = 20;
57 typedef IntType Integer;
58 using ILNativeParams = ILParamsImpl<NativeInteger>;
68 usint bits = DEFAULT_NBITS);
78 const IntType &rootOfUnity)
79 :
ElemParams<IntType>(cyclotomic_order, modulus, 0, 0, 0) {
84 if (cyclotomic_order == 0)
return;
88 "in ILDCRTParams(const usint cyclotomic_order, const IntType &modulus, " 89 "const IntType& rootOfUnity");
90 DEBUGEXP(cyclotomic_order);
92 DEBUGEXP(rootOfUnity);
94 std::vector<NativeInteger> moduli;
95 std::vector<NativeInteger> rootsOfUnity;
98 FirstPrime<NativeInteger>(DEFAULT_NBITS, cyclotomic_order);
99 IntType compositeModulus(1);
103 rootsOfUnity.push_back(
RootOfUnity(cyclotomic_order, q));
104 compositeModulus = compositeModulus * IntType(q.
ConvertToInt());
105 if (compositeModulus >= modulus)
break;
110 originalModulus = modulus;
111 DEBUGEXP(compositeModulus);
113 DEBUGEXP(rootsOfUnity);
114 DEBUGEXP(m_parms.size());
115 for (
size_t i = 0; i < moduli.size(); i++) {
116 m_parms.push_back(std::make_shared<ILNativeParams>(
117 cyclotomic_order, moduli[i], rootsOfUnity[i]));
121 DEBUGEXP(m_parms.size());
137 const std::vector<NativeInteger> &moduli,
138 const std::vector<NativeInteger> &rootsOfUnity,
139 const std::vector<NativeInteger> &moduliBig = {},
140 const std::vector<NativeInteger> &rootsOfUnityBig = {},
141 const IntType &inputOriginalModulus = IntType(0))
143 this->originalModulus = inputOriginalModulus;
144 if (moduli.size() != rootsOfUnity.size()) {
146 "sizes of moduli and roots of unity do not match");
149 if (moduliBig.size() == moduli.size()) {
150 for (
size_t i = 0; i < moduli.size(); i++) {
151 m_parms.push_back(std::make_shared<ILNativeParams>(
152 cyclotomic_order, moduli[i], rootsOfUnity[i], moduliBig[i],
153 rootsOfUnityBig[i]));
157 for (
size_t i = 0; i < moduli.size(); i++) {
158 m_parms.push_back(std::make_shared<ILNativeParams>(
159 cyclotomic_order, moduli[i], rootsOfUnity[i]));
174 const std::vector<NativeInteger> &moduli,
175 const IntType &inputOriginalModulus = IntType(0))
176 :
ElemParams<IntType>(cyclotomic_order, 0, 0, 0, 0) {
177 this->originalModulus = inputOriginalModulus;
179 for (
size_t i = 0; i < moduli.size(); i++) {
180 m_parms.push_back(std::make_shared<ILNativeParams>(cyclotomic_order,
181 moduli[i], 0, 0, 0));
195 std::vector<std::shared_ptr<ILNativeParams>> &parms,
196 const IntType &inputOriginalModulus = IntType(0))
197 :
ElemParams<IntType>(cyclotomic_order, 0, 0, 0, 0), m_parms(parms) {
198 this->originalModulus = inputOriginalModulus;
211 originalModulus = rhs.originalModulus;
213 m_parms = rhs.m_parms;
223 const std::vector<std::shared_ptr<ILNativeParams>> &
GetParams()
const {
235 uint32_t start, uint32_t end)
const {
236 if (end < start || end > this->
GetParams().size()) {
238 "Incorrect parameters for GetParamPartition - (start: " +
239 std::to_string(start) +
240 ", end:" + std::to_string(end) +
")");
243 std::vector<std::shared_ptr<ILNativeParams>> resParams =
244 std::vector<std::shared_ptr<ILNativeParams>>(end - start + 1);
246 IntType q = IntType(1);
247 for (uint32_t i = 0; i <= (end - start); i++) {
248 resParams[i] = this->
GetParams()[i + start];
267 originalModulus = inputOriginalModulus;
284 this->ciphertextModulus /=
285 IntType(m_parms.back()->GetModulus().ConvertToInt());
301 const auto *dcrtParams =
dynamic_cast<const ILDCRTParams *
>(&other);
303 if (dcrtParams ==
nullptr)
return false;
307 if (m_parms.size() != dcrtParams->m_parms.size())
return false;
309 for (
size_t i = 0; i < m_parms.size(); i++) {
310 if (*m_parms[i] != *dcrtParams->m_parms[i])
return false;
324 this->ciphertextModulus = 1;
326 for (usint i = 0; i < m_parms.size(); i++) {
327 this->ciphertextModulus =
328 this->ciphertextModulus *
329 IntType(m_parms[i]->
GetModulus().ConvertToInt());
338 this->bigCiphertextModulus = 1;
340 for (usint i = 0; i < m_parms.size(); i++) {
341 this->bigCiphertextModulus =
342 this->bigCiphertextModulus *
347 template <
class Archive>
348 void save(Archive &ar, std::uint32_t
const version)
const {
350 ar(::cereal::make_nvp(
"p", m_parms));
351 ar(::cereal::make_nvp(
"m", originalModulus));
354 template <
class Archive>
355 void load(Archive &ar, std::uint32_t
const version) {
356 if (version > SerializedVersion()) {
358 "serialized object version " + std::to_string(version) +
359 " is from a later version of the library");
362 ar(::cereal::make_nvp(
"p", m_parms));
363 ar(::cereal::make_nvp(
"m", originalModulus));
366 std::string SerializedObjectName()
const {
return "DCRTParams"; }
367 static uint32_t SerializedVersion() {
return 1; }
370 std::ostream &doprint(std::ostream &out)
const {
371 out <<
"ILDCRTParams ";
373 out << std::endl <<
" Parms:" << std::endl;
374 for (
size_t i = 0; i < m_parms.size(); i++) {
375 out <<
" " << i <<
":" << *m_parms[i] << std::endl;
377 out <<
"OriginalModulus " << originalModulus << std::endl;
382 std::vector<std::shared_ptr<ILNativeParams>> m_parms;
389 Integer originalModulus;
bool operator==(const ElemParams< IntType > &other) const
Equality operator checks if the ElemParams are the same.
Definition: ildcrtparams.h:300
void SetOriginalModulus(const IntType &inputOriginalModulus)
Simple setter method for the original modulus, not the ciphertex modulus.
Definition: ildcrtparams.h:266
void RecalculateModulus()
Method to recalculate the composite modulus from the component moduli.
Definition: ildcrtparams.h:323
Definition: elemparams.h:43
ILDCRTParams(const usint cyclotomic_order, const std::vector< NativeInteger > &moduli, const std::vector< NativeInteger > &rootsOfUnity, const std::vector< NativeInteger > &moduliBig={}, const std::vector< NativeInteger > &rootsOfUnityBig={}, const IntType &inputOriginalModulus=IntType(0))
Constructor with some pre-computed parameters provided as input.
Definition: ildcrtparams.h:136
const ElemParams & operator=(const ElemParams &rhs)
Assignment operator using assignment operations of wrapped elements.
Definition: elemparams.h:103
std::shared_ptr< ILNativeParams > & operator[](const usint i)
Getter method for the component parameters of a specific index.
Definition: ildcrtparams.h:275
Definition: exception.h:147
Definition: exception.h:113
IntType NextPrime(const IntType &q, uint64_t cyclotomicOrder)
Definition: nbtheory.cpp:537
ILDCRTParams(const usint cyclotomic_order, std::vector< std::shared_ptr< ILNativeParams >> &parms, const IntType &inputOriginalModulus=IntType(0))
Constructor that takes in the cyclotomic order and the component parameters of the component moduli...
Definition: ildcrtparams.h:194
Wrapper class to hold the parameters for Element types and their inheritors.
const IntType & GetModulus() const
Simple getter method for the ciphertext modulus, not the big ciphertext modulus.
Definition: elemparams.h:146
~ILDCRTParams()
Definition: ildcrtparams.h:292
const std::vector< std::shared_ptr< ILNativeParams > > & GetParams() const
Getter method for the component parameters.
Definition: ildcrtparams.h:223
virtual std::ostream & doprint(std::ostream &out) const
Pretty print operator for the ElemParams type.
Definition: elemparams.h:246
void PopLastParam()
Removes the last parameter set and adjust the multiplied moduli.
Definition: ildcrtparams.h:283
const IntType & GetBigModulus() const
Simpler getter method for the big ciphertext modulus. This is not relevant for all applications...
Definition: elemparams.h:153
OutputType ConvertToInt() const
Definition: ubintnat.h:1886
const IntType & GetOriginalModulus() const
Simple getter method for the original modulus, not the ciphertex modulus.
Definition: ildcrtparams.h:260
std::vector< std::shared_ptr< ILNativeParams > > GetParamPartition(uint32_t start, uint32_t end) const
Getter method that returns a subset of the component parameters.
Definition: ildcrtparams.h:234
ILDCRTParams(const usint cyclotomic_order, const std::vector< NativeInteger > &moduli, const IntType &inputOriginalModulus=IntType(0))
Constructor with only cylotomic order and chain of moduli. Multiplied values of the chain of moduli i...
Definition: ildcrtparams.h:173
ILDCRTParams(const usint cyclotomic_order, const IntType &modulus, const IntType &rootOfUnity)
Constructor with basic parameters.
Definition: ildcrtparams.h:77
void RecalculateBigModulus()
Method to recalculate the big composite modulus from the component moduli.
Definition: ildcrtparams.h:337
IntType RootOfUnity(usint m, const IntType &modulo)
Definition: nbtheory.cpp:270
Definition: binfhecontext.h:36
Wrapper class to hold the parameters for integer lattice operations and their inheritors.
ILDCRTParams(usint order=0, usint depth=1, usint bits=DEFAULT_NBITS)
Constructor with basic parameter set. q is selected as FirstPrime(bits, order)
Definition: ildcrtparams.cpp:29
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60
Parameters for array of ideal lattices (used for Double-CRT).
Definition: backend.h:71
const ILDCRTParams & operator=(const ILDCRTParams &rhs)
Definition: ildcrtparams.h:209