Skip to content

Commit

Permalink
simplification of BuiltinRunner switch statements (keep-starknet-st…
Browse files Browse the repository at this point in the history
…range#358)

simplification of BuiltinRunner switch statements

Co-authored-by: lanaivina <[email protected]>
  • Loading branch information
tcoratger and lana-shanghai authored Feb 7, 2024
1 parent 0f686c1 commit fcbf210
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 47 deletions.
51 changes: 9 additions & 42 deletions src/vm/builtins/builtin_runner/builtin_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,8 @@ pub const BuiltinRunner = union(enum) {
/// The base value as a `usize`.
pub fn base(self: *const Self) usize {
return switch (self.*) {
.Bitwise => |*bitwise| bitwise.base,
.EcOp => |*ec| ec.base,
.Hash => |*hash| hash.base,
.Output => |*output| output.base,
.RangeCheck => |*range_check| range_check.base,
.Keccak => |*keccak| keccak.base,
.Signature => |*signature| signature.base,
.Poseidon => |*poseidon| poseidon.base,
.SegmentArena => |*segment_arena| @as(usize, @intCast(segment_arena.base.segment_index)),
.SegmentArena => |*segment_arena| @intCast(segment_arena.base.segment_index),
inline else => |*case| case.base,
};
}

Expand All @@ -96,15 +89,7 @@ pub const BuiltinRunner = union(enum) {
/// - `segments`: A pointer to the MemorySegmentManager managing memory segments.
pub fn initSegments(self: *Self, segments: *MemorySegmentManager) !void {
switch (self.*) {
.Bitwise => |*bitwise| try bitwise.initSegments(segments),
.EcOp => |*ec| try ec.initSegments(segments),
.Hash => |*hash| try hash.initSegments(segments),
.Output => |*output| try output.initSegments(segments),
.RangeCheck => |*range_check| try range_check.initSegments(segments),
.Keccak => |*keccak| try keccak.initSegments(segments),
.Signature => |*signature| try signature.initSegments(segments),
.Poseidon => |*poseidon| try poseidon.initSegments(segments),
.SegmentArena => |*segment_arena| try segment_arena.initSegments(segments),
inline else => |*case| try case.initSegments(segments),
}
}

Expand All @@ -115,15 +100,7 @@ pub const BuiltinRunner = union(enum) {
/// - `allocator`: The allocator to initialize the ArrayList.
pub fn initialStack(self: *Self, allocator: Allocator) !ArrayList(MaybeRelocatable) {
return switch (self.*) {
.Bitwise => |*bitwise| try bitwise.initialStack(allocator),
.EcOp => |*ec| try ec.initialStack(allocator),
.Hash => |*hash| try hash.initialStack(allocator),
.Output => |*output| try output.initialStack(allocator),
.RangeCheck => |*range_check| try range_check.initialStack(allocator),
.Keccak => |*keccak| try keccak.initialStack(allocator),
.Signature => |*signature| try signature.initialStack(allocator),
.Poseidon => |*poseidon| try poseidon.initialStack(allocator),
.SegmentArena => |*segment_arena| try segment_arena.initialStack(allocator),
inline else => |*case| try case.initialStack(allocator),
};
}

Expand All @@ -146,15 +123,12 @@ pub const BuiltinRunner = union(enum) {
memory: *Memory,
) !?MaybeRelocatable {
return switch (self.*) {
.Bitwise => |bitwise| try bitwise.deduceMemoryCell(address, memory),
.EcOp => |*ec| try ec.deduceMemoryCell(allocator, address, memory),
.Hash => |*hash| try hash.deduceMemoryCell(address, memory),
.Output => |output| output.deduceMemoryCell(address, memory),
.RangeCheck => |range_check| range_check.deduceMemoryCell(address, memory),
.Keccak => |*keccak| try keccak.deduceMemoryCell(allocator, address, memory),
.Signature => |signature| signature.deduceMemoryCell(address, memory),
.Poseidon => |*poseidon| try poseidon.deduceMemoryCell(allocator, address, memory),
.SegmentArena => |segment_arena| segment_arena.deduceMemoryCell(address, memory),
.Bitwise => |bitwise| try bitwise.deduceMemoryCell(address, memory),
.Hash => |*hash| try hash.deduceMemoryCell(address, memory),
inline else => |*case| case.deduceMemoryCell(address, memory),
};
}

Expand All @@ -172,15 +146,8 @@ pub const BuiltinRunner = union(enum) {
pub fn getMemorySegmentAddresses(self: *Self) Tuple(&.{ usize, ?usize }) {
// TODO: fill-in missing builtins when implemented
return switch (self.*) {
.Bitwise => |*bitwise| bitwise.getMemorySegmentAddresses(),
.EcOp => |*ec| ec.getMemorySegmentAddresses(),
.Hash => |*hash| hash.getMemorySegmentAddresses(),
.Output => |*output| output.getMemorySegmentAddresses(),
.RangeCheck => |*range_check| range_check.getMemorySegmentAddresses(),
.Keccak => |*keccak| keccak.getMemorySegmentAddresses(),
.Signature => .{ 0, 0 },
.Poseidon => |*poseidon| poseidon.getMemorySegmentAddresses(),
.SegmentArena => .{ 0, 0 },
.Signature, .SegmentArena => .{ 0, 0 },
inline else => |*case| case.getMemorySegmentAddresses(),
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/vm/builtins/builtin_runner/hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ pub const HashBuiltinRunner = struct {
0,
),
});
return result;
}
return result;
}
Expand Down Expand Up @@ -504,7 +503,6 @@ test "HashBuiltinRunner: deduce memory cell pedersen for preset memory already c
defer memory_segment_manager.deinit();
defer memory_segment_manager.memory.deinitData(std.testing.allocator);


try memory_segment_manager.memory.setUpMemory(std.testing.allocator, .{
.{ .{ 0, 3 }, .{32} },
.{ .{ 0, 4 }, .{72} },
Expand Down
1 change: 0 additions & 1 deletion src/vm/builtins/builtin_runner/keccak.zig
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ pub const KeccakBuiltinRunner = struct {
@intCast(self.base),
0,
));
return result;
}
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/vm/builtins/builtin_runner/range_check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ pub const RangeCheckBuiltinRunner = struct {
0,
),
});
return result;
}
return result;
}
Expand Down Expand Up @@ -234,7 +233,10 @@ pub const RangeCheckBuiltinRunner = struct {
return null;
}
const rc_segment = memory.data.items[self.base];
var rc_bounds = if (rc_segment.capacity > 0) [_]usize{ std.math.maxInt(usize), std.math.minInt(usize) } else return null;
var rc_bounds = if (rc_segment.capacity > 0)
[_]usize{ std.math.maxInt(usize), std.math.minInt(usize) }
else
return null;

for (rc_segment.items) |cell| {
var cellFelt = cell.?.maybe_relocatable.tryIntoFelt() catch null;
Expand Down

0 comments on commit fcbf210

Please sign in to comment.