sample.h
1 /* Copyright (C) 2012-2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 #ifndef HELIB_SAMPLE_H
13 #define HELIB_SAMPLE_H
17 #include <vector>
18 #include <NTL/xdouble.h>
19 #include <NTL/ZZX.h>
20 #include <NTL/ZZ_pX.h>
21 #include <helib/zzX.h>
22 
23 namespace helib {
24 
25 const int HELIB_GAUSS_TRUNC = 8;
26 // Gaussian sampling truncates to this many standard deviations
27 
31 void sampleSmall(zzX& poly, long n, double prob = 0.5);
32 void sampleSmall(NTL::ZZX& poly, long n, double prob = 0.5);
33 
35 void sampleHWt(zzX& poly, long n, long Hwt = 100);
36 void sampleHWt(NTL::ZZX& poly, long n, long Hwt = 100);
37 
39 void sampleGaussian(zzX& poly, long n, double stdev);
40 
42 void sampleUniform(zzX& poly, long n, long B = 100);
43 void sampleUniform(NTL::ZZX& poly, long n, const NTL::ZZ& B = NTL::ZZ(100L));
44 
46 void sampleGaussian(std::vector<double>& dvec, long n, double stdev);
47 
48 class Context;
49 class PAlgebra;
50 // Same as above, but sample mod X^m-1 and then reduce mod Phi_m(X)
51 double sampleHWt(zzX& poly, const Context& context, long Hwt = 100);
52 double sampleHWtBounded(zzX& poly, const Context& context, long Hwt = 100);
53 
54 double sampleHWtBoundedEffectiveBound(const Context& context, long Hwt = 100);
55 // This just returns the effective bound used by sampleHWt bounded
56 
57 // Return value is high-probability bound on L-infty norm of canonical embedding
58 double sampleSmall(zzX& poly, const Context& context);
59 // Same as above, but ensure the result is not too much larger than typical
60 double sampleSmallBounded(zzX& poly, const Context& context);
61 
62 // Return value is high-probability bound on L-infty norm of canonical embedding
63 double sampleGaussian(zzX& poly, const Context& context, double stdev);
64 // Same as above, but ensure the result is not too much larger than typical
65 double sampleGaussianBounded(zzX& poly, const Context& context, double stdev);
66 NTL::xdouble sampleGaussianBounded(NTL::ZZX& poly,
67  const Context& context,
68  NTL::xdouble stdev);
69 
70 // Return value times stdev is the bound used in sampleGaussianBounded
71 double sampleGaussianBoundedEffectiveBound(const Context& context);
72 
73 double sampleUniform(zzX& poly, const Context& context, long B = 100);
74 NTL::xdouble sampleUniform(NTL::ZZX& poly,
75  const Context& context,
76  const NTL::ZZ& B = NTL::ZZ(100L));
77 
82 double boundFreshNoise(long m, long phim, double sigma, double epsilon = 9e-13);
83 double boundRoundingNoise(long m, long phim, long p2r, double epsilon = 9e-13);
85 
86 void reduceModPhimX(zzX& poly, const PAlgebra& palg);
87 const NTL::zz_pXModulus& getPhimXMod(const PAlgebra& palg);
88 
89 } // namespace helib
90 
91 #endif // ifndef HELIB_SAMPLE_H
Maintaining the HE scheme parameters.
Definition: Context.h:100
The structure of (Z/mZ)* /(p)
Definition: PAlgebra.h:77
Definition: apiAttributes.h:21
double boundRoundingNoise(long m, long phim, long p2r, double epsilon=9e-13)
void sampleHWt(zzX &poly, long n, long Hwt=100)
Sample a degree-(n-1) poly as above, with only Hwt nonzero coefficients.
Definition: sample.cpp:29
NTL::Vec< long > zzX
Definition: zzX.h:24
double sampleSmallBounded(zzX &poly, const Context &context)
Definition: sample.cpp:342
double boundFreshNoise(long m, long phim, double sigma, double epsilon=9e-13)
Definition: sample.cpp:578
void reduceModPhimX(zzX &poly, const PAlgebra &palg)
Definition: zzX.cpp:111
void sampleUniform(zzX &poly, long n, long B=100)
Sample a degree-(n-1) ZZX, with coefficients uniform in [-B,B].
Definition: sample.cpp:204
double sampleHWtBoundedEffectiveBound(const Context &context, long Hwt=100)
Definition: sample.cpp:260
void sampleGaussian(zzX &poly, long n, double stdev)
Sample polynomials with Gaussian coefficients.
Definition: sample.cpp:175
const int HELIB_GAUSS_TRUNC
Definition: sample.h:25
const NTL::zz_pXModulus & getPhimXMod(const PAlgebra &palg)
Definition: zzX.cpp:79
double sampleGaussianBounded(zzX &poly, const Context &context, double stdev)
Definition: sample.cpp:459
void sampleSmall(zzX &poly, long n, double prob=0.5)
Definition: sample.cpp:67
double sampleHWtBounded(zzX &poly, const Context &context, long Hwt=100)
Definition: sample.cpp:269
double sampleGaussianBoundedEffectiveBound(const Context &context)
Definition: sample.cpp:445