Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

520 poseidon benchmark improve #528

Merged
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
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",
},
},
}
141 changes: 0 additions & 141 deletions src/poseidon_consts_gen.zig

This file was deleted.

2 changes: 2 additions & 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 Expand Up @@ -607,6 +608,7 @@ pub const BuiltinRunner = union(BuiltinName) {
return switch (self.*) {
.Output => 0,
.SegmentArena => |*segment_arena| segment_arena.n_input_cells_per_instance,
.Poseidon => PoseidonBuiltinRunner.INPUT_CELLS_PER_POSEIDON,
inline else => |*builtin| builtin.n_input_cells,
};
}
Expand Down
38 changes: 16 additions & 22 deletions src/vm/builtins/builtin_runner/poseidon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ const expectEqualSlices = std.testing.expectEqualSlices;
pub const PoseidonBuiltinRunner = struct {
const Self = @This();

pub const INPUT_CELLS_PER_POSEIDON = poseidon_instance_def.INPUT_CELLS_PER_POSEIDON;

/// Base
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 @@ -157,7 +155,7 @@ pub const PoseidonBuiltinRunner = struct {

// Calculate the expected stop pointer value based on the number of used instances.
const stop_ptr = stop_pointer.offset;
if (stop_ptr != try self.getUsedInstances(segments) * self.cells_per_instance)
if (stop_ptr != try self.getUsedInstances(segments) * poseidon_instance_def.CELLS_PER_POSEIDON)
return RunnerError.InvalidStopPointer;

// Set the stop pointer and return the address of the stop pointer.
Expand Down Expand Up @@ -201,7 +199,7 @@ pub const PoseidonBuiltinRunner = struct {
return std.math.divCeil(
usize,
try self.getUsedCells(segments),
self.cells_per_instance,
6,
);
}

Expand All @@ -226,32 +224,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 +254,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 @@ -869,8 +869,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
Loading