-
Notifications
You must be signed in to change notification settings - Fork 71
/
generate_test_vectors.py
280 lines (211 loc) · 7.85 KB
/
generate_test_vectors.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import json
import os
from umbral import pre
from umbral.keys import UmbralPrivateKey
from umbral.signing import Signer
from umbral.curvebn import CurveBN
from umbral.point import Point
from umbral.random_oracles import hash_to_curvebn, unsafe_hash_to_point, kdf
from umbral.config import set_default_curve, default_params
#######################
# Auxiliary functions #
#######################
def hexlify(data):
if isinstance(data, int):
return hex(data)[2:]
try:
return data.to_bytes().hex()
except AttributeError:
return bytes(data).hex()
def create_test_vector_file(vector, filename, generate_again=False):
path = os.path.join(os.path.dirname(__file__), filename)
mode = 'w' if generate_again else 'x'
try:
with open(path, mode) as f:
json.dump(vector, f, indent=2)
except FileExistsError:
pass
# If True, this will overwrite existing test vector files with new randomly generated instances
generate_again = False
#########
# SETUP #
#########
set_default_curve()
params = default_params()
curve = params.curve
# We create also some Umbral objects for later
delegating_privkey = UmbralPrivateKey.gen_key(params=params)
receiving_privkey = UmbralPrivateKey.gen_key(params=params)
signing_privkey = UmbralPrivateKey.gen_key(params=params)
verifying_key = signing_privkey.get_pubkey()
delegating_key = delegating_privkey.get_pubkey()
receiving_key = receiving_privkey.get_pubkey()
signer = Signer(signing_privkey)
kfrags = pre.generate_kfrags(delegating_privkey=delegating_privkey,
receiving_pubkey=receiving_key,
threshold=6,
N=10,
signer=signer,
)
plain_data = b'peace at dawn'
ciphertext, capsule = pre.encrypt(delegating_key, plain_data)
capsule.set_correctness_keys(delegating=delegating_key,
receiving=receiving_key,
verifying=verifying_key)
cfrag = pre.reencrypt(kfrags[0], capsule)
points = [capsule.point_e, cfrag.point_e1, cfrag.proof.point_e2,
capsule.point_v, cfrag.point_v1, cfrag.proof.point_v2,
capsule.params.u, cfrag.proof.point_kfrag_commitment, cfrag.proof.point_kfrag_pok]
z = cfrag.proof.bn_sig
#######################
# CurveBN arithmetics #
#######################
# Let's generate two random CurveBNs
bn1 = CurveBN.gen_rand(curve)
bn2 = CurveBN.gen_rand(curve)
# Expected results for some binary operations
expected = [('Addition', bn1 + bn2),
('Subtraction', bn1 - bn2),
('Multiplication', bn1 * bn2),
('Division', bn1 / bn2),
('Pow', bn1 ** bn2),
('Mod', bn1 % bn2),
('Inverse', ~bn1),
('Neg', -bn1),
]
expected = [{'operation': op, 'result': hexlify(result)} for (op, result) in expected]
# Definition of test vector
vector_suite = {
'name': 'Test vectors for CurveBN operations',
'params': 'default',
'first operand': hexlify(bn1),
'second operand': hexlify(bn2),
'vectors': expected
}
json_file = 'vectors_curvebn_operations.json'
create_test_vector_file(vector_suite, json_file, generate_again=generate_again)
###################
# hash_to_curvebn #
###################
# Test vectors for different kinds of inputs (bytes, Points, CurveBNs, etc.)
inputs = ([b''],
[b'abc'],
[capsule.point_e],
[z],
[capsule.point_e, z],
points,
)
vectors = list()
for input_to_hash in inputs:
bn_output = hash_to_curvebn(*input_to_hash, params=params)
json_input = [{'class': data.__class__.__name__,
'bytes': hexlify(data),
} for data in input_to_hash]
json_input = {'input': json_input, 'output': hexlify(bn_output) }
vectors.append(json_input)
vector_suite = {
'name' : 'Test vectors for umbral.curvebn.CurveBN.hash()',
'params' : 'default',
'vectors' : vectors
}
create_test_vector_file(vector_suite, 'vectors_curvebn_hash.json', generate_again=generate_again)
#print(json.dumps(vector_suite, indent=2))
##########
# Points #
##########
point1 = Point.gen_rand(curve)
point2 = Point.gen_rand(curve)
# Expected results for some Point operations
expected = [('Addition', point1 + point2),
('Subtraction', point1 - point2),
('Multiplication', bn1 * point1),
('Inversion', -point1),
('To_affine.X', point1.to_affine()[0]),
('To_affine.Y', point1.to_affine()[1]),
('kdf', kdf(point1, pre.DEM_KEYSIZE)),
]
expected = [{'operation': op, 'result': hexlify(result)} for (op, result) in expected]
# Definition of test vector
vector_suite = {
'name': 'Test vectors for Point operations',
'params': 'default',
'first Point operand': hexlify(point1),
'second Point operand': hexlify(point2),
'CurveBN operand': hexlify(bn1),
'vectors': expected
}
json_file = 'vectors_point_operations.json'
create_test_vector_file(vector_suite, json_file, generate_again=generate_again)
########################
# unsafe_hash_to_point #
########################
inputs = (b'',
b'abc',
b'NuCypher',
b'Nucypher',
)
vectors = list()
for data in inputs:
for label in inputs:
point = unsafe_hash_to_point(label=label, data=data, params=params)
json_input = {'data': hexlify(data),
'label': hexlify(label),
'point': hexlify(point),
}
vectors.append(json_input)
vector_suite = {
'name': 'Test vectors for umbral.point.Point.unsafe_hash_to_point',
'params': 'default',
'vectors': vectors
}
create_test_vector_file(vector_suite, 'vectors_unsafe_hash_to_point.json', generate_again=generate_again)
#print(json.dumps(vector_suite, indent=2))
##########
# KFrags #
##########
vectors = list()
for kfrag in kfrags:
assert kfrag.verify(verifying_key, delegating_key, receiving_key)
json_input = {'kfrag': hexlify(kfrag)}
vectors.append(json_input)
vector_suite = {
'name': 'Test vectors for KFrags',
'description': ('This is a collection of KFrags generated under the '
'enclosed delegating, verifying and receiving keys. '
'Each of them must deserialize correctly and the '
'call to verify() must succeed.'),
'params': 'default',
'verifying_key': hexlify(verifying_key),
'delegating_key': hexlify(delegating_key),
'receiving_key': hexlify(receiving_key),
'vectors': vectors
}
#print(json.dumps(vector_suite, indent=2))
create_test_vector_file(vector_suite, 'vectors_kfrags.json', generate_again=generate_again)
##########
# CFrags #
##########
capsule.set_correctness_keys(delegating=delegating_key,
receiving=receiving_key,
verifying=verifying_key)
vectors = list()
for kfrag in kfrags:
cfrag = pre.reencrypt(kfrag, capsule, provide_proof=False)
json_input = {'kfrag': hexlify(kfrag), 'cfrag': hexlify(cfrag)}
vectors.append(json_input)
vector_suite = {
'name': 'Test vectors for CFrags',
'description': ('This is a collection of CFrags, originated from the '
'enclosed Capsule, under the enclosed delegating, '
'verifying and receiving keys. Each CFrag must deserialize '
'correctly and can be replicated with a call to '
'`pre.reencrypt(kfrag, capsule, provide_proof=False)`'),
'params': 'default',
'capsule': hexlify(capsule),
'verifying_key': hexlify(verifying_key),
'delegating_key': hexlify(delegating_key),
'receiving_key': hexlify(receiving_key),
'vectors': vectors
}
#print(json.dumps(vector_suite, indent=2))
create_test_vector_file(vector_suite, 'vectors_cfrags.json', generate_again=generate_again)