-
Notifications
You must be signed in to change notification settings - Fork 2
/
tfhe_utils.py
192 lines (143 loc) · 7.2 KB
/
tfhe_utils.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
from ctypes import *
dir_path = os.path.dirname(os.path.realpath(__file__))
tfhe_io_path = dir_path + "/tfhe_io.so"
Torus32 = c_int32
class LweParams(Structure):
_fields_ = [("n", c_int),
("alpha_min", c_double),
("alpha_max", c_double)]
class TLweParams(Structure):
_fields_ = [("N", c_int),
("k", c_int),
("alpha_min", c_double),
("alpha_max", c_double),
("extracted_lweparams", LweParams)]
class TGswParams(Structure):
_fields_ = [("l", c_int),
("Bgbit", c_int),
("Bg", c_int),
("halfBg", c_int32),
("maskMod", c_uint32),
("tlwe_params", POINTER(TLweParams)),
("kpl", c_int),
("h", POINTER(Torus32)),
("offset", c_uint32)]
class LweKey(Structure):
_fields_ = [("params", POINTER(LweParams)),
("key", POINTER(c_int))]
class IntPolynomial(Structure):
_fields_ = [("N", c_int),
("coefs", POINTER(c_int))]
class TLweKey(Structure):
_fields_ = [("params", POINTER(TLweParams)),
("key", POINTER(IntPolynomial))]
class TGswKey(Structure):
_fields_ = [("params", POINTER(TGswParams)),
("tlwe_params", POINTER(TLweParams)),
("key", POINTER(IntPolynomial)),
("tlwe_key", TLweKey)]
class TFheGateBootstrappingParameterSet(Structure):
_fields_ = [("ks_t", c_int),
("ks_basebit", c_int),
("in_out_params", POINTER(LweParams)),
("tgsw_params", POINTER(TGswParams))]
class TorusPolynomial(Structure):
_fields_ = [("N", c_int),
("coefsT", POINTER(Torus32))]
class TLweSample(Structure):
_fields_ = [("a", POINTER(TorusPolynomial)),
("b", POINTER(TorusPolynomial)),
("current_variance", c_double),
("k", c_int)]
class TGswSample(Structure):
_fields_ = [("all_sample", POINTER(TLweSample)),
("bloc_sample", POINTER(POINTER(TLweSample))),
("k", c_int),
("l", c_int)]
class LweSample(Structure):
_fields_ = [("a", POINTER(Torus32)),
("b", Torus32),
("current_variance", c_double)]
class LweKeySwitchKey(Structure):
_fields_ = [("n", c_int),
("t", c_int),
("basebit", c_int),
("base", c_int),
("out_params", POINTER(LweParams)),
("ks0_raw", POINTER(LweSample)),
("ks1_raw", POINTER(POINTER(LweSample))),
("ks", POINTER(POINTER(POINTER(LweSample))))]
class LweBootstrappingKey(Structure):
_fields_ = [("in_out_params", POINTER(LweParams)),
("bk_params", POINTER(TGswParams)),
("accum_params", POINTER(TLweParams)),
("extract_params", POINTER(LweParams)),
("bk", POINTER(TGswSample)),
("ks", POINTER(LweKeySwitchKey))]
class LagrangeHalfCPolynomial(Structure):
_fields_ = [("data", POINTER(None)),
("precomp", POINTER(None))]
class TLweSampleFFT(Structure):
_fields_ = [("a", POINTER(LagrangeHalfCPolynomial)),
("b", POINTER(LagrangeHalfCPolynomial)),
("current_variance", c_double),
("k", c_int)]
class TGswSampleFFT(Structure):
_fields_ = [("all_samples", POINTER(TLweSampleFFT)),
("sample", POINTER(POINTER(TLweSampleFFT))),
("k", c_int),
("l", c_int)]
class LweBootstrappingKeyFFT(Structure):
_fields_ = [("in_out_params", POINTER(LweParams)),
("bk_params", POINTER(TGswParams)),
("accum_params", POINTER(TLweParams)),
("extract_params", POINTER(LweParams)),
("bkFFT", POINTER(TGswSampleFFT)),
("ks", POINTER(LweKeySwitchKey))]
class TFheGateBootstrappingCloudKeySet(Structure):
_fields_ = [("params", POINTER(TFheGateBootstrappingParameterSet)),
("bk", POINTER(LweBootstrappingKey)),
("bkFFT", POINTER(LweBootstrappingKeyFFT))]
class TFheGateBootstrappingSecretKeySet(Structure):
_fields_ = [("params", POINTER(TFheGateBootstrappingParameterSet)),
("lwe_key", POINTER(LweKey)),
("tgsw_key", POINTER(TGswKey)),
("cloud", TFheGateBootstrappingCloudKeySet)]
def initialize(library, architecture):
tfhe = cdll.LoadLibrary(library)
tfhe_io = cdll.LoadLibrary(tfhe_io_path)
tfhe.new_default_gate_bootstrapping_parameters.argtypes = [c_int]
tfhe.new_default_gate_bootstrapping_parameters.restype = POINTER(TFheGateBootstrappingParameterSet)
tfhe.delete_gate_bootstrapping_parameters.argtypes = [POINTER(TFheGateBootstrappingParameterSet)]
tfhe.delete_gate_bootstrapping_parameters.restype = None
tfhe.new_random_gate_bootstrapping_secret_keyset.argtypes = [POINTER(TFheGateBootstrappingParameterSet)]
tfhe.new_random_gate_bootstrapping_secret_keyset.restype = POINTER(TFheGateBootstrappingSecretKeySet)
tfhe.delete_gate_bootstrapping_secret_keyset.argtypes = [POINTER(TFheGateBootstrappingSecretKeySet)]
tfhe.delete_gate_bootstrapping_secret_keyset.restype = None
tfhe.new_gate_bootstrapping_ciphertext.argtypes = [POINTER(TFheGateBootstrappingParameterSet)]
tfhe.new_gate_bootstrapping_ciphertext.restype = POINTER(LweSample)
tfhe.delete_gate_bootstrapping_ciphertext.argtypes = [POINTER(LweSample)]
tfhe.delete_gate_bootstrapping_ciphertext.restype = None
# These are array operations that can't be typed unless you know the length ahead of time
tfhe.new_gate_bootstrapping_ciphertext_array.argtypes = [c_int, POINTER(TFheGateBootstrappingParameterSet)]
tfhe.new_gate_bootstrapping_ciphertext_array.restype = POINTER(LweSample) * architecture
tfhe.delete_gate_bootstrapping_ciphertext_array.argtypes = [c_int, POINTER(LweSample) * architecture]
tfhe.delete_gate_bootstrapping_ciphertext_array.restype = None
tfhe_io.export_gate_params.argtypes = [c_char_p, POINTER(TFheGateBootstrappingParameterSet)]
tfhe_io.import_gate_params.argtypes = [c_char_p]
tfhe_io.export_gate_params.restype = None
tfhe_io.import_gate_params.restype = POINTER(TFheGateBootstrappingParameterSet)
tfhe_io.export_secret_keyset.argtypes = [c_char_p, POINTER(TFheGateBootstrappingSecretKeySet)]
tfhe_io.import_secret_keyset.argtypes = [c_char_p]
tfhe_io.export_secret_keyset.restype = None
tfhe_io.import_secret_keyset.restype = POINTER(TFheGateBootstrappingSecretKeySet)
tfhe_io.export_cloud_keyset.argtypes = [c_char_p, POINTER(TFheGateBootstrappingCloudKeySet)]
tfhe_io.import_cloud_keyset.argtypes = [c_char_p]
tfhe_io.export_cloud_keyset.restype = None
tfhe_io.import_cloud_keyset.restype = POINTER(TFheGateBootstrappingCloudKeySet)
tfhe_io.export_ciphertext.argtypes = [c_char_p, POINTER(LweSample), POINTER(TFheGateBootstrappingParameterSet)]
tfhe_io.import_ciphertext.argtypes = [c_char_p, POINTER(LweSample), POINTER(TFheGateBootstrappingParameterSet)]
tfhe_io.export_ciphertext.restype = None
tfhe_io.import_ciphertext.restype = None
return tfhe, tfhe_io