diff --git a/src/bench.zig b/src/bench.zig index d0c4b32..4e180e8 100644 --- a/src/bench.zig +++ b/src/bench.zig @@ -8,12 +8,13 @@ const multiproof = @import("multiproof/multiproof.zig"); const polynomials = @import("polynomial/lagrange_basis.zig"); const ipa = @import("ipa/ipa.zig"); const Transcript = @import("ipa/transcript.zig"); +const precomp = @import("crs/msm.zig"); pub fn main() !void { - // benchFields(); + benchFields(); try benchPedersenHash(); - // try benchIPAs(); - // try benchMultiproofs(); + try benchIPAs(); + try benchMultiproofs(); } fn benchFields() void { @@ -47,7 +48,7 @@ fn benchFields() void { fn benchPedersenHash() !void { std.debug.print("Benchmarking Pedersen hashing...\n", .{}); const xcrs = crs.CRS.init(); - const N = 200; + const N = 500; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer { @@ -56,6 +57,9 @@ fn benchPedersenHash() !void { } var allocator = gpa.allocator(); + var precomp_msm = try precomp.PrecompMSM.init(allocator, &xcrs.Gs, 8); + defer precomp_msm.deinit(); + var vec_len: usize = 1; while (vec_len <= 256) : (vec_len <<= 1) { std.debug.print("\twith {} elements... ", .{vec_len}); @@ -75,7 +79,13 @@ fn benchPedersenHash() !void { for (0..N) |i| { _ = xcrs.commit(vecs[i]); } - std.debug.print("takes {}µs\n", .{@divTrunc((std.time.microTimestamp() - start), (N))}); + std.debug.print(" naive takes {}µs", .{@divTrunc((std.time.microTimestamp() - start), (N))}); + + start = std.time.microTimestamp(); + for (0..N) |i| { + _ = try precomp_msm.msm(vecs[i][0..vec_len]); + } + std.debug.print(", optimized takes {}µs\n", .{@divTrunc((std.time.microTimestamp() - start), (N))}); } } diff --git a/src/crs/msm.zig b/src/crs/msm.zig index 196c9e6..6b1f874 100644 --- a/src/crs/msm.zig +++ b/src/crs/msm.zig @@ -4,7 +4,7 @@ const banderwagon = @import("../banderwagon/banderwagon.zig"); const Element = banderwagon.Element; const Fr = banderwagon.Fr; -const PrecompMSM = struct { +pub const PrecompMSM = struct { allocator: Allocator, b: usize, basis_len: usize, @@ -47,7 +47,9 @@ const PrecompMSM = struct { const num_windows = self.basis_len / self.b; var accum = Element.identity(); for (0..253) |k| { - accum.double(accum); + if (k > 0) { + accum.double(accum); + } for (0..num_windows) |w| { if (w * self.b < scalars.len) { const window_scalars = scalars[w * self.b ..]; @@ -103,12 +105,8 @@ test "correctness" { } full_scalars[i] = Fr.zero(); } - std.debug.print("For {} ...", .{msm_length}); const exp = CRS.commit(full_scalars); - std.debug.print("ok", .{}); - const got = try precomp.msm(msm_scalars); - std.debug.print(" ok\n", .{}); try std.testing.expect(Element.equal(exp, got)); }