From 96f08a4581f7c70d447ccacfe9b3233286c9dc1d Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Mon, 18 Mar 2024 13:13:26 +0100 Subject: [PATCH] Complete `getMemorySegmentAddresses` impl for builtins and add unit tests (#478) Complete getMemorySegmentAddresses impl for builtins and add unit tests --- src/lib.zig | 11 ++ .../builtin_runner/builtin_runner.zig | 105 ++++++++++++++++-- 2 files changed, 105 insertions(+), 11 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index 2589f386..21349cdc 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -22,6 +22,17 @@ pub const vm = struct { pub usingnamespace @import("vm/types/layout.zig"); pub usingnamespace @import("vm/types/execution_scopes.zig"); pub usingnamespace @import("vm/decoding/decoder.zig"); + + pub usingnamespace @import("vm/builtins/builtin_runner/bitwise.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/builtin_runner.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/ec_op.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/hash.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/keccak.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/output.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/poseidon.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/range_check.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/segment_arena.zig"); + pub usingnamespace @import("vm/builtins/builtin_runner/signature.zig"); }; pub const math = struct { diff --git a/src/vm/builtins/builtin_runner/builtin_runner.zig b/src/vm/builtins/builtin_runner/builtin_runner.zig index a0d4be3d..17a8d930 100644 --- a/src/vm/builtins/builtin_runner/builtin_runner.zig +++ b/src/vm/builtins/builtin_runner/builtin_runner.zig @@ -298,9 +298,8 @@ pub const BuiltinRunner = union(BuiltinName) { /// - The starting address of the memory segment. /// - An optional stop address of the memory segment (may be `null`). pub fn getMemorySegmentAddresses(self: *Self) Tuple(&.{ usize, ?usize }) { - // TODO: fill-in missing builtins when implemented return switch (self.*) { - .Signature, .SegmentArena => .{ 0, 0 }, + .SegmentArena => .{ 0, 0 }, inline else => |*builtin| builtin.getMemorySegmentAddresses(), }; } @@ -1637,7 +1636,7 @@ test "BuiltinRunner: builtin name function" { try expectEqualStrings("output_builtin", output.name()); } -test "BuiltinRunner:getNumberInputCells with output builtin" { +test "BuiltinRunner: getNumberInputCells with output builtin" { // Initialize a BuiltinRunner union with the OutputBuiltinRunner variant var builtin: BuiltinRunner = .{ .Output = OutputBuiltinRunner.initDefault(std.testing.allocator) }; @@ -1648,7 +1647,7 @@ test "BuiltinRunner:getNumberInputCells with output builtin" { ); } -test "BuiltinRunner:getNumberInputCells with segment arena builtin" { +test "BuiltinRunner: getNumberInputCells with segment arena builtin" { // Initialize a BuiltinRunner union with the SegmentArenaBuiltinRunner variant var builtin: BuiltinRunner = .{ .SegmentArena = SegmentArenaBuiltinRunner.init(true) }; @@ -1659,7 +1658,7 @@ test "BuiltinRunner:getNumberInputCells with segment arena builtin" { ); } -test "BuiltinRunner:getNumberInputCells with bitwise builtin" { +test "BuiltinRunner: getNumberInputCells with bitwise builtin" { // Initialize a BuiltinRunner union with the BitwiseBuiltinRunner variant var builtin: BuiltinRunner = .{ .Bitwise = .{} }; @@ -1670,7 +1669,7 @@ test "BuiltinRunner:getNumberInputCells with bitwise builtin" { ); } -test "BuiltinRunner:getNumberInputCells with elliptic curve operations builtin" { +test "BuiltinRunner: getNumberInputCells with elliptic curve operations builtin" { // Initialize a BuiltinRunner union with the EcOpBuiltinRunner variant var builtin: BuiltinRunner = .{ .EcOp = EcOpBuiltinRunner.initDefault(std.testing.allocator) }; @@ -1681,7 +1680,7 @@ test "BuiltinRunner:getNumberInputCells with elliptic curve operations builtin" ); } -test "BuiltinRunner:getNumberInputCells with hash builtin" { +test "BuiltinRunner: getNumberInputCells with hash builtin" { // Initialize a BuiltinRunner union with the HashBuiltinRunner variant var builtin: BuiltinRunner = .{ .Hash = HashBuiltinRunner.init(std.testing.allocator, 256, true) }; @@ -1692,7 +1691,7 @@ test "BuiltinRunner:getNumberInputCells with hash builtin" { ); } -test "BuiltinRunner:getNumberInputCells with range check builtin" { +test "BuiltinRunner: getNumberInputCells with range check builtin" { // Initialize a BuiltinRunner union with the RangeCheckBuiltinRunner variant var builtin: BuiltinRunner = .{ .RangeCheck = .{} }; @@ -1703,7 +1702,7 @@ test "BuiltinRunner:getNumberInputCells with range check builtin" { ); } -test "BuiltinRunner:getNumberInputCells with keccak builtin" { +test "BuiltinRunner: getNumberInputCells with keccak builtin" { // Initialize a default Keccak instance definition. var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator); defer keccak_instance_def.deinit(); @@ -1727,7 +1726,7 @@ test "BuiltinRunner:getNumberInputCells with keccak builtin" { ); } -test "BuiltinRunner:getNumberInputCells with signature builtin" { +test "BuiltinRunner: getNumberInputCells with signature builtin" { // Initialize a default ECDSA instance definition. var ecdsa_instance_def = .{}; @@ -1744,7 +1743,7 @@ test "BuiltinRunner:getNumberInputCells with signature builtin" { ); } -test "BuiltinRunner:getNumberInputCells with poseidon builtin" { +test "BuiltinRunner: getNumberInputCells with poseidon builtin" { // Initialize a BuiltinRunner union with the PoseidonBuiltinRunner variant var builtin: BuiltinRunner = .{ .Poseidon = PoseidonBuiltinRunner.init(std.testing.allocator, 256, true) }; @@ -1754,3 +1753,87 @@ test "BuiltinRunner:getNumberInputCells with poseidon builtin" { builtin.getNumberInputCells(), ); } + +test "BuiltinRunner: getMemorySegmentAddresses" { + // Instantiate a `BuiltinRunner` for the Bitwise builtin. + var bitwise_builtin: BuiltinRunner = .{ + .Bitwise = BitwiseBuiltinRunner.init(&.{}, true), + }; + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + bitwise_builtin.getMemorySegmentAddresses(), + ); + + // Instantiate a `BuiltinRunner` for the EcOp builtin. + var ec_op_builtin: BuiltinRunner = .{ + .EcOp = EcOpBuiltinRunner.initDefault(std.testing.allocator), + }; + defer ec_op_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + ec_op_builtin.getMemorySegmentAddresses(), + ); + + // Instantiate a `BuiltinRunner` for the Hash builtin. + var hash_builtin: BuiltinRunner = .{ + .Hash = HashBuiltinRunner.init(std.testing.allocator, 8, true), + }; + defer hash_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + hash_builtin.getMemorySegmentAddresses(), + ); + + // Instantiate a `BuiltinRunner` for the Output builtin. + var output_builtin: BuiltinRunner = .{ + .Output = OutputBuiltinRunner.init(std.testing.allocator, true), + }; + defer output_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + output_builtin.getMemorySegmentAddresses(), + ); + + // Instantiate a `BuiltinRunner` for the RangeCheck builtin. + var range_check_builtin: BuiltinRunner = .{ + .RangeCheck = RangeCheckBuiltinRunner.init(8, 8, true), + }; + defer range_check_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + range_check_builtin.getMemorySegmentAddresses(), + ); + + // Initialize a Keccak instance definition. + var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator); + defer keccak_instance_def.deinit(); + // Instantiate a `BuiltinRunner` for the Keccak builtin. + var keccak_builtin: BuiltinRunner = .{ + .Keccak = try KeccakBuiltinRunner.init(std.testing.allocator, &keccak_instance_def, true), + }; + defer keccak_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + keccak_builtin.getMemorySegmentAddresses(), + ); + + // Instantiate a `BuiltinRunner` for the Signature builtin. + var signature_builtin: BuiltinRunner = .{ + .Signature = SignatureBuiltinRunner.init(std.testing.allocator, &.{}, true), + }; + defer signature_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + signature_builtin.getMemorySegmentAddresses(), + ); + + // Instantiate a `BuiltinRunner` for the Poseidon builtin. + var poseidon_builtin: BuiltinRunner = .{ + .Poseidon = PoseidonBuiltinRunner.init(std.testing.allocator, 256, true), + }; + defer poseidon_builtin.deinit(); + try expectEqual( + Tuple(&.{ usize, ?usize }){ 0, null }, + poseidon_builtin.getMemorySegmentAddresses(), + ); +}