def __invert__(self): result = Ctxt(self.pubkey_) st = fhe.Stream() st.Create() fhe.Synchronize() fhe.NOT(result.ctxt_, self.ctxt_, st) fhe.Synchronize() return result
def __or__(self, other): result = Ctxt(self.pubkey_) st = fhe.Stream() st.Create() fhe.Synchronize() fhe.OR(result.ctxt_, self.ctxt_, other.ctxt_, st) fhe.Synchronize() return result
def __invert__(self): result = CtxtList(len(self.ctxts_), self.pubkey_) st = [] for i in range(len(self.ctxts_)): st.append(fhe.Stream()) st[i].Create() fhe.Synchronize() for i in range(len(self.ctxts_)): fhe.AND(result.ctxts_[i].ctxt_, self.ctxts_[i].ctxt_, st[i]) fhe.Synchronize() return result
def __xor__(self, other): result = CtxtList(len(self.ctxts_), self.pubkey_) st = [] for i in range(len(self.ctxts_)): st.append(fhe.Stream()) st[i].Create() fhe.Synchronize() for i in range(len(self.ctxts_)): fhe.XOR(result.ctxts_[i].ctxt_, self.ctxts_[i].ctxt_, other.ctxts_[i].ctxt_, st[i]) fhe.Synchronize() return result
def __add__(self, other): k = len(self.ctxts_) st = [] for i in range(3 * k): st.append(fhe.Stream()) st[i].Create() fhe.Synchronize() ksa_p = CtxtList(k, self.pubkey_) ksa_g = CtxtList(k, self.pubkey_) ksa_c = CtxtList(k, self.pubkey_) ksa_s = CtxtList(k, self.pubkey_) for i in range(k): fhe.AND(ksa_g.ctxts_[i].ctxt_, self.ctxts_[i].ctxt_, other.ctxts_[i].ctxt_, st[3 * i]) fhe.XOR(ksa_p.ctxts_[i].ctxt_, self.ctxts_[i].ctxt_, other.ctxts_[i].ctxt_, st[3 * i + 1]) fhe.XOR(ksa_s.ctxts_[i].ctxt_, self.ctxts_[i].ctxt_, other.ctxts_[i].ctxt_, st[3 * i + 2]) fhe.Synchronize() begin = 0 step = 1 while begin + step < k: for i in range(begin + step, k): id = i - begin - step ctxt = ksa_p.ctxts_[i].ctxt_ fhe.AND(ksa_p.ctxts_[i].ctxt_, ctxt, ksa_p.ctxts_[i - step].ctxt_, st[2 * id]) fhe.AND(ksa_c.ctxts_[i].ctxt_, ctxt, ksa_g.ctxts_[i - step].ctxt_, st[2 * id + 1]) fhe.Synchronize() for i in range(begin + step, k): id = i - begin - step fhe.OR(ksa_g.ctxts_[i].ctxt_, ksa_c.ctxts_[i].ctxt_, ksa_g.ctxts_[i].ctxt_, st[id]) fhe.Synchronize() step += 1 begin += 1 for i in range(1, k): fhe.XOR(ksa_s.ctxts_[i].ctxt_, ksa_s.ctxts_[i].ctxt_, ksa_g.ctxts_[i - 1].ctxt_, st[i]) fhe.Synchronize() return ksa_s
st = [] for i in range(kNumTests): ptxts1.append(fhe.Ptxt()) ptxts2.append(fhe.Ptxt()) ptxts1[i].message = random.randint(0, 1) ptxts2[i].message = random.randint(0, 1) ctxts1.append(fhe.Ctxt()) ctxts2.append(fhe.Ctxt()) fhe.Encrypt(ctxts1[i], ptxts1[i], prikey) fhe.Encrypt(ctxts2[i], ptxts2[i], prikey) st.append(fhe.Stream()) st[i].Create() fhe.Synchronize() start_time = timeit.default_timer() for i in range(kNumTests): fhe.NAND(ctxts1[i], ctxts1[i], ctxts2[i], st[i]) fhe.Synchronize() elapsed = timeit.default_timer() - start_time passed = True for i in range(kNumTests): ptxt = fhe.Ptxt() fhe.Decrypt(ptxt, ctxts1[i], prikey) if ptxt.message != (1 - ptxts1[i].message * ptxts2[i].message): passed = False break if passed:
def Synchronize(): if use_gpu: fhe.Synchronize() else: pass