-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathblst.zig
More file actions
607 lines (506 loc) · 19.3 KB
/
blst.zig
File metadata and controls
607 lines (506 loc) · 19.3 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// DO NOT EDIT THIS FILE!!!
// The file is auto-generated by generate.py
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Copyright Supranational LLC
// SPDX-License-Identifier: Apache-2.0
const std = @import("std");
pub const c = @import("c.zig");
pub const Error = error{
BAD_ENCODING,
POINT_NOT_ON_CURVE,
POINT_NOT_IN_GROUP,
AGGR_TYPE_MISMATCH,
VERIFY_FAIL,
PK_IS_INFINITY,
BAD_SCALAR,
Unknown,
};
pub const ERROR = enum(c.ERROR) {
SUCCESS = c.SUCCESS,
BAD_ENCODING = c.BAD_ENCODING,
POINT_NOT_ON_CURVE = c.POINT_NOT_ON_CURVE,
POINT_NOT_IN_GROUP = c.POINT_NOT_IN_GROUP,
AGGR_TYPE_MISMATCH = c.AGGR_TYPE_MISMATCH,
VERIFY_FAIL = c.VERIFY_FAIL,
PK_IS_INFINITY = c.PK_IS_INFINITY,
BAD_SCALAR = c.BAD_SCALAR,
pub fn as_error(self: ERROR) Error {
return switch (self) {
.BAD_ENCODING => Error.BAD_ENCODING,
.POINT_NOT_ON_CURVE => Error.POINT_NOT_ON_CURVE,
.POINT_NOT_IN_GROUP => Error.POINT_NOT_IN_GROUP,
.AGGR_TYPE_MISMATCH => Error.AGGR_TYPE_MISMATCH,
.VERIFY_FAIL => Error.VERIFY_FAIL,
.PK_IS_INFINITY => Error.PK_IS_INFINITY,
.BAD_SCALAR => Error.BAD_SCALAR,
else => Error.Unknown,
};
}
};
pub const SecretKey = struct {
key: c.scalar = c.scalar{},
pub fn keygen(self: *SecretKey, IKM: []const u8, info: ?[]const u8) void {
const opt = info orelse &[_]u8{};
c.keygen(&self.key, @ptrCast(IKM), IKM.len,
@ptrCast(opt), opt.len);
}
pub fn deinit(self: *SecretKey) void {
self.key = c.scalar{};
}
};
pub const PT = c.fp12;
pub const Pairing = struct {
ctx: []u64 = &[_]u64{},
allocator: std.mem.Allocator,
pub fn init(hash_or_encode: bool, DST: []const u8,
allocator: std.mem.Allocator) !Pairing {
const nlimbs = (c.pairing_sizeof() + @sizeOf(u64) - 1) / @sizeOf(u64);
const buffer = try allocator.alloc(u64, nlimbs);
c.pairing_init(@ptrCast(buffer), hash_or_encode, &DST[0], DST.len);
return Pairing{
.ctx = buffer,
.allocator = allocator,
};
}
pub fn deinit(self: *Pairing) void {
self.allocator.free(self.ctx);
self.ctx = &[_]u64{};
}
pub fn aggregate(self: *Pairing, pk: anytype, sig: anytype,
msg: []const u8, aug: ?[]const u8) ERROR {
const opt = aug orelse &[_]u8{};
var err: c.ERROR = undefined;
switch (@TypeOf(pk)) {
*const P1_Affine, *P1_Affine => {
const sigp: [*c]const c.p2_affine = switch (@TypeOf(sig)) {
@TypeOf(null) => null,
else => &sig.point,
};
err = c.pairing_aggregate_pk_in_g1(@ptrCast(self.ctx),
&pk.point, sigp,
@ptrCast(msg), msg.len,
@ptrCast(opt), opt.len);
},
*const P2_Affine, *P2_Affine => {
const sigp: [*c]const c.p1_affine = switch (@TypeOf(sig)) {
@TypeOf(null) => null,
else => &sig.point,
};
err = c.pairing_aggregate_pk_in_g2(@ptrCast(self.ctx),
&pk.point, sigp,
@ptrCast(msg), msg.len,
@ptrCast(opt), opt.len);
},
else => |T| @compileError("expected type '*const blst.P1_Affine' "
++ "or '*const blst.P2_Affine', found '"
++ @typeName(T) ++ "'"),
}
return @as(ERROR, @enumFromInt(err));
}
pub fn commit(self: *Pairing) void {
c.pairing_commit(@ptrCast(self.ctx));
}
pub fn merge(self: *Pairing, second: *const Pairing) ERROR {
return c.pairing_merge(@ptrCast(self.ctx), @ptrCast(second.ctx));
}
pub fn finalverify(self: *Pairing, optional: ?*const PT) bool {
return c.pairing_finalverify(@ptrCast(self.ctx), optional);
}
pub fn raw_aggregate(self: *Pairing, q: *const P2_Affine,
p: *const P1_Affine) void {
c.pairing_raw_aggregate(@ptrCast(self.ctx), &q.point, &p.point);
}
pub fn as_fp12(self: *Pairing) *const PT {
return c.pairing_as_fp12(@ptrCast(self.ctx));
}
};
pub const Uniq = struct {
tree: []u64 = &[_]u64{},
allocator: std.mem.Allocator,
pub fn init(n: usize, allocator: std.mem.Allocator) !Uniq {
const nlimbs = (c.uniq_sizeof(n) + @sizeOf(u64) - 1) / @sizeOf(u64);
const buffer = try allocator.alloc(u64, nlimbs);
c.uniq_init(@ptrCast(buffer));
return Uniq{
.tree = buffer,
.allocator = allocator,
};
}
pub fn deinit(self: *Uniq) void {
self.allocator.free(self.tree);
self.tree = &[_]u64{};
}
pub fn is_uniq(self: *Uniq, msg: []const u8) bool {
return c.uniq_test(@ptrCast(self.tree), @ptrCast(msg), msg.len);
}
};
const FP_BYTES = 384/8;
pub const P1_COMPRESS_BYTES = FP_BYTES;
pub const P1_SERIALIZE_BYTES = FP_BYTES*2;
pub const P2_COMPRESS_BYTES = FP_BYTES*2;
pub const P2_SERIALIZE_BYTES = FP_BYTES*4;
pub const P1_Affine = struct {
point: c.p1_affine = c.p1_affine{},
pub fn from(in: anytype) !P1_Affine {
switch (@TypeOf(in)) {
*const P1,
*P1 => return in.to_affine(),
P1 => @compileError("expected type '*const blst.P1', found 'blst.P1'"),
else => |T| {
switch (@typeInfo(T)) {
.pointer => { const s: []const u8 = in; _ = s; },
else => @compileError("expected type '[]const u8', found '" ++ @typeName(T) ++ "'"),
}
var ret: P1_Affine = undefined;
const err = ret.deserialize(in);
return if (err == .SUCCESS) ret else err.as_error();
},
}
unreachable;
}
pub fn deserialize(self: *P1_Affine, in: []const u8) ERROR {
if (in.len == 0) {
return .BAD_ENCODING;
}
const expected = @as(usize, if (in[0]&0x80 != 0) P1_COMPRESS_BYTES
else P1_SERIALIZE_BYTES);
if (in.len != expected) {
return .BAD_ENCODING;
}
const err = c.p1_deserialize(&self.point, &in[0]);
return @as(ERROR, @enumFromInt(err));
}
pub fn serialize(self: *const P1_Affine) [P1_SERIALIZE_BYTES]u8 {
var ret: [P1_SERIALIZE_BYTES]u8 = undefined;
c.p1_affine_serialize(&ret[0], &self.point);
return ret;
}
pub fn compress(self: *const P1_Affine) [P1_COMPRESS_BYTES]u8 {
var ret: [P1_COMPRESS_BYTES]u8 = undefined;
c.p1_affine_compress(&ret[0], &self.point);
return ret;
}
pub fn dup(self: *const P1_Affine) P1_Affine {
return self.*;
}
pub fn on_curve(self: *const P1_Affine) bool {
return c.p1_affine_on_curve(&self.point);
}
pub fn in_group(self: *const P1_Affine) bool {
return c.p1_affine_in_g1(&self.point);
}
pub fn is_inf(self: *const P1_Affine) bool {
return c.p1_affine_is_inf(&self.point);
}
pub fn is_equal(self: *const P1_Affine, p: *const P1_Affine) bool {
return c.p1_affine_is_equal(&self.point, &p.point);
}
pub fn core_verify(self: *const P1_Affine, pk: *const P2_Affine,
hash_or_encode: bool, msg: []const u8, DST: []const u8,
aug: ?[]const u8) ERROR {
const opt = aug orelse &[_]u8{};
const err = c.core_verify_pk_in_g2(&pk.point, &self.point,
hash_or_encode,
@ptrCast(msg), msg.len,
@ptrCast(DST), DST.len,
@ptrCast(opt), opt.len);
return @as(ERROR, @enumFromInt(err));
}
pub fn generator() P1_Affine {
return P1_Affine{
.point = c.p1_affine_generator().*,
};
}
pub fn to_jacobian(self: *const P1_Affine) P1 {
var ret: P1 = undefined;
c.p1_from_affine(&ret.point, &self.point);
return ret;
}
};
pub const P1 = struct {
point: c.p1 = c.p1{},
pub fn from(in: anytype) !P1 {
switch (@TypeOf(in)) {
*const SecretKey,
*SecretKey => return P1.public_key(in),
SecretKey => @compileError("expected type '*const blst.SecretKey', found 'blst.SecretKey'"),
*const P1_Affine,
*P1_Affine => return in.to_jacobian(),
P1_Affine => @compileError("expected type '*const blst.P1_Affine', found 'blst.P1_Affine'"),
else => |T| {
switch (@typeInfo(T)) {
.pointer => { const s: []const u8 = in; _ = s; },
else => @compileError("expected type '[]const u8', found '" ++ @typeName(T) ++ "'"),
}
var ret: P1 = undefined;
const err = ret.deserialize(in);
return if (err == .SUCCESS) ret else err.as_error();
},
}
unreachable;
}
pub fn deserialize(self: *P1, in: []const u8) ERROR {
if (in.len == 0) {
return .BAD_ENCODING;
}
const expected = @as(usize, if (in[0]&0x80 != 0) P1_COMPRESS_BYTES
else P1_SERIALIZE_BYTES);
if (in.len != expected) {
return .BAD_ENCODING;
}
const err = c.p1_deserialize(@ptrCast(&self.point), &in[0]);
if (err == c.SUCCESS) {
c.p1_from_affine(&self.point, @ptrCast(&self.point));
}
return @as(ERROR, @enumFromInt(err));
}
pub fn serialize(self: *const P1) [P1_SERIALIZE_BYTES]u8 {
var ret: [P1_SERIALIZE_BYTES]u8 = undefined;
c.p1_serialize(&ret[0], &self.point);
return ret;
}
pub fn compress(self: *const P1) [P1_COMPRESS_BYTES]u8 {
var ret: [P1_COMPRESS_BYTES]u8 = undefined;
c.p1_compress(&ret[0], &self.point);
return ret;
}
pub fn public_key(sk: *const SecretKey) P1 {
var ret: P1 = undefined;
c.sk_to_pk_in_g1(&ret.point, &sk.key);
return ret;
}
pub fn dup(self: *const P1) P1 {
return self.*;
}
pub fn on_curve(self: *const P1) bool {
return c.p1_on_curve(&self.point);
}
pub fn in_group(self: *const P1) bool {
return c.p1_in_g1(&self.point);
}
pub fn is_inf(self: *const P1) bool {
return c.p1_is_inf(&self.point);
}
pub fn is_equal(self: *const P1, p: *const P1) bool {
return c.p1_is_equal(&self.point, &p.point);
}
pub fn aggregate(self: *P1, p: *const P1_Affine) !void {
if (!c.p1_affine_in_g1(&p.point)) {
return Error.POINT_NOT_IN_GROUP;
}
c.p1_add_or_double_affine(&self.point, &self.point, &p.point);
}
pub fn hash_to(msg: []const u8, DST: []const u8, aug: ?[]const u8) P1 {
const opt = aug orelse &[_]u8{};
var ret: P1 = undefined;
c.hash_to_g1(&ret.point, @ptrCast(msg), msg.len,
@ptrCast(DST), DST.len,
@ptrCast(opt), opt.len);
return ret;
}
pub fn encode_to(msg: []const u8, DST: []const u8, aug: ?[]const u8) P1 {
const opt = aug orelse &[_]u8{};
var ret: P1 = undefined;
c.encode_to_g1(&ret.point, @ptrCast(msg), msg.len,
@ptrCast(DST), DST.len,
@ptrCast(opt), opt.len);
return ret;
}
pub fn sign_with(self: *const P1, sk: *const SecretKey) *P1 {
c.sign_pk_in_g2(@constCast(&self.point), &self.point, &sk.key);
return @constCast(self);
}
pub fn to_affine(self: *const P1) P1_Affine {
var ret: P1_Affine = undefined;
c.p1_to_affine(&ret.point, &self.point);
return ret;
}
pub fn generator() P1 {
return P1{
.point = c.p1_generator().*,
};
}
};
pub const P2_Affine = struct {
point: c.p2_affine = c.p2_affine{},
pub fn from(in: anytype) !P2_Affine {
switch (@TypeOf(in)) {
*const P2,
*P2 => return in.to_affine(),
P2 => @compileError("expected type '*const blst.P2', found 'blst.P2'"),
else => |T| {
switch (@typeInfo(T)) {
.pointer => { const s: []const u8 = in; _ = s; },
else => @compileError("expected type '[]const u8', found '" ++ @typeName(T) ++ "'"),
}
var ret: P2_Affine = undefined;
const err = ret.deserialize(in);
return if (err == .SUCCESS) ret else err.as_error();
},
}
unreachable;
}
pub fn deserialize(self: *P2_Affine, in: []const u8) ERROR {
if (in.len == 0) {
return .BAD_ENCODING;
}
const expected = @as(usize, if (in[0]&0x80 != 0) P2_COMPRESS_BYTES
else P2_SERIALIZE_BYTES);
if (in.len != expected) {
return .BAD_ENCODING;
}
const err = c.p2_deserialize(&self.point, &in[0]);
return @as(ERROR, @enumFromInt(err));
}
pub fn serialize(self: *const P2_Affine) [P2_SERIALIZE_BYTES]u8 {
var ret: [P2_SERIALIZE_BYTES]u8 = undefined;
c.p2_affine_serialize(&ret[0], &self.point);
return ret;
}
pub fn compress(self: *const P2_Affine) [P2_COMPRESS_BYTES]u8 {
var ret: [P2_COMPRESS_BYTES]u8 = undefined;
c.p2_affine_compress(&ret[0], &self.point);
return ret;
}
pub fn dup(self: *const P2_Affine) P2_Affine {
return self.*;
}
pub fn on_curve(self: *const P2_Affine) bool {
return c.p2_affine_on_curve(&self.point);
}
pub fn in_group(self: *const P2_Affine) bool {
return c.p2_affine_in_g2(&self.point);
}
pub fn is_inf(self: *const P2_Affine) bool {
return c.p2_affine_is_inf(&self.point);
}
pub fn is_equal(self: *const P2_Affine, p: *const P2_Affine) bool {
return c.p2_affine_is_equal(&self.point, &p.point);
}
pub fn core_verify(self: *const P2_Affine, pk: *const P1_Affine,
hash_or_encode: bool, msg: []const u8, DST: []const u8,
aug: ?[]const u8) ERROR {
const opt = aug orelse &[_]u8{};
const err = c.core_verify_pk_in_g1(&pk.point, &self.point,
hash_or_encode,
@ptrCast(msg), msg.len,
@ptrCast(DST), DST.len,
@ptrCast(opt), opt.len);
return @as(ERROR, @enumFromInt(err));
}
pub fn generator() P2_Affine {
return P2_Affine{
.point = c.p2_affine_generator().*,
};
}
pub fn to_jacobian(self: *const P2_Affine) P2 {
var ret: P2 = undefined;
c.p2_from_affine(&ret.point, &self.point);
return ret;
}
};
pub const P2 = struct {
point: c.p2 = c.p2{},
pub fn from(in: anytype) !P2 {
switch (@TypeOf(in)) {
*const SecretKey,
*SecretKey => return P2.public_key(in),
SecretKey => @compileError("expected type '*const blst.SecretKey', found 'blst.SecretKey'"),
*const P2_Affine,
*P2_Affine => return in.to_jacobian(),
P2_Affine => @compileError("expected type '*const blst.P2_Affine', found 'blst.P2_Affine'"),
else => |T| {
switch (@typeInfo(T)) {
.pointer => { const s: []const u8 = in; _ = s; },
else => @compileError("expected type '[]const u8', found '" ++ @typeName(T) ++ "'"),
}
var ret: P2 = undefined;
const err = ret.deserialize(in);
return if (err == .SUCCESS) ret else err.as_error();
},
}
unreachable;
}
pub fn deserialize(self: *P2, in: []const u8) ERROR {
if (in.len == 0) {
return .BAD_ENCODING;
}
const expected = @as(usize, if (in[0]&0x80 != 0) P2_COMPRESS_BYTES
else P2_SERIALIZE_BYTES);
if (in.len != expected) {
return .BAD_ENCODING;
}
const err = c.p2_deserialize(@ptrCast(&self.point), &in[0]);
if (err == c.SUCCESS) {
c.p2_from_affine(&self.point, @ptrCast(&self.point));
}
return @as(ERROR, @enumFromInt(err));
}
pub fn serialize(self: *const P2) [P2_SERIALIZE_BYTES]u8 {
var ret: [P2_SERIALIZE_BYTES]u8 = undefined;
c.p2_serialize(&ret[0], &self.point);
return ret;
}
pub fn compress(self: *const P2) [P2_COMPRESS_BYTES]u8 {
var ret: [P2_COMPRESS_BYTES]u8 = undefined;
c.p2_compress(&ret[0], &self.point);
return ret;
}
pub fn public_key(sk: *const SecretKey) P2 {
var ret: P2 = undefined;
c.sk_to_pk_in_g2(&ret.point, &sk.key);
return ret;
}
pub fn dup(self: *const P2) P2 {
return self.*;
}
pub fn on_curve(self: *const P2) bool {
return c.p2_on_curve(&self.point);
}
pub fn in_group(self: *const P2) bool {
return c.p2_in_g2(&self.point);
}
pub fn is_inf(self: *const P2) bool {
return c.p2_is_inf(&self.point);
}
pub fn is_equal(self: *const P2, p: *const P2) bool {
return c.p2_is_equal(&self.point, &p.point);
}
pub fn aggregate(self: *P2, p: *const P2_Affine) !void {
if (!c.p2_affine_in_g2(&p.point)) {
return Error.POINT_NOT_IN_GROUP;
}
c.p2_add_or_double_affine(&self.point, &self.point, &p.point);
}
pub fn hash_to(msg: []const u8, DST: []const u8, aug: ?[]const u8) P2 {
const opt = aug orelse &[_]u8{};
var ret: P2 = undefined;
c.hash_to_g2(&ret.point, @ptrCast(msg), msg.len,
@ptrCast(DST), DST.len,
@ptrCast(opt), opt.len);
return ret;
}
pub fn encode_to(msg: []const u8, DST: []const u8, aug: ?[]const u8) P2 {
const opt = aug orelse &[_]u8{};
var ret: P2 = undefined;
c.encode_to_g2(&ret.point, @ptrCast(msg), msg.len,
@ptrCast(DST), DST.len,
@ptrCast(opt), opt.len);
return ret;
}
pub fn sign_with(self: *const P2, sk: *const SecretKey) *P2 {
c.sign_pk_in_g1(@constCast(&self.point), &self.point, &sk.key);
return @constCast(self);
}
pub fn to_affine(self: *const P2) P2_Affine {
var ret: P2_Affine = undefined;
c.p2_to_affine(&ret.point, &self.point);
return ret;
}
pub fn generator() P2 {
return P2{
.point = c.p2_generator().*,
};
}
};