本文整理汇总了C++中EncryptedArray类的典型用法代码示例。如果您正苦于以下问题:C++ EncryptedArray类的具体用法?C++ EncryptedArray怎么用?C++ EncryptedArray使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了EncryptedArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: replicateAllOrig
void replicateAllOrig(const EncryptedArray& ea, const Ctxt& ctxt,
ReplicateHandler *handler, RepAux* repAuxPtr)
{
long nSlots = ea.size();
long n = GreatestPowerOfTwo(nSlots); // 2^n <= nSlots
Ctxt ctxt1 = ctxt;
if ((1L << n) < nSlots)
SelectRange(ea, ctxt1, 0, 1L << n);
RepAux repAux;
if (repAuxPtr==NULL) repAuxPtr = &repAux;
recursiveReplicate(ea, ctxt1, n, n, 0, 1L << n,
*repAuxPtr, handler);
if ((1L << n) < nSlots) {
ctxt1 = ctxt;
SelectRange(ea, ctxt1, 1L << n, nSlots);
ea.rotate(ctxt1, -(1L << n));
recursiveReplicate(ea, ctxt1, n, n, 1L << n, nSlots, *repAuxPtr, handler);
}
}
示例2: totalSums
void totalSums(const EncryptedArray& ea, Ctxt& ctxt)
{
long n = ea.size();
if (n == 1) return;
Ctxt orig = ctxt;
long k = NumBits(n);
long e = 1;
for (long i = k-2; i >= 0; i--) {
Ctxt tmp1 = ctxt;
ea.rotate(tmp1, e);
ctxt += tmp1; // ctxt = ctxt + (ctxt >>> e)
e = 2*e;
if (bit(n, i)) {
Ctxt tmp2 = orig;
ea.rotate(tmp2, e);
ctxt += tmp2; // ctxt = ctxt + (orig >>> e)
// NOTE: we could have also computed
// ctxt = (ctxt >>> e) + orig, however,
// this would give us greater depth/noise
e += 1;
}
}
}
示例3: mapTo01
NTL_CLIENT
#include "FHE.h"
#include "timing.h"
#include "EncryptedArray.h"
#include <cstdio>
// Map all non-zero slots to 1, leaving zero slots as zero.
// Assumes that r=1, and that all the slot contain elements from GF(p^d).
//
// We compute x^{p^d-1} = x^{(1+p+...+p^{d-1})*(p-1)} by setting y=x^{p-1}
// and then outputting y * y^p * ... * y^{p^{d-1}}, with exponentiation to
// powers of p done via Frobenius.
// FIXME: the computation of the "norm" y * y^p * ... * y^{p^{d-1}}
// can be done using O(log d) automorphisms, rather than O(d).
void mapTo01(const EncryptedArray& ea, Ctxt& ctxt)
{
long p = ctxt.getPtxtSpace();
if (p != ea.getPAlgebra().getP()) // ptxt space is p^r for r>1
throw helib::LogicError("mapTo01 not implemented for r>1");
if (p>2)
ctxt.power(p-1); // set y = x^{p-1}
long d = ea.getDegree();
if (d>1) { // compute the product of the d automorphisms
std::vector<Ctxt> v(d, ctxt);
for (long i=1; i<d; i++)
v[i].frobeniusAutomorph(i);
totalProduct(ctxt, v);
}
}
示例4: incrementalZeroTest
// incrementalZeroTest sets each res[i], for i=0..n-1, to
// a ciphertext in which each slot is 0 or 1 according
// to whether or not bits 0..i of corresponding slot in ctxt
// is zero (1 if not zero, 0 if zero).
// It is assumed that res and each res[i] is already initialized
// by the caller.
// Complexity: O(d + n log d) smart automorphisms
// O(n d)
void incrementalZeroTest(Ctxt* res[], const EncryptedArray& ea,
const Ctxt& ctxt, long n)
{
FHE_TIMER_START;
long nslots = ea.size();
long d = ea.getDegree();
// compute linearized polynomial coefficients
vector< vector<ZZX> > Coeff;
Coeff.resize(n);
for (long i = 0; i < n; i++) {
// coeffients for mask on bits 0..i
// L[j] = X^j for j = 0..i, L[j] = 0 for j = i+1..d-1
vector<ZZX> L;
L.resize(d);
for (long j = 0; j <= i; j++)
SetCoeff(L[j], j);
vector<ZZX> C;
ea.buildLinPolyCoeffs(C, L);
Coeff[i].resize(d);
for (long j = 0; j < d; j++) {
// Coeff[i][j] = to the encoding that has C[j] in all slots
// FIXME: maybe encrtpted array should have this functionality
// built in
vector<ZZX> T;
T.resize(nslots);
for (long s = 0; s < nslots; s++) T[s] = C[j];
ea.encode(Coeff[i][j], T);
}
}
vector<Ctxt> Conj(d, ctxt);
// initialize Cong[j] to ctxt^{2^j}
for (long j = 0; j < d; j++) {
Conj[j].smartAutomorph(1L << j);
}
for (long i = 0; i < n; i++) {
res[i]->clear();
for (long j = 0; j < d; j++) {
Ctxt tmp = Conj[j];
tmp.multByConstant(Coeff[i][j]);
*res[i] += tmp;
}
// *res[i] now has 0..i in each slot
// next, we raise to the power 2^d-1
fastPower(*res[i], d);
}
FHE_TIMER_STOP;
}
示例5: x2iInSlots
// Return in poly a polynomial with X^i encoded in all the slots
static void x2iInSlots(ZZX& poly, long i,
vector<ZZX>& xVec, const EncryptedArray& ea)
{
xVec.resize(ea.size());
ZZX x2i = ZZX(i,1);
for (long j=0; j<(long)xVec.size(); j++) xVec[j] = x2i;
ea.encode(poly, xVec);
}
示例6: replicate
void replicate(const EncryptedArray& ea, Ctxt& ctxt, long pos)
{
long nSlots = ea.size();
assert(pos >= 0 && pos < nSlots);
ZZX mask;
ea.encodeUnitSelector(mask, pos);
ctxt.multByConstant(mask);
replicate0(ea, ctxt, pos);
}
示例7: select
Ctxt select(Ctxt ctxt, int value, EncryptedArray ea, const FHEPubKey& publicKey) {
PlaintextArray mask(ea);
mask.encode(getVote(value, ea.size()));
Ctxt maskCtxt(publicKey);
ea.encrypt(maskCtxt, publicKey, mask);
Ctxt ret(ctxt);
ret.multiplyBy(maskCtxt);
return ret;
}
示例8: runningSums
void runningSums(const EncryptedArray& ea, Ctxt& ctxt)
{
long n = ea.size();
long shamt = 1;
while (shamt < n) {
Ctxt tmp = ctxt;
ea.shift(tmp, shamt);
ctxt += tmp; // ctxt = ctxt + (ctxt >> shamt)
shamt = 2*shamt;
}
}
示例9: tmp
NTL::ZZX Vector<long>::encode(const EncryptedArray &ea) const
{
assert(this->size() <= ea.size());
NTL::ZZX encoded;
if (this->size() < ea.size()) {
auto tmp(*this);
tmp.resize(ea.size());
ea.encode(encoded, tmp);
} else {
ea.encode(encoded, *this);
}
return encoded;
}
示例10: SelectRange
// selects range of slots [lo..hi)
static
void SelectRange(const EncryptedArray& ea, ZZX& mask, long lo, long hi)
{
long nSlots = ea.size();
assert(lo >= 0 && lo <= hi && hi <= nSlots);
vector<long> maskArray;
maskArray.resize(nSlots);
for (long i = 0; i < nSlots; i++) maskArray[i] = 0;
for (long i = lo; i < hi; i++) maskArray[i] = 1;
ea.encode(mask, maskArray);
}
示例11: packedRecrypt
// Use packed bootstrapping, so we can bootstrap all in just one go.
void packedRecrypt(const CtPtrs& cPtrs,
const std::vector<zzX>& unpackConsts,
const EncryptedArray& ea)
{
FHEPubKey& pKey = (FHEPubKey&)cPtrs[0]->getPubKey();
// Allocate temporary ciphertexts for the recryption
int nPacked = divc(cPtrs.size(), ea.getDegree()); // ceil(totoalNum/d)
std::vector<Ctxt> cts(nPacked, Ctxt(pKey));
repack(CtPtrs_vectorCt(cts), cPtrs, ea); // pack ciphertexts
// cout << "@"<< lsize(cts)<<std::flush;
for (Ctxt& c: cts) { // then recrypt them
c.reducePtxtSpace(2); // we only have recryption data for binary ctxt
#ifdef DEBUG_PRINTOUT
ZZX ptxt;
decryptAndPrint((cout<<" before recryption "), c, *dbgKey, *dbgEa);
dbgKey->Decrypt(ptxt, c);
c.DummyEncrypt(ptxt);
decryptAndPrint((cout<<" after recryption "), c, *dbgKey, *dbgEa);
#else
pKey.reCrypt(c);
#endif
}
unpack(cPtrs, CtPtrs_vectorCt(cts), ea, unpackConsts);
}
示例12: replicateAll
// Returns the result as a vector of ciphertexts
void replicateAll(std::vector<Ctxt>& v, const EncryptedArray& ea,
const Ctxt& ctxt, long recBound, RepAuxDim* repAuxPtr)
{
v.resize(ea.size(), ctxt);
ExplicitReplicator handler(v);
replicateAll(ea, ctxt, &handler, recBound, repAuxPtr);
}
示例13: applyLinPoly1
// Apply the same linear transformation to all the slots.
// C is the output of ea.buildLinPolyCoeffs
void applyLinPoly1(const EncryptedArray& ea, Ctxt& ctxt, const vector<ZZX>& C)
{
assert(&ea.getContext() == &ctxt.getContext());
long d = ea.getDegree();
assert(d == lsize(C));
long nslots = ea.size();
vector<ZZX> encodedC(d);
for (long j = 0; j < d; j++) {
vector<ZZX> v(nslots);
for (long i = 0; i < nslots; i++) v[i] = C[j];
ea.encode(encodedC[j], v);
}
applyLinPolyLL(ctxt, encodedC, ea.getDegree());
}
示例14: benchmark
void benchmark(const EncryptedArray & ea,
const FHEPubKey & pk,
const FHESecKey & sk,
const MDL::Matrix<long>& data)
{
const long BATCH_SIZE = 5000;
MDL::Timer encTimer, evalTimer;
MDL::EncVector mu(pk), sigma(pk);
for (long part = 0; part *BATCH_SIZE < data.rows(); part++) {
long from = std::min<long>(part * BATCH_SIZE, data.rows());
long to = std::min<long>(from + BATCH_SIZE, data.rows());
encTimer.start();
auto ctxts = encrypt(data, pk, ea, from, to);
encTimer.end();
evalTimer.start();
auto sum = summation(ctxts);
mu += sum.first;
sigma += sum.second;
evalTimer.end();
}
evalTimer.start();
auto mu_mu = mu.covariance(ea, data.cols());
NTL::ZZX N;
std::vector<long> n(ea.size(), data.rows());
ea.encode(N, n);
sigma.multByConstant(N);
for (size_t col = 0; col < data.cols(); col++) {
ea.rotate(mu_mu[col], col * data.cols());
sigma -= mu_mu[col];
}
evalTimer.end();
MDL::Vector<long> mat;
sigma.unpack(mat, sk, ea, true);
for (int i = 0; i < data.cols(); i++) {
for (int j = 0; j < data.cols(); j++) {
std::cout << mat[i * data.cols() + j] << " ";
}
std::cout << std::endl;
}
printf("Covariance of %zd data, enc %f, eval %f\n", data.rows(),
encTimer.second(), evalTimer.second());
}
示例15: replicate0
void replicate0(const EncryptedArray& ea, Ctxt& ctxt, long pos)
{
long dim = ea.dimension();
for (long d = 0; d < dim; d++) {
if (!ea.nativeDimension(d)) {
long shamt = -ea.coordinate(d, pos);
ea.rotate1D(ctxt, d, shamt, true); // "don't care"
}
Ctxt ctxt_orig = ctxt;
long sz = ea.sizeOfDimension(d);
long k = NumBits(sz);
long e = 1;
// now process bits k-2 down to 0
for (long j = k-2; j >= 0; j--) {
// e -> 2*e
Ctxt tmp = ctxt;
ea.rotate1D(tmp, d, e, true); // "don't care"
ctxt += tmp;
e = 2*e;
long b = bit(sz, j); // bit j of sz
// e -> e+b
if (b) {
ea.rotate1D(ctxt, d, 1, true); // "don't care"
ctxt += ctxt_orig;
e++;
}
}
}
}