permutations.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  */
16 #ifndef HELIB_PERMUTATIONS_H
17 #define HELIB_PERMUTATIONS_H
18 
19 #include <helib/PAlgebra.h>
20 #include <helib/matching.h>
21 #include <helib/hypercube.h>
22 #include <helib/apiAttributes.h>
23 
24 namespace helib {
25 
27 typedef NTL::Vec<long> Permut;
28 
30 template <typename T>
31 void applyPermToVec(NTL::Vec<T>& out, const NTL::Vec<T>& in, const Permut& p1);
32 template <typename T>
33 void applyPermToVec(std::vector<T>& out,
34  const std::vector<T>& in,
35  const Permut& p1);
36 
38 template <typename T>
39 void applyPermsToVec(NTL::Vec<T>& out,
40  const NTL::Vec<T>& in,
41  const Permut& p2,
42  const Permut& p1);
43 template <typename T>
44 void applyPermsToVec(std::vector<T>& out,
45  const std::vector<T>& in,
46  const Permut& p2,
47  const Permut& p1);
48 
50 void randomPerm(Permut& perm, long n);
51 
90 class ColPerm : public HyperCube<long>
91 {
92 private:
93  long dim;
94  ColPerm(); // disabled
95 
96 public:
97  explicit ColPerm(const CubeSignature& _sig) : HyperCube<long>(_sig)
98  {
99  dim = -1;
100  }
101 
102  long getPermDim() const { return dim; }
103  void setPermDim(long _dim)
104  {
105  assertInRange(_dim,
106  0l,
107  getNumDims(),
108  "Algebra does not have a dimension of index _dim: " +
109  std::to_string(_dim));
110  dim = _dim;
111  }
112 
113  void makeExplicit(Permut& out) const; // Write the permutation explicitly
114 
118  long getShiftAmounts(Permut& out) const;
119 
123  void getBenesShiftAmounts(NTL::Vec<Permut>& out,
124  NTL::Vec<bool>& idID,
125  const NTL::Vec<long>& benesLvls) const;
126 
128  void printout(std::ostream& s)
129  { // a test/debugging method
130  s << "Cube signature: " << getSig() << std::endl;
131  s << " dim=" << dim << std::endl;
132  s << " data=" << getData() << std::endl;
133  }
134 };
135 std::ostream& operator<<(std::ostream& s, const ColPerm& p);
136 
143 void breakPermByDim(std::vector<ColPerm>& out,
144  const Permut& pi,
145  const CubeSignature& sig);
146 
152 {
153 private:
154  long n; // size of perm, n > 1, not necessarily power of 2
155  long k; // recursion depth, k = least integer k s/t 2^k >= n
156 
157  NTL::Vec<NTL::Vec<short>> level;
158  // there are 2*k - 1 levels, each with n nodes.
159  // level[i][j] is 0, 1, or -1,
160  // which designates an edge from node j at level i
161  // to node j + level[i][j]*shamt(i) at level i+1
162 
163  GeneralBenesNetwork(); // default constructor disabled
164 
165 public:
168  static long depth(long n)
169  {
170  long k = 1;
171  while ((1L << k) < n)
172  k++;
173  return k;
174  }
175 
178  static long levelToDepthMap(UNUSED long n, long k, long i)
179  {
180  assertInRange<InvalidArgument>(i,
181  0l,
182  2 * k - 1,
183  "Level number i not in [0, 2 * k - 1)");
184  return (k - 1) - labs((k - 1) - i);
185  }
186 
189  static long shamt(long n, long k, long i)
190  {
191  long d = levelToDepthMap(n, k, i);
192  return ((n >> d) + 1) >> 1;
193  }
194 
195  long getDepth() const { return k; }
196  long getSize() const { return n; }
197  long getNumLevels() const { return 2 * k - 1; }
198 
199  const NTL::Vec<short>& getLevel(long i) const
200  {
201  assertInRange<InvalidArgument>(i,
202  0l,
203  2 * k - 1,
204  "Level number i not in [0, 2 * k - 1)");
205  return level[i];
206  }
207 
208  long levelToDepthMap(long i) const { return levelToDepthMap(n, k, i); }
209  long shamt(long i) const { return shamt(n, k, i); }
210 
211  // constructor
212  GeneralBenesNetwork(const Permut& perm);
213 
214  // test correctness
215 
216  bool testNetwork(const Permut& perm) const;
217 };
218 
224 template <typename T>
225 class FullBinaryTree;
226 
231 template <typename T>
232 class TreeNode
233 {
234  T data;
235  long parent;
236  long leftChild, rightChild;
237  long prev, next; // useful, e.g., to connect all leaves in a list
238 
239  void makeNullIndexes() { parent = leftChild = rightChild = prev = next = -1; }
240 
241 public:
242  TreeNode() { makeNullIndexes(); }
243  explicit TreeNode(const T& d) : data(d) { makeNullIndexes(); }
244 
245  T& getData() { return data; }
246  const T& getData() const { return data; }
247 
248  long getParent() const { return parent; }
249  long getLeftChild() const { return leftChild; }
250  long getRightChild() const { return rightChild; }
251  long getPrev() const { return prev; }
252  long getNext() const { return next; }
253 
254  friend class FullBinaryTree<T>;
255 };
256 
257 // A binary tree, the root is always the node at index 0
258 template <typename T>
260 {
261  long aux; // a "foreign key" for this tree (holds generator index)
262  std::vector<TreeNode<T>> nodes;
263  long nLeaves; // how many leaves in this tree
264  long frstLeaf, lstLeaf; // index of the first/last leaves
265 
266 public:
267  FullBinaryTree(long _aux = 0)
268  {
269  aux = _aux;
270  nLeaves = 0;
271  frstLeaf = lstLeaf = -1;
272  } // empty tree
273 
274  explicit FullBinaryTree(const T& d, long _aux = 0) // tree with only a root
275  {
276  aux = _aux;
277  nLeaves = 1;
278  TreeNode<T> n(d);
279  nodes.push_back(n);
280  frstLeaf = lstLeaf = 0;
281  }
282 
283  void putDataInRoot(const T& d)
284  {
285  if (nodes.size() == 0) { // make new root
286  TreeNode<T> n(d);
287  nodes.push_back(n);
288  frstLeaf = lstLeaf = 0;
289  nLeaves = 1;
290  } else
291  nodes[0].data = d; // Root exists, just update data
292  }
293 
294  // Provide some of the interfaces of the underlying std::vector
295  long size() { return (long)nodes.size(); }
296 
297  TreeNode<T>& operator[](long i) { return nodes[i]; }
298  const TreeNode<T>& operator[](long i) const { return nodes[i]; }
299 
300  TreeNode<T>& at(long i) { return nodes.at(i); }
301  const TreeNode<T>& at(long i) const { return nodes.at(i); }
302 
303  T& DataOfNode(long i) { return nodes.at(i).data; }
304  const T& DataOfNode(long i) const { return nodes.at(i).data; }
305 
306  long getAuxKey() const { return aux; }
307  void setAuxKey(long _aux) { aux = _aux; }
308 
309  long getNleaves() const { return nLeaves; }
310  long firstLeaf() const { return frstLeaf; }
311  long nextLeaf(long i) const { return nodes.at(i).next; }
312  long prevLeaf(long i) const { return nodes.at(i).prev; }
313  long lastLeaf() const { return lstLeaf; }
314 
315  long rootIdx() const { return 0; }
316  long parentIdx(long i) const { return nodes.at(i).parent; }
317  long leftChildIdx(long i) const { return nodes.at(i).leftChild; }
318  long rightChildIdx(long i) const { return nodes.at(i).rightChild; }
319 
320  // FIXME: refactoring2.0: should we change also this?
321  void printout(std::ostream& s, long idx = 0) const
322  {
323  s << "[" << aux << " ";
324  s << nodes[idx].getData();
325  if (leftChildIdx(idx) >= 0)
326  printout(s, leftChildIdx(idx));
327  if (rightChildIdx(idx) >= 0)
328  printout(s, rightChildIdx(idx));
329  s << "] ";
330  }
331  // friend std::istream& operator>> (std::istream &s, DoubleCRT &d);
332 
337  long addChildren(long prntIdx, const T& leftData, const T& rightData);
338 
341  {
342  if (nodes.size() > 1) {
343  nodes.resize(1);
344  frstLeaf = lstLeaf = 0;
345  nLeaves = 1;
346  }
347  }
348 };
349 template <typename T>
351  const T& leftData,
352  const T& rightData)
353 {
354  assertInRange(prntIdx, 0l, (long)nodes.size(), "Parent node does not exist");
355 
356  // If parent is a leaf, add to it two children
357  if (nodes[prntIdx].leftChild == -1 && nodes[prntIdx].rightChild == -1) {
358  long childIdx = nodes.size();
359  TreeNode<T> n1(leftData);
360  nodes.push_back(n1); // add left child to std::vector
361  TreeNode<T> n2(rightData);
362  nodes.push_back(n2); // add right child to std::vector
363 
364  TreeNode<T>& parent = nodes[prntIdx];
365  TreeNode<T>& left = nodes[childIdx];
366  TreeNode<T>& right = nodes[childIdx + 1];
367 
368  parent.leftChild = childIdx; // point to children from parent
369  parent.rightChild = childIdx + 1;
370  left.parent = right.parent = prntIdx; // point to parent from children
371 
372  // remove parent and insert children to the linked list of leaves
373  left.prev = parent.prev;
374  left.next = childIdx + 1;
375  right.prev = childIdx;
376  right.next = parent.next;
377  if (parent.prev >= 0) { // parent was not the 1st leaf
378  nodes[parent.prev].next = childIdx;
379  parent.prev = -1;
380  } else // parent was the first leaf, now its left child is 1st
381  frstLeaf = childIdx;
382 
383  if (parent.next >= 0) { // parent was not the last leaf
384  nodes[parent.next].prev = childIdx + 1;
385  parent.next = -1;
386  } else // parent was the last leaf, now its left child is last
387  lstLeaf = childIdx + 1;
388 
389  nLeaves++; // we replaced a leaf by a parent w/ two leaves
390  } else { // parent is not a leaf, update the two children
391  TreeNode<T>& parent = nodes[prntIdx];
392  assertTrue(parent.leftChild >= 0, "Left child does not exist");
393  assertTrue(parent.rightChild >= 0, "Right child does not exist");
394 
395  TreeNode<T>& left = nodes[parent.leftChild];
396  TreeNode<T>& right = nodes[parent.rightChild];
397  left.data = leftData;
398  right.data = rightData;
399  }
400  return nodes[prntIdx].leftChild;
401 }
402 
405 {
406 public:
407  long genIdx; // the index of the corresponding generator in Zm*/(p)
408  long order; // the order of this generator (or a smaller subcube)
409  bool good; // is this a good generator?
410 
411  GenDescriptor(long _order, bool _good, long gen = 0) :
412  genIdx(gen), order(_order), good(_good)
413  {}
414 
416 };
417 
420 {
421  static const NTL::Vec<long> dummyBenes; // Useful for initialization
422 public:
423  long size; // size of cube slice, same as 'order' in GenDescriptor
424  bool good; // good or bad
425  long e; // shift-by-1 in this sub-dim is done via X -> X^{g^e}
426 
427  // A Benes leaf corresponds to either one or two Benes networks, depending
428  // on whether or not it is in the middle. If this object is in the middle
429  // then scndBenes.length()==0, else scndBenes.length()>=1. Each of the two
430  // Benes network can be "trivial", i.e., collapsed to a single layer, which
431  // is denoted by benes.length()==1.
432  NTL::Vec<long> frstBenes;
433  NTL::Vec<long> scndBenes;
434 
435  explicit SubDimension(long sz = 0,
436  bool gd = false,
437  long ee = 0,
438  const NTL::Vec<long>& bns1 = dummyBenes,
439  const NTL::Vec<long>& bns2 = dummyBenes)
440  {
441  size = sz;
442  good = gd;
443  e = ee;
444  frstBenes = bns1;
445  scndBenes = bns2;
446  }
447 
448  long totalLength() const { return frstBenes.length() + scndBenes.length(); }
449  /* SubDimension& operator=(const SubDimension& other)
450  { genIdx=other.genIdx; size=other.size;
451  e=other.e; good=other.good; benes=other.benes;
452  return *this;
453  }
454  */
455  friend std::ostream& operator<<(std::ostream& s, const SubDimension& tree);
456 };
457 typedef FullBinaryTree<SubDimension> OneGeneratorTree; // tree for one generator
458 
461 {
462  long depth; // How many layers in this permutation network
463  NTL::Vec<OneGeneratorTree> trees;
464  Permut map2cube, map2array;
465 
466 public:
467  GeneratorTrees() { depth = 0; } // default constructor
468 
469  // GeneratorTrees(const Vec<OneGeneratorTree>& _trees): trees(_trees)
470  // {depth=0;}
471  //
472  // Initialize trees with only the roots.
473  // GeneratorTrees(const Vec<SubDimension>& dims);
474 
475  long numLayers() const { return depth; } // depth of permutation network
476  long numTrees() const { return trees.length(); } // how many trees
477  long getSize() const { return map2cube.length(); } // hypercube size
478 
479  OneGeneratorTree& operator[](long i) { return trees[i]; }
480  const OneGeneratorTree& operator[](long i) const { return trees[i]; }
481  OneGeneratorTree& at(long i) { return trees.at(i); }
482  const OneGeneratorTree& at(long i) const { return trees.at(i); }
483 
484  OneGeneratorTree& getGenTree(long i) { return trees.at(i); }
485  const OneGeneratorTree& getGenTree(long i) const { return trees.at(i); }
486 
487  const Permut& mapToCube() const { return map2cube; }
488  const Permut& mapToArray() const { return map2array; }
489  Permut& mapToCube() { return map2cube; }
490  Permut& mapToArray() { return map2array; }
491 
492  long mapToCube(long i) const { return map2cube[i]; }
493  long mapToArray(long i) const { return map2array[i]; }
494 
497  void getCubeDims(NTL::Vec<long>& dims) const;
498 
501  void getCubeSubDims(NTL::Vec<long>& dims) const;
502 
503  // Returns coordinates of i relative to leaves of the tree
504  // void getCoordinates(Vec<long>&, long i) const;
505 
510  long buildOptimalTrees(const NTL::Vec<GenDescriptor>& vec, long depthBound);
511 
528  void ComputeCubeMapping();
529 
530  friend std::ostream& operator<<(std::ostream& s, const GeneratorTrees& t);
531 };
532 
533 // Permutation networks
534 
535 class Ctxt;
536 class EncryptedArray;
537 class Context;
538 class PermNetwork;
539 class PtxtArray;
540 
544 {
545  long genIdx; // shift-by-1 in this layer is done via X -> X^{g^e}
546  long e;
547  NTL::Vec<long> shifts; // shifts[i] is how much to shift slot i
548  bool isID; // a silly optimization, does this layer compute the identity?
549 
550  friend class PermNetwork;
551  friend std::ostream& operator<<(std::ostream& s, const PermNetwork& net);
552 
553 public:
554  long getGenIdx() const { return genIdx; }
555  long getE() const { return e; }
556  const NTL::Vec<long>& getShifts() const { return shifts; }
557  bool isIdentity() const { return isID; }
558 };
559 
562 {
563  NTL::Vec<PermNetLayer> layers;
564 
566  void setLayers4Leaf(long lyrIdx,
567  const ColPerm& p,
568  const NTL::Vec<long>& benesLvls,
569  long gIdx,
570  const SubDimension& leafData,
571  const Permut& map2cube);
572 
573 public:
574  PermNetwork(){}; // empty network
575  PermNetwork(const Permut& pi, const GeneratorTrees& trees)
576  {
577  buildNetwork(pi, trees);
578  }
579 
580  long depth() const { return layers.length(); }
581 
584  void buildNetwork(const Permut& pi, const GeneratorTrees& trees);
585 
587  void applyToCtxt(Ctxt& c, const EncryptedArray& ea) const;
588 
590  void applyToCube(HyperCube<long>& v) const;
591 
593  void applyToPtxt(NTL::ZZX& p, const EncryptedArray& ea) const;
594 
595  const PermNetLayer& getLayer(long i) const { return layers[i]; }
596 
597  friend std::ostream& operator<<(std::ostream& s, const PermNetwork& net);
598 };
599 
600 // some convenience classes that are easier to work with
601 // VJS-FIXME: document these
602 
604 {
605 
606  const EncryptedArray& ea;
607  GeneratorTrees trees;
608  long cost;
609 
610 public:
613 
614  PermIndepPrecomp(const Context& context, long depthBound);
615  PermIndepPrecomp(const EncryptedArray& ea, long depthBound);
616 
617  long getCost() const { return cost; }
618  // cost == NTL_MAX_LONG means contruction failed
619 
620  long getDepth() const { return trees.numLayers(); }
621 
622  friend class PermPrecomp;
623 };
624 
626 {
627 
628  const EncryptedArray& ea;
629  Permut pi;
630  PermNetwork net;
631 
632 public:
633  PermPrecomp(const PermPrecomp&) = delete;
634  PermPrecomp& operator=(const PermPrecomp&) = delete;
635 
636  PermPrecomp(const PermIndepPrecomp& pip, const Permut& _pi);
637 
638  void apply(Ctxt& ctxt) const { net.applyToCtxt(ctxt, ea); }
639 
640  void apply(PtxtArray& a) const;
641 
642  // VJS-FIXME: add support for addMatrices4Network?
643 };
644 
645 /* EXAMPLE USE:
646 
647  // do some permutation-independent pre-computations
648  PermIndepPrecomp pip(context, depthBound);
649 
650  // initialize some arbitrary permutation pi
651  Permut pi; // This is just an NTL::Vec<long>
652  pi.SetLength(nslots);
653  ...
654 
655  // now do a permutation-dependent pre-computation
656  PermPrecomp pp(pip, pi);
657 
658  // apply the permutation
659  pp.apply(ctxt);
660 
661 
662  // if the slots are originally [a_0,a_1,a_2,...]
663  // then they become [a_pi[0],a_pi[1],a_pi[2],...]
664 
665 
666 */
667 
668 } // namespace helib
669 
670 #endif // ifndef HELIB_PERMUTATIONS_H
Permuting a single dimension (column) of a hypercube.
Definition: permutations.h:91
ColPerm(const CubeSignature &_sig)
Definition: permutations.h:97
void setPermDim(long _dim)
Definition: permutations.h:103
long getShiftAmounts(Permut &out) const
Definition: permutations.cpp:126
void makeExplicit(Permut &out) const
Definition: permutations.cpp:111
void getBenesShiftAmounts(NTL::Vec< Permut > &out, NTL::Vec< bool > &idID, const NTL::Vec< long > &benesLvls) const
Definition: permutations.cpp:170
long getPermDim() const
Definition: permutations.h:102
void printout(std::ostream &s)
A test/debugging method.
Definition: permutations.h:128
Maintaining the HE scheme parameters.
Definition: Context.h:100
A Ctxt object holds a single ciphertext.
Definition: Ctxt.h:396
Holds a vector of dimensions for a hypercube and some additional data.
Definition: hypercube.h:28
A simple wrapper for a smart pointer to an EncryptedArrayBase. This is the interface that higher-leve...
Definition: EncryptedArray.h:1583
A simple implementation of full binary trees (each non-leaf has 2 children)
Definition: permutations.h:260
long getAuxKey() const
Definition: permutations.h:306
long prevLeaf(long i) const
Definition: permutations.h:312
TreeNode< T > & at(long i)
Definition: permutations.h:300
long getNleaves() const
Definition: permutations.h:309
long addChildren(long prntIdx, const T &leftData, const T &rightData)
Definition: permutations.h:350
T & DataOfNode(long i)
Definition: permutations.h:303
const TreeNode< T > & at(long i) const
Definition: permutations.h:301
FullBinaryTree(long _aux=0)
Definition: permutations.h:267
FullBinaryTree(const T &d, long _aux=0)
Definition: permutations.h:274
long rootIdx() const
Definition: permutations.h:315
void printout(std::ostream &s, long idx=0) const
Definition: permutations.h:321
long leftChildIdx(long i) const
Definition: permutations.h:317
const T & DataOfNode(long i) const
Definition: permutations.h:304
long nextLeaf(long i) const
Definition: permutations.h:311
long lastLeaf() const
Definition: permutations.h:313
long parentIdx(long i) const
Definition: permutations.h:316
const TreeNode< T > & operator[](long i) const
Definition: permutations.h:298
void setAuxKey(long _aux)
Definition: permutations.h:307
void collapseToRoot()
Remove all nodes in the tree except for the root.
Definition: permutations.h:340
long rightChildIdx(long i) const
Definition: permutations.h:318
TreeNode< T > & operator[](long i)
Definition: permutations.h:297
long size()
Definition: permutations.h:295
long firstLeaf() const
Definition: permutations.h:310
void putDataInRoot(const T &d)
Definition: permutations.h:283
A minimal description of a generator for the purpose of building tree.
Definition: permutations.h:405
bool good
Definition: permutations.h:409
long genIdx
Definition: permutations.h:407
GenDescriptor(long _order, bool _good, long gen=0)
Definition: permutations.h:411
long order
Definition: permutations.h:408
GenDescriptor()
Definition: permutations.h:415
Implementation of generalized Benes Permutation Network.
Definition: permutations.h:152
long getDepth() const
Definition: permutations.h:195
static long shamt(long n, long k, long i)
Definition: permutations.h:189
const NTL::Vec< short > & getLevel(long i) const
Definition: permutations.h:199
long getNumLevels() const
Definition: permutations.h:197
long shamt(long i) const
Definition: permutations.h:209
long levelToDepthMap(long i) const
Definition: permutations.h:208
static long levelToDepthMap(UNUSED long n, long k, long i)
Definition: permutations.h:178
bool testNetwork(const Permut &perm) const
Definition: BenesNetwork.cpp:308
long getSize() const
Definition: permutations.h:196
static long depth(long n)
Definition: permutations.h:168
A std::vector of generator trees, one per generator in Zm*‍/(p)
Definition: permutations.h:461
long numLayers() const
Definition: permutations.h:475
const OneGeneratorTree & getGenTree(long i) const
Definition: permutations.h:485
void ComputeCubeMapping()
Computes permutations mapping between linear array and the cube.
Definition: permutations.cpp:457
long numTrees() const
Definition: permutations.h:476
GeneratorTrees()
Definition: permutations.h:467
long mapToArray(long i) const
Definition: permutations.h:493
const OneGeneratorTree & operator[](long i) const
Definition: permutations.h:480
OneGeneratorTree & getGenTree(long i)
Definition: permutations.h:484
Permut & mapToArray()
Definition: permutations.h:490
const Permut & mapToCube() const
Definition: permutations.h:487
OneGeneratorTree & operator[](long i)
Definition: permutations.h:479
void getCubeDims(NTL::Vec< long > &dims) const
Definition: permutations.cpp:377
long mapToCube(long i) const
Definition: permutations.h:492
Permut & mapToCube()
Definition: permutations.h:489
void getCubeSubDims(NTL::Vec< long > &dims) const
Definition: permutations.cpp:391
long buildOptimalTrees(const NTL::Vec< GenDescriptor > &vec, long depthBound)
Definition: OptimizePermutations.cpp:943
const OneGeneratorTree & at(long i) const
Definition: permutations.h:482
OneGeneratorTree & at(long i)
Definition: permutations.h:481
friend std::ostream & operator<<(std::ostream &s, const GeneratorTrees &t)
Definition: permutations.cpp:519
long getSize() const
Definition: permutations.h:477
const Permut & mapToArray() const
Definition: permutations.h:488
A multi-dimensional cube.
Definition: hypercube.h:221
long getNumDims() const
number of dimensions
Definition: hypercube.h:276
const CubeSignature & getSig() const
const ref to signature
Definition: hypercube.h:262
NTL::Vec< long > & getData()
Definition: hypercube.h:267
Definition: permutations.h:604
long getDepth() const
Definition: permutations.h:620
long getCost() const
Definition: permutations.h:617
PermIndepPrecomp(const PermIndepPrecomp &)=delete
PermIndepPrecomp & operator=(const PermIndepPrecomp &)=delete
The information needed to apply one layer of a permutation network.
Definition: permutations.h:544
friend std::ostream & operator<<(std::ostream &s, const PermNetwork &net)
Definition: PermNetwork.cpp:20
const NTL::Vec< long > & getShifts() const
Definition: permutations.h:556
long getE() const
Definition: permutations.h:555
bool isIdentity() const
Definition: permutations.h:557
long getGenIdx() const
Definition: permutations.h:554
A full permutation network.
Definition: permutations.h:562
void buildNetwork(const Permut &pi, const GeneratorTrees &trees)
Definition: PermNetwork.cpp:77
void applyToPtxt(NTL::ZZX &p, const EncryptedArray &ea) const
Apply network to plaintext polynomial, used mostly for debugging.
friend std::ostream & operator<<(std::ostream &s, const PermNetwork &net)
Definition: PermNetwork.cpp:20
void applyToCube(HyperCube< long > &v) const
Apply network to array, used mostly for debugging.
Definition: PermNetwork.cpp:149
PermNetwork(const Permut &pi, const GeneratorTrees &trees)
Definition: permutations.h:575
const PermNetLayer & getLayer(long i) const
Definition: permutations.h:595
PermNetwork()
Definition: permutations.h:574
long depth() const
Definition: permutations.h:580
void applyToCtxt(Ctxt &c, const EncryptedArray &ea) const
Apply network to permute a ciphertext.
Definition: PermNetwork.cpp:217
Definition: permutations.h:626
PermPrecomp(const PermPrecomp &)=delete
PermPrecomp & operator=(const PermPrecomp &)=delete
void apply(Ctxt &ctxt) const
Definition: permutations.h:638
Definition: EncryptedArray.h:2167
A node in a tree relative to some generator.
Definition: permutations.h:420
long totalLength() const
Definition: permutations.h:448
long e
Definition: permutations.h:425
bool good
Definition: permutations.h:424
SubDimension(long sz=0, bool gd=false, long ee=0, const NTL::Vec< long > &bns1=dummyBenes, const NTL::Vec< long > &bns2=dummyBenes)
Definition: permutations.h:435
friend std::ostream & operator<<(std::ostream &s, const SubDimension &tree)
Definition: permutations.cpp:510
long size
Definition: permutations.h:423
NTL::Vec< long > scndBenes
Definition: permutations.h:433
NTL::Vec< long > frstBenes
Definition: permutations.h:432
A node in a full binary tree.
Definition: permutations.h:233
const T & getData() const
Definition: permutations.h:246
TreeNode()
Definition: permutations.h:242
long getNext() const
Definition: permutations.h:252
TreeNode(const T &d)
Definition: permutations.h:243
long getPrev() const
Definition: permutations.h:251
T & getData()
Definition: permutations.h:245
long getParent() const
Definition: permutations.h:248
long getLeftChild() const
Definition: permutations.h:249
long getRightChild() const
Definition: permutations.h:250
Definition: apiAttributes.h:21
void applyPermsToVec(NTL::Vec< T > &out, const NTL::Vec< T > &in, const Permut &p2, const Permut &p1)
Apply two permutations to a std::vector out[i]=in[p2[p1[i]]] (NOT in-place)
Definition: permutations.cpp:47
void applyPermToVec(NTL::Vec< T > &out, const NTL::Vec< T > &in, const Permut &p1)
Apply a permutation to a std::vector, out[i]=in[p1[i]] (NOT in-place)
Definition: permutations.cpp:24
void breakPermByDim(std::vector< ColPerm > &out, const Permut &pi, const CubeSignature &sig)
Takes a permutation pi over m-dimensional cube C=Z_{n1} x...x Z_{nm} and expresses pi as a product pi...
Definition: permutations.cpp:334
FullBinaryTree< SubDimension > OneGeneratorTree
Definition: permutations.h:457
void assertInRange(const T &elem, const T &min, const T &max, const std::string &message, bool right_inclusive=false)
Definition: assertions.h:183
void randomPerm(Permut &perm, long n)
A random size-n permutation.
Definition: permutations.cpp:94
void assertTrue(const T &value, const std::string &message)
Definition: assertions.h:61
std::ostream & operator<<(std::ostream &os, const ContextBuilder< SCHEME > &cb)
ostream operator for serializing the ContextBuilder object.
NTL::Vec< long > Permut
A simple permutation is just a vector with p[i]=\pi_i.
Definition: permutations.h:27