def Decrypt(ctxt, prikey): ptxt = fhe.Ptxt() if isinstance(ctxt, Ctxt): fhe.Decrypt(ptxt, ctxt.ctxt_, prikey) return ptxt.message if isinstance(ctxt, CtxtList): ptxt_list = "" for c in reversed(ctxt.ctxts_): fhe.Decrypt(ptxt, c.ctxt_, prikey) ptxt_list += str(ptxt.message) return int(ptxt_list, 2)
def Encrypt(ptxt, prikey, count=1, pubkey=None): if pubkey is None: pubkey = PubKeyGen(prikey) if isinstance(ptxt, (int, long)): msg = ptxt ptxt = fhe.Ptxt() if count == 1: ptxt.message = msg ctxt = Ctxt(pubkey) fhe.Encrypt(ctxt.ctxt_, ptxt, prikey) return ctxt msg_bin = bin(msg)[2:].zfill(count) msg_list = [] ct = CtxtList(count, pubkey) for i in range(count): ptxt.message = int(msg_bin[i], 2) fhe.Encrypt(ct.ctxts_[count - i - 1].ctxt_, ptxt, prikey) return ct
kNumTests = 3000 # Keys pubkey = fhe.PubKey() prikey = fhe.PriKey() fhe.KeyGen(pubkey, prikey) fhe.Initialize(pubkey) # Plaintexts & Ciphertexts & Cuda Stream ptxts1 = [] ptxts2 = [] ctxts1 = [] ctxts2 = [] 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):
def PtxtMod(): return fhe.Ptxt().PtxtSpace