Repository navigation
Expand file tree
/
Copy pathboolean-serial-json-pke.py
More file actions
119 lines (85 loc) · 3.59 KB
/
Copy pathboolean-serial-json-pke.py
File metadata and controls
119 lines (85 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#
# Example for FHEW with JSON serialization and public key encryption
#
from openfhe import *
import os
import tempfile
# path where files will be written to
datafolder = "demoData"
def main_action():
# Generating the crypto context
cc1 = BinFHEContext()
cc1.GenerateBinFHEContext(TOY)
print("Generating keys.")
# Generating the secret key
sk1 = cc1.KeyGen()
# Generate the bootstrapping keys and public key
cc1.BTKeyGen(sk1, PUB_ENCRYPT)
print("Done generating all keys.")
pk1 = cc1.GetPublicKey()
# Encryption for a ciphertext that will be serialized
ct1 = cc1.Encrypt(pk1, 1)
# CODE FOR SERIALIZATION
# Serializing key-independent crypto context
if not SerializeToFile(datafolder + "/cryptoContext.txt", cc1, JSON):
raise Exception("Error serializing the cryptocontext")
print("The cryptocontext has been serialized.")
# Serializing refreshing and key switching keys (needed for bootstrapping)
if not SerializeToFile(datafolder + "/btKey.txt", cc1.GetBTKey(), JSON):
raise Exception("Error serializing the bootstrapping keys")
print("The bootstrapping keys have been serialized.")
# Serializing secret key
if not SerializeToFile(datafolder + "/sk1.txt", sk1, JSON):
raise Exception("Error serializing sk1")
print("The secret key sk1 key been serialized.")
# Serializing public key
if not SerializeToFile(datafolder + "/pk1.txt", pk1, JSON):
raise Exception("Error serializing pk1")
print("The public key pk1 key been serialized.")
# Serializing a ciphertext
if not SerializeToFile(datafolder + "/ct1.txt", ct1, JSON):
raise Exception("Error serializing ct1")
print("A ciphertext has been serialized.")
# CODE FOR DESERIALIZATION
# Deserializing the cryptocontext
cc, res = DeserializeBinFHECryptoContext(datafolder + "/cryptoContext.txt", JSON)
if not res:
raise Exception("Could not deserialize the cryptocontext")
print("The cryptocontext has been deserialized.")
# deserializing the refreshing and switching keys (for bootstrapping)
bt_key, res = DeserializeBinFHEBTKey(datafolder + "/btKey.txt", JSON)
if not res:
raise Exception("Could not deserialize the bootstrapping keys")
print("The bootstrapping keys have been deserialized.")
# Loading the keys in the cryptocontext
cc.BTKeyLoad(bt_key)
# Deserializing the secret key
sk, res = DeserializeLWEPrivateKey(datafolder + "/sk1.txt", JSON)
if not res:
raise Exception("Could not deserialize the secret key")
print("The secret key has been deserialized.")
# Deserializing the public key
pk, res = DeserializeLWEPublicKey(datafolder + "/pk1.txt", JSON)
if not res:
raise Exception("Could not deserialize the public key")
print("The public key has been deserialized.")
# Deserializing a previously serialized ciphertext
ct, res = DeserializeLWECiphertext(datafolder + "/ct1.txt", JSON)
if not res:
raise Exception("Could not deserialize the ciphertext")
print("The ciphertext has been deserialized.")
# OPERATIONS WITH DESERIALIZED KEYS AND CIPHERTEXTS
ct2 = cc.Encrypt(pk, 1)
print("Running the computation")
ct_result = cc.EvalBinGate(AND, ct, ct2)
print("The computation has completed")
result = cc.Decrypt(sk, ct_result)
print(f"result of 1 AND 1 = {result}")
def main():
global datafolder
with tempfile.TemporaryDirectory() as td:
datafolder = td + "/" + datafolder
os.mkdir(datafolder)
main_action()
if __name__ == "__main__":
main()