Skip to content

Commit

Permalink
Merge branch 'main' into missing_dict_functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
MaksymMalicki authored Jan 10, 2025
2 parents a32add7 + ef6735c commit 07f4016
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 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 @@ -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 {
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
2 changes: 2 additions & 0 deletions 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}

for _, token := range preCostTokenTypes {
cost, err := getTokenGasCost(token)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 07f4016

Please sign in to comment.