Skip to content

Commit

Permalink
crs/msm: optimizations
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed Sep 21, 2023
1 parent a828a95 commit bf51871
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/bench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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});
Expand All @@ -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))});
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/crs/msm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ..];
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit bf51871

Please sign in to comment.