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++; } } } }
// 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); }