Skip to content

Commit ef6735c

Browse files
Add fixes for hints, change the order of tokens in gas.go (#679)
* Fixes for the generation of entry code, fixes of hints parsing * Add modifications to the runner * Add fixes for the entrycode generation * Refactor main CLI, offset the hints indexes by entry code size, load arguments and initial gas to the memory * Add available gas and user args (#677) * Add parsing logic for input user args * Add flags for available gas, input user args, writing args to memory * Fix unit tests for user arguments parsing * Lint the PR * Add user args to hint context * Refactor the code * Fix unconditional append of ExternalWriteArgsToMemory, bug fixes in integration tests * Add fixes of the call size calculation and include ExternalWriteArgsToMemory hint when gas present * Add layouts for integration tests * Add error handling * Fixes in entry code generation * Address changes mentioned in a discussion * Add comment regarding writing to memory in a hint for the future reference in the integration tests with args * Changes in calculations of the initial PC offset, CALL opcode offset incremented by mainFuncOffset, writing user args to the AP in the hint * Turn back VM config to private field * Add error handling on assign of `userArgs` to the initial scope * Lint project * Bump go version from 1.20 -> 1.21 (#678) * Bump go version from 1.20 -> 1.21 * Update golangci-lint * Simplify the Makefile * Correction in the makefile * Fix the integration tests * Fixes in the runner * Fixes in the runner * Fix the unit tests, uncomment pythonVm execution in integration tests, code cleanups * Add writing tokens gas cost to memory * Proper builtins initialization for cairo mode * Add fixes for hints, change the order of tokens in gas.go * Address comments in the PR * gofmt * remove systemtype builtin
1 parent 75ec869 commit ef6735c

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

pkg/hintrunner/core/hint.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
VM "github.com/NethermindEth/cairo-vm-go/pkg/vm"
1515
"github.com/NethermindEth/cairo-vm-go/pkg/vm/builtins"
1616
mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
17+
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
1718
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
1819
)
1920

@@ -431,45 +432,31 @@ func (hint DivMod) Execute(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) e
431432
return fmt.Errorf("cannot be divided by zero, rhs: %v", rhsFelt)
432433
}
433434

434-
lhsvalue := uint256.Int(lhsFelt.Bits())
435-
rhsvalue := uint256.Int(rhsFelt.Bits())
436-
437-
// get quotient
438-
quo := uint256.Int{}
439-
quo.Div(&lhsvalue, &rhsvalue)
440-
441-
quotient := f.Element{}
442-
quoVal := quo.Uint64()
443-
quotient.SetUint64(quoVal)
435+
lhsBig := big.NewInt(0)
436+
lhsFelt.BigInt(lhsBig)
437+
rhsBig := big.NewInt(0)
438+
rhsFelt.BigInt(rhsBig)
439+
quoBig, remBig := new(big.Int).DivMod(lhsBig, rhsBig, new(big.Int))
444440

441+
quotient := new(f.Element).SetBigInt(quoBig)
442+
remainder := new(f.Element).SetBigInt(remBig)
445443
quotientAddr, err := hint.quotient.Get(vm)
446444
if err != nil {
447445
return fmt.Errorf("get quotient cell: %v", err)
448446
}
449447

450-
quotientVal := mem.MemoryValueFromFieldElement(&quotient)
448+
quotientVal := mem.MemoryValueFromFieldElement(quotient)
451449
err = vm.Memory.WriteToAddress(&quotientAddr, &quotientVal)
452450
if err != nil {
453451
return fmt.Errorf("write cell: %v", err)
454452
}
455453

456-
// get remainder: lhs - (rhs * quotient)
457-
temp := uint256.Int{}
458-
temp.Mul(&rhsvalue, &quo)
459-
460-
rem := uint256.Int{}
461-
rem.Sub(&lhsvalue, &temp)
462-
463-
remainder := f.Element{}
464-
remVal := rem.Uint64()
465-
remainder.SetUint64(remVal)
466-
467454
remainderAddr, err := hint.remainder.Get(vm)
468455
if err != nil {
469456
return fmt.Errorf("get remainder cell: %v", err)
470457
}
471458

472-
remainderVal := mem.MemoryValueFromFieldElement(&remainder)
459+
remainderVal := mem.MemoryValueFromFieldElement(remainder)
473460
err = vm.Memory.WriteToAddress(&remainderAddr, &remainderVal)
474461
if err != nil {
475462
return fmt.Errorf("write cell: %v", err)
@@ -667,6 +654,9 @@ func (hint Uint256InvModN) Execute(vm *VM.VirtualMachine, _ *hinter.HintRunnerCo
667654
} else {
668655
r.Rem(&r, n)
669656

657+
if r.Cmp(big.NewInt(0)) == -1 {
658+
r.Add(&r, n)
659+
}
670660
k := new(big.Int).Mul(&r, b)
671661
k.Sub(k, big.NewInt(1))
672662
k.Div(k, n)
@@ -1859,7 +1849,8 @@ func (hint *RandomEcPoint) Execute(vm *VM.VirtualMachine, _ *hinter.HintRunnerCo
18591849
}
18601850

18611851
xVal := mem.MemoryValueFromFieldElement(&randomX)
1862-
yVal := mem.MemoryValueFromFieldElement(randomYSquared.Square(&randomYSquared))
1852+
sqrt := new(fp.Element).Sqrt(&randomYSquared)
1853+
yVal := mem.MemoryValueFromFieldElement(sqrt)
18631854

18641855
xAddr, err := hint.x.Get(vm)
18651856
if err != nil {

pkg/hintrunner/core/hint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ func TestRandomEcPoint(t *testing.T) {
20352035
&f.Element{12217889558999792019, 3067322962467879919, 3160430244162662030, 474947714424245026},
20362036
)
20372037
expectedY := mem.MemoryValueFromFieldElement(
2038-
&f.Element{12193331470568888984, 1737428559173019240, 11500517745011090163, 245183001587853482},
2038+
&f.Element{1841133414678692521, 1145993510131007954, 1525768223135088880, 238810195105172937},
20392039
)
20402040

20412041
actualX := utils.ReadFrom(vm, VM.ExecutionSegment, 0)

pkg/runner/gas.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ func gasInitialization(memory *mem.Memory) error {
5050
if err != nil {
5151
return err
5252
}
53-
preCostTokenTypes := []TokenGasCost{PedersenToken, PoseidonToken, BitwiseToken, EcOpToken, AddModToken, MulModToken}
53+
54+
preCostTokenTypes := []TokenGasCost{PedersenToken, BitwiseToken, EcOpToken, PoseidonToken, AddModToken, MulModToken}
55+
5456
for _, token := range preCostTokenTypes {
5557
cost, err := getTokenGasCost(token)
5658
if err != nil {

0 commit comments

Comments
 (0)