Skip to content

Commit

Permalink
Add generic methods to BuiltinRunner implementation (keep-starknet-…
Browse files Browse the repository at this point in the history
…strange#387)

* some new general builtin implementations

* relocatable change fn names

* add builtin getAllocatedMemoryUnits implementation and tests

* add getRangeCheckUsage implementation for BuiltinRunner

* add getUsedCellsAndAllocatedSize implementations and tests

* add getUsedDilutedCheckUnits implementation

* add builtin unit tests for name function

* fix tests

---------

Co-authored-by: lanaivina <[email protected]>
  • Loading branch information
tcoratger and lana-shanghai authored Feb 27, 2024
1 parent a591101 commit a73e3d6
Show file tree
Hide file tree
Showing 21 changed files with 1,066 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/hint_processor/hint_processor_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ pub fn getOffsetValueReference(
///Computes the memory address of the ids variable indicated by the HintReference as a [Relocatable]
pub fn computeAddrFromReference(hint_reference: HintReference, hint_ap_tracking: ApTracking, vm: *CairoVM) ?Relocatable {
const offset1 = switch (hint_reference.offset1) {
.reference => getOffsetValueReference(vm, hint_reference, hint_ap_tracking, hint_reference.offset1).?.tryIntoRelocatable() catch unreachable,
.reference => getOffsetValueReference(vm, hint_reference, hint_ap_tracking, hint_reference.offset1).?.intoRelocatable() catch unreachable,
else => return null,
};

return switch (hint_reference.offset2) {
.reference => offset1.addFelt(getOffsetValueReference(vm, hint_reference, hint_ap_tracking, hint_reference.offset2).?.tryIntoFelt() catch unreachable) catch unreachable,
.reference => offset1.addFelt(getOffsetValueReference(vm, hint_reference, hint_ap_tracking, hint_reference.offset2).?.intoFelt() catch unreachable) catch unreachable,

.value => |val| offset1.addInt(val) catch unreachable,

Expand Down
11 changes: 4 additions & 7 deletions src/hint_processor/math_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,7 @@ test "MathHints: isPositive false" {
},
.{
.name = "is_positive",
.elems = &.{
null,
},
.elems = &.{null},
},
}, &vm);
defer ids_data.deinit();
Expand Down Expand Up @@ -1179,6 +1177,7 @@ test "MathHints: unsigned div rem incorrect ids" {
hint_processor.executeHint(std.testing.allocator, &vm, &hint_data, undefined, undefined),
);
}

test "MathHints: assertLeFelt valid" {
var vm = try CairoVM.init(
std.testing.allocator,
Expand All @@ -1191,9 +1190,7 @@ test "MathHints: assertLeFelt valid" {

var exec_scopes = try ExecutionScopes.init(std.testing.allocator);
defer exec_scopes.deinit();
try exec_scopes.assignOrUpdateVariable("exclued", .{
.u64 = 1,
});
try exec_scopes.assignOrUpdateVariable("exclued", .{ .u64 = 1 });

var constants = std.StringHashMap(Felt252).init(std.testing.allocator);
defer constants.deinit();
Expand All @@ -1219,7 +1216,7 @@ test "MathHints: assertLeFelt valid" {
.{
.name = "range_check_ptr",
.elems = &.{
MaybeRelocatable.fromRelocatable(Relocatable.init(1, 0)),
MaybeRelocatable.fromRelocatable(Relocatable.init(2, 0)),
},
},
}, &vm);
Expand Down
1 change: 1 addition & 0 deletions src/hint_processor/testing_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn setupIdsForTest(allocator: std.mem.Allocator, data: []const struct { name

var current_offset: usize = 0;
var base_addr = vm.run_context.getFP();
_ = try vm.addMemorySegment();

for (data) |d| {
try result.put(d.name, .{
Expand Down
1 change: 0 additions & 1 deletion src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub const math = struct {
pub usingnamespace @import("math/fields/starknet.zig");
pub usingnamespace @import("math/crypto/poseidon/poseidon.zig");
pub usingnamespace @import("math/crypto/pedersen/pedersen.zig");

pub usingnamespace @import("math/fields/elliptic_curve.zig");
};

Expand Down
2 changes: 1 addition & 1 deletion src/math/fields/fields.zig
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ pub fn Field(comptime F: type, comptime modulo: u256) type {
/// Try to convert the field element to a u64 if its value is small enough.
///
/// Attempts to convert the field element to a u64 if its value is within the representable range.
pub fn tryIntoU64(self: Self) !u64 {
pub fn intoU64(self: Self) !u64 {
const asU256 = self.toInteger();
// Check if the value is small enough to fit into a u64
if (asU256 > @as(
Expand Down
8 changes: 4 additions & 4 deletions src/math/fields/starknet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -284,24 +284,24 @@ test "Felt252 toBytes" {
);
}

test "Felt252 tryIntoU64" {
test "Felt252 intoU64" {
try expectEqual(
@as(
u64,
10,
),
try Felt252.fromInt(u8, 10).tryIntoU64(),
try Felt252.fromInt(u8, 10).intoU64(),
);
try expectEqual(
@as(
u64,
std.math.maxInt(u64),
),
try Felt252.fromInt(u64, std.math.maxInt(u64)).tryIntoU64(),
try Felt252.fromInt(u64, std.math.maxInt(u64)).intoU64(),
);
try std.testing.expectError(
error.ValueTooLarge,
Felt252.fromInt(u128, std.math.maxInt(u64) + 1).tryIntoU64(),
Felt252.fromInt(u128, std.math.maxInt(u64) + 1).intoU64(),
);
}

Expand Down
14 changes: 8 additions & 6 deletions src/vm/builtins/builtin_runner/bitwise.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");

const bitwise_instance_def = @import("../../types/bitwise_instance_def.zig");
const BitwiseInstanceDef = @import("../../types/bitwise_instance_def.zig").BitwiseInstanceDef;
const Relocatable = @import("../../memory/relocatable.zig").Relocatable;
const Segments = @import("../../memory/segments.zig");
const Error = @import("../../error.zig");
Expand All @@ -24,7 +25,7 @@ pub const BitwiseError = error{
InvalidAddressForBitwise,
};

const BITWISE_INSTANCE_DEF = bitwise_instance_def.BitwiseInstanceDef{};
const BITWISE_INSTANCE_DEF = BitwiseInstanceDef{};

/// Bitwise built-in runner
pub const BitwiseBuiltinRunner = struct {
Expand All @@ -40,7 +41,7 @@ pub const BitwiseBuiltinRunner = struct {
/// The rest of the cells are considered output.
n_input_cells: u32 = bitwise_instance_def.INPUT_CELLS_PER_BITWISE,
/// Built-in bitwise instance
bitwise_builtin: bitwise_instance_def.BitwiseInstanceDef = BITWISE_INSTANCE_DEF,
bitwise_builtin: BitwiseInstanceDef = BITWISE_INSTANCE_DEF,
/// Stop pointer
stop_ptr: ?usize = null,
/// Included boolean flag
Expand All @@ -62,7 +63,7 @@ pub const BitwiseBuiltinRunner = struct {
///
/// A new `BitwiseBuiltinRunner` instance.
pub fn init(
instance_def: *const bitwise_instance_def.BitwiseInstanceDef,
instance_def: *const BitwiseInstanceDef,
included: bool,
) Self {
return .{
Expand Down Expand Up @@ -270,6 +271,7 @@ pub const BitwiseBuiltinRunner = struct {
@intCast(self.base),
) orelse MemoryError.MissingSegmentUsedSizes);
var result = ArrayList(Relocatable).init(allocator);
errdefer result.deinit();
for (0..segment_size) |i| {
try result.append(.{
.segment_index = @intCast(self.base),
Expand Down Expand Up @@ -338,7 +340,7 @@ pub const BitwiseBuiltinRunner = struct {
) !Relocatable {
if (self.included) {
const stop_pointer_addr = pointer.subUint(1) catch return RunnerError.NoStopPointer;
const stop_pointer = try (segments.memory.get(stop_pointer_addr) orelse return RunnerError.NoStopPointer).tryIntoRelocatable();
const stop_pointer = try (segments.memory.get(stop_pointer_addr) orelse return RunnerError.NoStopPointer).intoRelocatable();
if (self.base != stop_pointer.segment_index) {
return RunnerError.InvalidStopPointerIndex;
}
Expand Down Expand Up @@ -407,7 +409,7 @@ test "BitwiseBuiltinRunner: initialStack should return an empty array list if in
defer expected.deinit();

// given a builtin when not included
var default: bitwise_instance_def.BitwiseInstanceDef = .{};
var default: BitwiseInstanceDef = .{};
var builtin = BitwiseBuiltinRunner.init(&default, false);

// then
Expand Down Expand Up @@ -876,7 +878,7 @@ test "BitwiseBuiltinRunner: should return expectededuceMemoryCell bitwise-or" {

test "BitwiseBuiltinRunner: finalStack should return relocatable pointer if not included" {
// given
var default: bitwise_instance_def.BitwiseInstanceDef = .{};
var default: BitwiseInstanceDef = .{};
var builtin = BitwiseBuiltinRunner.init(&default, false);

// when
Expand Down
Loading

0 comments on commit a73e3d6

Please sign in to comment.