-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_case.zig
More file actions
426 lines (358 loc) · 15.2 KB
/
test_case.zig
File metadata and controls
426 lines (358 loc) · 15.2 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
const std = @import("std");
const yaml = @import("yaml");
const blst = @import("blst");
const Allocator = std.mem.Allocator;
pub fn aggregate(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const AggregateTestData = struct {
input: [][]const u8,
output: []const u8,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const aggregate_test_data = try data_yaml.parse(allocator, AggregateTestData);
{
const signatures = try allocator.alloc(blst.Signature, aggregate_test_data.input.len);
defer allocator.free(signatures);
var sig_buf: [blst.Signature.COMPRESS_SIZE]u8 = undefined;
for (aggregate_test_data.input, 0..) |sig_hex_bytes, i| {
const sig_bytes = try std.fmt.hexToBytes(
&sig_buf,
sig_hex_bytes[2..], // skip "0x" prefix
);
signatures[i] = try blst.Signature.deserialize(sig_bytes);
}
// yaml library parses `null` as a string
if (std.mem.eql(u8, aggregate_test_data.output, "null")) {
// expect failure
try std.testing.expectError(blst.BlstError.AggrTypeMismatch, blst.AggregateSignature.aggregate(signatures, true));
} else {
const expected = try std.fmt.hexToBytes(
&sig_buf,
aggregate_test_data.output[2..], // skip "0x" prefix
);
const aggregate_sig = try blst.AggregateSignature.aggregate(signatures, false);
const actual = aggregate_sig.toSignature().compress();
try std.testing.expectEqualSlices(u8, expected, &actual);
}
}
}
pub fn aggregate_verify(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const AggregateVerifyTestData = struct {
input: struct {
pubkeys: [][]const u8,
messages: [][]const u8,
signature: []const u8,
},
output: bool,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const aggregate_verify_test_data = try data_yaml.parse(allocator, AggregateVerifyTestData);
{
const num_sigs = aggregate_verify_test_data.input.pubkeys.len;
try std.testing.expect(num_sigs == aggregate_verify_test_data.input.messages.len);
const pubkeys = try allocator.alloc(blst.PublicKey, num_sigs);
defer allocator.free(pubkeys);
const messages = try allocator.alloc([32]u8, num_sigs);
defer allocator.free(messages);
var pk_buf: [blst.PublicKey.COMPRESS_SIZE]u8 = undefined;
var sig_buf: [blst.Signature.COMPRESS_SIZE]u8 = undefined;
var pairing_buf: [blst.Pairing.sizeOf()]u8 align(blst.Pairing.buf_align) = undefined;
for (aggregate_verify_test_data.input.pubkeys, 0..) |pk_hex_bytes, i| {
const pk_bytes = try std.fmt.hexToBytes(
&pk_buf,
pk_hex_bytes[2..], // skip "0x" prefix
);
pubkeys[i] = try blst.PublicKey.deserialize(pk_bytes);
}
for (aggregate_verify_test_data.input.messages, 0..) |msg_hex_bytes, i| {
_ = try std.fmt.hexToBytes(
messages[i][0..],
msg_hex_bytes[2..], // skip "0x" prefix
);
}
const sig_bytes = try std.fmt.hexToBytes(
&sig_buf,
aggregate_verify_test_data.input.signature[2..], // skip "0x" prefix
);
const signature = blst.Signature.deserialize(sig_bytes) catch {
// if signature is invalid, expect false
try std.testing.expect(!aggregate_verify_test_data.output);
return;
};
const result = signature.aggregateVerify(
true,
&pairing_buf,
messages,
blst.DST,
pubkeys,
true,
) catch false;
try std.testing.expectEqual(aggregate_verify_test_data.output, result);
}
}
pub fn fast_aggregate_verify(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const FastAggregateVerifyTestData = struct {
input: struct {
pubkeys: [][]const u8,
message: []const u8,
signature: []const u8,
},
output: bool,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const fast_aggregate_verify_test_data = try data_yaml.parse(allocator, FastAggregateVerifyTestData);
{
const num_sigs = fast_aggregate_verify_test_data.input.pubkeys.len;
const pubkeys = try allocator.alloc(blst.PublicKey, num_sigs);
defer allocator.free(pubkeys);
var msg_bytes: [32]u8 = undefined;
var pk_buf: [blst.PublicKey.COMPRESS_SIZE]u8 = undefined;
var sig_buf: [blst.Signature.COMPRESS_SIZE]u8 = undefined;
var pairing_buf: [blst.Pairing.sizeOf()]u8 align(blst.Pairing.buf_align) = undefined;
for (fast_aggregate_verify_test_data.input.pubkeys, 0..) |pk_hex_bytes, i| {
const pk_bytes = try std.fmt.hexToBytes(
&pk_buf,
pk_hex_bytes[2..], // skip "0x" prefix
);
pubkeys[i] = try blst.PublicKey.deserialize(pk_bytes);
}
_ = try std.fmt.hexToBytes(
&msg_bytes,
fast_aggregate_verify_test_data.input.message[2..], // skip "0x" prefix
);
const sig_bytes = try std.fmt.hexToBytes(
&sig_buf,
fast_aggregate_verify_test_data.input.signature[2..], // skip "0x" prefix
);
const signature = blst.Signature.deserialize(sig_bytes) catch {
// if signature is invalid, expect false
try std.testing.expect(!fast_aggregate_verify_test_data.output);
return;
};
const result = signature.fastAggregateVerify(
true,
&pairing_buf,
&msg_bytes,
blst.DST,
pubkeys,
true,
) catch false;
try std.testing.expectEqual(fast_aggregate_verify_test_data.output, result);
}
}
pub fn sign(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const SignTestData = struct {
input: struct {
privkey: []const u8,
message: []const u8,
},
output: []const u8,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const sign_test_data = try data_yaml.parse(allocator, SignTestData);
{
var privkey: [32]u8 = undefined;
_ = try std.fmt.hexToBytes(&privkey, sign_test_data.input.privkey[2..]); // skip "0x" prefix
var msg: [32]u8 = undefined;
_ = try std.fmt.hexToBytes(&msg, sign_test_data.input.message[2..]); // skip "0x" prefix
const sk = blst.SecretKey.deserialize(&privkey) catch {
// if secret key is invalid, expect signature to be "null"
try std.testing.expect(std.mem.eql(u8, sign_test_data.output, "null"));
return;
};
const sig = sk.sign(&msg, blst.DST, null);
const actual = sig.compress();
var sig_buf: [blst.Signature.COMPRESS_SIZE]u8 = undefined;
const expected = try std.fmt.hexToBytes(&sig_buf, sign_test_data.output[2..]); // skip "0x" prefix
try std.testing.expectEqualSlices(u8, expected, &actual);
}
}
pub fn verify(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const VerifyTestData = struct {
input: struct {
pubkey: []const u8,
message: []const u8,
signature: []const u8,
},
output: bool,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const verify_test_data = try data_yaml.parse(allocator, VerifyTestData);
{
var pk_buf: [blst.PublicKey.COMPRESS_SIZE]u8 = undefined;
var sig_buf: [blst.Signature.COMPRESS_SIZE]u8 = undefined;
var msg_bytes: [32]u8 = undefined;
const pk_bytes = try std.fmt.hexToBytes(&pk_buf, verify_test_data.input.pubkey[2..]); // skip "0x" prefix
const pk = blst.PublicKey.deserialize(pk_bytes) catch {
// if public key is invalid, expect false
try std.testing.expect(!verify_test_data.output);
return;
};
_ = try std.fmt.hexToBytes(&msg_bytes, verify_test_data.input.message[2..]); // skip "0x" prefix
const sig_bytes = try std.fmt.hexToBytes(&sig_buf, verify_test_data.input.signature[2..]); // skip "0x" prefix
const signature = blst.Signature.deserialize(sig_bytes) catch {
// if signature is invalid, expect false
try std.testing.expectEqual(verify_test_data.output, false);
return;
};
signature.verify(
true,
&msg_bytes,
blst.DST,
null,
&pk,
true,
) catch {
// if verification fails, expect false
try std.testing.expectEqual(verify_test_data.output, false);
return;
};
try std.testing.expectEqual(verify_test_data.output, true);
}
}
pub fn eth_aggregate_pubkeys(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const EthAggregatePubkeysTestData = struct {
input: [][]const u8,
output: []const u8,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const eth_aggregate_pubkeys_test_data = try data_yaml.parse(allocator, EthAggregatePubkeysTestData);
{
const pubkeys = try allocator.alloc(blst.PublicKey, eth_aggregate_pubkeys_test_data.input.len);
defer allocator.free(pubkeys);
var pk_buf: [blst.PublicKey.COMPRESS_SIZE]u8 = undefined;
for (eth_aggregate_pubkeys_test_data.input, 0..) |pk_hex_bytes, i| {
const pk_bytes = try std.fmt.hexToBytes(
&pk_buf,
pk_hex_bytes[2..], // skip "0x" prefix
);
pubkeys[i] = blst.PublicKey.deserialize(pk_bytes) catch {
// if any public key is invalid, expect output to be "null"
try std.testing.expect(std.mem.eql(u8, eth_aggregate_pubkeys_test_data.output, "null"));
return;
};
}
// yaml library parses `null` as a string
if (std.mem.eql(u8, eth_aggregate_pubkeys_test_data.output, "null")) {
// expect failure
_ = blst.AggregatePublicKey.aggregate(pubkeys, true) catch {
return;
};
try std.testing.expect(false);
} else {
const expected = try std.fmt.hexToBytes(
&pk_buf,
eth_aggregate_pubkeys_test_data.output[2..], // skip "0x" prefix
);
const aggregate_pk = try blst.AggregatePublicKey.aggregate(pubkeys, false);
const actual = aggregate_pk.toPublicKey().compress();
try std.testing.expectEqualSlices(u8, expected, &actual);
}
}
}
pub fn eth_fast_aggregate_verify(gpa: Allocator, path: std.fs.Dir) !void {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const allocator = arena.allocator();
const data_file = try path.openFile("data.yaml", .{});
defer data_file.close();
const data_bytes = try data_file.readToEndAlloc(allocator, 100_000_000);
const EthFastAggregateVerifyTestData = struct {
input: struct {
pubkeys: [][]const u8,
message: []const u8,
signature: []const u8,
},
output: bool,
};
var data_yaml = yaml.Yaml{ .source = data_bytes };
try data_yaml.load(allocator);
const eth_fast_aggregate_verify_test_data = try data_yaml.parse(allocator, EthFastAggregateVerifyTestData);
{
const num_sigs = eth_fast_aggregate_verify_test_data.input.pubkeys.len;
const pubkeys = try allocator.alloc(blst.PublicKey, num_sigs);
defer allocator.free(pubkeys);
var msg_bytes: [32]u8 = undefined;
var pk_buf: [blst.PublicKey.COMPRESS_SIZE]u8 = undefined;
var sig_buf: [blst.Signature.COMPRESS_SIZE]u8 = undefined;
var pairing_buf: [blst.Pairing.sizeOf()]u8 align(blst.Pairing.buf_align) = undefined;
for (eth_fast_aggregate_verify_test_data.input.pubkeys, 0..) |pk_hex_bytes, i| {
const pk_bytes = try std.fmt.hexToBytes(
&pk_buf,
pk_hex_bytes[2..], // skip "0x" prefix
);
pubkeys[i] = blst.PublicKey.deserialize(pk_bytes) catch {
// if any public key is invalid, expect false
try std.testing.expect(!eth_fast_aggregate_verify_test_data.output);
return;
};
}
_ = try std.fmt.hexToBytes(
&msg_bytes,
eth_fast_aggregate_verify_test_data.input.message[2..], // skip "0x" prefix
);
const sig_bytes = try std.fmt.hexToBytes(
&sig_buf,
eth_fast_aggregate_verify_test_data.input.signature[2..], // skip "0x" prefix
);
const signature = blst.Signature.deserialize(sig_bytes) catch {
// if signature is invalid, expect false
try std.testing.expect(!eth_fast_aggregate_verify_test_data.output);
return;
};
if (pubkeys.len == 0 and signature.isInfinity()) {
try std.testing.expectEqual(eth_fast_aggregate_verify_test_data.output, true);
return;
}
const result = signature.fastAggregateVerify(
true,
&pairing_buf,
&msg_bytes,
blst.DST,
pubkeys,
true,
) catch false;
try std.testing.expectEqual(eth_fast_aggregate_verify_test_data.output, result);
}
}