Skip to content

Commit 35fd4ba

Browse files
committed
fixup! Add initial zig bindings.
1 parent 72a8059 commit 35fd4ba

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

bindings/zig/blst.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ pub const Error = error {
2121
Unknown,
2222
};
2323

24-
pub const ERROR = enum(c.BLST_ERROR) {
25-
SUCCESS = c.BLST_SUCCESS,
26-
BAD_ENCODING = c.BLST_BAD_ENCODING,
27-
POINT_NOT_ON_CURVE = c.BLST_POINT_NOT_ON_CURVE,
28-
POINT_NOT_IN_GROUP = c.BLST_POINT_NOT_IN_GROUP,
29-
AGGR_TYPE_MISMATCH = c.BLST_AGGR_TYPE_MISMATCH,
30-
VERIFY_FAIL = c.BLST_VERIFY_FAIL,
31-
PK_IS_INFINITY = c.BLST_PK_IS_INFINITY,
32-
BAD_SCALAR = c.BLST_BAD_SCALAR,
24+
pub const ERROR = enum(c.ERROR) {
25+
SUCCESS = c.SUCCESS,
26+
BAD_ENCODING = c.BAD_ENCODING,
27+
POINT_NOT_ON_CURVE = c.POINT_NOT_ON_CURVE,
28+
POINT_NOT_IN_GROUP = c.POINT_NOT_IN_GROUP,
29+
AGGR_TYPE_MISMATCH = c.AGGR_TYPE_MISMATCH,
30+
VERIFY_FAIL = c.VERIFY_FAIL,
31+
PK_IS_INFINITY = c.PK_IS_INFINITY,
32+
BAD_SCALAR = c.BAD_SCALAR,
3333

3434
pub fn as_error(self: ERROR) Error {
3535
return switch (self) {
@@ -86,7 +86,7 @@ pub const Pairing = struct {
8686
pub fn aggregate(self: *Pairing, pk: anytype, sig: anytype,
8787
msg: []const u8, aug: ?[]const u8) ERROR {
8888
const opt = aug orelse &[_]u8{};
89-
var err: c.BLST_ERROR = undefined;
89+
var err: c.ERROR = undefined;
9090

9191
switch (@TypeOf(pk)) {
9292
*const P1_Affine, *P1_Affine => {
@@ -299,7 +299,7 @@ pub const P1 = struct {
299299
return .BAD_ENCODING;
300300
}
301301
const err = c.p1_deserialize(@ptrCast(&self.point), &in[0]);
302-
if (err == c.BLST_SUCCESS) {
302+
if (err == c.SUCCESS) {
303303
c.p1_from_affine(&self.point, @ptrCast(&self.point));
304304
}
305305
return @as(ERROR, @enumFromInt(err));
@@ -516,7 +516,7 @@ pub const P2 = struct {
516516
return .BAD_ENCODING;
517517
}
518518
const err = c.p2_deserialize(@ptrCast(&self.point), &in[0]);
519-
if (err == c.BLST_SUCCESS) {
519+
if (err == c.SUCCESS) {
520520
c.p2_from_affine(&self.point, @ptrCast(&self.point));
521521
}
522522
return @as(ERROR, @enumFromInt(err));

bindings/zig/c.zig

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// automatically generated with 'zig translate-c'
2-
pub const BLST_SUCCESS: c_int = 0;
3-
pub const BLST_BAD_ENCODING: c_int = 1;
4-
pub const BLST_POINT_NOT_ON_CURVE: c_int = 2;
5-
pub const BLST_POINT_NOT_IN_GROUP: c_int = 3;
6-
pub const BLST_AGGR_TYPE_MISMATCH: c_int = 4;
7-
pub const BLST_VERIFY_FAIL: c_int = 5;
8-
pub const BLST_PK_IS_INFINITY: c_int = 6;
9-
pub const BLST_BAD_SCALAR: c_int = 7;
10-
pub const BLST_ERROR = c_uint;
2+
const BLST_SUCCESS: c_int = 0;
3+
const BLST_BAD_ENCODING: c_int = 1;
4+
const BLST_POINT_NOT_ON_CURVE: c_int = 2;
5+
const BLST_POINT_NOT_IN_GROUP: c_int = 3;
6+
const BLST_AGGR_TYPE_MISMATCH: c_int = 4;
7+
const BLST_VERIFY_FAIL: c_int = 5;
8+
const BLST_PK_IS_INFINITY: c_int = 6;
9+
const BLST_BAD_SCALAR: c_int = 7;
10+
const BLST_ERROR = c_uint;
1111
pub const byte = u8;
1212
pub const limb_t = u64;
1313
const blst_scalar = extern struct {
@@ -269,6 +269,15 @@ extern fn blst_fp_from_le_bytes(ret: [*c]blst_fp, in: [*c]const byte, len: usize
269269
extern fn blst_fp_from_be_bytes(ret: [*c]blst_fp, in: [*c]const byte, len: usize) void;
270270
extern fn blst_sha256(out: [*c]byte, msg: [*c]const byte, msg_len: usize) void;
271271
// reexport symbols without blst_ prefix
272+
pub const SUCCESS = BLST_SUCCESS;
273+
pub const BAD_ENCODING = BLST_BAD_ENCODING;
274+
pub const POINT_NOT_ON_CURVE = BLST_POINT_NOT_ON_CURVE;
275+
pub const POINT_NOT_IN_GROUP = BLST_POINT_NOT_IN_GROUP;
276+
pub const AGGR_TYPE_MISMATCH = BLST_AGGR_TYPE_MISMATCH;
277+
pub const VERIFY_FAIL = BLST_VERIFY_FAIL;
278+
pub const PK_IS_INFINITY = BLST_PK_IS_INFINITY;
279+
pub const BAD_SCALAR = BLST_BAD_SCALAR;
280+
pub const ERROR = BLST_ERROR;
272281
pub const scalar = blst_scalar;
273282
pub const fr = blst_fr;
274283
pub const fp = blst_fp;

bindings/zig/generate.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
Unknown,
2222
};
2323
24-
pub const ERROR = enum(c.BLST_ERROR) {
25-
SUCCESS = c.BLST_SUCCESS,
26-
BAD_ENCODING = c.BLST_BAD_ENCODING,
27-
POINT_NOT_ON_CURVE = c.BLST_POINT_NOT_ON_CURVE,
28-
POINT_NOT_IN_GROUP = c.BLST_POINT_NOT_IN_GROUP,
29-
AGGR_TYPE_MISMATCH = c.BLST_AGGR_TYPE_MISMATCH,
30-
VERIFY_FAIL = c.BLST_VERIFY_FAIL,
31-
PK_IS_INFINITY = c.BLST_PK_IS_INFINITY,
32-
BAD_SCALAR = c.BLST_BAD_SCALAR,
24+
pub const ERROR = enum(c.ERROR) {
25+
SUCCESS = c.SUCCESS,
26+
BAD_ENCODING = c.BAD_ENCODING,
27+
POINT_NOT_ON_CURVE = c.POINT_NOT_ON_CURVE,
28+
POINT_NOT_IN_GROUP = c.POINT_NOT_IN_GROUP,
29+
AGGR_TYPE_MISMATCH = c.AGGR_TYPE_MISMATCH,
30+
VERIFY_FAIL = c.VERIFY_FAIL,
31+
PK_IS_INFINITY = c.PK_IS_INFINITY,
32+
BAD_SCALAR = c.BAD_SCALAR,
3333
3434
pub fn as_error(self: ERROR) Error {
3535
return switch (self) {
@@ -86,7 +86,7 @@
8686
pub fn aggregate(self: *Pairing, pk: anytype, sig: anytype,
8787
msg: []const u8, aug: ?[]const u8) ERROR {
8888
const opt = aug orelse &[_]u8{};
89-
var err: c.BLST_ERROR = undefined;
89+
var err: c.ERROR = undefined;
9090
9191
switch (@TypeOf(pk)) {
9292
*const P1_Affine, *P1_Affine => {
@@ -299,7 +299,7 @@
299299
return .BAD_ENCODING;
300300
}
301301
const err = c.p1_deserialize(@ptrCast(&self.point), &in[0]);
302-
if (err == c.BLST_SUCCESS) {
302+
if (err == c.SUCCESS) {
303303
c.p1_from_affine(&self.point, @ptrCast(&self.point));
304304
}
305305
return @as(ERROR, @enumFromInt(err));
@@ -409,21 +409,22 @@ def newer(*files):
409409
ret = subprocess.run(["zig", "translate-c", "../blst.h", "-D__BLST_ZIG__"],
410410
capture_output=True, text=True)
411411
with open("c.zig", "w") as fd:
412-
pubs = []
412+
pubs = {}
413413
print("// automatically generated with 'zig translate-c'", file=fd)
414414
for line in ret.stdout.splitlines():
415415
if "no file" in line:
416416
break
417417
elif not line.startswith("pub const _"):
418-
m = re.match(r'^pub ([\w\s]+? blst_(\w+).*)', line)
418+
m = re.match(r'^pub ([\w\s]+? ((?:blst|BLST)_(\w+)).*)', line)
419419
if m:
420420
print(m.group(1), file=fd)
421-
pubs.append(m.group(2))
421+
pubs[m.group(3)] = m.group(2)
422422
else:
423423
print(line, file=fd)
424424
print("// reexport symbols without blst_ prefix", file=fd)
425-
for pub in pubs:
426-
print("pub const {0} = blst_{0};".format(pub), file=fd)
425+
for key, val in pubs.items():
426+
print("pub const {} = {};".format(key, val), file=fd)
427+
del pubs
427428

428429

429430
def xchg_1vs2(matchobj):

0 commit comments

Comments
 (0)