Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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):
Ejemplo n.º 4
0
def PtxtMod():
    return fhe.Ptxt().PtxtSpace