diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index e78c31461..de2c13f0e 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -97,7 +97,7 @@ func NewVirtualMachine(initialContext Context, memory *mem.Memory, config Virtua Memory: memory, Trace: trace, config: config, - instructions: make(map[uint64]*Instruction), + instructions: make(map[uint64]*assembler.Instruction), }, nil } @@ -138,8 +138,7 @@ func (vm *VirtualMachine) RunStep(hintRunner HintRunner) error { return nil } - -func (vm *VirtualMachine) RunInstruction(instruction *Instruction) error { +func (vm *VirtualMachine) RunInstruction(instruction *assembler.Instruction) error { dstAddr, err := vm.getDstAddr(instruction) if err != nil { return fmt.Errorf("dst cell: %w", err) @@ -202,7 +201,7 @@ func (vm *VirtualMachine) ExecutionTrace() ([]Trace, error) { return vm.relocateTrace(), nil } -func (vm *VirtualMachine) getDstAddr(instruction *Instruction) (mem.MemoryAddress, error) { +func (vm *VirtualMachine) getDstAddr(instruction *assembler.Instruction) (mem.MemoryAddress, error) { var dstRegister uint64 if instruction.DstRegister == assembler.Ap { dstRegister = vm.Context.Ap @@ -217,7 +216,7 @@ func (vm *VirtualMachine) getDstAddr(instruction *Instruction) (mem.MemoryAddres return mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: addr}, nil } -func (vm *VirtualMachine) getOp0Addr(instruction *Instruction) (mem.MemoryAddress, error) { +func (vm *VirtualMachine) getOp0Addr(instruction *assembler.Instruction) (mem.MemoryAddress, error) { var op0Register uint64 if instruction.Op0Register == assembler.Ap { op0Register = vm.Context.Ap @@ -232,8 +231,7 @@ func (vm *VirtualMachine) getOp0Addr(instruction *Instruction) (mem.MemoryAddres return mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: addr}, nil } - -func (vm *VirtualMachine) getOp1Addr(instruction *Instruction, op0Addr *mem.MemoryAddress) (mem.MemoryAddress, error) { +func (vm *VirtualMachine) getOp1Addr(instruction *assembler.Instruction, op0Addr *mem.MemoryAddress) (mem.MemoryAddress, error) { var op1Address mem.MemoryAddress switch instruction.Op1Source { case assembler.Op0: @@ -269,10 +267,10 @@ func (vm *VirtualMachine) getOp1Addr(instruction *Instruction, op0Addr *mem.Memo // dstCell value and either op0Cell xor op1Cell. This function infers the // unknow operand as well as the `res` auxiliar value func (vm *VirtualMachine) inferOperand( - instruction *Instruction, dstAddr *mem.MemoryAddress, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress, + instruction *assembler.Instruction, dstAddr *mem.MemoryAddress, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress, ) (mem.MemoryValue, error) { - if instruction.Opcode != AssertEq || - (instruction.Res == Unconstrained) { + if instruction.Opcode != assembler.OpCodeAssertEq || + (instruction.Res == assembler.Unconstrained) { return mem.MemoryValue{}, nil } @@ -298,7 +296,7 @@ func (vm *VirtualMachine) inferOperand( return mem.MemoryValue{}, fmt.Errorf("value at dst is unknown") } - if instruction.Res == Op1 && !op1Value.Known() { + if instruction.Res == assembler.Op1 && !op1Value.Known() { if err = vm.Memory.WriteToAddress(op1Addr, &dstValue); err != nil { return mem.MemoryValue{}, err } @@ -315,8 +313,8 @@ func (vm *VirtualMachine) inferOperand( unknownOpAddr = op0Addr } - var missingVal mem.MemoryValue - if instruction.Res == assembler.AddOperands { + var missingVal mem.MemoryValue + if instruction.Res == assembler.AddOperands { missingVal = mem.EmptyMemoryValueAs(dstValue.IsAddress()) err = missingVal.Sub(&dstValue, &knownOpValue) } else { @@ -334,12 +332,12 @@ func (vm *VirtualMachine) inferOperand( } func (vm *VirtualMachine) computeRes( - instruction *Instruction, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress, + instruction *assembler.Instruction, op0Addr *mem.MemoryAddress, op1Addr *mem.MemoryAddress, ) (mem.MemoryValue, error) { switch instruction.Res { - case Unconstrained: + case assembler.Unconstrained: return mem.MemoryValue{}, nil - case Op1: + case assembler.Op1: return vm.Memory.ReadFromAddress(op1Addr) default: op0, err := vm.Memory.ReadFromAddress(op0Addr) @@ -353,9 +351,9 @@ func (vm *VirtualMachine) computeRes( } res := mem.EmptyMemoryValueAs(op0.IsAddress() || op1.IsAddress()) - if instruction.Res == AddOperands { + if instruction.Res == assembler.AddOperands { err = res.Add(&op0, &op1) - } else if instruction.Res == MulOperands { + } else if instruction.Res == assembler.MulOperands { err = res.Mul(&op0, &op1) } else { return mem.MemoryValue{}, fmt.Errorf("invalid res flag value: %d", instruction.Res) @@ -365,13 +363,13 @@ func (vm *VirtualMachine) computeRes( } func (vm *VirtualMachine) opcodeAssertions( - instruction *Instruction, + instruction *assembler.Instruction, dstAddr *mem.MemoryAddress, op0Addr *mem.MemoryAddress, res *mem.MemoryValue, ) error { switch instruction.Opcode { - case Call: + case assembler.OpCodeCall: fpAddr := vm.Context.AddressFp() fpMv := mem.MemoryValueFromMemoryAddress(&fpAddr) // Store at [ap] the current fp @@ -482,7 +480,7 @@ func (vm *VirtualMachine) updateAp(instruction *assembler.Instruction, res *mem. return 0, fmt.Errorf("cannot update ap, unknown ApUpdate flag: %d", instruction.ApUpdate) } -func (vm *VirtualMachine) updateFp(instruction *Instruction, dstAddr *mem.MemoryAddress) (uint64, error) { +func (vm *VirtualMachine) updateFp(instruction *assembler.Instruction, dstAddr *mem.MemoryAddress) (uint64, error) { switch instruction.Opcode { case assembler.OpCodeCall: // [ap] and [ap + 1] are written to memory diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index a45844cc6..92a93fdca 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -71,9 +71,9 @@ func TestGetCellApDstWithDifferentOffsets(t *testing.T) { writeToDataSegment(vm, uint64(ap+offset), mem.MemoryValueFromInt(200)) - instruction := Instruction{ + instruction := assembler.Instruction{ OffDest: int16(offset), - DstRegister: Ap, + DstRegister: assembler.Ap, } addr, err := vm.getDstAddr(&instruction) @@ -173,9 +173,9 @@ func TestGetApCellOp0NegOff(t *testing.T) { vm.Context.Ap = ap writeToDataSegment(vm, ap+offOp0, mem.MemoryValueFromInt(155)) - instruction := Instruction{ + instruction := assembler.Instruction{ OffOp0: offOp0, - Op0Register: Ap, + Op0Register: assembler.Ap, } addr, err := vm.getOp0Addr(&instruction) @@ -199,9 +199,9 @@ func TestGetFpCellOp0(t *testing.T) { vm.Context.Fp = fp writeToDataSegment(vm, fp+offOp0, mem.MemoryValueFromInt(365)) - instruction := Instruction{ + instruction := assembler.Instruction{ OffOp0: offOp0, - Op0Register: Fp, + Op0Register: assembler.Fp, } addr, err := vm.getOp0Addr(&instruction) @@ -225,9 +225,9 @@ func TestGetFpCellOp0NegOff(t *testing.T) { vm.Context.Fp = fp writeToDataSegment(vm, fp+offOp0, mem.MemoryValueFromInt(286)) - instruction := Instruction{ + instruction := assembler.Instruction{ OffOp0: offOp0, - Op0Register: Fp, + Op0Register: assembler.Fp, } addr, err := vm.getOp0Addr(&instruction) @@ -411,7 +411,7 @@ func TestGetApNegCellOp1(t *testing.T) { func TestInferOperandSub(t *testing.T) { vm, _ := defaultVirtualMachine() instruction := assembler.Instruction{ - Opcode: assembler.AssertEq, + Opcode: assembler.OpCodeAssertEq, Res: assembler.AddOperands, } writeToDataSegment(vm, 0, mem.MemoryValueFromSegmentAndOffset(3, 15)) //destCell @@ -432,9 +432,9 @@ func TestInferOperandSub(t *testing.T) { func TestInferResOp1(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{ - Opcode: AssertEq, - Res: Op1, + instruction := assembler.Instruction{ + Opcode: assembler.OpCodeAssertEq, + Res: assembler.Op1, } writeToDataSegment(vm, 0, mem.MemoryValueFromInt(1337)) //destCell dstAddr := mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: 0} @@ -453,7 +453,7 @@ func TestInferResOp1(t *testing.T) { func TestComputeResUnconstrained(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := assembler.Instruction{Res: Unconstrained} + instruction := assembler.Instruction{Res: assembler.Unconstrained} res, err := vm.computeRes(&instruction, nil, nil) require.NoError(t, err) require.False(t, res.Known()) @@ -461,7 +461,7 @@ func TestComputeResUnconstrained(t *testing.T) { func TestComputeResOp1(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: Op1} + instruction := assembler.Instruction{Res: assembler.Op1} writeToDataSegment(vm, 3, mem.MemoryValueFromInt(15)) op1Addr := mem.MemoryAddress{SegmentIndex: ExecutionSegment, Offset: 3} @@ -475,7 +475,7 @@ func TestComputeResOp1(t *testing.T) { func TestComputeAddResAddrToFelt(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: AddOperands} + instruction := assembler.Instruction{Res: assembler.AddOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10)) op1Addr := writeToDataSegment(vm, 8, mem.MemoryValueFromInt(15)) @@ -489,7 +489,7 @@ func TestComputeAddResAddrToFelt(t *testing.T) { func TestComputeAddResFeltToAddr(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: AddOperands} + instruction := assembler.Instruction{Res: assembler.AddOperands} op0Addr := writeToDataSegment(vm, 2, mem.MemoryValueFromInt(8)) op1Addr := writeToDataSegment(vm, 5, mem.MemoryValueFromSegmentAndOffset(2, 7)) @@ -502,7 +502,7 @@ func TestComputeAddResFeltToAddr(t *testing.T) { func TestComputeAddResBothAddrs(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: AddOperands} + instruction := assembler.Instruction{Res: assembler.AddOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromSegmentAndOffset(2, 15)) @@ -513,7 +513,7 @@ func TestComputeAddResBothAddrs(t *testing.T) { func TestComputeAddResBothFelts(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: AddOperands} + instruction := assembler.Instruction{Res: assembler.AddOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15)) @@ -528,7 +528,7 @@ func TestComputeAddResBothFelts(t *testing.T) { func TestComputeMulResPosToPosFelt(t *testing.T) { //Positive Felt to Positive Felt compute vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15)) @@ -541,7 +541,7 @@ func TestComputeMulResPosToPosFelt(t *testing.T) { func TestComputeMulResNegToPosFelts(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} //Negative to Positive op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(-10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15)) @@ -554,7 +554,7 @@ func TestComputeMulResNegToPosFelts(t *testing.T) { func TestComputeMulResPosToNegFelt(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} //Positive to Negative op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(-15)) @@ -567,7 +567,7 @@ func TestComputeMulResPosToNegFelt(t *testing.T) { func TestComputeMulResNegToNegFelt(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} //Netagive to Negative op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(-10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(-15)) @@ -582,7 +582,7 @@ func TestComputeMulResNegToNegFelt(t *testing.T) { // three failing cases func TestComputeMulResAddrToFelt(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromInt(15)) @@ -593,7 +593,7 @@ func TestComputeMulResAddrToFelt(t *testing.T) { func TestComputeMulResFeltToAddr(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromInt(10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromSegmentAndOffset(2, 15)) @@ -604,7 +604,7 @@ func TestComputeMulResFeltToAddr(t *testing.T) { func TestComputeMulResBothAddrs(t *testing.T) { vm, _ := defaultVirtualMachine() - instruction := Instruction{Res: MulOperands} + instruction := assembler.Instruction{Res: assembler.MulOperands} op0Addr := writeToDataSegment(vm, 3, mem.MemoryValueFromSegmentAndOffset(2, 10)) op1Addr := writeToDataSegment(vm, 4, mem.MemoryValueFromSegmentAndOffset(2, 15)) @@ -748,9 +748,9 @@ func TestUpdateApSameAp(t *testing.T) { vm, _ := defaultVirtualMachine() vm.Context.Ap = 5 - instruction := Instruction{ - Opcode: Nop, - ApUpdate: SameAp, + instruction := assembler.Instruction{ + Opcode: assembler.OpCodeNop, + ApUpdate: assembler.SameAp, } nextAp, err := vm.updateAp(&instruction, nil) @@ -762,9 +762,9 @@ func TestUpdateApAddImmPos(t *testing.T) { vm, _ := defaultVirtualMachine() vm.Context.Ap = 5 - instruction := Instruction{ - Opcode: Nop, - ApUpdate: AddImm, + instruction := assembler.Instruction{ + Opcode: assembler.OpCodeNop, + ApUpdate: assembler.AddRes, } res := mem.MemoryValueFromInt(7) @@ -778,9 +778,9 @@ func TestUpdateApAddImmNeg(t *testing.T) { vm, _ := defaultVirtualMachine() vm.Context.Ap = 10 - instruction := Instruction{ - Opcode: Nop, - ApUpdate: AddImm, + instruction := assembler.Instruction{ + Opcode: assembler.OpCodeNop, + ApUpdate: assembler.AddRes, } res := mem.MemoryValueFromInt(-3) @@ -808,9 +808,9 @@ func TestUpdateApAddTwo(t *testing.T) { vm, _ := defaultVirtualMachine() vm.Context.Ap = 5 - instruction := Instruction{ - Opcode: Nop, - ApUpdate: Add2, + instruction := assembler.Instruction{ + Opcode: assembler.OpCodeNop, + ApUpdate: assembler.Add2, } nextAp, err := vm.updateAp(&instruction, nil)