Skip to content

Commit

Permalink
Merge branch 'main' into builtin-keccak
Browse files Browse the repository at this point in the history
  • Loading branch information
jkktom committed Nov 2, 2023
2 parents 16ef95e + a302d45 commit 0746710
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 50 deletions.
12 changes: 6 additions & 6 deletions pkg/hintrunner/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/holiman/uint256"

"github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
VM "github.com/NethermindEth/cairo-vm-go/pkg/vm"
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
Expand Down Expand Up @@ -235,7 +235,7 @@ func (hint *WideMul128) String() string {
}

func (hint *WideMul128) Execute(vm *VM.VirtualMachine, _ *HintRunnerContext) error {
mask := &safemath.Uint256Max128
mask := &utils.Uint256Max128

lhs, err := hint.lhs.Resolve(vm)
if err != nil {
Expand Down Expand Up @@ -404,7 +404,7 @@ func (hint *AllocFelt252Dict) Execute(vm *VM.VirtualMachine, ctx *HintRunnerCont
}

// find for the amount of initialized dicts
initializedDictsOffset, overflow := safemath.SafeOffset(arenaPtr.Offset, -2)
initializedDictsOffset, overflow := utils.SafeOffset(arenaPtr.Offset, -2)
if overflow {
return fmt.Errorf("look for initialized dicts: overflow: %s - 2", arenaPtr)
}
Expand All @@ -418,7 +418,7 @@ func (hint *AllocFelt252Dict) Execute(vm *VM.VirtualMachine, ctx *HintRunnerCont
}

// find for the segment info pointer
segmentInfoOffset, overflow := safemath.SafeOffset(arenaPtr.Offset, -3)
segmentInfoOffset, overflow := utils.SafeOffset(arenaPtr.Offset, -3)
if overflow {
return fmt.Errorf("look for segment info pointer: overflow: %s - 3", arenaPtr)
}
Expand Down Expand Up @@ -586,7 +586,7 @@ func (hint *InitSquashData) Execute(vm *VM.VirtualMachine, ctx *HintRunnerContex
}
for key, val := range ctx.SquashedDictionaryManager.KeyToIndices {
// reverse each indice access list per key
safemath.Reverse(val)
utils.Reverse(val)
// store each key
ctx.SquashedDictionaryManager.Keys = append(ctx.SquashedDictionaryManager.Keys, key)
}
Expand All @@ -603,7 +603,7 @@ func (hint *InitSquashData) Execute(vm *VM.VirtualMachine, ctx *HintRunnerContex
}
biggestKey := ctx.SquashedDictionaryManager.Keys[0]
cmpRes := mem.MemoryValueFromUint[uint64](0)
if biggestKey.Cmp(&safemath.FeltMax128) > 0 {
if biggestKey.Cmp(&utils.FeltMax128) > 0 {
cmpRes = mem.MemoryValueFromUint[uint64](1)
}
err = vm.Memory.WriteToAddress(&bigKeysAddr, &cmpRes)
Expand Down
14 changes: 7 additions & 7 deletions pkg/hintrunner/operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package hintrunner
import (
"fmt"

"github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
VM "github.com/NethermindEth/cairo-vm-go/pkg/vm"
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
Expand All @@ -25,9 +25,9 @@ func (ap ApCellRef) String() string {
}

func (ap ApCellRef) Get(vm *VM.VirtualMachine) (mem.MemoryAddress, error) {
res, overflow := safemath.SafeOffset(vm.Context.Ap, int16(ap))
res, overflow := utils.SafeOffset(vm.Context.Ap, int16(ap))
if overflow {
return mem.UnknownAddress, safemath.NewSafeOffsetError(vm.Context.Ap, int16(ap))
return mem.UnknownAddress, fmt.Errorf("overflow %d + %d", vm.Context.Ap, int16(ap))
}
return mem.MemoryAddress{SegmentIndex: VM.ExecutionSegment, Offset: res}, nil
}
Expand All @@ -39,9 +39,9 @@ func (fp FpCellRef) String() string {
}

func (fp FpCellRef) Get(vm *VM.VirtualMachine) (mem.MemoryAddress, error) {
res, overflow := safemath.SafeOffset(vm.Context.Fp, int16(fp))
res, overflow := utils.SafeOffset(vm.Context.Fp, int16(fp))
if overflow {
return mem.MemoryAddress{}, safemath.NewSafeOffsetError(vm.Context.Ap, int16(fp))
return mem.UnknownAddress, fmt.Errorf("overflow %d + %d", vm.Context.Fp, int16(fp))
}
return mem.MemoryAddress{SegmentIndex: VM.ExecutionSegment, Offset: res}, nil
}
Expand Down Expand Up @@ -92,9 +92,9 @@ func (dderef DoubleDeref) Resolve(vm *VM.VirtualMachine) (mem.MemoryValue, error
return mem.UnknownValue, err
}

newOffset, overflow := safemath.SafeOffset(address.Offset, dderef.offset)
newOffset, overflow := utils.SafeOffset(address.Offset, dderef.offset)
if overflow {
return mem.UnknownValue, safemath.NewSafeOffsetError(address.Offset, dderef.offset)
return mem.UnknownValue, fmt.Errorf("overflow %d + %d", address.Offset, dderef.offset)
}
resAddr := mem.MemoryAddress{
SegmentIndex: address.SegmentIndex,
Expand Down
4 changes: 2 additions & 2 deletions pkg/runners/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner"
"github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
"github.com/NethermindEth/cairo-vm-go/pkg/vm"
"github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins"
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (runner *ZeroRunner) Run() error {
if runner.proofmode {
// +1 because proof mode require an extra instruction run
// pow2 because proof mode also requires that the trace is a power of two
pow2Steps := safemath.NextPowerOfTwo(runner.vm.Step + 1)
pow2Steps := utils.NextPowerOfTwo(runner.vm.Step + 1)
if err := runner.RunFor(pow2Steps); err != nil {
return err
}
Expand Down
21 changes: 0 additions & 21 deletions pkg/safemath/error.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/safemath/arrays.go → pkg/utils/arrays.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package safemath
package utils

func Reverse[T any](a []T) {
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/safemath/constant.go → pkg/utils/constant.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package safemath
package utils

import (
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
Expand Down
2 changes: 1 addition & 1 deletion pkg/safemath/safemath.go → pkg/utils/math.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package safemath
package utils

import (
"math/bits"
Expand Down
2 changes: 1 addition & 1 deletion pkg/safemath/safemath_test.go → pkg/utils/math_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package safemath
package utils

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions pkg/vm/builtins/range_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

"github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
)

Expand All @@ -19,7 +19,7 @@ func (r *RangeCheck) CheckWrite(segment *memory.Segment, offset uint64, value *m
}

// felt >= (2^128)
if felt.Cmp(&safemath.FeltMax128) != -1 {
if felt.Cmp(&utils.FeltMax128) != -1 {
return fmt.Errorf("check write: 2**128 < %s", value)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/vm/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

"github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

Expand Down Expand Up @@ -135,7 +135,7 @@ func (segment *Segment) IncreaseSegmentSize(newSize uint64) {
if cap(segmentData) > int(newSize) {
newSegmentData = segmentData[:cap(segmentData)]
} else {
newSegmentData = make([]MemoryValue, safemath.Max(newSize, uint64(len(segmentData)*2)))
newSegmentData = make([]MemoryValue, utils.Max(newSize, uint64(len(segmentData)*2)))
copy(newSegmentData, segmentData)
}
segment.Data = newSegmentData
Expand Down
4 changes: 2 additions & 2 deletions pkg/vm/memory/memory_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"unsafe"

"github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
"golang.org/x/exp/constraints"
)
Expand All @@ -26,7 +26,7 @@ func (address *MemoryAddress) Equal(other *MemoryAddress) bool {

// It crates a new memory address with the modified offset
func (address *MemoryAddress) AddOffset(offset int16) (MemoryAddress, error) {
newOffset, overflow := safemath.SafeOffset(address.Offset, offset)
newOffset, overflow := utils.SafeOffset(address.Offset, offset)
if overflow {
return UnknownAddress,
fmt.Errorf(
Expand Down
8 changes: 4 additions & 4 deletions pkg/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

a "github.com/NethermindEth/cairo-vm-go/pkg/assembler"
safemath "github.com/NethermindEth/cairo-vm-go/pkg/safemath"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)
Expand Down Expand Up @@ -215,7 +215,7 @@ func (vm *VirtualMachine) getDstAddr(instruction *a.Instruction) (mem.MemoryAddr
dstRegister = vm.Context.Fp
}

addr, isOverflow := safemath.SafeOffset(dstRegister, instruction.OffDest)
addr, isOverflow := utils.SafeOffset(dstRegister, instruction.OffDest)
if isOverflow {
return mem.UnknownAddress, fmt.Errorf("offset overflow: %d + %d", dstRegister, instruction.OffDest)
}
Expand All @@ -230,7 +230,7 @@ func (vm *VirtualMachine) getOp0Addr(instruction *a.Instruction) (mem.MemoryAddr
op0Register = vm.Context.Fp
}

addr, isOverflow := safemath.SafeOffset(op0Register, instruction.OffOp0)
addr, isOverflow := utils.SafeOffset(op0Register, instruction.OffOp0)
if isOverflow {
return mem.UnknownAddress,
fmt.Errorf("offset overflow: %d + %d", op0Register, instruction.OffOp0)
Expand Down Expand Up @@ -261,7 +261,7 @@ func (vm *VirtualMachine) getOp1Addr(instruction *a.Instruction, op0Addr *mem.Me
op1Address = vm.Context.AddressAp()
}

newOffset, isOverflow := safemath.SafeOffset(op1Address.Offset, instruction.OffOp1)
newOffset, isOverflow := utils.SafeOffset(op1Address.Offset, instruction.OffOp1)
if isOverflow {
return mem.UnknownAddress, fmt.Errorf("offset overflow: %d + %d", op1Address.Offset, instruction.OffOp1)
}
Expand Down

0 comments on commit 0746710

Please sign in to comment.