Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Install Zig
uses: mlugg/setup-zig@v2
with:
version: "0.16.0" # Set the required Zig version
version: "0.17.1" # Set the required Zig version

- name: Build and Test
run: |
Expand Down
9 changes: 8 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const Translator = @import("translate_c").Translator;

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
Expand Down Expand Up @@ -31,7 +32,7 @@ pub fn build(b: *std.Build) void {

const os = target.result.os.tag;
const abi = target.result.abi;
if (target.result.cpu.arch.isArm() or target.result.cpu.arch.isAARCH64()) {
if (target.result.cpu.arch.isArm() or target.result.cpu.arch.isAarch64()) {
lib.root_module.addCSourceFiles(.{
.root = upstream.path("src"),
.files = &[_][]const u8{
Expand Down Expand Up @@ -85,6 +86,12 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
module.linkLibrary(lib);
const headers: Translator = .init(b.dependency("translate_c", .{}), .{
.c_source_file = upstream.path("src/hashtree.h"),
.target = target,
.optimize = optimize,
});
module.addImport("hashtree_c", headers.mod);

const mod_unit_tests = b.addTest(.{
.root_module = module,
Expand Down
6 changes: 5 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
.name = .hashtree,
.version = "0.1.0",
.dependencies = .{
.translate_c = .{
.url = "git+https://codeberg.org/ziglang/translate-c#cfac21f3199b2b02ce3fd7db652aa456421fe4ab",
.hash = "translate_c-2.0.2-Q_BUWvBNBwDnyehSiuAJmdFcMYHYGA-Jc3KiYdrV2Dqx",
},
.hashtree = .{
.url = "git+https://github.com/offchainlabs/hashtree#fba14494dada4420440d985c2c516341d9c24b9b",
.hash = "N-V-__8AAJ03FACfm8Hlfs97yP3-2Hd5ZzSv9yYuoEi3Yi_B",
},
},
.fingerprint = 0xf963e115bea813b0,
.minimum_zig_version = "0.16.0",
.minimum_zig_version = "0.17.1",
.paths = .{
"build.zig",
"build.zig.zon",
Expand Down
18 changes: 8 additions & 10 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");

const c = @cImport({
@cInclude("hashtree.h");
});
const c = @import("hashtree_c");

/// If hashtree is not supported, fallback to using the (cross-platform) std lib
fn fallback(out: [][32]u8, in: []const [32]u8) void {
Expand All @@ -17,15 +15,15 @@ fn fallback(out: [][32]u8, in: []const [32]u8) void {
}

fn isSupported() bool {
if (builtin.cpu.arch.isAARCH64()) {
if (builtin.target.cpu.arch.isAarch64()) {
return true;
}

// hashtree_hash selects SHA-NI, AVX-512, AVX2, AVX, SSE or generic C at runtime, so this must not depend on the
// compile-time CPU. The x86 backends are assembled on the same targets as in build.zig.
if (builtin.cpu.arch == .x86_64 and
builtin.os.tag != .macos and
!(builtin.os.tag == .windows and builtin.abi == .msvc))
if (builtin.target.cpu.arch == .x86_64 and
builtin.target.os.tag != .macos and
!(builtin.target.os.tag == .windows and builtin.target.abi == .msvc))
{
return true;
}
Expand All @@ -52,8 +50,8 @@ pub fn hash(out: [][32]u8, in: []const [32]u8) Error!void {
}

test {
const chunks: [2][32]u8 = [_][32]u8{[_]u8{0xAB} ** 32} ** 2;
var out: [1][32]u8 = [_][32]u8{[_]u8{0} ** 32};
const chunks: [2][32]u8 = @splat(@splat(0xAB));
var out: [1][32]u8 = @splat(@splat(0));
try hash(&out, &chunks);
const expected_hash: [32]u8 = [_]u8{
0xec, 0x65, 0xc8, 0x79, 0x8e, 0xcf, 0x95, 0x90, 0x24, 0x13, 0xc4, 0x0f, 0x7b, 0x9e,
Expand All @@ -65,7 +63,7 @@ test {
}

test "overlapping memory" {
var chunks: [50][32]u8 = [_][32]u8{[_]u8{0xAB} ** 32} ** 50;
var chunks: [50][32]u8 = @splat(@splat(0xAB));
var out: [25][32]u8 = undefined;
try hash(&out, &chunks);
try hash(chunks[0..25], &chunks);
Expand Down
Loading