PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
dgsampling.h
1 // @file dgsampling.h Provides detailed algorithms for G-sampling and
2 // perturbation sampling as described in https://eprint.iacr.org/2017/844.pdf,
3 // https://eprint.iacr.org/2018/946, and "Implementing Token-Based Obfuscation
4 // under (Ring) LWE" (not publicly available yet)
5 // @author TPOC: contact@palisade-crypto.org
6 //
7 // @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT)
8 // All rights reserved.
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 // 1. Redistributions of source code must retain the above copyright notice,
12 // this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright notice,
14 // this list of conditions and the following disclaimer in the documentation
15 // and/or other materials provided with the distribution. THIS SOFTWARE IS
16 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef LBCRYPTO_OBFMATH_DGSAMPLING_H
28 #define LBCRYPTO_OBFMATH_DGSAMPLING_H
29 
30 #include <memory>
31 #include <vector>
32 
33 #include "lattice/field2n.h"
34 #include "math/matrix.h"
35 #include "math/nbtheory.h"
36 
37 namespace lbcrypto {
38 
39 // Statistical error in Gaussian sampling
40 // corresponds to statistical error of 2^(-80)
41 const double DG_ERROR = 8.27181e-25;
42 
43 // Maximum ring dimension to be supported - up to 560 bits in the modulus
44 const int32_t N_MAX = 16384;
45 
46 // Smoothing parameter also used as a "standard deviation" for generating error
47 // polynomials
48 const double SIGMA = std::sqrt(std::log(2 * N_MAX / DG_ERROR) / M_PI);
49 
50 // Spectral norm for preimage samples
51 const double SPECTRAL_CONSTANT = 1.8;
52 const auto SPECTRAL_BOUND = [](uint64_t n, uint64_t k,
53  uint64_t base) -> double {
54  return SPECTRAL_CONSTANT * (base + 1) * SIGMA * SIGMA *
55  (std::sqrt(n * k) + std::sqrt(2 * n) + 4.7);
56 };
57 
58 // Spectral norm for preimage samples - for the case of matrices of ring
59 // elements
60 const auto SPECTRAL_BOUND_D = [](uint64_t n, uint64_t k, uint64_t base,
61  uint64_t d) -> double {
62  return SPECTRAL_CONSTANT * (base + 1) * SIGMA * SIGMA *
63  (std::sqrt(d * n * k) + std::sqrt(2 * n) + 4.7);
64 };
65 
72 template <class Element>
74  public:
89  static void GaussSampGq(const Element &u, double stddev, size_t k,
90  const typename Element::Integer &q, int64_t base,
91  typename Element::DggType &dgg, Matrix<int64_t> *z);
92 
107  static void GaussSampGqArbBase(const Element &u, double stddev, size_t k,
108  const typename Element::Integer &q,
109  int64_t base, typename Element::DggType &dgg,
110  Matrix<int64_t> *z);
111 
123  static void ZSampleSigma2x2(const Field2n &a, const Field2n &b,
124  const Field2n &d, const Matrix<Field2n> &c,
125  const typename Element::DggType &dgg,
126  shared_ptr<Matrix<int64_t>> p);
127 
139  static void SampleMat(const Matrix<Field2n> &A, const Matrix<Field2n> &B,
140  const Matrix<Field2n> &D, const Matrix<Field2n> &C,
141  const typename Element::DggType &dgg,
142  shared_ptr<Matrix<int64_t>> p);
143 
153  static shared_ptr<Matrix<int64_t>> ZSampleF(
154  const Field2n &f, const Field2n &c, const typename Element::DggType &dgg,
155  size_t n);
156 
157  private:
158  // subroutine used by GaussSampGq
159  // Discrete sampling variant
160  // As described in Figure 2 of https://eprint.iacr.org/2017/308.pdf
161  static void Perturb(double sigma, size_t k, size_t n, const vector<double> &l,
162  const vector<double> &h, int64_t base,
163  typename Element::DggType &dgg, vector<int64_t> *p);
164 
165  // subroutine used by GaussSampGqArbBase
166  // Continuous sampling variant
167  // As described in Algorithm 3 of https://eprint.iacr.org/2017/844.pdf
168  static void PerturbFloat(double sigma, size_t k, size_t n,
169  const vector<double> &l, const vector<double> &h,
170  int64_t base, typename Element::DggType &dgg,
171  vector<double> *p);
172 
173  // subroutine used by GaussSampGq
174  // As described in Algorithm 3 of https://eprint.iacr.org/2017/844.pdf
175  static void SampleC(const Matrix<double> &c, size_t k, size_t n, double sigma,
176  typename Element::DggType &dgg, Matrix<double> *a,
177  vector<int64_t> *z);
178 
179  // subroutine earlier used by ZSampleF
180  // Algorithm utilizes the same permutation algorithm as discussed in
181  // https://eprint.iacr.org/2017/844.pdf
182  static Matrix<int32_t> Permute(Matrix<int32_t> *p);
183 
184  // subroutine used by ZSampleF
185  // Algorithm utilizes the same inverse permutation algorithm as discussed in
186  // https://eprint.iacr.org/2017/844.pdf
187  static void InversePermute(shared_ptr<Matrix<int64_t>> p);
188 };
189 
190 } // namespace lbcrypto
191 
192 #endif
static void GaussSampGq(const Element &u, double stddev, size_t k, const typename Element::Integer &q, int64_t base, typename Element::DggType &dgg, Matrix< int64_t > *z)
Definition: dgsampling.cpp:39
A class to represent field elements with power-of-2 dimension.
Definition: field2n.h:42
static void ZSampleSigma2x2(const Field2n &a, const Field2n &b, const Field2n &d, const Matrix< Field2n > &c, const typename Element::DggType &dgg, shared_ptr< Matrix< int64_t >> p)
Definition: dgsampling.cpp:272
static void SampleMat(const Matrix< Field2n > &A, const Matrix< Field2n > &B, const Matrix< Field2n > &D, const Matrix< Field2n > &C, const typename Element::DggType &dgg, shared_ptr< Matrix< int64_t >> p)
Definition: dgsampling.cpp:313
static void GaussSampGqArbBase(const Element &u, double stddev, size_t k, const typename Element::Integer &q, int64_t base, typename Element::DggType &dgg, Matrix< int64_t > *z)
Definition: dgsampling.cpp:120
static shared_ptr< Matrix< int64_t > > ZSampleF(const Field2n &f, const Field2n &c, const typename Element::DggType &dgg, size_t n)
Definition: dgsampling.cpp:450
Definition: matrix.h:47
Utility class containing operations needed for lattice sampling; Sources: https://eprint.iacr.org/2017/844.pdf and https://eprint.iacr.org/2017/308.pdf This construction is based on the hardness of Ring-LWE problem.
Definition: dgsampling.h:73
Definition: binfhecontext.h:36