In [21]:
import random
from syft.frameworks.torch.he.fv.modulus import CoeffModulus
from syft.frameworks.torch.he.fv.encryption_params import EncryptionParams
from syft.frameworks.torch.he.fv.context import Context
from syft.frameworks.torch.he.fv.integer_encoder import IntegerEncoder
from syft.frameworks.torch.he.fv.key_generator import KeyGenerator
from syft.frameworks.torch.he.fv.encryptor import Encryptor
from syft.frameworks.torch.he.fv.decryptor import Decryptor
from syft.frameworks.torch.he.fv.integer_encoder import IntegerEncoder
from syft.frameworks.torch.he.fv.modulus import SeqLevelType
from syft.frameworks.torch.he.fv.evaluator import Evaluator
In [22]:
poly_modulus = 64
bit_sizes= [40]
plain_modulus = 64
ctx = Context(EncryptionParams(poly_modulus, CoeffModulus().create(poly_modulus, bit_sizes), plain_modulus))
keygenerator = KeyGenerator(ctx)
sk, pk = keygenerator.keygen()
In [23]:
print(ctx.param.coeff_modulus)
In [24]:
# print(len(sk.data))
print('secret key values : ', sk.data)
In [25]:
# print(pk.data)
# print('public key values : ', pk.data)
In [26]:
int_encoder = IntegerEncoder(ctx)
ri1 = random.randint(0,10)
ri2 = random.randint(0,10)
ri3 = random.randint(0,10)
pt1 = int_encoder.encode(ri1)
pt2 = int_encoder.encode(ri2)
pt3 = int_encoder.encode(ri3)
print(pt1.data," ", pt2.data, " ", pt3.data)
# print('plaintext data',plaintext.data)
In [27]:
print(int_encoder.decode(pt1))
print(int_encoder.decode(pt2))
print(int_encoder.decode(pt3))
In [28]:
encrypter = Encryptor(ctx, pk)
In [29]:
ct1 = encrypter.encrypt(pt1)
ct2 = encrypter.encrypt(pt2)
ct3 = encrypter.encrypt(pt3)
Encrypt Plaintext to ciphertext using secret_key
In [30]:
decrypter = Decryptor(ctx, sk)
In [31]:
dec1 = decrypter.decrypt(ct1)
dec2 = decrypter.decrypt(ct2)
dec3 = decrypter.decrypt(ct3)
In [32]:
print(int_encoder.decode(dec1), " ", int_encoder.decode(dec2), " ", int_encoder.decode(dec3))
In [33]:
eval = Evaluator(ctx)
In [34]:
cc12 = eval.add(ct1, ct2)
cc12 = decrypter.decrypt(cc12)
print(int_encoder.decode(cc12))
In [35]:
pc12 = eval.add(pt1, ct2)
pc12 = decrypter.decrypt(pc12)
print(int_encoder.decode(pc12))
In [36]:
pp12 = eval.add(pt1, pt2)
print(int_encoder.decode(pp12))
In [37]:
assert int_encoder.decode(cc12) == int_encoder.decode(pc12) == int_encoder.decode(pp12) == ri1+ri2
In [38]:
result = eval._mul_cipher_cipher(ct1, ct2)
print("\n\nct1 :",ct1.data)
print("\n\nct2 :",ct2.data)
print('\n\n')
result = decrypter.decrypt(result)
result = int_encoder.decode(result)
print('final result: ', result)
In [39]:
print(ri1 * ri2, " ", result)
assert ri1 * ri2 == result
In [ ]:
In [ ]: