Skip to content

Commit

Permalink
Merge branch 'main' into poseidon_integration_tests_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksymMalicki authored Aug 7, 2024
2 parents 989b45e + 389edf7 commit 1a3cb2d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func main() {
}

fmt.Println("Running....")
runner, err := runnerzero.NewRunner(program, hints, proofmode, maxsteps, layoutName)
runner, err := runnerzero.NewRunner(program, hints, proofmode, collectTrace, maxsteps, layoutName)
if err != nil {
return fmt.Errorf("cannot create runner: %w", err)
}
Expand Down
39 changes: 21 additions & 18 deletions pkg/runners/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ type ZeroRunner struct {
}

// Creates a new Runner of a Cairo Zero program
func NewRunner(program *Program, hints map[uint64][]hinter.Hinter, proofmode bool, maxsteps uint64, layoutName string) (ZeroRunner, error) {
func NewRunner(program *Program, hints map[uint64][]hinter.Hinter, proofmode bool, collectTrace bool, maxsteps uint64, layoutName string) (ZeroRunner, error) {
hintrunner := hintrunner.NewHintRunner(hints)
layout, err := builtins.GetLayout(layoutName)
if err != nil {
return ZeroRunner{}, err
}
return ZeroRunner{
program: program,
hintrunner: hintrunner,
proofmode: proofmode,
maxsteps: maxsteps,
layout: layout,
program: program,
hintrunner: hintrunner,
proofmode: proofmode,
collectTrace: collectTrace,
maxsteps: maxsteps,
layout: layout,
}, nil
}

Expand All @@ -56,11 +57,16 @@ func (runner *ZeroRunner) RunEntryPoint(pc uint64) error {
return err
}

stack, err := runner.initializeBuiltins(memory)
if err != nil {
return err
}

// Builtins are initialized as a part of initializeEntrypoint().

returnFp := memory.AllocateEmptySegment()
mvReturnFp := mem.MemoryValueFromMemoryAddress(&returnFp)
end, err := runner.initializeEntrypoint(pc, nil, &mvReturnFp, memory)
end, err := runner.initializeEntrypoint(pc, nil, &mvReturnFp, memory, stack)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,6 +123,11 @@ func (runner *ZeroRunner) InitializeMainEntrypoint() (mem.MemoryAddress, error)
return mem.UnknownAddress, err
}

stack, err := runner.initializeBuiltins(memory)
if err != nil {
return mem.UnknownAddress, err
}

if runner.proofmode {
initialPCOffset, ok := runner.program.Labels["__start__"]
if !ok {
Expand All @@ -129,10 +140,6 @@ func (runner *ZeroRunner) InitializeMainEntrypoint() (mem.MemoryAddress, error)
errors.New("end label not found. Try compiling with `--proof_mode`")
}

stack, err := runner.initializeBuiltins(memory)
if err != nil {
return mem.UnknownAddress, err
}
// Add the dummy last fp and pc to the public memory, so that the verifier can enforce [fp - 2] = fp.
stack = append([]mem.MemoryValue{mem.MemoryValueFromSegmentAndOffset(
vm.ProgramSegment,
Expand All @@ -158,16 +165,12 @@ func (runner *ZeroRunner) InitializeMainEntrypoint() (mem.MemoryAddress, error)
if !ok {
return mem.UnknownAddress, errors.New("can't find an entrypoint for main")
}
return runner.initializeEntrypoint(mainPCOffset, nil, &mvReturnFp, memory)
return runner.initializeEntrypoint(mainPCOffset, nil, &mvReturnFp, memory, stack)
}

func (runner *ZeroRunner) initializeEntrypoint(
initialPCOffset uint64, arguments []*fp.Element, returnFp *mem.MemoryValue, memory *mem.Memory,
initialPCOffset uint64, arguments []*fp.Element, returnFp *mem.MemoryValue, memory *mem.Memory, stack []mem.MemoryValue,
) (mem.MemoryAddress, error) {
stack, err := runner.initializeBuiltins(memory)
if err != nil {
return mem.UnknownAddress, err
}
for i := range arguments {
stack = append(stack, mem.MemoryValueFromFieldElement(arguments[i]))
}
Expand Down Expand Up @@ -379,7 +382,7 @@ func (runner *ZeroRunner) BuildMemory() ([]byte, error) {
return vm.EncodeMemory(relocatedMemory), nil
}

// BuildMemory relocates the trace and returns it
// BuildTrace relocates the trace and returns it
func (runner *ZeroRunner) BuildTrace() ([]byte, error) {
relocatedTrace := runner.vm.RelocateTrace()
return vm.EncodeTrace(relocatedTrace), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/runners/zero/zero_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func BenchmarkRunnerWithFibonacci(b *testing.B) {
panic(err)
}

runner, err := NewRunner(program, hints, true, math.MaxUint64, "plain")
runner, err := NewRunner(program, hints, true, false, math.MaxUint64, "plain")
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/runners/zero/zero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestSimpleProgram(t *testing.T) {
`)

hints := make(map[uint64][]hinter.Hinter)
runner, err := NewRunner(program, hints, false, math.MaxUint64, "plain")
runner, err := NewRunner(program, hints, false, false, math.MaxUint64, "plain")
require.NoError(t, err)

endPc, err := runner.InitializeMainEntrypoint()
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestStepLimitExceeded(t *testing.T) {
`)

hints := make(map[uint64][]hinter.Hinter)
runner, err := NewRunner(program, hints, false, 3, "plain")
runner, err := NewRunner(program, hints, false, false, 3, "plain")
require.NoError(t, err)

endPc, err := runner.InitializeMainEntrypoint()
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestStepLimitExceededProofMode(t *testing.T) {
// when maxstep = 6, it fails executing the extra step required by proof mode
// when maxstep = 7, it fails trying to get the trace to be a power of 2
hints := make(map[uint64][]hinter.Hinter)
runner, err := NewRunner(program, hints, true, uint64(maxstep), "plain")
runner, err := NewRunner(program, hints, true, false, uint64(maxstep), "plain")
require.NoError(t, err)

err = runner.Run()
Expand Down Expand Up @@ -368,7 +368,7 @@ func createRunner(code string, layoutName string, builtins ...sn.Builtin) ZeroRu
program := createProgramWithBuiltins(code, builtins...)

hints := make(map[uint64][]hinter.Hinter)
runner, err := NewRunner(program, hints, false, math.MaxUint64, layoutName)
runner, err := NewRunner(program, hints, false, false, math.MaxUint64, layoutName)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const RC_OFFSET_BITS = 16

func (vm *VirtualMachine) RunInstruction(instruction *a.Instruction) error {

var off0 int = int(instruction.OffDest) + 1<<(RC_OFFSET_BITS-1)
var off0 int = int(instruction.OffDest) + (1 << (RC_OFFSET_BITS - 1))
var off1 int = int(instruction.OffOp0) + (1 << (RC_OFFSET_BITS - 1))
var off2 int = int(instruction.OffOp1) + (1 << (RC_OFFSET_BITS - 1))

Expand Down

0 comments on commit 1a3cb2d

Please sign in to comment.