Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement proof_mode functionalities #682

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d7b74e6
Fixes for the generation of entry code, fixes of hints parsing
MaksymMalicki Nov 15, 2024
dfd9ec8
Add modifications to the runner
MaksymMalicki Nov 21, 2024
a7814a1
Merge branch 'main' into add_code_entry
MaksymMalicki Dec 2, 2024
83872d8
Add fixes for the entrycode generation
MaksymMalicki Dec 3, 2024
cd04099
Refactor main CLI, offset the hints indexes by entry code size, load …
MaksymMalicki Dec 10, 2024
5eb27ab
Add available gas and user args (#677)
MaksymMalicki Dec 25, 2024
8812042
Fix the integration tests
MaksymMalicki Dec 26, 2024
78cf788
Fixes in the runner
MaksymMalicki Dec 27, 2024
af47a3d
Fixes in the runner
MaksymMalicki Dec 28, 2024
73f6dd5
Fix the unit tests, uncomment pythonVm execution in integration tests…
MaksymMalicki Dec 28, 2024
17b17df
Add writing tokens gas cost to memory
MaksymMalicki Jan 7, 2025
688b3d2
Proper builtins initialization for cairo mode
MaksymMalicki Jan 8, 2025
f105506
Address comments in the PR
MaksymMalicki Jan 9, 2025
cc82938
Fix bugs regarding dicts
MaksymMalicki Jan 10, 2025
a32add7
Remove prints
MaksymMalicki Jan 10, 2025
07f4016
Merge branch 'main' into missing_dict_functionalities
MaksymMalicki Jan 10, 2025
53d4190
Fixes of the last tests for the dicts
MaksymMalicki Jan 11, 2025
31aeeed
Add dict_non_squashed dir to the integration tests
MaksymMalicki Jan 11, 2025
086011a
Add checks for the matching args size, rename files, modify the integ…
MaksymMalicki Jan 11, 2025
fc09b66
Almost all pass
MaksymMalicki Jan 13, 2025
72b3c14
Fix lint and unit tests
MaksymMalicki Jan 13, 2025
ce93d4f
Fix loading gas to the memory as an argument
MaksymMalicki Jan 15, 2025
734d1e7
Fix run for tensor__small.cairo
MaksymMalicki Jan 16, 2025
b54e9bb
Merge branch 'main' into inputs
MaksymMalicki Jan 16, 2025
c511329
check for input and output types for proofmode
MaksymMalicki Jan 17, 2025
74c2110
Implementation for entry code
MaksymMalicki Jan 23, 2025
6883060
Implement inline casm for segment arena
MaksymMalicki Jan 23, 2025
b757a2e
Merge branch 'main' into proof_mode
MaksymMalicki Jan 23, 2025
8d50465
Lint the project
MaksymMalicki Jan 23, 2025
78d3bbc
remove system type
MaksymMalicki Jan 23, 2025
eeef9a2
Modifications for the runner
MaksymMalicki Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func main() {
if err != nil {
return fmt.Errorf("cannot parse args: %w", err)
}
program, hints, userArgs, err := runner.AssembleProgram(cairoProgram, userArgs, availableGas)
program, hints, userArgs, err := runner.AssembleProgram(cairoProgram, userArgs, availableGas, proofmode)
if err != nil {
return fmt.Errorf("cannot assemble program: %w", err)
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func runVM(
availableGas uint64,
) error {
fmt.Println("Running....")
runner, err := runner.NewRunner(&program, hints, runnerMode, collectTrace, maxsteps, layoutName, userArgs, availableGas)
cairoRunner, err := runner.NewRunner(&program, hints, runnerMode, collectTrace, maxsteps, layoutName, userArgs, availableGas)
if err != nil {
return fmt.Errorf("cannot create runner: %w", err)
}
Expand All @@ -268,25 +268,34 @@ func runVM(
// but these functions are implemented differently in both this and cairo-rs VMs
// and the difference is quite subtle.
if entrypointOffset == 0 {
if err := runner.Run(); err != nil {
if err := cairoRunner.Run(); err != nil {
return fmt.Errorf("runtime error: %w", err)
}
} else {
if err := runner.RunEntryPoint(entrypointOffset); err != nil {
if err := cairoRunner.RunEntryPoint(entrypointOffset); err != nil {
return fmt.Errorf("runtime error (entrypoint=%d): %w", entrypointOffset, err)
}
}

if airPublicInputLocation != "" {
if runnerMode == runner.ProofModeCairo {
fmt.Println("finalizing builtins for cairo")
} else if runnerMode == runner.ExecutionModeCairo {
fmt.Println("finalizing builtins for cairo")
}

}
if proofmode {
if err := runner.EndRun(); err != nil {
if err := cairoRunner.EndRun(); err != nil {
return fmt.Errorf("cannot end run: %w", err)
}
if err := runner.FinalizeSegments(); err != nil {
if err := cairoRunner.FinalizeSegments(); err != nil {
return fmt.Errorf("cannot finalize segments: %w", err)
}
}

if proofmode || collectTrace {
trace, err := runner.BuildTrace()
trace, err := cairoRunner.BuildTrace()
if err != nil {
return fmt.Errorf("cannot build trace: %w", err)
}
Expand All @@ -299,7 +308,7 @@ func runVM(
}

if proofmode || buildMemory {
memory, err := runner.BuildMemory()
memory, err := cairoRunner.BuildMemory()
if err != nil {
return fmt.Errorf("cannot build memory: %w", err)
}
Expand All @@ -313,7 +322,7 @@ func runVM(

if proofmode {
if airPublicInputLocation != "" {
airPublicInput, err := runner.GetAirPublicInput()
airPublicInput, err := cairoRunner.GetAirPublicInput()
if err != nil {
return err
}
Expand All @@ -336,7 +345,7 @@ func runVM(
if err != nil {
return err
}
airPrivateInput, err := runner.GetAirPrivateInput(tracePath, memoryPath)
airPrivateInput, err := cairoRunner.GetAirPrivateInput(tracePath, memoryPath)
if err != nil {
return err
}
Expand All @@ -352,7 +361,7 @@ func runVM(
}

fmt.Println("Success!")
output := runner.Output()
output := cairoRunner.Output()
if len(output) > 0 {
fmt.Println("Program output:")
for _, val := range output {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fn arr_sum(mut vals: Array<felt252>) -> felt252 {
let mut sum: felt252 = 0;

loop {
match vals.pop_front() {
Option::Some(v) => {
sum += v;
},
Option::None(_) => {
break sum;
}
};
}
}

fn main(vals: Array<felt252>) -> Array<felt252> {
let sum1 = arr_sum(vals);
let mut res: Array<felt252> = ArrayTrait::new();
res.append(sum1);
res.append(sum1);
res
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use array::ArrayTrait;
use array::SpanTrait;
use dict::Felt252DictTrait;
use pedersen::PedersenTrait;

fn main(mut vals: Array<felt252>) -> Array<felt252> {
// Create a dictionary using segment arena
let mut dict = felt252_dict_new::<felt252>();

// Store each value in the dictionary with its index
let mut index: felt252 = 0;
loop {
match vals.pop_front() {
Option::Some(v) => {
dict.insert(index, v);
index += 1;
},
Option::None(_) => {
break;
}
};
};

// Create result array
let mut res: Array<felt252> = ArrayTrait::new();

// Read values back from dictionary and hash them
let mut i: felt252 = 0;
loop {
let i_u128: u128 = i.try_into().unwrap();
let index_u128: u128 = index.try_into().unwrap();

if i_u128 >= index_u128 {
break;
}

let value = dict.get(i);
let hash = pedersen::pedersen(value, i);
res.append(hash);

i += 1;
};

res
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use array::ArrayTrait;
use array::SpanTrait;
use pedersen::PedersenTrait;
use core::num::traits::Bounded;

fn check_and_sum(mut vals: Array<felt252>) -> felt252 {
let mut sum: felt252 = 0;
let upper_bound: u128 = 1000000; // Example bound

loop {
match vals.pop_front() {
Option::Some(v) => {
// Convert felt252 to u128 for range check
let v_u128: u128 = v.try_into().unwrap();
// This will ensure v is non-negative and within u128 bounds
assert(v_u128 <= upper_bound, 'Value exceeds bound');

sum += v;
},
Option::None(_) => {
break sum;
}
};
}
}

fn main(mut vals: Array<felt252>) -> Array<felt252> {
// Calculate sum with range checks
let sum = check_and_sum(vals);

// Hash the sum using Pedersen
let hash = pedersen::pedersen(sum, 0);

// Create result array
let mut res: Array<felt252> = ArrayTrait::new();

// Add original sum
res.append(sum);
// Add hash of sum
res.append(hash);

// Add a u128 conversion to demonstrate more range checks
let sum_u128: u128 = sum.try_into().unwrap();
res.append(sum_u128.into());

res
}
29 changes: 16 additions & 13 deletions integration_tests/cairo_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,24 @@ func TestCairoFiles(t *testing.T) {
zero bool
}
roots := []TestCase{
{"./cairo_zero_hint_tests/", true},
{"./cairo_zero_file_tests/", true},
{"./builtin_tests/", true},
// {"./cairo_1_programs/", false},
// {"./cairo_1_programs/dict_non_squashed", false},
// {"./cairo_1_programs/with_input", false},
// {"./cairo_zero_hint_tests/", true},
// {"./cairo_zero_file_tests/", true},
// {"./builtin_tests/", true},
{"./cairo_1_programs/", false},
{"./cairo_1_programs/dict_non_squashed", false},
{"./cairo_1_programs/with_input", false},
{"./cairo_1_programs/serialized_output", false},
}

// inputArgsMap is used to provide input arguments to the tests that require them. Whenever the args are needed for the new files, they can simply be added here.
inputArgsMap := map[string]string{
"cairo_1_programs/with_input/array_input_sum__small.cairo": "2 [111 222 333] 1 [444 555 666 777]",
"cairo_1_programs/with_input/array_length__small.cairo": "[1 2 3 4 5 6] [7 8 9 10]",
"cairo_1_programs/with_input/branching.cairo": "123",
"cairo_1_programs/with_input/dict_with_input__small.cairo": "[1 2 3 4]",
"cairo_1_programs/with_input/tensor__small.cairo": "[1 4] [1 5]",
"cairo_1_programs/with_input/array_input_sum__small.cairo": "2 [111 222 333] 1 [444 555 666 777]",
"cairo_1_programs/with_input/array_length__small.cairo": "[1 2 3 4 5 6] [7 8 9 10]",
"cairo_1_programs/with_input/branching.cairo": "123",
"cairo_1_programs/with_input/dict_with_input__small.cairo": "[1 2 3 4]",
"cairo_1_programs/with_input/tensor__small.cairo": "[1 4] [1 5]",
"cairo_1_programs/with_input/proofmode__small.cairo": "[1 2 3 4 5]",
"cairo_1_programs/with_input/proofmode_with_builtins__small.cairo": "[1 2 3 4 5]",
"cairo_1_programs/with_input/proofmode_segment_arena__small.cairo": "[1 2 3 4 5]",
}

// filter is for debugging purposes
Expand Down Expand Up @@ -473,7 +476,7 @@ func runVm(path, layout string, zero bool, inputArgs string) (time.Duration, str
if !zero {
args = []string{
cliCommand,
// "--proofmode",
"--proofmode",
"--tracefile",
traceOutput,
"--memoryfile",
Expand Down
8 changes: 4 additions & 4 deletions pkg/parsers/starknet/starknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ type Arg struct {
}

type EntryPointByFunction struct {
Offset int `json:"offset"`
Builtins []builtins.BuiltinType `json:"builtins"`
InputArgs []Arg `json:"input_args"`
ReturnArg []Arg `json:"return_arg"`
Offset int `json:"offset"`
Builtins []builtins.BuiltinType `json:"builtins"`
InputArgs []Arg `json:"input_args"`
ReturnArgs []Arg `json:"return_arg"`
}

type Hints struct {
Expand Down
Loading
Loading