PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
rlwe.h
1 // @file rlwe.h -- PALISADE ring-learn-with-errors functionality.
2 // @author TPOC: contact@palisade-crypto.org
3 //
4 // @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT)
5 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution. THIS SOFTWARE IS
13 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
14 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #ifndef LBCRYPTO_CRYPTO_RLWE_H
25 #define LBCRYPTO_CRYPTO_RLWE_H
26 
27 #include <memory>
28 #include <string>
29 
30 #include "lattice/dcrtpoly.h"
31 #include "lattice/poly.h"
32 #include "lattice/stdlatticeparms.h"
33 #include "utils/serializable.h"
34 
35 namespace lbcrypto {
36 
37 // noise flooding distribution parameter
38 // for distributed decryption in
39 // threshold FHE
40 const double MP_SD = 1048576;
41 
46 template <class Element>
47 class LPCryptoParametersRLWE : public LPCryptoParameters<Element> {
48  public:
53  m_distributionParameter = 0.0f;
54  m_assuranceMeasure = 0.0f;
55  m_securityLevel = 0.0f;
56  m_relinWindow = 1;
57  m_dgg.SetStd(m_distributionParameter);
58  m_depth = 0;
59  m_maxDepth = 2;
60  m_mode = RLWE;
61  m_stdLevel = HEStd_NotSet;
62  }
63 
69  : LPCryptoParameters<Element>(rhs.GetElementParams(),
70  rhs.GetPlaintextModulus()) {
71  m_distributionParameter = rhs.m_distributionParameter;
72  m_assuranceMeasure = rhs.m_assuranceMeasure;
73  m_securityLevel = rhs.m_securityLevel;
74  m_relinWindow = rhs.m_relinWindow;
75  m_dgg.SetStd(m_distributionParameter);
76  m_depth = rhs.m_depth;
77  m_maxDepth = rhs.m_maxDepth;
78  m_mode = rhs.m_mode;
79  m_stdLevel = rhs.m_stdLevel;
80  }
81 
97  LPCryptoParametersRLWE(shared_ptr<typename Element::Params> params,
98  EncodingParams encodingParams,
99  float distributionParameter, float assuranceMeasure,
100  float securityLevel, usint relinWindow, int depth = 1,
101  int maxDepth = 2, MODE mode = RLWE)
102  : LPCryptoParameters<Element>(params, encodingParams) {
103  m_distributionParameter = distributionParameter;
104  m_assuranceMeasure = assuranceMeasure;
105  m_securityLevel = securityLevel;
106  m_relinWindow = relinWindow;
107  m_dgg.SetStd(m_distributionParameter);
108  m_depth = depth;
109  m_maxDepth = maxDepth;
110  m_mode = mode;
111  m_stdLevel = HEStd_NotSet;
112  }
113 
130  LPCryptoParametersRLWE(shared_ptr<typename Element::Params> params,
131  EncodingParams encodingParams,
132  float distributionParameter, float assuranceMeasure,
133  SecurityLevel stdLevel, usint relinWindow,
134  int depth = 1, int maxDepth = 2, MODE mode = RLWE)
135  : LPCryptoParameters<Element>(params, encodingParams) {
136  m_distributionParameter = distributionParameter;
137  m_assuranceMeasure = assuranceMeasure;
138  m_securityLevel = 0;
139  m_relinWindow = relinWindow;
140  m_dgg.SetStd(m_distributionParameter);
141  m_depth = depth;
142  m_maxDepth = maxDepth;
143  m_mode = mode;
144  m_stdLevel = stdLevel;
145  }
146 
151 
158  float GetDistributionParameter() const { return m_distributionParameter; }
159 
165  float GetAssuranceMeasure() const { return m_assuranceMeasure; }
166 
172  float GetSecurityLevel() const { return m_securityLevel; }
173 
179  usint GetRelinWindow() const { return m_relinWindow; }
180 
187  int GetDepth() const { return m_depth; }
188 
195  size_t GetMaxDepth() const { return m_maxDepth; }
196 
202  MODE GetMode() const { return m_mode; }
203 
209  SecurityLevel GetStdLevel() const { return m_stdLevel; }
210 
216  const typename Element::DggType &GetDiscreteGaussianGenerator() const {
217  return m_dgg;
218  }
219 
220  // @Set Properties
221 
226  void SetDistributionParameter(float distributionParameter) {
227  m_distributionParameter = distributionParameter;
228  m_dgg.SetStd(m_distributionParameter);
229  }
230 
235  void SetAssuranceMeasure(float assuranceMeasure) {
236  m_assuranceMeasure = assuranceMeasure;
237  }
238 
243  void SetSecurityLevel(float securityLevel) {
244  m_securityLevel = securityLevel;
245  }
246 
251  void SetStdLevel(SecurityLevel securityLevel) { m_stdLevel = securityLevel; }
252 
257  void SetRelinWindow(usint relinWindow) { m_relinWindow = relinWindow; }
258 
264  void SetDepth(int depth) { m_depth = depth; }
265 
271  void SetMaxDepth(size_t maxDepth) { m_maxDepth = maxDepth; }
272 
277  void SetMode(MODE mode) { m_mode = mode; }
278 
284  bool operator==(const LPCryptoParameters<Element> &rhs) const {
285  const auto *el =
286  dynamic_cast<const LPCryptoParametersRLWE<Element> *>(&rhs);
287 
288  if (el == nullptr) return false;
289 
290  return this->GetPlaintextModulus() == el->GetPlaintextModulus() &&
291  *this->GetElementParams() == *el->GetElementParams() &&
292  *this->GetEncodingParams() == *el->GetEncodingParams() &&
293  m_distributionParameter == el->GetDistributionParameter() &&
294  m_assuranceMeasure == el->GetAssuranceMeasure() &&
295  m_securityLevel == el->GetSecurityLevel() &&
296  m_relinWindow == el->GetRelinWindow() && m_mode == el->GetMode() &&
297  m_stdLevel == el->GetStdLevel();
298  }
299 
300  void PrintParameters(std::ostream &os) const {
302 
303  os << "Distrib parm " << GetDistributionParameter()
304  << ", Assurance measure " << GetAssuranceMeasure() << ", Security level "
305  << GetSecurityLevel() << ", Relin window " << GetRelinWindow()
306  << ", Depth " << GetDepth() << ", Mode " << GetMode()
307  << ", Standard security level " << GetStdLevel() << std::endl;
308  }
309 
310  template <class Archive>
311  void save(Archive &ar, std::uint32_t const version) const {
312  ar(::cereal::base_class<LPCryptoParameters<Element>>(this));
313  ar(::cereal::make_nvp("dp", m_distributionParameter));
314  ar(::cereal::make_nvp("am", m_assuranceMeasure));
315  ar(::cereal::make_nvp("sl", m_securityLevel));
316  ar(::cereal::make_nvp("rw", m_relinWindow));
317  ar(::cereal::make_nvp("d", m_depth));
318  ar(::cereal::make_nvp("md", m_maxDepth));
319  ar(::cereal::make_nvp("mo", m_mode));
320  ar(::cereal::make_nvp("slv", m_stdLevel));
321  }
322 
323  template <class Archive>
324  void load(Archive &ar, std::uint32_t const version) {
325  ar(::cereal::base_class<LPCryptoParameters<Element>>(this));
326  ar(::cereal::make_nvp("dp", m_distributionParameter));
327  m_dgg.SetStd(m_distributionParameter);
328  ar(::cereal::make_nvp("am", m_assuranceMeasure));
329  ar(::cereal::make_nvp("sl", m_securityLevel));
330  ar(::cereal::make_nvp("rw", m_relinWindow));
331  ar(::cereal::make_nvp("d", m_depth));
332  ar(::cereal::make_nvp("md", m_maxDepth));
333  ar(::cereal::make_nvp("mo", m_mode));
334  ar(::cereal::make_nvp("slv", m_stdLevel));
335  }
336 
337  std::string SerializedObjectName() const { return "RLWESchemeParameters"; }
338 
339  protected:
340  // standard deviation in Discrete Gaussian Distribution
341  float m_distributionParameter;
342  // assurance measure alpha
343  float m_assuranceMeasure;
344  // root Hermite value /delta
345  float m_securityLevel;
346  // relinearization window
347  usint m_relinWindow;
348  // depth of computations; used for FHE
349  int m_depth;
350  // maximum depth support of a ciphertext without keyswitching
351  // corresponds to the highest power of secret key for which evaluation keys
352  // are genererated
353  uint32_t m_maxDepth;
354  // specifies whether the secret polynomials are generated from discrete
355  // Gaussian distribution or ternary distribution with the norm of unity
356  MODE m_mode;
357  // Security level according in the HomomorphicEncryption.org standard
358  SecurityLevel m_stdLevel;
359 
360  typename Element::DggType m_dgg;
361 };
362 } // namespace lbcrypto
363 
364 #endif
void SetRelinWindow(usint relinWindow)
Definition: rlwe.h:257
LPCryptoParametersRLWE(shared_ptr< typename Element::Params > params, EncodingParams encodingParams, float distributionParameter, float assuranceMeasure, float securityLevel, usint relinWindow, int depth=1, int maxDepth=2, MODE mode=RLWE)
Definition: rlwe.h:97
Template for crypto parameters.
Definition: rlwe.h:47
virtual const PlaintextModulus & GetPlaintextModulus() const
Definition: pubkeylp.h:3051
LPCryptoParametersRLWE(shared_ptr< typename Element::Params > params, EncodingParams encodingParams, float distributionParameter, float assuranceMeasure, SecurityLevel stdLevel, usint relinWindow, int depth=1, int maxDepth=2, MODE mode=RLWE)
Definition: rlwe.h:130
bool operator==(const LPCryptoParameters< Element > &rhs) const
Definition: rlwe.h:284
Ideal lattice using a vector representation.
SecurityLevel GetStdLevel() const
Definition: rlwe.h:209
size_t GetMaxDepth() const
Definition: rlwe.h:195
LPCryptoParametersRLWE(const LPCryptoParametersRLWE &rhs)
Definition: rlwe.h:68
void SetSecurityLevel(float securityLevel)
Definition: rlwe.h:243
virtual const EncodingParams GetEncodingParams() const
Definition: pubkeylp.h:3069
void SetAssuranceMeasure(float assuranceMeasure)
Definition: rlwe.h:235
const Element::DggType & GetDiscreteGaussianGenerator() const
Definition: rlwe.h:216
MODE GetMode() const
Definition: rlwe.h:202
void SetMode(MODE mode)
Definition: rlwe.h:277
void SetDepth(int depth)
Definition: rlwe.h:264
LPCryptoParametersRLWE()
Definition: rlwe.h:52
int GetDepth() const
Definition: rlwe.h:187
main implementation class to capture essential cryptoparameters of any LBC system ...
Definition: pubkeylp.h:73
usint GetRelinWindow() const
Definition: rlwe.h:179
void SetStdLevel(SecurityLevel securityLevel)
Definition: rlwe.h:251
Definition: binfhecontext.h:36
void SetMaxDepth(size_t maxDepth)
Definition: rlwe.h:271
virtual ~LPCryptoParametersRLWE()
Definition: rlwe.h:150
virtual const shared_ptr< typename Element::Params > GetElementParams() const
Definition: pubkeylp.h:3060
float GetAssuranceMeasure() const
Definition: rlwe.h:165
float GetSecurityLevel() const
Definition: rlwe.h:172
void SetDistributionParameter(float distributionParameter)
Definition: rlwe.h:226
float GetDistributionParameter() const
Definition: rlwe.h:158