Skip to content

Commit f2ca8ab

Browse files
committed
fixup! Add initial zig bindings.
1 parent a114034 commit f2ca8ab

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

bindings/zig/blst.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ pub const Pairing = struct {
139139
}
140140
};
141141

142+
pub const Uniq = struct {
143+
tree: []u64 = &[_]u64{},
144+
allocator: std.mem.Allocator,
145+
146+
pub fn init(n: usize, allocator: std.mem.Allocator) !Uniq {
147+
const nlimbs = (c.blst_uniq_sizeof(n) + @sizeOf(u64) - 1) / @sizeOf(u64);
148+
const buffer = try allocator.alloc(u64, nlimbs);
149+
150+
c.blst_uniq_init(@ptrCast(buffer));
151+
152+
return Uniq{
153+
.tree = buffer,
154+
.allocator = allocator,
155+
};
156+
}
157+
158+
pub fn deinit(self: *Uniq) void {
159+
self.allocator.free(self.tree);
160+
self.tree = &[_]u64{};
161+
}
162+
163+
pub fn is_uniq(self: *Uniq, msg: []const u8) bool {
164+
return c.blst_uniq_test(@ptrCast(self.tree), @ptrCast(msg), msg.len);
165+
}
166+
};
167+
142168
const FP_BYTES = 384/8;
143169
pub const P1_COMPRESS_BYTES = FP_BYTES;
144170
pub const P1_SERIALIZE_BYTES = FP_BYTES*2;

bindings/zig/generate.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@
139139
}
140140
};
141141
142+
pub const Uniq = struct {
143+
tree: []u64 = &[_]u64{},
144+
allocator: std.mem.Allocator,
145+
146+
pub fn init(n: usize, allocator: std.mem.Allocator) !Uniq {
147+
const nlimbs = (c.blst_uniq_sizeof(n) + @sizeOf(u64) - 1) / @sizeOf(u64);
148+
const buffer = try allocator.alloc(u64, nlimbs);
149+
150+
c.blst_uniq_init(@ptrCast(buffer));
151+
152+
return Uniq{
153+
.tree = buffer,
154+
.allocator = allocator,
155+
};
156+
}
157+
158+
pub fn deinit(self: *Uniq) void {
159+
self.allocator.free(self.tree);
160+
self.tree = &[_]u64{};
161+
}
162+
163+
pub fn is_uniq(self: *Uniq, msg: []const u8) bool {
164+
return c.blst_uniq_test(@ptrCast(self.tree), @ptrCast(msg), msg.len);
165+
}
166+
};
167+
142168
const FP_BYTES = 384/8;
143169
pub const P1_COMPRESS_BYTES = FP_BYTES;
144170
pub const P1_SERIALIZE_BYTES = FP_BYTES*2;

bindings/zig/tests.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ test "sign/verify" {
3636

3737
std.debug.print("OK\n", .{});
3838
}
39+
40+
test "uniq" {
41+
const msgs = &[_][]const u8 {
42+
"three", "two", "one", "three",
43+
};
44+
45+
var ctx = try blst.Uniq.init(msgs.len, std.testing.allocator);
46+
defer ctx.deinit();
47+
48+
for (msgs, 1..) |msg, next| {
49+
try std.testing.expectEqual(ctx.is_uniq(msg), next < msgs.len);
50+
}
51+
}

0 commit comments

Comments
 (0)