Skip to content

Commit

Permalink
Secp as module, update to master ver of zig (#6)
Browse files Browse the repository at this point in the history
* secp as module, update to master ver of zig

using secp as external module, update zul to latest version, update code
to latest ver of zig

* bump zig version

* tmp use copy of zig-cli, until author fix
  • Loading branch information
StringNick authored Sep 2, 2024
1 parent a95defd commit b4bca69
Show file tree
Hide file tree
Showing 172 changed files with 51 additions and 63,320 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [main]

env:
ZIG_VERSION: 0.14.0-dev.909+f9f894200
ZIG_VERSION: 0.14.0-dev.1402+cad65307b

jobs:
build:
Expand Down
62 changes: 17 additions & 45 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,6 @@ const external_dependencies = [_]build_helpers.Dependency{
},
};

fn buildSecp256k1(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step.Compile {
const lib = b.addStaticLibrary(.{ .name = "libsecp", .target = target, .optimize = optimize });

lib.addIncludePath(b.path("libsecp256k1/"));
lib.addIncludePath(b.path("libsecp256k1/src"));

var flags = std.ArrayList([]const u8).init(b.allocator);
defer flags.deinit();

try flags.appendSlice(&.{"-DENABLE_MODULE_RECOVERY=1"});
try flags.appendSlice(&.{"-DENABLE_MODULE_SCHNORRSIG=1"});
try flags.appendSlice(&.{"-DENABLE_MODULE_ECDH=1"});
try flags.appendSlice(&.{"-DENABLE_MODULE_EXTRAKEYS=1"});
lib.addCSourceFiles(.{ .root = b.path("libsecp256k1/"), .flags = flags.items, .files = &.{ "./src/secp256k1.c", "./src/precomputed_ecmult.c", "./src/precomputed_ecmult_gen.c" } });
lib.defineCMacro("USE_FIELD_10X26", "1");
lib.defineCMacro("USE_SCALAR_8X32", "1");
lib.defineCMacro("USE_ENDOMORPHISM", "1");
lib.defineCMacro("USE_NUM_NONE", "1");
lib.defineCMacro("USE_FIELD_INV_BUILTIN", "1");
lib.defineCMacro("USE_SCALAR_INV_BUILTIN", "1");
lib.installHeadersDirectory(b.path("libsecp256k1/src"), "", .{ .include_extensions = &.{".h"} });
lib.installHeadersDirectory(b.path("libsecp256k1/include/"), "", .{ .include_extensions = &.{".h"} });
lib.linkLibC();

return lib;
}

pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
Expand Down Expand Up @@ -74,10 +47,6 @@ pub fn build(b: *std.Build) !void {
.imports = deps,
});

// libsecp256k1 static C library.
const libsecp256k1 = try buildSecp256k1(b, target, optimize);
b.installArtifact(libsecp256k1);

// httpz dependency
const httpz_module = b.dependency("httpz", .{ .target = target, .optimize = optimize }).module("httpz");

Expand All @@ -92,6 +61,11 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
}).module("zul");

const secp256k1 = b.dependency("secp256k1", .{
.target = target,
.optimize = optimize,
});

const base58_module = b.dependency("base58-zig", .{
.target = target,
.optimize = optimize,
Expand Down Expand Up @@ -127,7 +101,6 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libsecp256k1);
exe.root_module.addImport("httpz", httpz_module);
exe.root_module.addImport("pg", pg.module("pg"));
exe.root_module.addImport("zul", zul);
Expand All @@ -154,9 +127,10 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libsecp256k1);
exe.root_module.addImport("httpz", httpz_module);
exe.root_module.addImport("zul", zul);
exe.root_module.addImport("secp256k1", secp256k1.module("secp256k1"));
exe.root_module.linkLibrary(secp256k1.artifact("libsecp"));
exe.root_module.addImport("pg", pg.module("pg"));

// Add dependency modules to the executable.
Expand Down Expand Up @@ -188,7 +162,6 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
exe.linkLibrary(libsecp256k1);
// Add dependency modules to the executable.
for (deps) |mod| exe.root_module.addImport(
mod.name,
Expand All @@ -213,24 +186,16 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
lib_unit_tests.linkLibrary(libsecp256k1);
lib_unit_tests.root_module.addImport("zul", zul);
lib_unit_tests.root_module.addImport("secp256k1", secp256k1.module("secp256k1"));
lib_unit_tests.root_module.linkLibrary(secp256k1.artifact("libsecp"));
lib_unit_tests.root_module.addImport("httpz", httpz_module);
lib_unit_tests.root_module.addImport("base58", base58_module);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);

// Add benchmark step
const bench = b.addExecutable(.{
Expand All @@ -240,8 +205,15 @@ pub fn build(b: *std.Build) !void {
.optimize = .ReleaseFast,
});

bench.linkLibrary(libsecp256k1);
// Add dependency modules to the executable.
for (deps) |mod| bench.root_module.addImport(
mod.name,
mod.module,
);

bench.root_module.addImport("zul", zul);
bench.root_module.addImport("secp256k1", secp256k1.module("secp256k1"));
bench.root_module.linkLibrary(secp256k1.artifact("libsecp"));

const run_bench = b.addRunArtifact(bench);

Expand Down
12 changes: 8 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
// internet connectivity.
.dependencies = .{
.zul = .{
.url = "https://github.com/karlseguin/zul/archive/ae0c27350c0db6b460f22cba30b6b0c4a02d1ffd.zip",
.hash = "1220457e2c8867f6734520d9b335f01e1d851d6fe7adaa7f6f0756158acaf6c5e87f",
.url = "https://github.com/karlseguin/zul/archive/08c989bf6871e87807a4668232913ee245425863.zip",
.hash = "12206f5d1e5bd4793fe952bbae891b7424a19026e0d296a1381074c7d21d5d76c1a1",
},
.@"zig-cli" = .{
.url = "https://github.com/sam701/zig-cli/archive/9a94c4803a52e54c26b198096d63fb5bde752da2.zip",
.hash = "1220ab73fb7cc11b2308edc3364988e05efcddbcac31b707f55e6216d1b9c0da13f1",
.url = "https://github.com/StringNick/zig-cli/archive/c9b9d17b14c524785a32a5b7c930b9584a331372.zip",
.hash = "12204099afd10139d640e1c5b5338c7434bf5d7bb8336728007f575b8b3a05821e96",
},
.httpz = .{
.url = "git+https://github.com/karlseguin/http.zig#7080d974aeee6438038ae7744509367317fdf5a0",
Expand All @@ -43,6 +43,10 @@
.url = "https://github.com/ultd/base58-zig/archive/e1001fbe8b41eed36d81e37931ada66b784e51dc.zip",
.hash = "12206e5050a03cd9dcb896781de0cf541081488006532675371653f61d00c1f27433",
},
.secp256k1 = .{
.url = "https://github.com/zig-bitcoin/libsecp256k1-zig/archive/5f70bc5aa2a5ebc69c78a9a75fb83c2d7035bde1.zip",
.hash = "12208e2a2f181feabb9fa01db64232e9cdefdf1b8f2919f1dd7f24c2393cad2b947b",
},
},
.paths = .{
"build.zig",
Expand Down
184 changes: 0 additions & 184 deletions libsecp256k1/CHANGELOG.md

This file was deleted.

Loading

0 comments on commit b4bca69

Please sign in to comment.