Skip to content

Commit

Permalink
Remove memory manager (#109)
Browse files Browse the repository at this point in the history
* Remove memory manager

* Add test for segmentsOffsets and clean up

* Remove memory field

* Fix linter errors

* refactor
  • Loading branch information
jmjac authored Oct 12, 2023
1 parent a0ec8a6 commit 56d430e
Show file tree
Hide file tree
Showing 12 changed files with 328 additions and 321 deletions.
10 changes: 5 additions & 5 deletions pkg/hintrunner/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestAllocSegment(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 3
vm.Context.Fp = 0

Expand Down Expand Up @@ -42,7 +42,7 @@ func TestAllocSegment(t *testing.T) {
}

func TestTestLessThanTrue(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 0
vm.Context.Fp = 0
writeTo(vm, VM.ExecutionSegment, 0, memory.MemoryValueFromInt(23))
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestTestLessThanFalse(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.expectedMsg, func(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 0
vm.Context.Fp = 0
writeTo(vm, VM.ExecutionSegment, 0, memory.MemoryValueFromInt(17))
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestTestLessThanOrEqTrue(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.expectedMsg, func(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 0
vm.Context.Fp = 0
writeTo(vm, VM.ExecutionSegment, 0, memory.MemoryValueFromInt(23))
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestTestLessThanOrEqTrue(t *testing.T) {
}

func TestTestLessThanOrEqFalse(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 0
vm.Context.Fp = 0
writeTo(vm, VM.ExecutionSegment, 0, memory.MemoryValueFromInt(17))
Expand Down
4 changes: 2 additions & 2 deletions pkg/hintrunner/hintrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestExistingHint(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 3

var ap ApCellRef = 5
Expand All @@ -33,7 +33,7 @@ func TestExistingHint(t *testing.T) {
}

func TestNoHint(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 3

var ap ApCellRef = 5
Expand Down
14 changes: 7 additions & 7 deletions pkg/hintrunner/operand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestGetAp(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 5
writeTo(vm, VM.ExecutionSegment, vm.Context.Ap+7, memory.MemoryValueFromInt(11))

Expand All @@ -26,7 +26,7 @@ func TestGetAp(t *testing.T) {
}

func TestGetFp(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Fp = 15
writeTo(vm, VM.ExecutionSegment, vm.Context.Fp-7, memory.MemoryValueFromInt(11))

Expand All @@ -41,7 +41,7 @@ func TestGetFp(t *testing.T) {
}

func TestResolveDeref(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 5
writeTo(vm, VM.ExecutionSegment, vm.Context.Ap+7, memory.MemoryValueFromInt(11))

Expand All @@ -55,7 +55,7 @@ func TestResolveDeref(t *testing.T) {
}

func TestResolveDoubleDerefPositiveOffset(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 5
writeTo(
vm,
Expand All @@ -77,7 +77,7 @@ func TestResolveDoubleDerefPositiveOffset(t *testing.T) {
}

func TestResolveDoubleDerefNegativeOffset(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
vm.Context.Ap = 5
writeTo(
vm,
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestResolveImmediate(t *testing.T) {
}

func TestResolveAddOp(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
// Set the information used by the lhs
vm.Context.Fp = 0
vm.Context.Ap = 5
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestResolveAddOp(t *testing.T) {
}

func TestResolveMulOp(t *testing.T) {
vm, _ := defaultVirtualMachine()
vm := defaultVirtualMachine()
// Set the information used by the lhs
vm.Context.Fp = 0
vm.Context.Ap = 5
Expand Down
12 changes: 6 additions & 6 deletions pkg/hintrunner/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
)

func defaultVirtualMachine() (*vm.VirtualMachine, *memory.MemoryManager) {
manager := memory.CreateMemoryManager()
manager.Memory.AllocateEmptySegment()
manager.Memory.AllocateEmptySegment()
func defaultVirtualMachine() *vm.VirtualMachine {
memory := memory.InitializeEmptyMemory()
memory.AllocateEmptySegment()
memory.AllocateEmptySegment()

vm, err := vm.NewVirtualMachine(vm.Context{}, manager.Memory, vm.VirtualMachineConfig{})
vm, err := vm.NewVirtualMachine(vm.Context{}, memory, vm.VirtualMachineConfig{})
if err != nil {
panic(err)
}
return vm, manager
return vm
}

func writeTo(vm *VM.VirtualMachine, segment uint64, offset uint64, val memory.MemoryValue) {
Expand Down
63 changes: 28 additions & 35 deletions pkg/runners/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (

type ZeroRunner struct {
// core components
program *Program
vm *vm.VirtualMachine
hintrunner hintrunner.HintRunner
memoryManager *memory.MemoryManager
program *Program
vm *vm.VirtualMachine
hintrunner hintrunner.HintRunner
// config
proofmode bool
maxsteps uint64
Expand All @@ -27,22 +26,14 @@ type ZeroRunner struct {

// Creates a new Runner of a Cairo Zero program
func NewRunner(program *Program, proofmode bool, maxsteps uint64) (*ZeroRunner, error) {
memoryManager := memory.CreateMemoryManager()
_, err := memoryManager.Memory.AllocateSegment(program.Bytecode) // ProgramSegment
if err != nil {
return nil, err
}
memoryManager.Memory.AllocateEmptySegment() // ExecutionSegment

// todo(rodro): given the program get the appropiate hints
hintrunner := hintrunner.NewHintRunner(make(map[uint64]hintrunner.Hinter))

return &ZeroRunner{
program: program,
hintrunner: hintrunner,
memoryManager: memoryManager,
proofmode: proofmode,
maxsteps: maxsteps,
program: program,
hintrunner: hintrunner,
proofmode: proofmode,
maxsteps: maxsteps,
}, nil
}

Expand Down Expand Up @@ -74,6 +65,13 @@ func (runner *ZeroRunner) Run() error {
}

func (runner *ZeroRunner) InitializeMainEntrypoint() (memory.MemoryAddress, error) {
mem := memory.InitializeEmptyMemory()
_, err := mem.AllocateSegment(runner.program.Bytecode) // ProgramSegment
if err != nil {
return memory.UnknownAddress, err
}

mem.AllocateEmptySegment() // ExecutionSegment
if runner.proofmode {
initialPCOffset, ok := runner.program.Labels["__start__"]
if !ok {
Expand All @@ -92,18 +90,18 @@ func (runner *ZeroRunner) InitializeMainEntrypoint() (memory.MemoryAddress, erro
return memory.MemoryAddress{SegmentIndex: vm.ProgramSegment, Offset: endPcOffset}, runner.initializeVm(&memory.MemoryAddress{
SegmentIndex: vm.ProgramSegment,
Offset: initialPCOffset,
}, stack)
}, stack, mem)
}

returnFp := memory.MemoryValueFromSegmentAndOffset(
runner.memory().AllocateEmptySegment(),
mem.AllocateEmptySegment(),
0,
)
return runner.InitializeEntrypoint("main", nil, &returnFp)

return runner.InitializeEntrypoint("main", nil, &returnFp, mem)
}

func (runner *ZeroRunner) InitializeEntrypoint(
funcName string, arguments []*f.Element, returnFp *memory.MemoryValue,
funcName string, arguments []*f.Element, returnFp *memory.MemoryValue, mem *memory.Memory,
) (memory.MemoryAddress, error) {
initialPCOffset, ok := runner.program.Entrypoints[funcName]
if !ok {
Expand All @@ -115,18 +113,21 @@ func (runner *ZeroRunner) InitializeEntrypoint(
stack = append(stack, memory.MemoryValueFromFieldElement(arguments[i]))
}
end := memory.MemoryAddress{
SegmentIndex: uint64(runner.memory().AllocateEmptySegment()),
SegmentIndex: uint64(mem.AllocateEmptySegment()),
Offset: 0,
}

stack = append(stack, *returnFp, memory.MemoryValueFromMemoryAddress(&end))
return end, runner.initializeVm(&memory.MemoryAddress{
SegmentIndex: vm.ProgramSegment,
Offset: initialPCOffset,
}, stack)
}, stack, mem)
}

func (runner *ZeroRunner) initializeVm(initialPC *memory.MemoryAddress, stack []memory.MemoryValue) error {
executionSegment := runner.segments()[vm.ExecutionSegment]
func (runner *ZeroRunner) initializeVm(initialPC *memory.MemoryAddress, stack []memory.MemoryValue, memory *memory.Memory) error {
//Initialize memory

executionSegment := memory.Segments[vm.ExecutionSegment]
offset := executionSegment.Len()
for idx := range stack {
if err := executionSegment.Write(offset+uint64(idx), &stack[idx]); err != nil {
Expand All @@ -140,7 +141,7 @@ func (runner *ZeroRunner) initializeVm(initialPC *memory.MemoryAddress, stack []
Pc: *initialPC,
Ap: offset + uint64(len(stack)),
Fp: offset + uint64(len(stack)),
}, runner.memoryManager.Memory, vm.VirtualMachineConfig{ProofMode: runner.proofmode})
}, memory, vm.VirtualMachineConfig{ProofMode: runner.proofmode})
return err
}

Expand Down Expand Up @@ -191,15 +192,7 @@ func (runner *ZeroRunner) BuildProof() ([]byte, []byte, error) {
return nil, nil, err
}

return EncodeTrace(relocatedTrace), EncodeMemory(runner.memoryManager.RelocateMemory()), nil
}

func (runner *ZeroRunner) memory() *memory.Memory {
return runner.memoryManager.Memory
}

func (runner *ZeroRunner) segments() []*memory.Segment {
return runner.memoryManager.Memory.Segments
return EncodeTrace(relocatedTrace), EncodeMemory(runner.vm.RelocateMemory()), nil
}

func (runner *ZeroRunner) pc() memory.MemoryAddress {
Expand Down
6 changes: 3 additions & 3 deletions pkg/runners/zero/zero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestSimpleProgram(t *testing.T) {
err = runner.RunUntilPc(&endPc)
require.NoError(t, err)

executionSegment := runner.segments()[vm.ExecutionSegment]
executionSegment := runner.vm.Memory.Segments[vm.ExecutionSegment]

assert.Equal(
t,
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestStepLimitExceeded(t *testing.T) {
err = runner.RunUntilPc(&endPc)
require.ErrorContains(t, err, "step limit exceeded")

executionSegment := runner.segments()[vm.ExecutionSegment]
executionSegment := runner.vm.Memory.Segments[vm.ExecutionSegment]

assert.Equal(
t,
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestStepLimitExceededProofMode(t *testing.T) {
err = runner.Run()
require.ErrorContains(t, err, "step limit exceeded")

executionSegment := runner.segments()[vm.ExecutionSegment]
executionSegment := runner.vm.Memory.Segments[vm.ExecutionSegment]

assert.Equal(
t,
Expand Down
18 changes: 18 additions & 0 deletions pkg/vm/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,21 @@ func (memory *Memory) Peek(segmentIndex uint64, offset uint64) (MemoryValue, err
func (memory *Memory) PeekFromAddress(address *MemoryAddress) (MemoryValue, error) {
return memory.Peek(address.SegmentIndex, address.Offset)
}

// It returns all segment offsets and max memory used
func (memory *Memory) RelocationOffsets() ([]uint64, uint64) {
// Prover expects maxMemoryUsed to start at one
var maxMemoryUsed uint64 = 1

// segmentsOffsets[0] = 1
// segmentsOffsets[1] = 1 + len(segment[0])
// segmentsOffsets[N] = 1 + len(segment[n-1]) + sum of segements[n-1-i] for i in [1, n-1]
segmentsOffsets := make([]uint64, uint64(len(memory.Segments))+1)
segmentsOffsets[0] = 1
for i, segment := range memory.Segments {
segmentLength := segment.Len()
maxMemoryUsed += segmentLength
segmentsOffsets[i+1] = segmentsOffsets[i] + segmentLength
}
return segmentsOffsets, maxMemoryUsed
}
61 changes: 0 additions & 61 deletions pkg/vm/memory/memory_manager.go

This file was deleted.

Loading

0 comments on commit 56d430e

Please sign in to comment.