Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ModBuiltin + EvalCircuit hint #641

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/hintrunner/core/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"github.com/NethermindEth/cairo-vm-go/pkg/parsers/starknet"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
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"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)
Expand Down Expand Up @@ -1888,3 +1889,42 @@

return vm.Memory.WriteToAddress(&sqrtAddr, &sqrtVal)
}

type EvalCircuit struct {
nAddMods hinter.ResOperander

Check failure on line 1894 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / build

undefined: hinter.ResOperander

Check failure on line 1894 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander

Check failure on line 1894 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander
addModBuiltin hinter.ResOperander

Check failure on line 1895 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / build

undefined: hinter.ResOperander

Check failure on line 1895 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander

Check failure on line 1895 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander
nMulMods hinter.ResOperander

Check failure on line 1896 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / build

undefined: hinter.ResOperander

Check failure on line 1896 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander

Check failure on line 1896 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander
mulModBuiltin hinter.ResOperander

Check failure on line 1897 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / build

undefined: hinter.ResOperander

Check failure on line 1897 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander

Check failure on line 1897 in pkg/hintrunner/core/hint.go

View workflow job for this annotation

GitHub Actions / lint

undefined: hinter.ResOperander
}

func (hint *EvalCircuit) String() string {
return "EvalCircuit"
}

func (hint *EvalCircuit) Execute(vm *VM.VirtualMachine) error {
addModBuiltinAddress, err := hinter.ResolveAsAddress(vm, hint.addModBuiltin)
if err != nil {
return fmt.Errorf("resolve addModBuiltin pointer: %w", err)
}
nAddMods, err := hint.nAddMods.Resolve(vm)
if err != nil {
return fmt.Errorf("resolve nAddMods operand %s: %v", hint.nAddMods, err)
}
nAddModsFelt, err := nAddMods.Uint64()
if err != nil {
return err
}
mulModBuiltinAddress, err := hinter.ResolveAsAddress(vm, hint.mulModBuiltin)
if err != nil {
return fmt.Errorf("resolve mulModBuiltin pointer: %w", err)
}
nMulMods, err := hint.nMulMods.Resolve(vm)
if err != nil {
return fmt.Errorf("resolve nMulMods operand %s: %v", hint.nMulMods, err)
}
nMulModsFelt, err := nMulMods.Uint64()
if err != nil {
return err
}
return builtins.FillMemory(vm.Memory, *addModBuiltinAddress, nAddModsFelt, *mulModBuiltinAddress, nMulModsFelt)
}
12 changes: 12 additions & 0 deletions pkg/utils/math.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import (
"errors"
"fmt"
"math"
"math/big"
"math/bits"
Expand Down Expand Up @@ -156,3 +158,13 @@ func Int16FromBigInt(n *big.Int) (int16, bool) {
func RightRot(value uint32, n uint32) uint32 {
return (value >> n) | ((value & ((1 << n) - 1)) << (32 - n))
}

func SafeDivUint64(x, y uint64) (uint64, error) {
if y == 0 {
return 0, fmt.Errorf("cannot divide: y division is zero")
}
if x%y != 0 {
return 0, errors.New("cannot divide: x is not divisible by y")
}
return x / y, nil
}
Loading
Loading