Skip to content

Commit

Permalink
Complete getMemorySegmentAddresses impl for builtins and add unit t…
Browse files Browse the repository at this point in the history
…ests (keep-starknet-strange#478)

Complete getMemorySegmentAddresses impl for builtins and add unit tests
  • Loading branch information
tcoratger authored Mar 18, 2024
1 parent a98a222 commit 96f08a4
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
105 changes: 94 additions & 11 deletions src/vm/builtins/builtin_runner/builtin_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
}
Expand Down Expand Up @@ -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) };

Expand All @@ -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) };

Expand All @@ -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 = .{} };

Expand All @@ -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) };

Expand All @@ -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) };

Expand All @@ -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 = .{} };

Expand All @@ -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();
Expand All @@ -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 = .{};

Expand All @@ -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) };

Expand All @@ -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(),
);
}

0 comments on commit 96f08a4

Please sign in to comment.