Skip to content

Commit

Permalink
Add RunResources (#306)
Browse files Browse the repository at this point in the history
* Add ExecutionResources struct

* Fix: Remove vm argument from CairoRunner methods

* Finish func + remove todos

* Add unit test

* Implement GenArg

* Remove recursive processing

* Add unit tests

* Fix test values

* Start fn

* Add RunFromEntryPoint

* Add test for RunFromEntryPoint

* Add unit tests

* Add comments

* Add skeleton

* Implement GetMemoryAccesses for BuiltinRunner

* Start implementing RunSecurityChecks

* Implement VerifyAutoDeductionsForAddr

* Finish func + remove import cycle

* Add fix + unit test

* Fix logic, add test

* Fix logic

* Fix logic

* Fix logic + add test

* Add unit tests

* Move RunSecurityChecks to external func in builtins package

* Remove todos from BuiltinRunner interface

* Finish verifySecureRunner

* Add verifySecure to RunFromEntryPoint

* Add unit tests

* Add secure_run flag

* Fix typo

* Add RunResources

* Add RunResources

* Fix bug

* Use saturating subtraction
  • Loading branch information
fmoletta committed Oct 24, 2023
1 parent 5a2d812 commit 432dd89
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
12 changes: 10 additions & 2 deletions pkg/runners/cairo_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,18 @@ func (r *CairoRunner) RunUntilPC(end memory.Relocatable, hintProcessor vm.HintPr
return err
}
constants := r.Program.ExtractConstants()
for r.Vm.RunContext.Pc != end {
for r.Vm.RunContext.Pc != end &&
(r.Vm.RunResources == nil || !r.Vm.RunResources.Consumed()) {
err := r.Vm.Step(hintProcessor, &hintDataMap, &constants, &r.execScopes)
if err != nil {
return err
}
if r.Vm.RunResources != nil {
r.Vm.RunResources.ConsumeStep()
}
}
if r.Vm.RunContext.Pc != end {
return errors.New("Could not reach the end of the program. RunResources has no remaining steps.")
}
return nil
}
Expand Down Expand Up @@ -588,7 +595,8 @@ If `verifySecure` is set to true, [verifySecureRunner] will be called to run ext
`programSegmentSize` is only used by the [verifySecureRunner] function and will be ignored if `verifySecure` is set to false.
Each arg can be either MaybeRelocatable, []MaybeRelocatable or [][]MaybeRelocatable
*/
func (runner *CairoRunner) RunFromEntrypoint(entrypoint uint, args []any, hintProcessor vm.HintProcessor, verifySecure bool, programSegmentSize *uint) error {
func (runner *CairoRunner) RunFromEntrypoint(entrypoint uint, args []any, hintProcessor vm.HintProcessor, runResources *vm.RunResources, verifySecure bool, programSegmentSize *uint) error {
runner.Vm.RunResources = runResources
stack := make([]memory.MaybeRelocatable, 0)
for _, arg := range args {
val, err := runner.Vm.Segments.GenArg(arg)
Expand Down
26 changes: 25 additions & 1 deletion pkg/runners/cairo_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ func TestRunFromEntryPointFibonacci(t *testing.T) {

runner.InitializeBuiltins()
runner.InitializeSegments()
err := runner.RunFromEntrypoint(uint(entrypoint), args, &hintProcessor, true, nil)
err := runner.RunFromEntrypoint(uint(entrypoint), args, &hintProcessor, nil, true, nil)

if err != nil {
t.Errorf("Running fib entrypoint failed with error %s", err.Error())
Expand All @@ -759,3 +759,27 @@ func TestRunFromEntryPointFibonacci(t *testing.T) {
}

}

func TestRunFromEntryPointFibonacciNotEnoughSteps(t *testing.T) {
compiledProgram, _ := parser.Parse("../../cairo_programs/fibonacci.json")
programJson := vm.DeserializeProgramJson(compiledProgram)

entrypoint := programJson.Identifiers["__main__.fib"].PC
args := []any{
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltOne()),
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltOne()),
*memory.NewMaybeRelocatableFelt(lambdaworks.FeltFromUint(10)),
}
runner, _ := runners.NewCairoRunner(programJson, "all_cairo", false)
hintProcessor := hints.CairoVmHintProcessor{}

runner.InitializeBuiltins()
runner.InitializeSegments()
runResources := vm.NewRunResources(2)
err := runner.RunFromEntrypoint(uint(entrypoint), args, &hintProcessor, &runResources, true, nil)

if err == nil {
t.Errorf("Running fib entrypoint should have failed")
}

}
23 changes: 23 additions & 0 deletions pkg/vm/run_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package vm

type RunResources struct {
nSteps *uint
}

func NewRunResources(nSteps uint) RunResources {
return RunResources{nSteps: &nSteps}
}

func (r *RunResources) Consumed() bool {
return r.nSteps != nil && *r.nSteps == 0
}

func (r *RunResources) ConsumeStep() {
if r.nSteps != nil && *r.nSteps != 0 {
*r.nSteps--
}
}

func (r *RunResources) GetNSteps() *uint {
return r.nSteps
}
1 change: 1 addition & 0 deletions pkg/vm/vm_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type VirtualMachine struct {
RunFinished bool
RcLimitsMin *int
RcLimitsMax *int
RunResources *RunResources
}

func NewVirtualMachine() *VirtualMachine {
Expand Down

0 comments on commit 432dd89

Please sign in to comment.