Exemplo n.º 1
0
# loading the two ciphertexts. There is clearly potential for improvement here
c2a = PyCtxt(pyfhel=HE_Cl, fileName=sec_con / "ca.ctxt", encoding=float)
c2b = PyCtxt(pyfhel=HE_Cl, fileName=sec_con / "cb.ctxt", encoding=float)

# Attempting to decrypt results raises an error (missing secret key)
#> ---------------------------------------------------------------------------
#> RuntimeError                              Traceback (most recent call last)
#> Pyfhel/Pyfhel.pyx in Pyfhel.Pyfhel.Pyfhel.decryptFrac()
#> RuntimeError: Missing a Private Key [...]
try:
    print(HE_Cl.decrypt(c2a))
    raise Exception("This should not be reached!")
except RuntimeError:
    print("The cloud tried to decrypt, but couldn't!")

# The cloud operates with the ciphertexts:
c_mean = (c2a + c2b) / 2

# And sends the result back
c_mean.to_file(sec_con / "c_mean.ctxt")

##### CLIENT
# Load and decrypt Result
c_res = PyCtxt(pyfhel=HE, fileName=sec_con / "c_mean.ctxt", encoding=float)
print(c_res.decrypt())

# Cleaning up secure channel
secure_channel.cleanup()

# sphinx_gallery_thumbnail_path = 'static/thumbnails/clientServer.png'
Exemplo n.º 2
0
##### SERVER
HE_server = Pyfhel()    
HE_server.load_context("mycontext.con")
HE_server.load_public_key("mypk.pk") # no secret key
# Load ciphertexts
ca = PyCtxt(pyfhel=HE_server, fileName="ctxt_a.ctxt")
cb = PyCtxt(pyfhel=HE_server, fileName="ctxt_b.ctxt")
# Compute homomorphically and send result
cr = (ca + cb) * 2
cr.save("cr.ctxt")

##### CLIENT 
# Load and decrypt result
c_res = PyCtxt(pyfhel=HE, fileName="cr.ctxt")
print("-----------------------------------------------------")
print("Client_server result:", c_res.decrypt())
for f in ["mypk.pk", "mycontext.con", "ctxt_a.ctxt", "ctxt_b.ctxt", "cr.ctxt"]:
    os.remove(f)



# %% --------------------------------------------------
# 5. Exploring CKKS pitfalls (Sec. 5.1)
# -----------------------------------------------------
print("-----------------------------------------------------")
from Pyfhel import PyCtxt, Pyfhel, PyPtxt
HE = Pyfhel()
HE.contextGen(scheme='CKKS', n=16384, qi=[30,30,30,30,30], scale=1)
HE.keyGen()
ctxt_x = HE.encrypt(3.1, scale=2 ** 30) # implicit encode
ctxt_y = HE.encrypt(4.1, scale=2 ** 30)