Skip to content

Commit

Permalink
poseidon benchmark impr
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed Jun 24, 2024
1 parent 6329127 commit d695174
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
.hash = "1220ab73fb7cc11b2308edc3364988e05efcddbcac31b707f55e6216d1b9c0da13f1",
},
.starknet = .{
.url = "https://github.com/StringNick/starknet-zig/archive/57810b7a64364f1bf12725ba823385c2a213bfa5.zip",
.hash = "1220d848be799ff21a80c6751c088ea619891ec450f20017cc7aa5cbbeb5904ae8b8",
.url = "https://github.com/StringNick/starknet-zig/archive/7d51aed59982146df3581d3d3320d509b0b10f54.zip",
.hash = "1220b00c055ce40da237598e0f1c8758e3434470be648816fa4701a09f683146d4cd",
},
},
}
1 change: 1 addition & 0 deletions src/vm/builtins/builtin_runner/builtin_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub const BuiltinRunner = union(BuiltinName) {
pub fn cellsPerInstance(self: *const BuiltinRunner) u32 {
return switch (self.*) {
.Output => 0,
.Poseidon => 6,
inline else => |*builtin| builtin.cells_per_instance,
};
}
Expand Down
34 changes: 13 additions & 21 deletions src/vm/builtins/builtin_runner/poseidon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ pub const PoseidonBuiltinRunner = struct {
base: usize = 0,
/// Ratio
ratio: ?u32,
/// Number of cells per instance
cells_per_instance: u32 = poseidon_instance_def.CELLS_PER_POSEIDON,
/// Number of input cells
n_input_cells: u32 = poseidon_instance_def.INPUT_CELLS_PER_POSEIDON,
/// Stop pointer
stop_ptr: ?usize = null,
/// Included boolean flag
Expand Down Expand Up @@ -201,7 +197,7 @@ pub const PoseidonBuiltinRunner = struct {
return std.math.divCeil(
usize,
try self.getUsedCells(segments),
self.cells_per_instance,
6,
);
}

Expand All @@ -226,32 +222,27 @@ pub const PoseidonBuiltinRunner = struct {
address: Relocatable,
memory: *Memory,
) !?MaybeRelocatable {
_ = allocator; // autofix
// Calculate the index of the memory cell.
const index = @mod(
const index: usize = @mod(
@as(usize, @intCast(address.offset)),
@as(usize, @intCast(self.cells_per_instance)),
poseidon_instance_def.CELLS_PER_POSEIDON,
);

// Check if the index corresponds to an input cell, if so, return null.
if (index < self.n_input_cells) return null;
if (index < poseidon_instance_def.INPUT_CELLS_PER_POSEIDON) return null;

// Check if the cell value is already cached, if so, return it.
if (self.cache.get(address)) |felt| return .{ .felt = felt };

// Calculate the addresses for the first input cell and first output cell.
const first_input_addr = try address.subUint(index);
const first_output_addr = try first_input_addr.addUint(self.n_input_cells);
const first_output_addr = try first_input_addr.addUint(poseidon_instance_def.INPUT_CELLS_PER_POSEIDON);

// Initialize an array list to store input cell values.
var input_felts = try ArrayList(Felt252).initCapacity(allocator, self.n_input_cells);
defer input_felts.deinit();

// Iterate over input cells, retrieve their values, and append them to the array list.
for (0..self.n_input_cells) |i| {
const val = memory.get(try first_input_addr.addUint(i)) orelse return null;
try input_felts.append(val.intoFelt() catch
return RunnerError.BuiltinExpectedInteger);
}
var input_felts = memory.getFeltRange(first_input_addr, poseidon_instance_def.INPUT_CELLS_PER_POSEIDON) catch return RunnerError.BuiltinExpectedInteger;
defer input_felts.deinit();

// Perform Poseidon permutation computation on the input cells.
// TODO: optimize to use pointer on state
Expand All @@ -261,11 +252,12 @@ pub const PoseidonBuiltinRunner = struct {

PoseidonHasher.permuteComp();

@memcpy(input_felts.items[0..3], PoseidonHasher.state[0..3]);

// Iterate over input cells and cache their computed values.
for (0..self.n_input_cells, input_felts.items) |i, elem| {
try self.cache.put(try first_output_addr.addUint(i), elem);
inline for (0..3) |i| {
try self.cache.put(
try first_output_addr.addUint(i),
PoseidonHasher.state[i],
);
}

// Return the cached value for the specified memory cell address.
Expand Down
12 changes: 10 additions & 2 deletions src/vm/memory/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,16 @@ pub const Memory = struct {
);
errdefer values.deinit();

for (0..size) |i| {
try values.append(try self.getFelt(try address.addUint(i)));
var segment = self.getSegmentAtIndex(address.segment_index) orelse return MemoryError.UnknownMemoryCell;
if (segment.len < address.offset + size) return MemoryError.UnknownMemoryCell;

for (segment[address.offset .. address.offset + size]) |cell| {
if (cell.getValue()) |mr| {
switch (mr) {
inline .felt => |f| values.appendAssumeCapacity(f),
inline else => return MemoryError.ExpectedInteger,
}
} else return MemoryError.UnknownMemoryCell;
}

return values;
Expand Down
2 changes: 1 addition & 1 deletion src/vm/run_context.zig
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub const RunContext = struct {
try base_addr.subUint(@abs(instruction.off_0))
else
// Convert i16 to u64 safely
try base_addr.addUint(@bitCast(instruction.off_0));
try base_addr.addUint(@intCast(instruction.off_0));
}

/// Compute OP 0 address for a given instruction.
Expand Down

0 comments on commit d695174

Please sign in to comment.