Something went wrong. Try again.
High-speed cryptography library in Zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125const std = @import("std");
const Inputs = struct { algorithm: []const u8, numberOfTests: u32, testGroups: []const Group,
const Group = struct { type: []const u8, publicKey: PublicKey, tests: []const Test,
const PublicKey = struct { keySize: u32, pk: []const u8, };
const Test = struct { tcId: u32, comment: []const u8,
msg: []const u8, sig: []const u8,
result: []const u8, }; };};
pub fn main() !void { const allocator = std.heap.smp_allocator;
const args = try std.process.argsAlloc(allocator); defer std.process.argsFree(allocator, args);
if (args.len != 3) @panic("gen_wycheproof [path to test case json] [path to output zig]");
const proof_path = args[1]; const output_path = args[2];
const contents = try std.fs.cwd().readFileAlloc(allocator, proof_path, 1 * 1024 * 1024); defer allocator.free(contents);
const parsed: Inputs = try std.json.parseFromSliceLeaky( Inputs, allocator, contents, .{ .ignore_unknown_fields = true }, );
const output_file = try std.fs.cwd().createFile(output_path, .{}); defer output_file.close();
var buffer: [4096]u8 = undefined; var output_writer = output_file.writer(&buffer); const writer = &output_writer.interface;
try writer.print( \\//! DO NOT EDIT! File generated by `gen_wycheproof.zig`. \\//! algorithm: {s} \\//! numberOfTests: {d} \\ , .{ parsed.algorithm, parsed.numberOfTests });
try writer.writeAll( \\ \\const Group = struct { \\ pubkey: []const u8, \\ cases: []const Case, \\ \\ const Case = struct { \\ msg: []const u8, \\ sig: []const u8, \\ expected: enum { valid, invalid }, \\ }; \\}; \\ \\pub const groups: []const Group = &.{ \\ );
for (parsed.testGroups) |group| { try writer.print( \\ .{{ \\ .pubkey = "{s}", \\ .cases = &.{{ \\ , .{group.publicKey.pk});
for (group.tests) |case| { if (case.comment.len != 0) try writer.print( \\ // {s} \\ , .{std.mem.trimRight(u8, case.comment, &.{' '})});
const expected = if (std.mem.eql(u8, case.result, "invalid")) "invalid" else if (std.mem.eql(u8, case.result, "valid")) "valid" else std.debug.panic("unknown result: '{s}'", .{case.result});
try writer.print( \\ .{{ .msg = "{s}", .sig = "{s}", .expected = .{s} }}, \\ , .{ case.msg, case.sig, expected, }); }
try writer.writeAll( \\ }, \\ }, \\ ); }
try writer.writeAll( \\}; \\ );
try writer.flush();}