Skip to content

Commit

Permalink
Add fixes for hints, change the order of tokens in gas.go (#679)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
MaksymMalicki authored Jan 10, 2025
1 parent 75ec869 commit ef6735c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
39 changes: 15 additions & 24 deletions pkg/hintrunner/core/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(&quotient)
quotientVal := mem.MemoryValueFromFieldElement(quotient)
err = vm.Memory.WriteToAddress(&quotientAddr, &quotientVal)
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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1859,7 +1849,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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hintrunner/core/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/runner/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func gasInitialization(memory *mem.Memory) error {
if err != nil {
return err
}
preCostTokenTypes := []TokenGasCost{PedersenToken, PoseidonToken, BitwiseToken, EcOpToken, AddModToken, MulModToken}

preCostTokenTypes := []TokenGasCost{PedersenToken, BitwiseToken, EcOpToken, PoseidonToken, AddModToken, MulModToken}

for _, token := range preCostTokenTypes {
cost, err := getTokenGasCost(token)
if err != nil {
Expand Down

0 comments on commit ef6735c

Please sign in to comment.