-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathsimple-ckks-bootstrapping.py
More file actions
144 lines (99 loc) · 4.69 KB
/
simple-ckks-bootstrapping.py
File metadata and controls
144 lines (99 loc) · 4.69 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from openfhe import *
def main():
simple_bootstrap_example()
def simple_bootstrap_example():
parameters = CCParamsCKKSRNS()
secret_key_dist = SecretKeyDist.UNIFORM_TERNARY
parameters.SetSecretKeyDist(secret_key_dist)
parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
parameters.SetRingDim(1<<12)
if get_native_int()==128:
rescale_tech = ScalingTechnique.FIXEDAUTO
dcrt_bits = 78
first_mod = 89
else:
rescale_tech = ScalingTechnique.FLEXIBLEAUTO
dcrt_bits = 59
first_mod = 60
parameters.SetScalingModSize(dcrt_bits)
parameters.SetScalingTechnique(rescale_tech)
parameters.SetFirstModSize(first_mod)
level_budget = [4, 4]
levels_available_after_bootstrap = 10
depth = levels_available_after_bootstrap + FHECKKSRNS.GetBootstrapDepth(level_budget, secret_key_dist)
parameters.SetMultiplicativeDepth(depth)
cryptocontext = GenCryptoContext(parameters)
cryptocontext.Enable(PKESchemeFeature.PKE)
cryptocontext.Enable(PKESchemeFeature.KEYSWITCH)
cryptocontext.Enable(PKESchemeFeature.LEVELEDSHE)
cryptocontext.Enable(PKESchemeFeature.ADVANCEDSHE)
cryptocontext.Enable(PKESchemeFeature.FHE)
ring_dim = cryptocontext.GetRingDimension()
# This is the mazimum number of slots that can be used full packing.
num_slots = int(ring_dim / 2)
print(f"CKKS is using ring dimension {ring_dim}")
cryptocontext.EvalBootstrapSetup(level_budget)
key_pair = cryptocontext.KeyGen()
cryptocontext.EvalMultKeyGen(key_pair.secretKey)
cryptocontext.EvalBootstrapKeyGen(key_pair.secretKey, num_slots)
x = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
encoded_length = len(x)
ptxt = cryptocontext.MakeCKKSPackedPlaintext(x,1,depth-1)
ptxt.SetLength(encoded_length)
print(f"Input: {ptxt}")
ciph = cryptocontext.Encrypt(key_pair.publicKey, ptxt)
print(f"Initial number of levels remaining: {depth - ciph.GetLevel()}")
ciphertext_after = cryptocontext.EvalBootstrap(ciph)
print(f"Number of levels remaining after bootstrapping: {depth - ciphertext_after.GetLevel() - (ciphertext_after.GetNoiseScaleDeg() - 1)}")
result = cryptocontext.Decrypt(ciphertext_after,key_pair.secretKey)
result.SetLength(encoded_length)
print(f"Output after bootstrapping: {result}")
def simple_bootstrap_stc_example():
parameters = CCParamsCKKSRNS()
secret_key_dist = SecretKeyDist.UNIFORM_TERNARY
parameters.SetSecretKeyDist(secret_key_dist)
parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
parameters.SetRingDim(1<<12)
if get_native_int()==128:
rescale_tech = ScalingTechnique.FIXEDAUTO
dcrt_bits = 78
first_mod = 89
else:
rescale_tech = ScalingTechnique.FLEXIBLEAUTO
dcrt_bits = 59
first_mod = 60
parameters.SetScalingModSize(dcrt_bits)
parameters.SetScalingTechnique(rescale_tech)
parameters.SetFirstModSize(first_mod)
level_budget = [4, 4]
levels_available_after_bootstrap = 10 + level_budget[1]
depth = levels_available_after_bootstrap + FHECKKSRNS.GetBootstrapDepth({level_budget[0], 0}, secret_key_dist)
parameters.SetMultiplicativeDepth(depth)
cryptocontext = GenCryptoContext(parameters)
cryptocontext.Enable(PKESchemeFeature.PKE)
cryptocontext.Enable(PKESchemeFeature.KEYSWITCH)
cryptocontext.Enable(PKESchemeFeature.LEVELEDSHE)
cryptocontext.Enable(PKESchemeFeature.ADVANCEDSHE)
cryptocontext.Enable(PKESchemeFeature.FHE)
ring_dim = cryptocontext.GetRingDimension()
# This is the mazimum number of slots that can be used full packing.
num_slots = int(ring_dim / 2)
print(f"CKKS is using ring dimension {ring_dim}")
cryptocontext.EvalBootstrapSetup(level_budget, [0, 0], num_slots, 0, True, True)
key_pair = cryptocontext.KeyGen()
cryptocontext.EvalMultKeyGen(key_pair.secretKey)
cryptocontext.EvalBootstrapKeyGen(key_pair.secretKey, num_slots)
x = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
encoded_length = len(x)
ptxt = cryptocontext.MakeCKKSPackedPlaintext(x, 1, depth-1-levelBudget[1], None, num_slots)
ptxt.SetLength(encoded_length)
print(f"Input: {ptxt}")
ciph = cryptocontext.Encrypt(key_pair.publicKey, ptxt)
print(f"Initial number of levels remaining: {depth - ciph.GetLevel()}")
ciphertext_after = cryptocontext.EvalBootstrap(ciph)
print(f"Number of levels remaining after bootstrapping: {depth - ciphertext_after.GetLevel() - (ciphertext_after.GetNoiseScaleDeg() - 1)}")
result = cryptocontext.Decrypt(ciphertext_after,key_pair.secretKey)
result.SetLength(encoded_length)
print(f"Output after bootstrapping: {result}")
if __name__ == '__main__':
main()