Skip to content

Commit

Permalink
Draft: All nuts types + methods + hashing algos of btc (#5)
Browse files Browse the repository at this point in the history
* bech32 impl

* bech32 decoder/encoder

* recover id + recoverable signature for secp256k1

* very raw lnbit backend and NUT04

* rm test

* nut-04 without testing

* RIPEMD 160 BIP32 secp256k1 updates

* base nuts types implemented

* remove old code + bip39 min impl

* add test to github step, fix benchmarks

* add bip39 mnemonic to nut13
  • Loading branch information
StringNick authored Sep 2, 2024
1 parent 6fe5ba6 commit a95defd
Show file tree
Hide file tree
Showing 61 changed files with 10,582 additions and 943 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ jobs:
- name: Build and Run
run: zig build run -- info

- name: Unit testing
run: zig build test --summary all

- name: Run benchmarks
run: zig build bench -Doptimize=ReleaseFast
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ Cargo.lock
**/.zig-cache
**/zig-out

**/benchmark_report.csv
**/benchmark_report.csv


.vscode

**/.DS_Store
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

Coconut 🥥 is a Cashu Wallet and Mint implementation in Zig.

## Test
```sh
zig build test --summary all
```

## Build

```sh
Expand Down
49 changes: 47 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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 = "zig-libsecp256k1", .target = target, .optimize = optimize });
const lib = b.addStaticLibrary(.{ .name = "libsecp", .target = target, .optimize = optimize });

lib.addIncludePath(b.path("libsecp256k1/"));
lib.addIncludePath(b.path("libsecp256k1/src"));
Expand All @@ -21,6 +21,9 @@ fn buildSecp256k1(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std
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");
Expand Down Expand Up @@ -84,6 +87,16 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});

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

const base58_module = b.dependency("base58-zig", .{
.target = target,
.optimize = optimize,
}).module("base58-zig");

// **************************************************************
// * COCONUT AS A LIBRARY *
// **************************************************************
Expand All @@ -103,6 +116,34 @@ pub fn build(b: *std.Build) !void {
// running `zig build`).
b.installArtifact(lib);

// **************************************************************
// * CHECK STEP AS AN EXECUTABLE *
// **************************************************************
// for lsp build on save step
{
const exe = b.addExecutable(.{
.name = "coconut-mint",
.root_source_file = b.path("src/mint.zig"),
.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);

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

// These two lines you might want to copy
// (make sure to rename 'exe_check')
const check = b.step("check", "Check if foo compiles");
check.dependOn(&exe.step);
}

// **************************************************************
// * COCONUT-MINT AS AN EXECUTABLE *
// **************************************************************
Expand All @@ -115,6 +156,7 @@ pub fn build(b: *std.Build) !void {
});
exe.linkLibrary(libsecp256k1);
exe.root_module.addImport("httpz", httpz_module);
exe.root_module.addImport("zul", zul);
exe.root_module.addImport("pg", pg.module("pg"));

// Add dependency modules to the executable.
Expand Down Expand Up @@ -172,6 +214,9 @@ pub fn build(b: *std.Build) !void {
.optimize = optimize,
});
lib_unit_tests.linkLibrary(libsecp256k1);
lib_unit_tests.root_module.addImport("zul", zul);
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);

Expand All @@ -196,7 +241,7 @@ pub fn build(b: *std.Build) !void {
});

bench.linkLibrary(libsecp256k1);
bench.root_module.addImport("zul", b.dependency("zul", .{}).module("zul"));
bench.root_module.addImport("zul", zul);

const run_bench = b.addRunArtifact(bench);

Expand Down
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
.url = "https://github.com/karlseguin/pg.zig/archive/1491270ac43c7eba91992bb06b3758254c36e39a.zip",
.hash = "1220bcc68967188de7ad5d520a4629c0d1e169c111d87e6978a3c128de5ec2b6bdd0",
},
.@"base58-zig" = .{
.url = "https://github.com/ultd/base58-zig/archive/e1001fbe8b41eed36d81e37931ada66b784e51dc.zip",
.hash = "12206e5050a03cd9dcb896781de0cf541081488006532675371653f61d00c1f27433",
},
},
.paths = .{
"build.zig",
Expand Down
Loading

0 comments on commit a95defd

Please sign in to comment.