Skip to content

Commit

Permalink
proof mode air input and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
StringNick committed Jul 9, 2024
2 parents 4feb465 + 17a4837 commit 14f6ea9
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 229 deletions.
4 changes: 2 additions & 2 deletions src/cmd/cmd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn run() !void {
};

// Command-line option for enabling secure run.
const secure_rune = cli.Option{
const secure_run = cli.Option{
// The full name of the option.
.long_name = "secure-run",
// Description of the option's purpose.
Expand Down Expand Up @@ -212,7 +212,7 @@ pub fn run() !void {
layout,
program_option,
output_trace,
secure_rune,
secure_run,
memory_file,
air_input_public,
air_input_private,
Expand Down
17 changes: 15 additions & 2 deletions src/hint_processor/set.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const HintProcessor = @import("hint_processor_def.zig").CairoVMHintProcessor;
const HintData = @import("hint_processor_def.zig").HintData;
const Relocatable = @import("../vm/memory/relocatable.zig").Relocatable;
const MaybeRelocatable = @import("../vm/memory/relocatable.zig").MaybeRelocatable;
const MemoryCell = @import("../vm/memory/memory.zig").MemoryCell;
const Felt252 = @import("../math/fields/starknet.zig").Felt252;
const hint_codes = @import("builtin_hint_codes.zig");
const MathError = @import("../vm/error.zig").MathError;
Expand Down Expand Up @@ -60,10 +61,22 @@ pub fn setAdd(
// Calculate the range limit.
const range_limit = (try set_end_ptr.sub(set_ptr)).offset;

// load all list, and then we compare elements
var elm_segment = vm.segments.memory.getSegmentAtIndex(elm_ptr.segment_index) orelse return HintError.InvalidSetRange;

if (elm_segment.len < elm_ptr.offset + elm_size) return HintError.InvalidSetRange;

var set_segment = vm.segments.memory.getSegmentAtIndex(set_ptr.segment_index) orelse return HintError.InvalidSetRange;

if (set_ptr.offset + range_limit > set_segment.len) return HintError.InvalidSetRange;

elm_segment = elm_segment[elm_ptr.offset .. elm_ptr.offset + elm_size];
set_segment = set_segment[set_ptr.offset .. set_ptr.offset + range_limit];

// Iterate over the set elements.
for (0..range_limit) |i| {
for (0..range_limit / elm_size) |i| {
// Check if the element is in the set.
if (try vm.memEq(elm_ptr, try set_ptr.addUint(elm_size * i), elm_size)) {
if (MemoryCell.eqlSlice(elm_segment, set_segment[i * elm_size .. (i + 1) * elm_size])) {
// Insert index of the element into the virtual machine.
try hint_utils.insertValueFromVarName(
allocator,
Expand Down
141 changes: 0 additions & 141 deletions src/poseidon_consts_gen.zig

This file was deleted.

2 changes: 2 additions & 0 deletions src/vm/builtins/builtin_runner/builtin_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pub const BuiltinRunner = union(BuiltinName) {
pub fn cellsPerInstance(self: *const BuiltinRunner) u32 {
return switch (self.*) {
.Output => 0,
.Poseidon => 6,
inline else => |*builtin| builtin.cells_per_instance,
};
}
Expand Down Expand Up @@ -632,6 +633,7 @@ pub const BuiltinRunner = union(BuiltinName) {
return switch (self.*) {
.Output => 0,
.SegmentArena => |*segment_arena| segment_arena.n_input_cells_per_instance,
.Poseidon => PoseidonBuiltinRunner.INPUT_CELLS_PER_POSEIDON,
inline else => |*builtin| builtin.n_input_cells,
};
}
Expand Down
38 changes: 16 additions & 22 deletions src/vm/builtins/builtin_runner/poseidon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ const expectEqualSlices = std.testing.expectEqualSlices;
pub const PoseidonBuiltinRunner = struct {
const Self = @This();

pub const INPUT_CELLS_PER_POSEIDON = poseidon_instance_def.INPUT_CELLS_PER_POSEIDON;

/// Base
base: usize = 0,
/// Ratio
ratio: ?u32,
/// Number of cells per instance
cells_per_instance: u32 = poseidon_instance_def.CELLS_PER_POSEIDON,
/// Number of input cells
n_input_cells: u32 = poseidon_instance_def.INPUT_CELLS_PER_POSEIDON,
/// Stop pointer
stop_ptr: ?usize = null,
/// Included boolean flag
Expand Down Expand Up @@ -157,7 +155,7 @@ pub const PoseidonBuiltinRunner = struct {

// Calculate the expected stop pointer value based on the number of used instances.
const stop_ptr = stop_pointer.offset;
if (stop_ptr != try self.getUsedInstances(segments) * self.cells_per_instance)
if (stop_ptr != try self.getUsedInstances(segments) * poseidon_instance_def.CELLS_PER_POSEIDON)
return RunnerError.InvalidStopPointer;

// Set the stop pointer and return the address of the stop pointer.
Expand Down Expand Up @@ -201,7 +199,7 @@ pub const PoseidonBuiltinRunner = struct {
return std.math.divCeil(
usize,
try self.getUsedCells(segments),
self.cells_per_instance,
6,
);
}

Expand All @@ -226,32 +224,27 @@ pub const PoseidonBuiltinRunner = struct {
address: Relocatable,
memory: *Memory,
) !?MaybeRelocatable {
_ = allocator; // autofix
// Calculate the index of the memory cell.
const index = @mod(
const index: usize = @mod(
@as(usize, @intCast(address.offset)),
@as(usize, @intCast(self.cells_per_instance)),
poseidon_instance_def.CELLS_PER_POSEIDON,
);

// Check if the index corresponds to an input cell, if so, return null.
if (index < self.n_input_cells) return null;
if (index < poseidon_instance_def.INPUT_CELLS_PER_POSEIDON) return null;

// Check if the cell value is already cached, if so, return it.
if (self.cache.get(address)) |felt| return .{ .felt = felt };

// Calculate the addresses for the first input cell and first output cell.
const first_input_addr = try address.subUint(index);
const first_output_addr = try first_input_addr.addUint(self.n_input_cells);
const first_output_addr = try first_input_addr.addUint(poseidon_instance_def.INPUT_CELLS_PER_POSEIDON);

// Initialize an array list to store input cell values.
var input_felts = try ArrayList(Felt252).initCapacity(allocator, self.n_input_cells);
defer input_felts.deinit();

// Iterate over input cells, retrieve their values, and append them to the array list.
for (0..self.n_input_cells) |i| {
const val = memory.get(try first_input_addr.addUint(i)) orelse return null;
try input_felts.append(val.intoFelt() catch
return RunnerError.BuiltinExpectedInteger);
}
var input_felts = memory.getFeltRange(first_input_addr, poseidon_instance_def.INPUT_CELLS_PER_POSEIDON) catch return RunnerError.BuiltinExpectedInteger;
defer input_felts.deinit();

// Perform Poseidon permutation computation on the input cells.
// TODO: optimize to use pointer on state
Expand All @@ -261,11 +254,12 @@ pub const PoseidonBuiltinRunner = struct {

PoseidonHasher.permuteComp();

@memcpy(input_felts.items[0..3], PoseidonHasher.state[0..3]);

// Iterate over input cells and cache their computed values.
for (0..self.n_input_cells, input_felts.items) |i, elem| {
try self.cache.put(try first_output_addr.addUint(i), elem);
inline for (0..3) |i| {
try self.cache.put(
try first_output_addr.addUint(i),
PoseidonHasher.state[i],
);
}

// Return the cached value for the specified memory cell address.
Expand Down
16 changes: 14 additions & 2 deletions src/vm/cairo_run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !CairoRunner {

var entrypoint: []const u8 = "main";

// TODO: add flag for extensive_hints
var runner = try CairoRunner.init(
allocator,
try parsed_program.value.parseProgramJson(allocator, &entrypoint),
Expand All @@ -206,7 +205,6 @@ pub fn runConfig(allocator: Allocator, config: Config) !CairoRunner {

const end = try runner.setupExecutionState(config.allow_missing_builtins orelse config.proof_mode);

// TODO: make flag for extensive_hints
var hint_processor: HintProcessor = .{};
try runner.runUntilPC(end, &hint_processor);

Expand All @@ -219,6 +217,7 @@ pub fn runConfig(allocator: Allocator, config: Config) !CairoRunner {
&hint_processor,
);


try runner.vm.verifyAutoDeductions(allocator);

// cairo_runner.read_return_values(allow_missing_builtins)?;
Expand All @@ -229,6 +228,19 @@ pub fn runConfig(allocator: Allocator, config: Config) !CairoRunner {
if (secure_run)
try security.verifySecureRunner(allocator, &runner, true, null);


if (config.print_output) {
var buf = try std.ArrayList(u8).initCapacity(allocator, 100);
defer buf.deinit();

try buf.appendSlice("Program Output:\n");
try vm.writeOutput(buf.writer());

std.log.debug("{s}", .{buf.items});
}

// TODO readReturnValues necessary for builtins

if (config.output_trace != null or config.output_memory != null) {
try runner.relocate();

Expand Down
2 changes: 2 additions & 0 deletions src/vm/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub const Config = struct {
output_trace: ?[]const u8 = undefined,
/// Write memory to binary file
output_memory: ?[]const u8 = undefined,
/// Print output from Output runner
print_output: bool = false,

allow_missing_builtins: ?bool = null,
};
22 changes: 11 additions & 11 deletions src/vm/core.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const RelocatedTraceEntry = @import("trace_context.zig").RelocatedTraceEntry;
const RangeCheckBuiltinRunner = @import("builtins/builtin_runner/range_check.zig").RangeCheckBuiltinRunner;
const SignatureBuiltinRunner = @import("builtins/builtin_runner/signature.zig").SignatureBuiltinRunner;
const BuiltinRunner = @import("builtins/builtin_runner/builtin_runner.zig").BuiltinRunner;
const builtin_runner = @import("builtins/builtin_runner/builtin_runner.zig");
const Felt252 = @import("../math/fields/starknet.zig").Felt252;
const HashBuiltinRunner = @import("./builtins/builtin_runner/hash.zig").HashBuiltinRunner;
const Instruction = instructions.Instruction;
Expand Down Expand Up @@ -1361,22 +1362,21 @@ pub const CairoVM = struct {
///
/// Returns an error if writing the output fails.
pub fn writeOutput(self: *Self, writer: anytype) !void {
var builtin: ?*BuiltinRunner = null;
var builtin: *BuiltinRunner = val: {

// Iterate through the built-in runners to find the output runner.
for (self.builtin_runners.items) |*runner| {
if (runner.* == .Output) {
builtin = runner;
break;
// Iterate through the built-in runners to find the output runner.
for (self.builtin_runners.items) |*runner| {
if (runner.* == .Output) {
break :val runner;
}
}
}

// If no output runner is found, return.
if (builtin == null) return;
// Output runner is not exist, so we just return
return;
};

// Compute effective sizes of memory segments.
const segment_used_sizes = try self.segments.computeEffectiveSize(false);
const segment_index = builtin.?.base();
const segment_index = builtin.base();

// Iterate through the memory segments and write output based on their content.
for (0..segment_used_sizes.items[@intCast(segment_index)]) |i| {
Expand Down
Loading

0 comments on commit 14f6ea9

Please sign in to comment.