Skip to content

Commit 1f93298

Browse files
committed
upgrade to zig 0.16.0
1 parent 518a137 commit 1f93298

6 files changed

Lines changed: 43 additions & 17 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ Cargo.lock
2222

2323
# Zig specific path
2424
zig-out/
25+
zig-pkg/
2526
.zig-cache/
2627

2728
# OS-specific files
2829
**.DS_Store
2930

3031
# Misc
3132
.vscode/
33+
.tool-versions

build.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ fn buildSecp256k1(libsecp_c: *std.Build.Dependency, b: *std.Build, target: std.B
1111
}),
1212
});
1313

14-
lib.addIncludePath(libsecp_c.path(""));
15-
lib.addIncludePath(libsecp_c.path("src"));
16-
lib.addIncludePath(libsecp_c.path("include"));
14+
lib.root_module.addIncludePath(libsecp_c.path(""));
15+
lib.root_module.addIncludePath(libsecp_c.path("src"));
16+
lib.root_module.addIncludePath(libsecp_c.path("include"));
1717

18-
lib.addCSourceFiles(.{
18+
lib.root_module.addCSourceFiles(.{
1919
.root = libsecp_c.path(""),
2020
.flags = &.{
2121
"-DENABLE_MODULE_RECOVERY=1",
@@ -38,7 +38,7 @@ fn buildSecp256k1(libsecp_c: *std.Build.Dependency, b: *std.Build, target: std.B
3838

3939
lib.installHeadersDirectory(libsecp_c.path("src"), "", .{ .include_extensions = &.{".h"} });
4040
lib.installHeadersDirectory(libsecp_c.path("include/"), "", .{ .include_extensions = &.{".h"} });
41-
lib.linkLibC();
41+
lib.root_module.link_libc = true;
4242

4343
return lib;
4444
}
@@ -82,7 +82,7 @@ pub fn build(b: *std.Build) !void {
8282
.target = target,
8383
}),
8484
});
85-
lib_unit_tests.linkLibrary(libsecp256k1);
85+
lib_unit_tests.root_module.linkLibrary(libsecp256k1);
8686
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
8787

8888
const example_tests = b.addTest(.{
@@ -91,7 +91,7 @@ pub fn build(b: *std.Build) !void {
9191
.target = target,
9292
}),
9393
});
94-
example_tests.linkLibrary(libsecp256k1);
94+
example_tests.root_module.linkLibrary(libsecp256k1);
9595
const run_example_test = b.addRunArtifact(example_tests);
9696

9797
// Similar to creating the run step earlier, this exposes a `test` step to

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
.name = .secp256k1,
1010
.fingerprint = 0xed75d6d9e762dcbe,
1111
.version = "0.0.3",
12-
.minimum_zig_version = "0.15.2",
12+
.minimum_zig_version = "0.16.0",
1313
.dependencies = .{
1414
.libsecp256k1 = .{
15-
.url = "https://github.com/bitcoin-core/secp256k1/archive/refs/tags/v0.5.1.tar.gz",
16-
.hash = "1220bb683a6df744e618f58a008eaae3eb62b70a78334cec676bd82b1b9e8e944eeb",
15+
.url = "https://github.com/bitcoin-core/secp256k1/archive/refs/tags/v0.7.1.tar.gz",
16+
.hash = "N-V-__8AAO1SWwCbwhTAU7t6L3XQtTy6TzE8CiOGHjENn_rl",
1717
},
1818
},
1919
.paths = .{

src/ecdsa/ecdsa.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ test "test sign ecdsa with nonce data" {
307307
// Generate a random private key (this would normally come from a wallet or similar)
308308
var private_key_data: [32]u8 = undefined;
309309

310-
std.crypto.random.bytes(&private_key_data);
310+
secp.secureRandomBytes(&private_key_data);
311311

312312
const sk = try secp.SecretKey.fromSlice(&private_key_data);
313313

@@ -320,7 +320,7 @@ test "test sign ecdsa with nonce data" {
320320
// Generate a nonce manually for this test
321321
var nonce_data: [32]u8 = undefined;
322322

323-
std.crypto.random.bytes(&nonce_data);
323+
secp.secureRandomBytes(&nonce_data);
324324

325325
// Sign the message using the private key and nonce
326326
const signature = s.signEcdsaWithNoncedata(&message, &sk, nonce_data);

src/examples.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ const std = @import("std");
22
const secp256k1 = @import("./secp256k1.zig");
33

44
const Sha256 = std.crypto.hash.sha2.Sha256;
5-
const rand = std.crypto.random;
65

76
test "generateKeypair" {
87
const secp = secp256k1.Secp256k1.genNew();
98
defer secp.deinit();
109

10+
var csprng = secp256k1.defaultCsprng();
11+
const rand = csprng.random();
12+
1113
// First option:
1214
{
1315
const seckey, const pubkey = secp.generateKeypair(rand);
@@ -25,7 +27,8 @@ test "signAndVerifyEcdsa" {
2527
const secp = secp256k1.Secp256k1.genNew();
2628
defer secp.deinit();
2729

28-
const seckey = secp256k1.SecretKey.generateWithRandom(rand);
30+
var csprng = secp256k1.defaultCsprng();
31+
const seckey = secp256k1.SecretKey.generateWithRandom(csprng.random());
2932
const pubkey = secp256k1.PublicKey.fromSecretKey(secp, seckey);
3033

3134
var buf: [32]u8 = undefined;

src/secp256k1.zig

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ const schnorr_lib = @import("schnorr.zig");
66

77
pub const constants = @import("constants.zig");
88

9+
pub fn secureRandomBytes(buf: []u8) void {
10+
const posix = std.posix;
11+
const fd = posix.openatZ(std.c.AT.FDCWD, "/dev/urandom", .{ .ACCMODE = .RDONLY }, 0) catch |e|
12+
std.debug.panic("cannot open /dev/urandom: {s}", .{@errorName(e)});
13+
defer _ = std.c.close(fd);
14+
var off: usize = 0;
15+
while (off < buf.len) {
16+
const n = posix.read(fd, buf[off..]) catch |e|
17+
std.debug.panic("cannot read /dev/urandom: {s}", .{@errorName(e)});
18+
if (n == 0) std.debug.panic("unexpected EOF on /dev/urandom", .{});
19+
off += n;
20+
}
21+
}
22+
23+
pub fn defaultCsprng() std.Random.DefaultCsprng {
24+
var seed: [std.Random.DefaultCsprng.secret_seed_length]u8 = undefined;
25+
secureRandomBytes(&seed);
26+
return std.Random.DefaultCsprng.init(seed);
27+
}
28+
929
/// The main error type for this library.
1030
pub const Error = error{
1131
/// Signature failed verification.
@@ -205,7 +225,7 @@ pub const Secp256k1 = struct {
205225

206226
// Create 32 byte random seed.
207227
var seed: [32]u8 = undefined;
208-
crypto.random.bytes(&seed);
228+
secureRandomBytes(&seed);
209229

210230
const res = secp256k1.secp256k1_context_randomize(ctx, &seed);
211231
std.debug.assert(res == 1);
@@ -441,7 +461,7 @@ pub const SecretKey = struct {
441461
defer secp.deinit();
442462

443463
var aux: [32]u8 = undefined;
444-
std.crypto.random.bytes(&aux);
464+
secureRandomBytes(&aux);
445465

446466
return secp.signSchnorrHelper(&hash, try KeyPair.fromSecretKey(&secp, self), &aux);
447467
}
@@ -458,7 +478,8 @@ pub const SecretKey = struct {
458478

459479
/// Generate random [`SecretKey`] with default random
460480
pub fn generate() SecretKey {
461-
return generateWithRandom(std.crypto.random);
481+
var csprng = defaultCsprng();
482+
return generateWithRandom(csprng.random());
462483
}
463484

464485
pub fn fromString(data: []const u8) (Error || ErrorParseHex)!@This() {

0 commit comments

Comments
 (0)