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; }
// 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); }
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; } } }
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); } }
// 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; }
// 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); }
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); }
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; }
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; } }
// 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); }
// 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()); }
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()); }
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; } } }
// 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); }
// 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; recursiveReplicateDim(ea, ctxt_right, d, extent, k, pos, limit, dimProd, recBound, repAux, handler); }
void replicateAllNextDim(const EncryptedArray& ea, const Ctxt& ctxt, long d, long dimProd, long recBound, RepAuxDim& repAux, ReplicateHandler *handler) { assert(d >= 0); // If already fully replicated (or we need to stop early), call the handler if (d >= ea.dimension() || handler->earlyStop(d,/*k=*/-1,dimProd)) { handler->handle(ctxt); return; } long dSize = ea.sizeOfDimension(d); dimProd *= dSize; // product of all dimensions including this one long n = GreatestPowerOfTwo(dSize); // 2^n <= dSize long k = n; // We replicate 2^k-size blocks along this dimension, then call the // recursive procedure to handle the smaller subblocks. Consider for // example a 2D 5x2 cube, so the original slots are // // ( s0 s2 s4 s6 s8 ) // ( s1 s3 s5 s7 s9 ) // // Say that we start with k=2 in the 1st dimension (of size 5), we // will prepare floor(5/2)=2 ciphertexts as follows: // // ( s0 s2 s0 s2 0 ) ( s4 s6 s4 s6 0 ) // ( s1 s3 s1 s3 0 ) ( s5 s7 s5 s7 0 ) // // The call to recursiveReplicateDim (still with k=2) will first copy // s0/s1 and s4/s5 to the zero column at the end, then make a recursive // call with k=1 that will complete the replication along the current // dimension, resulting in the 4 ciphertexts // // (s0 s0 s0 s0 s0) (s2 s2 s2 s2 s2) (s4 s4 s4 s4 s4) (s6 s6 s6 s6 s6) // (s1 s1 s1 s1 s1) (s3 s3 s3 s3 s3) (s5 s5 s5 s5 s5) (s7 s7 s7 s7 s7) // // Then a recursive call for the next dimension will complete the // replication of these entries, and a final step will deal with the // "leftover" positions s8 s9 // The logic below cut the recursion depth by starting from smaller // blocks (by default size approx n rather than 2^n). // The inital block size is controlled by the recBound parameter: // + recBound>0: blocks of size min(~n, 2^recBound). this ensures // recursion depth <= recBound, and typically much smaller (~log n) // + recBound=0: blocks of size 1 (no recursion) // + recBound<0: blocks of size 2^n (full recursion) if (recBound >= 0) { // use heuristic recursion bound k = 0; if (dSize > 2 && dimProd*NumBits(dSize) > ea.size() / 8) { k = NumBits(NumBits(dSize))-1; if (k > n) k = n; if (k > recBound) k = recBound; } } else { // SHAI: I don't understand this else case k = -recBound; if (k > n) k = n; } long blockSize = 1L << k; // blocks of size 2^k long numBlocks = dSize/blockSize; long extent = numBlocks * blockSize; // extent is an integral multiple of the block size, the recursive // call replicates only these slots, and then we have a separate // call for the leftover slots. Ctxt ctxt1 = ctxt; if (extent < dSize) { // select only the slots 0..extent-1 in this dimension if (repAux.tab1(d, 0).null()) { // generate mask if not already there ZZX mask; SelectRangeDim(ea, mask, 0, extent, d); repAux.tab1(d, 0).set_ptr(new DoubleCRT(mask, ea.getContext())); // store mask in 2nd table (tab1) } ctxt1.multByConstant(*repAux.tab1(d, 0)); // mult by mask to zero out slots } if (numBlocks == 1) { // just one block, call the recursive replication recursiveReplicateDim(ea, ctxt1, d, extent, k, 0, extent, dimProd, recBound, repAux, handler); } else { // replicate the slots in each block separately for (long pos = 0; pos < numBlocks; pos++) { Ctxt ctxt2 = ctxt1; // zero-out all the slots outside the current block SelectRangeDim(ea, ctxt2, pos*blockSize, (pos+1)*blockSize, d); // replicate the current block across this dimenssion using a // simple shift-and-add procedure. replicateOneBlock(ea, ctxt2, pos, blockSize, d); // now call the recursive replication to do the rest of the work recursiveReplicateDim(ea, ctxt2, d, extent, k, 0, extent, dimProd, recBound, repAux, handler); } } // If dSize is not an integral number of blocks, then we still need // to deal with the leftover slots. if (extent < dSize) { // zero-out the slots from before, leaving only the leftover slots ctxt1 = ctxt; if (repAux.tab1(d, 1).null()) { // generate mask if not already there ZZX mask; SelectRangeDim(ea, mask, extent, dSize, d); repAux.tab1(d, 1).set_ptr(new DoubleCRT(mask, ea.getContext())); } ctxt1.multByConstant(*repAux.tab1(d,1)); // mult by mask to zero out slots // move relevant slots to the beginning ea.rotate1D(ctxt1, d, -extent, /*don't-care-flag=*/true); // replicate the leftover block across this dimenssion using a // simple shift-and-add procedure. replicateOneBlock(ea, ctxt1, 0, blockSize, d); // now call the recursive replication to do the rest of the work recursiveReplicateDim(ea, ctxt1, d, extent, k, extent, dSize, dimProd, recBound, repAux, handler); } }
static void recursiveReplicate(const EncryptedArray& ea, const Ctxt& ctxt, long n, long k, long pos, long limit, RepAux& repAux, ReplicateHandler *handler) { if (pos >= limit) return; if (replicateVerboseFlag) { // DEBUG code cerr << "check: " << k; CheckCtxt(ctxt, ""); } long nSlots = ea.size(); if (k == 0) { if ( (1L << n) >= nSlots) { handler->handle(ctxt); return; } // need to replicate to fill positions [ (1L << n) .. nSlots ) if (repAux.tab(0).null()) { // need to generate mask ZZX mask; SelectRange(ea, mask, 0, nSlots - (1L << n)); repAux.tab(0).set_ptr(new DoubleCRT(mask, ea.getContext())); } Ctxt ctxt_tmp = ctxt; ctxt_tmp.multByConstant(*repAux.tab(0)); ea.rotate(ctxt_tmp, 1L << n); ctxt_tmp += ctxt; handler->handle(ctxt_tmp); return; } k--; Ctxt ctxt_masked = ctxt; { // artificial scope to miminize storage in // the recursion { // another artificial scope // mask should be at index k+1 if (repAux.tab(k+1).null()) { // need to generate mask vector< long > maskArray; maskArray.resize(nSlots); for (long i = 0; i < (1L << n); i++) maskArray[i] = 1- bit(i, k); // the reverse of bit k of i for (long i = (1L << n); i < nSlots; i++) maskArray[i] = 0; ZZX mask; ea.encode(mask, maskArray); repAux.tab(k+1).set_ptr(new DoubleCRT(mask, ea.getContext())); } ctxt_masked.multByConstant(*repAux.tab(k+1)); } Ctxt ctxt_left = ctxt_masked; ea.rotate(ctxt_left, 1L << k); ctxt_left += ctxt_masked; recursiveReplicate(ea, ctxt_left, n, k, pos, limit, 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.rotate(ctxt_masked, -(1L << k)); ctxt_right += ctxt_masked; recursiveReplicate(ea, ctxt_right, n, k, pos, limit, repAux, handler); }