Skip to content

Commit 5f9a7b0

Browse files
authored
fix: state transition (#46)
* feat: publish blst module * feat: publish blst_min_pk and blst_min_sig modules * fix: add blst to all modules * fix: publish MemoryPool * fix: publish MemoryPool * fix: publish initializeThreadPool() deinitializeThreadPool() * fix: PublicKey.fromBytes() consumers --------- Co-authored-by: Tuyen Nguyen <[email protected]>
1 parent ece895f commit 5f9a7b0

File tree

6 files changed

+83
-35
lines changed

6 files changed

+83
-35
lines changed

blst

Lines changed: 0 additions & 1 deletion
This file was deleted.

build.zig

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ pub fn build(b: *std.Build) !void {
2020

2121
const blst_dep = b.dependency("blst", .{ .target = target, .optimize = optimize });
2222

23+
// passed by "zig build -Dportable=true"
24+
const portable = b.option(bool, "portable", "Enable portable implementation") orelse false;
25+
// passed by "zig build -Dforce-adx=true"
26+
const force_adx = b.option(bool, "force-adx", "Enable ADX optimizations") orelse false;
27+
2328
// build blst-z static library
2429
const staticLib = b.addStaticLibrary(.{
2530
.name = "blst-z",
@@ -30,18 +35,28 @@ pub fn build(b: *std.Build) !void {
3035
.optimize = optimize,
3136
});
3237

33-
// passed by "zig build -Dportable=true"
34-
const portable = b.option(bool, "portable", "Enable portable implementation") orelse false;
35-
// passed by "zig build -Dforce-adx=true"
36-
const force_adx = b.option(bool, "force-adx", "Enable ADX optimizations") orelse false;
38+
const module_blst_min_pk = b.createModule(.{
39+
.root_source_file = b.path("src/root_min_pk.zig"),
40+
.target = target,
41+
.optimize = optimize,
42+
});
43+
try withBlst(blst_dep, module_blst_min_pk, target, false, portable, force_adx);
44+
45+
b.modules.put(b.dupe("blst_min_pk"), module_blst_min_pk) catch @panic("OOM");
46+
47+
const module_blst_min_sig = b.createModule(.{
48+
.root_source_file = b.path("src/root_min_sig.zig"),
49+
.target = target,
50+
.optimize = optimize,
51+
});
52+
try withBlst(blst_dep, module_blst_min_sig, target, false, portable, force_adx);
53+
b.modules.put(b.dupe("blst_min_sig"), module_blst_min_sig) catch @panic("OOM");
3754

3855
// blst does not need libc, however we need to link it to enable threading
3956
// see https://github.com/ChainSafe/blst-bun/issues/4
4057
staticLib.linkLibC();
41-
try withBlst(blst_dep, staticLib, target, false, portable, force_adx);
42-
4358
// the folder where blst.h is located
44-
staticLib.addIncludePath(blst_dep.path("bindings"));
59+
try withBlst(blst_dep, staticLib.root_module, target, false, portable, force_adx);
4560

4661
// This declares intent for the library to be installed into the standard
4762
// location when the user invokes the "install" step (the default step when
@@ -59,8 +74,7 @@ pub fn build(b: *std.Build) !void {
5974
// blst does not need libc, however we need to link it to enable threading
6075
// see https://github.com/ChainSafe/blst-bun/issues/4
6176
sharedLib.linkLibC();
62-
try withBlst(blst_dep, sharedLib, target, true, portable, force_adx);
63-
sharedLib.addIncludePath(blst_dep.path("bindings"));
77+
try withBlst(blst_dep, sharedLib.root_module, target, true, portable, force_adx);
6478
b.installArtifact(sharedLib);
6579

6680
const exe = b.addExecutable(.{
@@ -106,8 +120,7 @@ pub fn build(b: *std.Build) !void {
106120
.optimize = optimize,
107121
});
108122

109-
lib_unit_tests.linkLibrary(staticLib);
110-
lib_unit_tests.addIncludePath(blst_dep.path("bindings"));
123+
try withBlst(blst_dep, lib_unit_tests.root_module, target, true, portable, force_adx);
111124

112125
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
113126

@@ -131,7 +144,8 @@ pub fn build(b: *std.Build) !void {
131144
/// and zig will handle a mixture of C, assembly and Zig code
132145
/// reference to https://github.com/supranational/blst/blob/v0.3.13/bindings/rust/build.rs
133146
/// TODO: port all missing flows from the Rust build script
134-
fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: ResolvedTarget, is_shared_lib: bool, portable: bool, force_adx: bool) !void {
147+
fn withBlst(blst_dep: *std.Build.Dependency, module: *std.Build.Module, target: ResolvedTarget, is_shared_lib: bool, portable: bool, force_adx: bool) !void {
148+
module.addIncludePath(blst_dep.path("bindings"));
135149
// add later, once we have cflags
136150
const arch = target.result.cpu.arch;
137151

@@ -141,10 +155,10 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
141155
if (portable == true and force_adx == false) {
142156
// TODO: panic if target_env is sgx
143157
// use this instead
144-
blst_z_lib.root_module.addCMacro("__BLST_PORTABLE__", "");
158+
module.addCMacro("__BLST_PORTABLE__", "");
145159
} else if (portable == false and force_adx == true) {
146160
if (arch == .x86_64) {
147-
blst_z_lib.root_module.addCMacro("__ADX__", "");
161+
module.addCMacro("__ADX__", "");
148162
} else {
149163
std.debug.print("`force-adx` is ignored for non-x86_64 targets \n", .{});
150164
}
@@ -153,16 +167,14 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
153167
// if std::is_x86_feature_detected!("adx") {
154168
if (arch == .x86_64) {
155169
std.debug.print("ADX is turned on by default for x86_64 targets \n", .{});
156-
blst_z_lib.root_module.addCMacro("__ADX__", "");
170+
module.addCMacro("__ADX__", "");
157171
}
158172
// otherwise get: "undefined symbol redcx_mont_256" when run tests in Linux
159173
} else {
160174
// both are true
161175
@panic("Cannot set both `portable` and `force-adx` to true");
162176
}
163177

164-
blst_z_lib.no_builtin = true;
165-
166178
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
167179
const allocator = gpa.allocator();
168180

@@ -184,8 +196,8 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
184196
try cflags.append("-fPIC");
185197
}
186198

187-
blst_z_lib.addCSourceFile(.{ .file = blst_dep.path("src/server.c"), .flags = cflags.items });
188-
blst_z_lib.addCSourceFile(.{ .file = blst_dep.path("build/assembly.S"), .flags = cflags.items });
199+
module.addCSourceFile(.{ .file = blst_dep.path("src/server.c"), .flags = cflags.items });
200+
module.addCSourceFile(.{ .file = blst_dep.path("build/assembly.S"), .flags = cflags.items });
189201

190202
// TODO: we may not need this since we linkLibC() above
191203
const os = target.result.os;
@@ -195,12 +207,12 @@ fn withBlst(blst_dep: *std.Build.Dependency, blst_z_lib: *Compile, target: Resol
195207
if (os.tag == .linux) {
196208
// since "zig cc" works fine, we just follow it
197209
// zig cc -E -Wp,-v -
198-
blst_z_lib.addIncludePath(.{ .cwd_relative = "/usr/local/include" });
199-
blst_z_lib.addIncludePath(.{ .cwd_relative = "/usr/include" });
210+
module.addIncludePath(.{ .cwd_relative = "/usr/local/include" });
211+
module.addIncludePath(.{ .cwd_relative = "/usr/include" });
200212
if (arch == .x86_64) {
201-
blst_z_lib.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu" });
213+
module.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu" });
202214
} else if (arch == .aarch64) {
203-
blst_z_lib.addIncludePath(.{ .cwd_relative = "/usr/include/aarch64-linux-gnu" });
215+
module.addIncludePath(.{ .cwd_relative = "/usr/include/aarch64-linux-gnu" });
204216
}
205217
}
206218
}

src/root_c_abi_min_pk.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ pub const AggregatePublicKey = SigVariant.createAggregatePublicKey();
8484
pub const Signature = SigVariant.createSignature();
8585
pub const AggregateSignature = SigVariant.createAggregateSignature();
8686
pub const SecretKey = SigVariant.createSecretKey();
87+
pub const MemoryPool = SigVariant.getMemoryPoolType();
8788

8889
/// exported C-ABI functions need to be declared at top level, and they only work with extern struct
89-
const PublicKeyType = SigVariant.getPublicKeyType();
90-
const AggregatePublicKeyType = SigVariant.getAggregatePublicKeyType();
91-
const SignatureType = SigVariant.getSignatureType();
92-
const AggregateSignatureType = SigVariant.getAggregateSignatureType();
93-
const SecretKeyType = SigVariant.getSecretKeyType();
94-
const SignatureSetType = SigVariant.getSignatureSetType();
95-
const PkAndSerializedSigType = SigVariant.getPkAndSerializedSigType();
96-
const CallBackFn = SigVariant.getCallBackFn();
97-
const MemoryPool = SigVariant.getMemoryPoolType();
90+
pub const PublicKeyType = SigVariant.getPublicKeyType();
91+
pub const AggregatePublicKeyType = SigVariant.getAggregatePublicKeyType();
92+
pub const SignatureType = SigVariant.getSignatureType();
93+
pub const AggregateSignatureType = SigVariant.getAggregateSignatureType();
94+
pub const SecretKeyType = SigVariant.getSecretKeyType();
95+
pub const SignatureSetType = SigVariant.getSignatureSetType();
96+
pub const PkAndSerializedSigType = SigVariant.getPkAndSerializedSigType();
97+
pub const CallBackFn = SigVariant.getCallBackFn();
9898

9999
/// PublicKey functions
100100
export fn defaultPublicKey() PublicKeyType {

src/root_min_pk.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const min_pk_sig_variant = @import("./root_c_abi_min_pk.zig");
2+
pub const PublicKey = min_pk_sig_variant.PublicKey;
3+
pub const AggregatePublicKey = min_pk_sig_variant.AggregatePublicKey;
4+
pub const Signature = min_pk_sig_variant.Signature;
5+
pub const AggregateSignature = min_pk_sig_variant.AggregateSignature;
6+
pub const SecretKey = min_pk_sig_variant.SecretKey;
7+
pub const MemoryPool = min_pk_sig_variant.MemoryPool;
8+
pub const initializeThreadPool = @import("./thread_pool.zig").initializeThreadPool;
9+
pub const deinitializeThreadPool = @import("./thread_pool.zig").deinitializeThreadPool;
10+
pub const aggregateWithRandomness = min_pk_sig_variant.aggregateWithRandomness;
11+
12+
test "test_sign_n_verify" {
13+
// sample code for consumer like on Readme
14+
const ikm: [32]u8 = [_]u8{
15+
0x93, 0xad, 0x7e, 0x65, 0xde, 0xad, 0x05, 0x2a, 0x08, 0x3a,
16+
0x91, 0x0c, 0x8b, 0x72, 0x85, 0x91, 0x46, 0x4c, 0xca, 0x56,
17+
0x60, 0x5b, 0xb0, 0x56, 0xed, 0xfe, 0x2b, 0x60, 0xa6, 0x3c,
18+
0x48, 0x99,
19+
};
20+
const sk = try SecretKey.keyGen(ikm[0..], null);
21+
const pk = sk.skToPk();
22+
23+
const dst = "BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_";
24+
const msg = "hello foo";
25+
// aug is null
26+
const sig = sk.sign(msg[0..], dst[0..], null);
27+
28+
// aug is null
29+
try sig.verify(true, msg[0..], dst[0..], null, &pk, true);
30+
}

src/root_min_sig.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const min_sig_sig_variant = @import("./root_c_abi_min_sig.zig");
2+
pub const PublicKey = min_sig_sig_variant.PublicKey;
3+
pub const AggregatePublicKey = min_sig_sig_variant.AggregatePublicKey;
4+
pub const Signature = min_sig_sig_variant.Signature;
5+
pub const AggregateSignature = min_sig_sig_variant.AggregateSignature;
6+
pub const SecretKey = min_sig_sig_variant.SecretKey;
7+
pub const aggregateWithRandomness = min_sig_sig_variant.aggregateWithRandomness;

src/sig_variant.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,13 @@ pub fn createSigVariant(
410410
if (pks.len == 0) {
411411
return BLST_ERROR.AGGR_TYPE_MISMATCH;
412412
}
413-
var pk = PublicKey.fromBytes(pks[0]);
413+
var pk = try PublicKey.fromBytes(pks[0]);
414414
if (pks_validate) {
415415
try pk.validate();
416416
}
417417
var agg_pk = @This().fromPublicKey(&pk);
418418
for (pks[1..]) |s| {
419-
pk = PublicKey.fromBytes(s);
419+
pk = try PublicKey.fromBytes(s);
420420
if (pks_validate) {
421421
try pk.validate();
422422
}

0 commit comments

Comments
 (0)