replicate.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_REPLICATE_H
13 #define HELIB_REPLICATE_H
34 #include <helib/EncryptedArray.h>
35 #include <helib/Ptxt.h>
36 
37 namespace helib {
38 
39 // set to true to see some more info...
40 NTL_THREAD_LOCAL
41 extern bool replicateVerboseFlag;
42 
43 class RepAux; // forward declarations
44 class RepAuxDim;
45 
48 void replicate(const EncryptedArray& ea, Ctxt& ctx, long pos);
49 
52 void replicate0(const EncryptedArray& ea, Ctxt& ctxt, long pos);
53 
56 {
57 public:
58  virtual void handle(const Ctxt& ctxt) = 0;
59  virtual ~ReplicateHandler() {}
60 
61  // The earlyStop call can be used to quit the replication mid-way, leaving
62  // a ciphertext with (e.g.) two different entries, each replicated n/2 times
63  // FIXME: Why does this function have arguments? Maybe remove them and then
64  // remove the pragma.
65 #pragma GCC diagnostic push
66 #pragma GCC diagnostic ignored "-Wunused-parameter"
67  virtual bool earlyStop(long d, long k, long prodDim) { return false; }
68 #pragma GCC diagnostic pop
69 };
70 // Applications will derive from this class a handler that actually
71 // does something with the replicated ciphertexts. But it can be used
72 // by itself as a do-nothing replicator for debugging, or to calculate
73 // the required automorphisms (see automorphVals in numbTh.h)
74 
90 void replicateAll(const EncryptedArray& ea,
91  const Ctxt& ctxt,
92  ReplicateHandler* handler,
93  long recBound = 64,
94  RepAuxDim* repAuxPtr = nullptr);
95 
98 void replicateAll(std::vector<Ctxt>& v,
99  const EncryptedArray& ea,
100  const Ctxt& ctxt,
101  long recBound = 64,
102  RepAuxDim* repAuxPtr = nullptr);
103 
114 template <typename Scheme>
115 void replicateAll(std::vector<Ptxt<Scheme>>& v,
116  const EncryptedArray&,
117  const Ptxt<Scheme>& ptxt)
118 {
119  v = ptxt.replicateAll();
120 }
121 
125 void replicateAllOrig(const EncryptedArray& ea,
126  const Ctxt& ctxt,
127  ReplicateHandler* handler,
128  RepAux* repAuxPtr = nullptr);
129 
130 // FIXME: Change to new PtxtArray API
131 void replicate(const EncryptedArray& ea, PlaintextArray& pa, long i);
132 
140 template <typename Scheme>
141 void replicate(const EncryptedArray&, Ptxt<Scheme>& ptxt, long i)
142 {
143  ptxt.replicate(i);
144 }
145 
146 // Structures to keep tables of masking constants that are used in
147 // replication. A calling application can either supply this structure
148 // itself (if it is going to replicate the same tables in multiple
149 // replicate operations), or is cal let the replication code use its
150 // own tables (in which case they are destroyed at the end of the
151 // replication process)
152 
154 class RepAux
155 { // one table for the whole thing
156 private:
157  std::vector<CopiedPtr<FatEncodedPtxt>> _tab;
158 
159 public:
160  CopiedPtr<FatEncodedPtxt>& tab(long i)
161  {
162  if (i >= lsize(_tab))
163  _tab.resize(i + 1);
164  return _tab[i];
165  }
166 };
167 
168 class RepAuxDim
169 { // two tables per dimension
170 private:
171  std::vector<std::vector<CopiedPtr<FatEncodedPtxt>>> _tab, _tab1;
172 
173 public:
174  CopiedPtr<FatEncodedPtxt>& tab(long d, long i)
175  {
176  if (d >= lsize(_tab))
177  _tab.resize(d + 1);
178  if (i >= lsize(_tab[d]))
179  _tab[d].resize(i + 1);
180  return _tab[d][i];
181  }
182 
183  CopiedPtr<FatEncodedPtxt>& tab1(long d, long i)
184  {
185  if (d >= lsize(_tab1))
186  _tab1.resize(d + 1);
187  if (i >= lsize(_tab1[d]))
188  _tab1[d].resize(i + 1);
189  return _tab1[d][i];
190  }
191 };
193 
194 } // namespace helib
195 
196 #endif // ifndef HELIB_REPLICATE_H
A Ctxt object holds a single ciphertext.
Definition: Ctxt.h:396
A simple wrapper for a smart pointer to an EncryptedArrayBase. This is the interface that higher-leve...
Definition: EncryptedArray.h:1583
An object that mimics the functionality of the Ctxt object, and acts as a convenient entry point for ...
Definition: Ptxt.h:188
Ptxt< Scheme > & replicate(long pos)
Replicate single slot across all slots.
Definition: Ptxt.cpp:719
std::vector< Ptxt< Scheme > > replicateAll() const
Generate a vector of plaintexts with each slot replicated in each plaintext.
Definition: Ptxt.cpp:729
An abstract class to handle call-backs to get the output of replicate.
Definition: replicate.h:56
virtual ~ReplicateHandler()
Definition: replicate.h:59
virtual bool earlyStop(long d, long k, long prodDim)
Definition: replicate.h:67
virtual void handle(const Ctxt &ctxt)=0
Definition: apiAttributes.h:21
void replicate(const EncryptedArray &ea, Ctxt &ctx, long pos)
The value in slot #pos is replicated in all other slots. On an n-slot ciphertext, this algorithm perf...
Definition: replicate.cpp:26
NTL_THREAD_LOCAL bool replicateVerboseFlag
Definition: replicate.cpp:20
void replicateAll(const EncryptedArray &ea, const Ctxt &ctxt, ReplicateHandler *handler, long recBound=64, RepAuxDim *repAuxPtr=nullptr)
Definition: replicate.cpp:666
void replicate0(const EncryptedArray &ea, Ctxt &ctxt, long pos)
A lower-level routine. Same as replicate, but assumes all slots are zero except slot #pos.
Definition: replicate.cpp:43
void replicateAllOrig(const EncryptedArray &ea, const Ctxt &ctxt, ReplicateHandler *handler, RepAux *repAuxPtr=nullptr)
Definition: replicate.cpp:225
long lsize(const std::vector< T > &v)
Size of STL vector as a long (rather than unsigned long)
Definition: NumbTh.h:701