diff --git a/pkg/hintrunner/core/hint.go b/pkg/hintrunner/core/hint.go index f6e348d7..b87c6fd7 100644 --- a/pkg/hintrunner/core/hint.go +++ b/pkg/hintrunner/core/hint.go @@ -14,6 +14,7 @@ import ( VM "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" + "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -431,45 +432,31 @@ func (hint DivMod) Execute(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) e return fmt.Errorf("cannot be divided by zero, rhs: %v", rhsFelt) } - lhsvalue := uint256.Int(lhsFelt.Bits()) - rhsvalue := uint256.Int(rhsFelt.Bits()) - - // get quotient - quo := uint256.Int{} - quo.Div(&lhsvalue, &rhsvalue) - - quotient := f.Element{} - quoVal := quo.Uint64() - quotient.SetUint64(quoVal) + lhsBig := big.NewInt(0) + lhsFelt.BigInt(lhsBig) + rhsBig := big.NewInt(0) + rhsFelt.BigInt(rhsBig) + quoBig, remBig := new(big.Int).DivMod(lhsBig, rhsBig, new(big.Int)) + quotient := new(f.Element).SetBigInt(quoBig) + remainder := new(f.Element).SetBigInt(remBig) quotientAddr, err := hint.quotient.Get(vm) if err != nil { return fmt.Errorf("get quotient cell: %v", err) } - quotientVal := mem.MemoryValueFromFieldElement("ient) + quotientVal := mem.MemoryValueFromFieldElement(quotient) err = vm.Memory.WriteToAddress("ientAddr, "ientVal) if err != nil { return fmt.Errorf("write cell: %v", err) } - // get remainder: lhs - (rhs * quotient) - temp := uint256.Int{} - temp.Mul(&rhsvalue, &quo) - - rem := uint256.Int{} - rem.Sub(&lhsvalue, &temp) - - remainder := f.Element{} - remVal := rem.Uint64() - remainder.SetUint64(remVal) - remainderAddr, err := hint.remainder.Get(vm) if err != nil { return fmt.Errorf("get remainder cell: %v", err) } - remainderVal := mem.MemoryValueFromFieldElement(&remainder) + remainderVal := mem.MemoryValueFromFieldElement(remainder) err = vm.Memory.WriteToAddress(&remainderAddr, &remainderVal) if err != nil { return fmt.Errorf("write cell: %v", err) @@ -667,6 +654,9 @@ func (hint Uint256InvModN) Execute(vm *VM.VirtualMachine, _ *hinter.HintRunnerCo } else { r.Rem(&r, n) + if r.Cmp(big.NewInt(0)) == -1 { + r.Add(&r, n) + } k := new(big.Int).Mul(&r, b) k.Sub(k, big.NewInt(1)) k.Div(k, n) @@ -1868,7 +1858,8 @@ func (hint *RandomEcPoint) Execute(vm *VM.VirtualMachine, _ *hinter.HintRunnerCo } xVal := mem.MemoryValueFromFieldElement(&randomX) - yVal := mem.MemoryValueFromFieldElement(randomYSquared.Square(&randomYSquared)) + sqrt := new(fp.Element).Sqrt(&randomYSquared) + yVal := mem.MemoryValueFromFieldElement(sqrt) xAddr, err := hint.x.Get(vm) if err != nil { diff --git a/pkg/hintrunner/core/hint_test.go b/pkg/hintrunner/core/hint_test.go index 058c44ee..f33dd22b 100644 --- a/pkg/hintrunner/core/hint_test.go +++ b/pkg/hintrunner/core/hint_test.go @@ -2035,7 +2035,7 @@ func TestRandomEcPoint(t *testing.T) { &f.Element{12217889558999792019, 3067322962467879919, 3160430244162662030, 474947714424245026}, ) expectedY := mem.MemoryValueFromFieldElement( - &f.Element{12193331470568888984, 1737428559173019240, 11500517745011090163, 245183001587853482}, + &f.Element{1841133414678692521, 1145993510131007954, 1525768223135088880, 238810195105172937}, ) actualX := utils.ReadFrom(vm, VM.ExecutionSegment, 0) diff --git a/pkg/runner/gas.go b/pkg/runner/gas.go index b8c9ab72..7e75670e 100644 --- a/pkg/runner/gas.go +++ b/pkg/runner/gas.go @@ -50,7 +50,9 @@ func gasInitialization(memory *mem.Memory) error { if err != nil { return err } + preCostTokenTypes := []TokenGasCost{PedersenToken, PoseidonToken, BitwiseToken, EcOpToken, AddModToken, MulModToken} + for _, token := range preCostTokenTypes { cost, err := getTokenGasCost(token) if err != nil { diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index d82244af..80ee11bb 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -584,6 +584,7 @@ func GetEntryCodeInstructions(function starknet.EntryPointByFunction, finalizeFo } apOffset += paramsSize usedArgs := 0 + for _, builtin := range function.Builtins { if offset, isBuiltin := builtinsOffsetsMap[builtin]; isBuiltin { ctx.AddInlineCASM(