keySwitching.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 
13 #ifndef HELIB_KEY_SWITCHING_H
14 #define HELIB_KEY_SWITCHING_H
23 #include <climits>
24 #include <helib/DoubleCRT.h>
25 #include <helib/Context.h>
26 #include <helib/Ctxt.h>
27 
28 namespace helib {
29 
86 class KeySwitch
87 {
88 public:
93  static constexpr std::string_view typeName = "KeySwitch";
94 
95  SKHandle fromKey; // A handle for the key s'
96  long toKeyID; // Index of the key s that we are switching into
97  long ptxtSpace; // either p or p^r
98 
99  std::vector<DoubleCRT> b; // The top row, consisting of the bi's
100  NTL::ZZ prgSeed; // a seed to generate the random ai's in the bottom row
101  NTL::xdouble noiseBound; // high probability bound on noise magnitude
102  // in each column
103 
104  explicit KeySwitch(long sPow = 0,
105  long xPow = 0,
106  long fromID = 0,
107  long toID = 0,
108  long p = 0);
109  explicit KeySwitch(const SKHandle& _fromKey,
110  long fromID = 0,
111  long toID = 0,
112  long p = 0);
113 
114  bool operator==(const KeySwitch& other) const;
115  bool operator!=(const KeySwitch& other) const;
116 
117  unsigned long NumCols() const;
118 
120  static const KeySwitch& dummy();
121  bool isDummy() const;
122 
124  void verify(SecKey& sk);
125 
127  void readMatrix(std::istream& str, const Context& context);
128 
130 
134  void writeTo(std::ostream& str) const;
135 
142  static KeySwitch readFrom(std::istream& str, const Context& context);
143 
149  void writeToJSON(std::ostream& str) const;
150 
155  JsonWrapper writeToJSON() const;
156 
164  static KeySwitch readFromJSON(std::istream& str, const Context& context);
165 
173  static KeySwitch readFromJSON(const JsonWrapper& j, const Context& context);
174 
181  void readJSON(std::istream& str, const Context& context);
182 
189  void readJSON(const JsonWrapper& j, const Context& context);
190 };
191 std::ostream& operator<<(std::ostream& str, const KeySwitch& matrix);
192 // We DO NOT have std::istream& operator>>(std::istream& str, KeySwitch&
193 // matrix); instead must use the readMatrix method above, where you can specify
194 // context
195 
198 
201 #define HELIB_KEYSWITCH_THRESH (50)
202 
206 #define HELIB_KEYSWITCH_MIN_THRESH (8)
207 
210 long KSGiantStepSize(long D);
211 
214 void addAllMatrices(SecKey& sKey, long keyID = 0);
215 
218 void addFewMatrices(SecKey& sKey, long keyID = 0);
219 
223 void addSome1DMatrices(SecKey& sKey,
224  long bound = HELIB_KEYSWITCH_THRESH,
225  long keyID = 0);
226 
230 void add1DMatrices(SecKey& sKey, long keyID = 0);
231 
232 void addBSGS1DMatrices(SecKey& sKey, long keyID = 0);
233 
235 void addSomeFrbMatrices(SecKey& sKey,
236  long bound = HELIB_KEYSWITCH_THRESH,
237  long keyID = 0);
238 
239 void addFrbMatrices(SecKey& sKey, long keyID = 0);
240 
241 void addBSGSFrbMatrices(SecKey& sKey, long keyID = 0);
242 
244 void addMinimal1DMatrices(SecKey& sKey, long keyID = 0);
245 void addMinimalFrbMatrices(SecKey& sKey, long keyID = 0);
246 
248 class PermNetwork;
249 void addMatrices4Network(SecKey& sKey, const PermNetwork& net, long keyID = 0);
250 
252 void addTheseMatrices(SecKey& sKey,
253  const std::set<long>& automVals,
254  long keyID = 0);
255 
256 } // namespace helib
257 
258 #endif // HELIB_KEY_SWITCHING_H
Maintaining the HE scheme parameters.
Definition: Context.h:100
Key-switching matrices.
Definition: keySwitching.h:87
void writeTo(std::ostream &str) const
Raw IO.
Definition: keySwitching.cpp:196
bool isDummy() const
Definition: keySwitching.cpp:76
std::vector< DoubleCRT > b
Definition: keySwitching.h:99
KeySwitch(const SKHandle &_fromKey, long fromID=0, long toID=0, long p=0)
SKHandle fromKey
Definition: keySwitching.h:95
NTL::xdouble noiseBound
Definition: keySwitching.h:101
bool operator==(const KeySwitch &other) const
Definition: keySwitching.cpp:46
KeySwitch(long sPow=0, long xPow=0, long fromID=0, long toID=0, long p=0)
Definition: keySwitching.cpp:35
void verify(SecKey &sk)
A debugging method.
Definition: keySwitching.cpp:78
long ptxtSpace
Definition: keySwitching.h:97
JsonWrapper writeToJSON() const
Write out the switch key (KeySwitch) object to a JsonWrapper.
Definition: keySwitching.cpp:243
void readMatrix(std::istream &str, const Context &context)
Read a key-switching matrix from input.
Definition: keySwitching.cpp:191
static KeySwitch readFromJSON(std::istream &str, const Context &context)
Read from the stream the serialized switch key (KeySwitch) object using JSON format.
Definition: keySwitching.cpp:264
void readJSON(std::istream &str, const Context &context)
In-place read from the stream the serialized switch key (KeySwitch) object using JSON format.
Definition: keySwitching.cpp:278
bool operator!=(const KeySwitch &other) const
Definition: keySwitching.cpp:69
long toKeyID
Definition: keySwitching.h:96
static KeySwitch readFrom(std::istream &str, const Context &context)
Read from the stream the serialized KeySwitch object in binary format.
Definition: keySwitching.cpp:221
static constexpr std::string_view typeName
Class label to be added to JSON serialization as object type information.
Definition: keySwitching.h:93
NTL::ZZ prgSeed
Definition: keySwitching.h:100
unsigned long NumCols() const
Definition: keySwitching.cpp:74
static const KeySwitch & dummy()
returns a dummy static matrix with toKeyId == -1
Definition: keySwitching.cpp:177
A full permutation network.
Definition: permutations.h:562
A handle, describing the secret-key element that "matches" a part, of the form s^r(X^t).
Definition: Ctxt.h:81
The secret key.
Definition: keys.h:311
Definition: apiAttributes.h:21
void addMinimal1DMatrices(SecKey &sKey, long keyID=0)
These routines just add a single matrix (or two, for bad dimensions)
Definition: keySwitching.cpp:648
void addSome1DMatrices(SecKey &sKey, long bound=HELIB_KEYSWITCH_THRESH, long keyID=0)
Generate some matrices of the form s(X^{g^i})->s(X), but not all. For a generator g whose order is la...
Definition: keySwitching.cpp:573
void addSomeFrbMatrices(SecKey &sKey, long bound=HELIB_KEYSWITCH_THRESH, long keyID=0)
Generate all/some Frobenius matrices of the form s(X^{p^i})->s(X)
Definition: keySwitching.cpp:599
void addAllMatrices(SecKey &sKey, long keyID=0)
Maximalistic approach: generate matrices s(X^e)->s(X) for all e in Zm*.
Definition: keySwitching.cpp:307
long KSGiantStepSize(long D)
Function that returns number of baby steps. Used to keep this and matmul routines "in sync".
Definition: keySwitching.cpp:297
void addFrbMatrices(SecKey &sKey, long keyID=0)
Definition: keySwitching.cpp:610
void addBSGS1DMatrices(SecKey &sKey, long keyID=0)
Definition: keySwitching.cpp:593
void addMatrices4Network(SecKey &sKey, const PermNetwork &net, long keyID=0)
Definition: keySwitching.cpp:667
void add1DMatrices(SecKey &sKey, long keyID=0)
Generate all matrices s(X^{g^i})->s(X) for generators g of Zm* /(p) and i<ord(g). If g has different ...
Definition: keySwitching.cpp:588
void addTheseMatrices(SecKey &sKey, const std::set< long > &automVals, long keyID=0)
Generate specific key-switching matrices, described by the given set.
Definition: keySwitching.cpp:688
void addMinimalFrbMatrices(SecKey &sKey, long keyID=0)
Definition: keySwitching.cpp:660
void addFewMatrices(SecKey &sKey, long keyID=0)
Generate matrices so every s(X^e) can be reLinearized in at most two steps.
std::ostream & operator<<(std::ostream &os, const ContextBuilder< SCHEME > &cb)
ostream operator for serializing the ContextBuilder object.
void addBSGSFrbMatrices(SecKey &sKey, long keyID=0)
Definition: keySwitching.cpp:615
Definition: JsonWrapper.h:9