Skip to content

Commit

Permalink
Use Allocator instead of *const Allocator. (keep-starknet-strange#57
Browse files Browse the repository at this point in the history
)

Use Allocator instead of *const Allocator

Co-authored-by: Abdel @ StarkWare <[email protected]>
  • Loading branch information
tcoratger and AbdelStark authored Oct 27, 2023
1 parent 111b4d0 commit 95d7d5f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn execute(_: []const []const u8) !void {

// Create a new VM instance.
var vm = try vm_core.CairoVM.init(
&gpa_allocator,
gpa_allocator,
config,
);
defer vm.deinit(); // <-- This ensures that resources are freed when exiting the scope
Expand Down
32 changes: 12 additions & 20 deletions src/vm/builtins/bitwise/bitwise.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const CELLS_PER_BITWISE: u64 = 5;
const BITWISE_TOTAL_N_BITS = 251;
const BITWISE_INPUT_CELLS_PER_INSTANCE = 2;


/// Retrieve the felt in memory that an address denotes as an integer.
/// # Arguments
/// - address: The address belonging to the Bitwise builtin's segment
Expand All @@ -44,14 +43,14 @@ fn getValue(address: Relocatable, memory: *Memory) Error!u256 {
if (felt.toInteger() > std.math.pow(u256, 2, BITWISE_TOTAL_N_BITS)) {
return Error.UnsupportedNumberOfBits;
}

return felt.toInteger();
}

/// Compute the auto-deduction rule for Bitwise
/// # Arguments
/// - address: The address belonging to the Bitwise builtin's segment
/// - memory: The cairo memory where addresses are looked up
/// - memory: The cairo memory where addresses are looked up
/// # Returns
/// The deduced value as a `MaybeRelocatable`
pub fn deduce(address: Relocatable, memory: *Memory) Error!MaybeRelocatable {
Expand Down Expand Up @@ -86,80 +85,73 @@ pub fn deduce(address: Relocatable, memory: *Memory) Error!MaybeRelocatable {
const expectEqual = std.testing.expectEqual;
const expectError = std.testing.expectError;


test "deduce when address.offset less than BITWISE_INPUT_CELLS_PER_INSTANCE" {

// given
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
var address = Relocatable.new(0, 5);

// then
try expectError(Error.InvalidBitwiseIndex, deduce(address, mem));

}

test "deduce when address points to nothing in memory" {

// given
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
var address = Relocatable.new(0,3);
var address = Relocatable.new(0, 3);

// then
try expectError(Error.InvalidAddressForBitwise, deduce(address, mem));

}

test "deduce when address points to relocatable variant of MaybeRelocatable " {

// given
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
var address = Relocatable.new(0,3);
var address = Relocatable.new(0, 3);

try mem.set(Relocatable.new(0, 5), newFromRelocatable(address));

// then
try expectError(Error.InvalidAddressForBitwise, deduce(address, mem));

}


test "deduce when address points to felt greater than BITWISE_TOTAL_N_BITS" {

// given
const number = std.math.pow(u256, 2, BITWISE_TOTAL_N_BITS) + 1;
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
var address = Relocatable.new(0,3);
var address = Relocatable.new(0, 3);

try mem.set(Relocatable.new(0, 0), fromFelt(Felt252.fromInteger(number)));

// then
try expectError(Error.UnsupportedNumberOfBits, deduce(address, mem));


}

// happy path tests graciously ported from https://github.com/lambdaclass/cairo-vm_in_go/blob/main/pkg/builtins/bitwise_test.go#L13
test "valid bitwise and" {

// given
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
Expand All @@ -179,7 +171,7 @@ test "valid bitwise xor" {

// given
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
Expand All @@ -199,7 +191,7 @@ test "valid bitwise or" {

// given
var allocator = std.testing.allocator;
var mem = try Memory.init(&allocator);
var mem = try Memory.init(allocator);
defer mem.deinit();

// when
Expand Down
44 changes: 22 additions & 22 deletions src/vm/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub const CairoVM = struct {
// ************************************************************

/// The memory allocator. Can be needed for the deallocation of the VM resources.
allocator: *const Allocator,
allocator: Allocator,
/// The run context.
run_context: *RunContext,
/// The memory segment manager.
Expand All @@ -45,15 +45,15 @@ pub const CairoVM = struct {
/// # Errors
/// - If a memory allocation fails.
pub fn init(
allocator: *const Allocator,
allocator: Allocator,
config: Config,
) !CairoVM {
// Initialize the memory segment manager.
const memory_segment_manager = try segments.MemorySegmentManager.init(allocator);
// Initialize the run context.
const run_context = try RunContext.init(allocator);
// Initialize the trace context.
const trace_context = try TraceContext.init(allocator.*, config.enable_trace);
const trace_context = try TraceContext.init(allocator, config.enable_trace);

return CairoVM{
.allocator = allocator,
Expand Down Expand Up @@ -429,7 +429,7 @@ test "update pc regular no imm" {
instruction.op_1_addr = instructions.Op1Src.AP;
const operands = OperandsResult.default();
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -462,7 +462,7 @@ test "update pc regular with imm" {
instruction.op_1_addr = instructions.Op1Src.Imm;
const operands = OperandsResult.default();
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -495,7 +495,7 @@ test "update pc jump with operands res null" {
var operands = OperandsResult.default();
operands.res = null;
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand All @@ -519,7 +519,7 @@ test "update pc jump with operands res not relocatable" {
var operands = OperandsResult.default();
operands.res = relocatable.fromU64(0);
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand All @@ -546,7 +546,7 @@ test "update pc jump with operands res relocatable" {
42,
));
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -579,7 +579,7 @@ test "update pc jump rel with operands res null" {
var operands = OperandsResult.default();
operands.res = null;
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand All @@ -606,7 +606,7 @@ test "update pc jump rel with operands res not felt" {
42,
));
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand All @@ -630,7 +630,7 @@ test "update pc jump rel with operands res felt" {
var operands = OperandsResult.default();
operands.res = relocatable.fromU64(42);
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -663,7 +663,7 @@ test "update pc update jnz with operands dst zero" {
var operands = OperandsResult.default();
operands.dst = relocatable.fromU64(0);
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -700,7 +700,7 @@ test "update pc update jnz with operands dst not zero op1 not felt" {
42,
));
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -728,7 +728,7 @@ test "update pc update jnz with operands dst not zero op1 felt" {
operands.dst = relocatable.fromU64(1);
operands.op_1 = relocatable.fromU64(42);
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -760,7 +760,7 @@ test "update ap add with operands res unconstrained" {
var operands = OperandsResult.default();
operands.res = null; // Simulate unconstrained res
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand All @@ -782,7 +782,7 @@ test "update ap add1" {
instruction.ap_update = instructions.ApUpdate.Add1;
var operands = OperandsResult.default();
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -814,7 +814,7 @@ test "update ap add2" {
instruction.ap_update = instructions.ApUpdate.Add2;
var operands = OperandsResult.default();
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -846,7 +846,7 @@ test "update fp appplus2" {
instruction.fp_update = instructions.FpUpdate.APPlus2;
var operands = OperandsResult.default();
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -882,7 +882,7 @@ test "update fp dst relocatable" {
42,
));
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -915,7 +915,7 @@ test "update fp dst felt" {
var operands = OperandsResult.default();
operands.dst = relocatable.fromU64(42);
// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down Expand Up @@ -948,7 +948,7 @@ test "trace is enabled" {
var config = Config{ .proof_mode = false, .enable_trace = true };

var vm = try CairoVM.init(
&allocator,
allocator,
config,
);
defer vm.deinit();
Expand All @@ -975,7 +975,7 @@ test "trace is disabled" {
var allocator = std.testing.allocator;

// Create a new VM instance.
var vm = try CairoVM.init(&allocator, .{});
var vm = try CairoVM.init(allocator, .{});
defer vm.deinit();

// ************************************************************
Expand Down
12 changes: 6 additions & 6 deletions src/vm/memory/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub const Memory = struct {
// * FIELDS *
// ************************************************************
/// The allocator used to allocate the memory.
allocator: *const Allocator,
allocator: Allocator,
// The data in the memory.
data: std.HashMap(
relocatable.Relocatable,
Expand Down Expand Up @@ -42,20 +42,20 @@ pub const Memory = struct {
// - `allocator` - The allocator to use.
// # Returns
// The new memory.
pub fn init(allocator: *const Allocator) !*Memory {
pub fn init(allocator: Allocator) !*Memory {
var memory = try allocator.create(Memory);

memory.* = Memory{
.allocator = allocator,
.data = std.AutoHashMap(
relocatable.Relocatable,
relocatable.MaybeRelocatable,
).init(allocator.*),
).init(allocator),
.num_segments = 0,
.validated_addresses = std.AutoHashMap(
relocatable.Relocatable,
bool,
).init(allocator.*),
).init(allocator),
};
return memory;
}
Expand Down Expand Up @@ -124,7 +124,7 @@ test "memory get without value raises error" {
var allocator = std.testing.allocator;

// Initialize a memory instance.
var memory = try Memory.init(&allocator);
var memory = try Memory.init(allocator);
defer memory.deinit();

// Get a value from the memory at an address that doesn't exist.
Expand All @@ -143,7 +143,7 @@ test "memory set and get" {
var allocator = std.testing.allocator;

// Initialize a memory instance.
var memory = try Memory.init(&allocator);
var memory = try Memory.init(allocator);
defer memory.deinit();

const address_1 = relocatable.Relocatable.new(
Expand Down
Loading

0 comments on commit 95d7d5f

Please sign in to comment.