本文整理汇总了C++中EncryptedArray::size方法的典型用法代码示例。如果您正苦于以下问题:C++ EncryptedArray::size方法的具体用法?C++ EncryptedArray::size怎么用?C++ EncryptedArray::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EncryptedArray
的用法示例。
在下文中一共展示了EncryptedArray::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: 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);
}
示例3: 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;
}
}
}
示例4: 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);
}
}
示例5: 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;
}
示例6: ZZX
// 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);
}
示例7: 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);
}
示例8: 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;
}
示例9: 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;
}
}
示例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: 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());
}
示例12: 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());
}
示例13: decryptAndPrint
void decryptAndPrint(ostream& s, const Ctxt& ctxt, const FHESecKey& sk,
const EncryptedArray& ea, long flags)
{
const FHEcontext& context = ctxt.getContext();
xdouble noiseEst = sqrt(ctxt.getNoiseVar());
xdouble modulus = xexp(context.logOfProduct(ctxt.getPrimeSet()));
vector<ZZX> ptxt;
ZZX p, pp;
sk.Decrypt(p, ctxt, pp);
s << "plaintext space mod "<<ctxt.getPtxtSpace()
<< ", level="<<ctxt.findBaseLevel()
<< ", \n |noise|=q*" << (coeffsL2Norm(pp)/modulus)
<< ", |noiseEst|=q*" << (noiseEst/modulus)
<<endl;
if (flags & FLAG_PRINT_ZZX) {
s << " before mod-p reduction=";
printZZX(s,pp) <<endl;
}
if (flags & FLAG_PRINT_POLY) {
s << " after mod-p reduction=";
printZZX(s,p) <<endl;
}
if (flags & FLAG_PRINT_VEC) {
ea.decode(ptxt, p);
if (ea.getAlMod().getTag() == PA_zz_p_tag
&& ctxt.getPtxtSpace() != ea.getAlMod().getPPowR()) {
long g = GCD(ctxt.getPtxtSpace(), ea.getAlMod().getPPowR());
for (long i=0; i<ea.size(); i++)
PolyRed(ptxt[i], g, true);
}
s << " decoded to ";
if (deg(p) < 40) // just pring the whole thing
s << ptxt << endl;
else if (ptxt.size()==1) // a single slot
printZZX(s, ptxt[0]) <<endl;
else { // print first and last slots
printZZX(s, ptxt[0],20) << "--";
printZZX(s, ptxt[ptxt.size()-1], 20) <<endl;
}
}
}
示例14: SelectRangeDim
// selects range of slots [lo..hi) in dimension d
static
void SelectRangeDim(const EncryptedArray& ea, ZZX& mask, long lo, long hi,
long d)
{
long nSlots = ea.size();
assert(d >= 0 && d < ea.dimension());
assert(lo >= 0 && lo <= hi && hi <= ea.sizeOfDimension(d));
vector<long> maskArray;
maskArray.resize(nSlots);
for (long i = 0; i < nSlots; i++) {
long c = ea.coordinate(d, i);
if (c >= lo && c < hi)
maskArray[i] = 1;
else
maskArray[i] = 0;
}
ea.encode(mask, maskArray);
}
示例15: recursiveReplicateDim
// recursiveReplicateDim:
// d = dimension
// ea.sizeOfDimension(d)/2 <= extent <= ea.sizeOfDimension(d),
// only positions [0..extent) are non-zero
// 1 <= 2^k <= extent: size of current interval
// 0 <= pos < ea.sizeOfDimension(d): relative position of first vector
// 0 <= limit < ea.sizeOfDimension(): max # of positions to process
// dimProd: product of dimensions 0..d
// recBound: recursion bound (controls noise)
//
// SHAI: limit and extent are always the same, it seems
static
void recursiveReplicateDim(const EncryptedArray& ea, const Ctxt& ctxt,
long d, long extent, long k, long pos, long limit,
long dimProd, long recBound,
RepAuxDim& repAux,
ReplicateHandler *handler)
{
if (pos >= limit) return;
if (replicateVerboseFlag) { // DEBUG code
cerr << "check: " << k; CheckCtxt(ctxt, "");
}
long dSize = ea.sizeOfDimension(d);
long nSlots = ea.size();
if (k == 0) { // last level in this dimension: blocks of size 2^k=1
if ( extent >= dSize) { // nothing to do in this dimension
replicateAllNextDim(ea, ctxt, d+1, dimProd, recBound, repAux, handler);
return;
} // SHAI: Will we ever have extent > dSize??
// need to replicate to fill positions [ (1L << n) .. dSize-1 ]
if (repAux.tab(d,0).null()) { // generate mask if not there already
ZZX mask;
SelectRangeDim(ea, mask, 0, dSize - extent, d);
repAux.tab(d, 0).set_ptr(new DoubleCRT(mask, ea.getContext()));
}
Ctxt ctxt_tmp = ctxt;
ctxt_tmp.multByConstant(*repAux.tab(d, 0));
ea.rotate1D(ctxt_tmp, d, extent, /*don't-care-flag=*/true);
ctxt_tmp += ctxt;
replicateAllNextDim(ea, ctxt_tmp, d+1, dimProd, recBound, repAux, handler);
return;
}
// If we need to stop early, call the handler
if (handler->earlyStop(d, k, dimProd)) {
handler->handle(ctxt);
return;
}
k--;
Ctxt ctxt_masked = ctxt;
{ // artificial scope to miminize storage in the recursion
{ // another artificial scope (SHAI: this seems redundant)
// generate mask at index k+1, if not there yet
if (repAux.tab(d, k+1).null()) { // need to generate
vector< long > maskArray(nSlots,0);
for (long i = 0; i < nSlots; i++) {
long c = ea.coordinate(d, i);
if (c < extent && bit(c, k) == 0)
maskArray[i] = 1;
}
// store this mask in the repAux table
ZZX mask;
ea.encode(mask, maskArray);
repAux.tab(d, k+1).set_ptr(new DoubleCRT(mask, ea.getContext()));
}
// Apply mask to zero out slots in ctxt
ctxt_masked.multByConstant(*repAux.tab(d, k+1));
}
Ctxt ctxt_left = ctxt_masked;
ea.rotate1D(ctxt_left, d, 1L << k, /*don't-care-flag=*/true);
ctxt_left += ctxt_masked;
recursiveReplicateDim(ea, ctxt_left, d, extent, k, pos, limit,
dimProd, recBound, repAux, handler);
}
pos += (1L << k);
if (pos >= limit)
return;
Ctxt ctxt_right = ctxt;
ctxt_right -= ctxt_masked;
ctxt_masked = ctxt_right; // reuse ctxt_masked as a temp
ea.rotate1D(ctxt_masked, d, -(1L << k), /*don't-care-flag=*/true);
ctxt_right += ctxt_masked;
//.........这里部分代码省略.........