Skip to content

Commit

Permalink
build: move main.zig to example.zig (#8)
Browse files Browse the repository at this point in the history
* build: move main.zig to example.zig

* use default optimize for tests
  • Loading branch information
tdelabro authored Sep 6, 2024
1 parent db39b5c commit 022b409
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ jobs:
${{ runner.os }}-zig-build-
- name: Unit testing
run: zig build test --summary all
run: zig build test
53 changes: 9 additions & 44 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,61 +68,26 @@ pub fn build(b: *std.Build) !void {

b.installArtifact(libsecp256k1);

const exe = b.addExecutable(.{
.name = "libsecp256k1-zig",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
exe.addIncludePath(libsecp_c.path(""));
exe.addIncludePath(libsecp_c.path("src"));

exe.root_module.addImport("secp256k1", module);
exe.root_module.linkLibrary(libsecp256k1);

// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);

// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);

// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());

// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}

// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/secp256k1.zig"),
.target = target,
.optimize = optimize,
});
lib_unit_tests.linkLibrary(libsecp256k1);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const example_tests = b.addTest(.{
.root_source_file = b.path("src/examples.zig"),
.target = target,
});
example_tests.linkLibrary(libsecp256k1);
const run_example_test = b.addRunArtifact(example_tests);

// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_example_test.step);
}
17 changes: 5 additions & 12 deletions src/main.zig → src/examples.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const std = @import("std");
const secp256k1 = @import("secp256k1");
const secp256k1 = @import("./secp256k1.zig");

const Sha256 = std.crypto.hash.sha2.Sha256;
const rand = std.crypto.random;
pub fn generateKeypair() !void {

test "generateKeypair" {
const secp = try secp256k1.Secp256k1.genNew();
defer secp.deinit();

Expand All @@ -19,7 +21,7 @@ pub fn generateKeypair() !void {
}
}

pub fn signAndVerifyEcdsa() !void {
test "signAndVerifyEcdsa" {
const secp = try secp256k1.Secp256k1.genNew();
defer secp.deinit();

Expand All @@ -33,12 +35,3 @@ pub fn signAndVerifyEcdsa() !void {
const signature = try secp.signEcdsa(seckey, messageHash[0..32].*);
try secp.verifyEcdsa(signature, messageHash[0..32].*, pubkey);
}

pub fn main() !void {

// generate key pair example
try generateKeypair();

// ecdsa example
try signAndVerifyEcdsa();
}

0 comments on commit 022b409

Please sign in to comment.