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

Add ExecutionResources + fix CairoRunner methods that received vm as an argument #300

Merged
merged 6 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
120 changes: 70 additions & 50 deletions pkg/runners/cairo_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ func (r *CairoRunner) RunUntilPC(end memory.Relocatable, hintProcessor vm.HintPr
return nil
}

func (runner *CairoRunner) EndRun(disableTracePadding bool, disableFinalizeAll bool, vm *vm.VirtualMachine, hintProcessor vm.HintProcessor) error {
func (runner *CairoRunner) EndRun(disableTracePadding bool, disableFinalizeAll bool, hintProcessor vm.HintProcessor) error {
if runner.RunEnded {
return ErrRunnerCalledTwice
}

// TODO: This seems to have to do with temporary segments
// vm.Segments.Memory.RelocateMemory()

err := vm.EndRun()
err := runner.Vm.EndRun()
if err != nil {
return err
}
Expand All @@ -254,28 +254,28 @@ func (runner *CairoRunner) EndRun(disableTracePadding bool, disableFinalizeAll b
return nil
}

vm.Segments.ComputeEffectiveSizes()
runner.Vm.Segments.ComputeEffectiveSizes()
if runner.ProofMode && !disableTracePadding {
err := runner.RunUntilNextPowerOfTwo(vm, hintProcessor)
err := runner.RunUntilNextPowerOfTwo(hintProcessor)
if err != nil {
return err
}

for true {
err := runner.CheckUsedCells(vm)
err := runner.CheckUsedCells()
if errors.Unwrap(err) == memory.ErrInsufficientAllocatedCells {
} else if err != nil {
return err
} else {
break
}

err = runner.RunForSteps(1, vm, hintProcessor)
err = runner.RunForSteps(1, hintProcessor)
if err != nil {
return err
}

err = runner.RunUntilNextPowerOfTwo(vm, hintProcessor)
err = runner.RunUntilNextPowerOfTwo(hintProcessor)
if err != nil {
return err
}
Expand All @@ -286,7 +286,7 @@ func (runner *CairoRunner) EndRun(disableTracePadding bool, disableFinalizeAll b
return nil
}

func (r *CairoRunner) FinalizeSegments(virtualMachine vm.VirtualMachine) error {
func (r *CairoRunner) FinalizeSegments() error {
if r.SegmentsFinalized {
return nil
}
Expand All @@ -304,7 +304,7 @@ func (r *CairoRunner) FinalizeSegments(virtualMachine vm.VirtualMachine) error {
publicMemory = append(publicMemory, i)
}

virtualMachine.Segments.Finalize(size, uint(r.ProgramBase.SegmentIndex), &publicMemory)
r.Vm.Segments.Finalize(size, uint(r.ProgramBase.SegmentIndex), &publicMemory)

publicMemory = make([]uint, 0)
execBase := r.executionBase
Expand All @@ -316,9 +316,9 @@ func (r *CairoRunner) FinalizeSegments(virtualMachine vm.VirtualMachine) error {
publicMemory = append(publicMemory, elem+execBase.Offset)
}

virtualMachine.Segments.Finalize(nil, uint(execBase.SegmentIndex), &publicMemory)
for _, builtin := range virtualMachine.BuiltinRunners {
_, size, err := builtin.GetUsedCellsAndAllocatedSizes(&virtualMachine.Segments, virtualMachine.CurrentStep)
r.Vm.Segments.Finalize(nil, uint(execBase.SegmentIndex), &publicMemory)
for _, builtin := range r.Vm.BuiltinRunners {
_, size, err := builtin.GetUsedCellsAndAllocatedSizes(&r.Vm.Segments, r.Vm.CurrentStep)
if err != nil {
return err
}
Expand All @@ -329,25 +329,25 @@ func (r *CairoRunner) FinalizeSegments(virtualMachine vm.VirtualMachine) error {
for i = 0; i < size; i++ {
publicMemory = append(publicMemory, i)
}
virtualMachine.Segments.Finalize(&size, uint(builtin.Base().SegmentIndex), &publicMemory)
r.Vm.Segments.Finalize(&size, uint(builtin.Base().SegmentIndex), &publicMemory)
} else {
virtualMachine.Segments.Finalize(&size, uint(builtin.Base().SegmentIndex), nil)
r.Vm.Segments.Finalize(&size, uint(builtin.Base().SegmentIndex), nil)
}
}

r.SegmentsFinalized = true
return nil
}

func (r *CairoRunner) ReadReturnValues(virtualMachine *vm.VirtualMachine) error {
func (r *CairoRunner) ReadReturnValues() error {
if !r.RunEnded {
return errors.New("Tried to read return values before run ended")
}

pointer := virtualMachine.RunContext.Ap
pointer := r.Vm.RunContext.Ap

for i := len(virtualMachine.BuiltinRunners) - 1; i >= 0; i-- {
newPointer, err := virtualMachine.BuiltinRunners[i].FinalStack(&virtualMachine.Segments, pointer)
for i := len(r.Vm.BuiltinRunners) - 1; i >= 0; i-- {
newPointer, err := r.Vm.BuiltinRunners[i].FinalStack(&r.Vm.Segments, pointer)
if err != nil {
return err
}
Expand All @@ -363,7 +363,7 @@ func (r *CairoRunner) ReadReturnValues(virtualMachine *vm.VirtualMachine) error
execBase := r.executionBase
begin := pointer.Offset - execBase.Offset

ap := virtualMachine.RunContext.Ap
ap := r.Vm.RunContext.Ap
end := ap.Offset - execBase.Offset

var publicMemoryExtension []uint
Expand All @@ -379,59 +379,59 @@ func (r *CairoRunner) ReadReturnValues(virtualMachine *vm.VirtualMachine) error

}

func (runner *CairoRunner) CheckUsedCells(virtualMachine *vm.VirtualMachine) error {
for _, builtin := range virtualMachine.BuiltinRunners {
func (runner *CairoRunner) CheckUsedCells() error {
for _, builtin := range runner.Vm.BuiltinRunners {
// I guess we call this just in case it errors out, even though later on we also call it?
_, _, err := builtin.GetUsedCellsAndAllocatedSizes(&virtualMachine.Segments, virtualMachine.CurrentStep)
_, _, err := builtin.GetUsedCellsAndAllocatedSizes(&runner.Vm.Segments, runner.Vm.CurrentStep)
if err != nil {
return err
}
}

err := runner.CheckRangeCheckUsage(virtualMachine)
err := runner.CheckRangeCheckUsage()
if err != nil {
return err
}

err = runner.CheckMemoryUsage(virtualMachine)
err = runner.CheckMemoryUsage()
if err != nil {
return err
}

err = runner.CheckDilutedCheckUsage(virtualMachine)
err = runner.CheckDilutedCheckUsage()
if err != nil {
return err
}

return nil
}

func (runner *CairoRunner) CheckMemoryUsage(virtualMachine *vm.VirtualMachine) error {
func (runner *CairoRunner) CheckMemoryUsage() error {
instance := runner.Layout

var builtinsMemoryUnits uint = 0

for _, builtin := range virtualMachine.BuiltinRunners {
result, err := builtin.GetAllocatedMemoryUnits(&virtualMachine.Segments, virtualMachine.CurrentStep)
for _, builtin := range runner.Vm.BuiltinRunners {
result, err := builtin.GetAllocatedMemoryUnits(&runner.Vm.Segments, runner.Vm.CurrentStep)
if err != nil {
return err
}

builtinsMemoryUnits += result
}

totalMemoryUnits := instance.MemoryUnitsPerStep * virtualMachine.CurrentStep
totalMemoryUnits := instance.MemoryUnitsPerStep * runner.Vm.CurrentStep
publicMemoryUnits := totalMemoryUnits / instance.PublicMemoryFraction
remainder := totalMemoryUnits % instance.PublicMemoryFraction

if remainder != 0 {
return errors.Errorf("Total Memory units was not divisible by the Public Memory Fraction. TotalMemoryUnits: %d PublicMemoryFraction: %d", totalMemoryUnits, instance.PublicMemoryFraction)
}

instructionMemoryUnits := 4 * virtualMachine.CurrentStep
instructionMemoryUnits := 4 * runner.Vm.CurrentStep
unusedMemoryUnits := totalMemoryUnits - (publicMemoryUnits + instructionMemoryUnits + builtinsMemoryUnits)

memoryAddressHoles, err := runner.GetMemoryHoles(virtualMachine)
memoryAddressHoles, err := runner.GetMemoryHoles()
if err != nil {
return err
}
Expand All @@ -443,26 +443,26 @@ func (runner *CairoRunner) CheckMemoryUsage(virtualMachine *vm.VirtualMachine) e
return nil
}

func (runner *CairoRunner) GetMemoryHoles(virtualMachine *vm.VirtualMachine) (uint, error) {
return virtualMachine.Segments.GetMemoryHoles(uint(len(virtualMachine.BuiltinRunners)))
func (runner *CairoRunner) GetMemoryHoles() (uint, error) {
return runner.Vm.Segments.GetMemoryHoles(uint(len(runner.Vm.BuiltinRunners)))
}

func (runner *CairoRunner) CheckDilutedCheckUsage(virtualMachine *vm.VirtualMachine) error {
func (runner *CairoRunner) CheckDilutedCheckUsage() error {
dilutedPoolInstance := runner.Layout.DilutedPoolInstance
if dilutedPoolInstance == nil {
return nil
}

var usedUnitsByBuiltins uint = 0

for _, builtin := range virtualMachine.BuiltinRunners {
for _, builtin := range runner.Vm.BuiltinRunners {
usedUnits := builtin.GetUsedDilutedCheckUnits(dilutedPoolInstance.Spacing, dilutedPoolInstance.NBits)

ratio := builtin.Ratio()
if ratio == 0 {
ratio = 1
}
multiplier, err := utils.SafeDiv(virtualMachine.CurrentStep, ratio)
multiplier, err := utils.SafeDiv(runner.Vm.CurrentStep, ratio)

if err != nil {
return err
Expand All @@ -471,7 +471,7 @@ func (runner *CairoRunner) CheckDilutedCheckUsage(virtualMachine *vm.VirtualMach
usedUnitsByBuiltins += usedUnits * multiplier
}

var dilutedUnits uint = dilutedPoolInstance.UnitsPerStep * virtualMachine.CurrentStep
var dilutedUnits uint = dilutedPoolInstance.UnitsPerStep * runner.Vm.CurrentStep
var unusedDilutedUnits uint = dilutedUnits - usedUnitsByBuiltins

var dilutedUsageUpperBound uint = 1 << dilutedPoolInstance.NBits
Expand All @@ -483,7 +483,7 @@ func (runner *CairoRunner) CheckDilutedCheckUsage(virtualMachine *vm.VirtualMach
return nil
}

func (runner *CairoRunner) CheckRangeCheckUsage(virtualMachine *vm.VirtualMachine) error {
func (runner *CairoRunner) CheckRangeCheckUsage() error {
var rcMin, rcMax *uint

for _, builtin := range runner.Vm.BuiltinRunners {
Expand Down Expand Up @@ -513,15 +513,15 @@ func (runner *CairoRunner) CheckRangeCheckUsage(virtualMachine *vm.VirtualMachin
var rcUnitsUsedByBuiltins uint = 0

for _, builtin := range runner.Vm.BuiltinRunners {
usedUnits, err := builtin.GetUsedPermRangeCheckLimits(&virtualMachine.Segments, virtualMachine.CurrentStep)
usedUnits, err := builtin.GetUsedPermRangeCheckLimits(&runner.Vm.Segments, runner.Vm.CurrentStep)
if err != nil {
return err
}

rcUnitsUsedByBuiltins += usedUnits
}

unusedRcUnits := (runner.Layout.RcUnits-3)*virtualMachine.CurrentStep - uint(rcUnitsUsedByBuiltins)
unusedRcUnits := (runner.Layout.RcUnits-3)*runner.Vm.CurrentStep - uint(rcUnitsUsedByBuiltins)

if unusedRcUnits < (*rcMax - *rcMin) {
return memory.InsufficientAllocatedCellsError(unusedRcUnits, *rcMax-*rcMin)
Expand All @@ -530,20 +530,19 @@ func (runner *CairoRunner) CheckRangeCheckUsage(virtualMachine *vm.VirtualMachin
return nil
}

// TODO: Add hint processor when it's done
func (runner *CairoRunner) RunForSteps(steps uint, virtualMachine *vm.VirtualMachine, hintProcessor vm.HintProcessor) error {
func (runner *CairoRunner) RunForSteps(steps uint, hintProcessor vm.HintProcessor) error {
hintDataMap, err := runner.BuildHintDataMap(hintProcessor)
if err != nil {
return err
}
constants := runner.Program.ExtractConstants()
var remainingSteps int
for remainingSteps = int(steps); remainingSteps > 0; remainingSteps-- {
if runner.finalPc != nil && *runner.finalPc == virtualMachine.RunContext.Pc {
if runner.finalPc != nil && *runner.finalPc == runner.Vm.RunContext.Pc {
return &vm.VirtualMachineError{Msg: fmt.Sprintf("EndOfProgram: %d", remainingSteps)}
}

err := virtualMachine.Step(hintProcessor, &hintDataMap, &constants, &runner.execScopes)
err := runner.Vm.Step(hintProcessor, &hintDataMap, &constants, &runner.execScopes)
if err != nil {
return err
}
Expand All @@ -552,12 +551,33 @@ func (runner *CairoRunner) RunForSteps(steps uint, virtualMachine *vm.VirtualMac
return nil
}

// TODO: Add hint processor when it's done
func (runner *CairoRunner) RunUntilSteps(steps uint, virtualMachine *vm.VirtualMachine, hintProcessor vm.HintProcessor) error {
return runner.RunForSteps(steps-virtualMachine.CurrentStep, virtualMachine, hintProcessor)
func (runner *CairoRunner) RunUntilSteps(steps uint, hintProcessor vm.HintProcessor) error {
return runner.RunForSteps(steps-runner.Vm.CurrentStep, hintProcessor)
}

// TODO: Add hint processor when it's done
func (runner *CairoRunner) RunUntilNextPowerOfTwo(virtualMachine *vm.VirtualMachine, hintProcessor vm.HintProcessor) error {
return runner.RunUntilSteps(utils.NextPowOf2(virtualMachine.CurrentStep), virtualMachine, hintProcessor)
func (runner *CairoRunner) RunUntilNextPowerOfTwo(hintProcessor vm.HintProcessor) error {
return runner.RunUntilSteps(utils.NextPowOf2(runner.Vm.CurrentStep), hintProcessor)
}

func (runner *CairoRunner) GetExecutionResources() (ExecutionResources, error) {
nSteps := uint(len(runner.Vm.Trace))
if nSteps == 0 {
nSteps = runner.Vm.CurrentStep
}
nMemoryHoles, err := runner.GetMemoryHoles()
if err != nil {
return ExecutionResources{}, err
}
builtinInstaceCounter := make(map[string]uint)
for i := 0; i < len(runner.Vm.BuiltinRunners); i++ {
builtinInstaceCounter[runner.Vm.BuiltinRunners[i].Name()], err = runner.Vm.BuiltinRunners[i].GetUsedInstances(&runner.Vm.Segments)
if err != nil {
return ExecutionResources{}, err
}
}
return ExecutionResources{
NSteps: nSteps,
NMemoryHoles: nMemoryHoles,
BuiltinsInstanceCounter: builtinInstaceCounter,
}, nil
}
Loading