-
Notifications
You must be signed in to change notification settings - Fork 21
/
simple-ckks-bootstrapping.py
76 lines (52 loc) · 2.35 KB
/
simple-ckks-bootstrapping.py
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
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}")
if __name__ == '__main__':
main()