Skip to content

Commit

Permalink
Modifications for the runner
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksymMalicki committed Jan 25, 2025
1 parent 78d3bbc commit eeef9a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
29 changes: 19 additions & 10 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
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
3 changes: 1 addition & 2 deletions integration_tests/cairo_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func TestCairoFiles(t *testing.T) {
{"./cairo_1_programs/", false},
{"./cairo_1_programs/dict_non_squashed", false},
{"./cairo_1_programs/with_input", false},
{"./cairo_1_programs/serialized_output", false},
}

inputArgsMap := map[string]string{
Expand Down Expand Up @@ -423,8 +424,6 @@ func runRustVm(path, layout string, zero bool, inputArgs string) (time.Duration,
memoryOutput,
"--layout",
layout,
"--args",
inputArgs,
}

if zero {
Expand Down
11 changes: 7 additions & 4 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewRunner(program *Program, hints map[uint64][]hinter.Hinter, runnerMode Ru
if err != nil {
return Runner{}, err
}
newHintRunnerContext := getNewHintRunnerContext(program, userArgs, availableGas)
newHintRunnerContext := getNewHintRunnerContext(program, userArgs, availableGas, runnerMode == ProofModeCairo || runnerMode == ProofModeZero)
hintrunner := hintrunner.NewHintRunner(hints, &newHintRunnerContext)
return Runner{
program: program,
Expand All @@ -61,7 +61,7 @@ func NewRunner(program *Program, hints map[uint64][]hinter.Hinter, runnerMode Ru
}, nil
}

func getNewHintRunnerContext(program *Program, userArgs []starknet.CairoFuncArgs, availableGas uint64) hinter.HintRunnerContext {
func getNewHintRunnerContext(program *Program, userArgs []starknet.CairoFuncArgs, availableGas uint64, proofmode bool) hinter.HintRunnerContext {
// The writeApOffset is the offset where the user arguments will be written. It is added to the current AP in the ExternalWriteArgsToMemory hint.
// The writeApOffset is significant for Cairo programs, because of the prepended Entry Code instructions.
// In the entry code instructions the builtins bases (excluding gas, segment arena and output) are written to the memory,
Expand All @@ -78,6 +78,9 @@ func getNewHintRunnerContext(program *Program, userArgs []starknet.CairoFuncArgs
writeApOffset += 3
}
}
if proofmode {
writeApOffset += uint64(len(program.Builtins)) - 1
}

newHintrunnerContext := *hinter.InitializeDefaultContext()
err := newHintrunnerContext.ScopeManager.AssignVariables(map[string]any{
Expand Down Expand Up @@ -814,10 +817,10 @@ func GetFooterInstructions() []*fp.Element {

func CheckOnlyArrayFeltInputAndReturntValue(mainFunc starknet.EntryPointByFunction) error {
if len(mainFunc.InputArgs) != 1 {
return fmt.Errorf("main function should have only one input argument")
return fmt.Errorf("main function in proofmode should have felt252 array as input argument")
}
if len(mainFunc.ReturnArgs) != 1 {
return fmt.Errorf("main function should have only one return argument")
return fmt.Errorf("main function in proofmode should have an felt252 array as return argument")
}
if mainFunc.InputArgs[0].Size != 2 || mainFunc.InputArgs[0].DebugName != "Array<felt252>" {
return fmt.Errorf("main function input argument should be Felt Array")
Expand Down

0 comments on commit eeef9a2

Please sign in to comment.