16 #ifndef HELIB_PERMUTATIONS_H
17 #define HELIB_PERMUTATIONS_H
19 #include <helib/PAlgebra.h>
20 #include <helib/matching.h>
21 #include <helib/hypercube.h>
22 #include <helib/apiAttributes.h>
34 const std::vector<T>& in,
40 const NTL::Vec<T>& in,
45 const std::vector<T>& in,
108 "Algebra does not have a dimension of index _dim: " +
109 std::to_string(_dim));
124 NTL::Vec<bool>& idID,
125 const NTL::Vec<long>& benesLvls)
const;
130 s <<
"Cube signature: " <<
getSig() << std::endl;
131 s <<
" dim=" << dim << std::endl;
132 s <<
" data=" <<
getData() << std::endl;
135 std::ostream&
operator<<(std::ostream& s,
const ColPerm& p);
145 const CubeSignature& sig);
157 NTL::Vec<NTL::Vec<short>> level;
171 while ((1L << k) < n)
180 assertInRange<InvalidArgument>(i,
183 "Level number i not in [0, 2 * k - 1)");
184 return (k - 1) - labs((k - 1) - i);
189 static long shamt(
long n,
long k,
long i)
192 return ((n >> d) + 1) >> 1;
201 assertInRange<InvalidArgument>(i,
204 "Level number i not in [0, 2 * k - 1)");
224 template <
typename T>
225 class FullBinaryTree;
231 template <
typename T>
236 long leftChild, rightChild;
239 void makeNullIndexes() { parent = leftChild = rightChild = prev = next = -1; }
243 explicit TreeNode(
const T& d) : data(d) { makeNullIndexes(); }
258 template <
typename T>
262 std::vector<TreeNode<T>> nodes;
264 long frstLeaf, lstLeaf;
271 frstLeaf = lstLeaf = -1;
280 frstLeaf = lstLeaf = 0;
285 if (nodes.size() == 0) {
288 frstLeaf = lstLeaf = 0;
295 long size() {
return (
long)nodes.size(); }
304 const T&
DataOfNode(
long i)
const {
return nodes.at(i).data; }
311 long nextLeaf(
long i)
const {
return nodes.at(i).next; }
312 long prevLeaf(
long i)
const {
return nodes.at(i).prev; }
316 long parentIdx(
long i)
const {
return nodes.at(i).parent; }
323 s <<
"[" << aux <<
" ";
324 s << nodes[idx].getData();
337 long addChildren(
long prntIdx,
const T& leftData,
const T& rightData);
342 if (nodes.size() > 1) {
344 frstLeaf = lstLeaf = 0;
349 template <
typename T>
354 assertInRange(prntIdx, 0l, (
long)nodes.size(),
"Parent node does not exist");
357 if (nodes[prntIdx].leftChild == -1 && nodes[prntIdx].rightChild == -1) {
358 long childIdx = nodes.size();
368 parent.leftChild = childIdx;
369 parent.rightChild = childIdx + 1;
370 left.parent = right.parent = prntIdx;
373 left.prev = parent.prev;
374 left.next = childIdx + 1;
375 right.prev = childIdx;
376 right.next = parent.next;
377 if (parent.prev >= 0) {
378 nodes[parent.prev].next = childIdx;
383 if (parent.next >= 0) {
384 nodes[parent.next].prev = childIdx + 1;
387 lstLeaf = childIdx + 1;
392 assertTrue(parent.leftChild >= 0,
"Left child does not exist");
393 assertTrue(parent.rightChild >= 0,
"Right child does not exist");
397 left.data = leftData;
398 right.data = rightData;
400 return nodes[prntIdx].leftChild;
421 static const NTL::Vec<long> dummyBenes;
438 const NTL::Vec<long>& bns1 = dummyBenes,
439 const NTL::Vec<long>& bns2 = dummyBenes)
463 NTL::Vec<OneGeneratorTree> trees;
464 Permut map2cube, map2array;
477 long getSize()
const {
return map2cube.length(); }
536 class EncryptedArray;
547 NTL::Vec<long> shifts;
555 long getE()
const {
return e; }
556 const NTL::Vec<long>&
getShifts()
const {
return shifts; }
563 NTL::Vec<PermNetLayer> layers;
566 void setLayers4Leaf(
long lyrIdx,
568 const NTL::Vec<long>& benesLvls,
580 long depth()
const {
return layers.length(); }
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