Skip to content

Commit

Permalink
improve init methods in CairoRunner with unit tests (keep-starknet-…
Browse files Browse the repository at this point in the history
…strange#338)

* improve init methods in CairoRunner with unit tests

* fix bugs

---------

Co-authored-by: lanaivina <[email protected]>
  • Loading branch information
tcoratger and lana-shanghai authored Feb 7, 2024
1 parent c55a416 commit b0ae64e
Show file tree
Hide file tree
Showing 3 changed files with 390 additions and 25 deletions.
68 changes: 68 additions & 0 deletions src/vm/builtins/builtin_runner/builtin_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ const MaybeRelocatable = @import("../../memory/relocatable.zig").MaybeRelocatabl
const Memory = @import("../../memory/memory.zig").Memory;
const ArrayList = std.ArrayList;

/// The name of the output builtin.
pub const OUTPUT_BUILTIN_NAME = "output_builtin";

/// The name of the Pedersen hash builtin.
pub const HASH_BUILTIN_NAME = "pedersen_builtin";

/// The name of the range check builtin.
pub const RANGE_CHECK_BUILTIN_NAME = "range_check_builtin";

/// The name of the ECDSA signature verification builtin.
pub const SIGNATURE_BUILTIN_NAME = "ecdsa_builtin";

/// The name of the bitwise operations builtin.
pub const BITWISE_BUILTIN_NAME = "bitwise_builtin";

/// The name of the elliptic curve operations builtin.
pub const EC_OP_BUILTIN_NAME = "ec_op_builtin";

/// The name of the Keccak hash builtin.
pub const KECCAK_BUILTIN_NAME = "keccak_builtin";

/// The name of the Poseidon hash builtin.
pub const POSEIDON_BUILTIN_NAME = "poseidon_builtin";

/// The name of the segment arena builtin.
pub const SEGMENT_ARENA_BUILTIN_NAME = "segment_arena_builtin";

/// Built-in runner
pub const BuiltinRunner = union(enum) {
const Self = @This();
Expand Down Expand Up @@ -156,4 +183,45 @@ pub const BuiltinRunner = union(enum) {
.SegmentArena => .{ 0, 0 },
};
}

/// Gets the name of the built-in runner.
///
/// This function returns the name associated with the specific type of built-in runner.
///
/// # Returns
///
/// A null-terminated byte slice representing the name of the built-in runner.
pub fn name(self: *Self) []const u8 {
return switch (self.*) {
.Bitwise => BITWISE_BUILTIN_NAME,
.EcOp => EC_OP_BUILTIN_NAME,
.Hash => HASH_BUILTIN_NAME,
.Output => OUTPUT_BUILTIN_NAME,
.RangeCheck => RANGE_CHECK_BUILTIN_NAME,
.Keccak => KECCAK_BUILTIN_NAME,
.Signature => SIGNATURE_BUILTIN_NAME,
.Poseidon => POSEIDON_BUILTIN_NAME,
.SegmentArena => SEGMENT_ARENA_BUILTIN_NAME,
};
}

/// Adds a validation rule to the built-in runner for memory validation.
///
/// This method is used to add a validation rule specific to the type of built-in runner.
/// For certain built-ins, like the Range Check built-in, additional memory validation rules may
/// be necessary to ensure proper execution.
///
/// # Arguments
///
/// - `memory`: A pointer to the Memory manager for the current context.
///
/// # Errors
///
/// An error is returned if adding the validation rule fails.
pub fn addValidationRule(self: *Self, memory: *Memory) !void {
switch (self.*) {
.RangeCheck => |*range_check| try range_check.addValidationRule(memory),
else => {},
}
}
};
8 changes: 8 additions & 0 deletions src/vm/error.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ pub const RunnerError = error{
IntegerBiggerThanPowerOfTwo,
/// Memory-related errors in the built-in runners.
Memory,
/// Raised when attempting to access the program counter (PC) when it is not available.
NoPC,
/// Raised when attempting to access the allocation pointer (AP) when it is not available.
NoAP,
/// Raised when attempting to access the function pointer (FP) when it is not available.
NoFP,
/// Raised when there are errors related to memory validation in Cairo runner.
MemoryValidationError,
};

/// Represents different error conditions that occur during mathematical operations.
Expand Down
Loading

0 comments on commit b0ae64e

Please sign in to comment.