Skip to content

Commit

Permalink
use u256
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Sep 29, 2023
1 parent 48278f1 commit b2c5ffc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pkg/hintrunner/constants.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package hintrunner

import (
"math/big"
"github.com/holiman/uint256"

f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
)

func MaxU128() *big.Int {
return big.NewInt(0).SetBits([]big.Word{18446744073709551615, 18446744073709551615})
func MaxU128() *uint256.Int {
return uint256.NewInt(0).SetBytes([]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255})
}

func MaxU128Felt() *f.Element {
Expand Down
12 changes: 7 additions & 5 deletions pkg/hintrunner/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"math/big"

"github.com/holiman/uint256"

VM "github.com/NethermindEth/cairo-vm-go/pkg/vm"
"github.com/NethermindEth/cairo-vm-go/pkg/vm/memory"
f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp"
Expand Down Expand Up @@ -129,18 +131,18 @@ func (hint WideMul128) Execute(vm *VM.VirtualMachine) error {
return fmt.Errorf("rhs operand %s should be u128", rhsFelt)
}

mul := big.NewInt(1).Mul(lhsFelt.BigInt(big.NewInt(1)), rhsFelt.BigInt(big.NewInt(1)))
mul := uint256.NewInt(1).Mul(uint256.MustFromBig(lhsFelt.BigInt(big.NewInt(1))), uint256.MustFromBig(rhsFelt.BigInt(big.NewInt(1))))
mask := MaxU128()

low := big.NewInt(1)
high := big.NewInt(1)
low := uint256.NewInt(1)
high := uint256.NewInt(1)
low.And(mul, mask)
high.Rsh(mul, 128)

lowFelt := &f.Element{}
lowFelt.SetBigInt(low)
lowFelt.SetBigInt(low.ToBig())
highFelt := &f.Element{}
highFelt.SetBigInt(high)
highFelt.SetBigInt(high.ToBig())

lowCell, err := hint.low.Get(vm)
if err != nil {
Expand Down

0 comments on commit b2c5ffc

Please sign in to comment.