Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Add a script & results for the Zig standard library #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ For a total of 12 test vectors.
- [ref10 from SUPERCOP through Python bindings](https://github.com/warner/python-ed25519) : in `scripts/python-ed25519.py`
- [tweetnacl](https://www.npmjs.com/package/tweetnacl) version 1.0.3 : in `scripts/tweetnacl`
- [Zebra](https://github.com/ZcashFoundation/ed25519-zebra) : in unit tests
- [Zig](https://ziglang.org) : in `scripts/zig`

## Results

Expand All @@ -109,6 +110,7 @@ For a total of 12 test vectors.
|ref10 | V | V | V | V | X | X | V | X | X | X | X | V |
|TweetNaCl-js | V | V | V | V | X | X | V | V | X | X | X | V |
|Zebra | V | V | V | V | V | V | X | X | X | V | V | V |
|Zig | X | X | V | V | V | V | X | X | X | X | X | X |
---------------------------------------------------------------
```

Expand All @@ -127,3 +129,4 @@ License
-------

This project is [Apache 2.0 licensed](./LICENSE).

1 change: 1 addition & 0 deletions results.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
|ref10 | V | V | V | V | X | X | V | X | X | X | X | V |
|TweetNaCl-js | V | V | V | V | X | X | V | V | X | X | X | V |
|Zebra | V | V | V | V | V | V | X | X | X | V | V | V |
|Zig | X | X | V | V | V | V | X | X | X | X | X | X |
7 changes: 7 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ fi

./test_script.sh
popd

# zig
if command -v zig &> /dev/null; then
pushd "$SOURCE_DIR/scripts/zig"
zig build run < ../../cases.txt
popd
fi
}

main > results.md 2>/dev/null
Expand Down
21 changes: 21 additions & 0 deletions scripts/zig/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});

const mode = b.standardReleaseOptions();

const exe = b.addExecutable("zig-edspeccheck", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();

const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
37 changes: 37 additions & 0 deletions scripts/zig/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");
const fmt = std.fmt;
const io = std.io;
const Ed25519 = std.crypto.sign.Ed25519;

pub fn main() !void {
var reader = io.getStdIn().reader();
var bw = io.bufferedWriter(io.getStdOut().writer());
var out = bw.writer();

var count_buf: [4]u8 = undefined;
const count = try fmt.parseInt(usize, try reader.readUntilDelimiter(&count_buf, '\n'), 10);

var msg: [32]u8 = undefined;
var pbk: [32]u8 = undefined;
var sig: [64]u8 = undefined;
var msg_hex_buf: [4 + msg.len * 2 + 1]u8 = undefined;
var pbk_hex_buf: [4 + pbk.len * 2 + 1]u8 = undefined;
var sig_hex_buf: [4 + sig.len * 2 + 1]u8 = undefined;

try out.writeAll("\n|Zig |");
var i: usize = 0;
while (i < count) : (i += 1) {
_ = try fmt.hexToBytes(&msg, (try reader.readUntilDelimiter(&msg_hex_buf, '\n'))[4..]);
_ = try fmt.hexToBytes(&pbk, (try reader.readUntilDelimiter(&pbk_hex_buf, '\n'))[4..]);
_ = try fmt.hexToBytes(&sig, (try reader.readUntilDelimiterOrEof(&sig_hex_buf, '\n')).?[4..]);
try out.writeAll(if (verify(msg, pbk, sig)) " V |" else |_| " X |");
}
try out.writeByte('\n');
try bw.flush();
}

fn verify(msg: [32]u8, pbk_bytes: [32]u8, sig_bytes: [64]u8) !void {
const pbk = try Ed25519.PublicKey.fromBytes(pbk_bytes);
const sig = Ed25519.Signature.fromBytes(sig_bytes);
try sig.verify(&msg, pbk);
}