Repository navigation
Expand file tree
/
Copy pathboolean-serial-json-dynamic-large-precision.py
More file actions
130 lines (96 loc) · 4.25 KB
/
Copy pathboolean-serial-json-dynamic-large-precision.py
File metadata and controls
130 lines (96 loc) · 4.25 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
120
121
122
123
124
125
126
127
128
129
130
#
# Example for FHEW with JSON serialization for large-precision sign evaluation
# with dynamic bootstrapping keys
#
from openfhe import *
import math
import os
import tempfile
# path where files will be written to
datafolder = "demoData"
def main_action():
# Generating the crypto context
cc1 = BinFHEContext()
logQ = 17
cc1.GenerateBinFHEContext(TOY, False, logQ, 0, GINX, True)
Q = 1 << logQ
q = 4096 # q
factor = 1 << int(logQ - math.log2(q)) # Q/q
p = cc1.GetMaxPlaintextSpace() * factor # Obtain the maximum plaintext space
print("Generating keys.")
# Generating the secret key
sk1 = cc1.KeyGen()
# Generate the bootstrapping keys
cc1.BTKeyGen(sk1)
print("Done generating all keys.")
# Encryption for a ciphertext that will be serialized
ct1 = cc1.Encrypt(sk1, 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.")
bt_key_map = cc1.GetBTKeyMap()
for index, thekey in bt_key_map.items():
if not SerializeToFile(datafolder + "/" + str(index) + "btKey.txt", thekey, JSON):
raise Exception("Error serializing the bootstrapping keys")
print(f"The BT map element for baseG = {index} has been serialized.")
# Serializing private keys
if not SerializeToFile(datafolder + "/sk1.txt", sk1, JSON):
raise Exception("Error serializing sk1")
print("The secret key sk1 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.")
base_g_list = [1 << 14, 1 << 18, 1 << 27]
for base_g in base_g_list:
map_key, res = DeserializeBinFHEBTKey(datafolder + "/" + str(base_g) + "btKey.txt", JSON)
if not res:
raise Exception("Could not deserialize the bootstrapping keys")
print(f"The BT map element for baseG = {base_g} has been deserialized.")
# Loading the keys in the cryptocontext
cc.BTKeyMapLoadSingleElement(base_g, map_key)
# 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 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
for i in range(8):
# We first encrypt with large Q
ct1 = cc.Encrypt(sk, p // 2 + i - 3, LARGE_DIM, p, Q)
# Get the MSB
ct1 = cc.EvalSign(ct1)
result = cc.Decrypt(sk, ct1, 2)
print(f"Input: {i}. Expected sign: {int(i >= 3)}. Evaluated Sign: {result}")
def main():
global datafolder
with tempfile.TemporaryDirectory() as td:
datafolder = td + "/" + datafolder
os.mkdir(datafolder)
main_action()
if __name__ == "__main__":
main()