Skip to content

Commit

Permalink
fix: inline split fn + fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed Jul 29, 2024
1 parent 310ec30 commit 1f20df9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
24 changes: 10 additions & 14 deletions pkg/hintrunner/zero/zerohint_uint512.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
const (
P_LOW = "201385395114098847380338600778089168199"
P_HIGH = "64323764613183177041862057485226039389"

BITSHIFT = 128
)

// InvModPUint512 hint computes the inverse modulo a prime number `p` of 512 bits
Expand Down Expand Up @@ -47,24 +49,18 @@ func newInvModPUint512Hint(x, xInverseModP hinter.ResOperander) hinter.Hinter {
return err
}

x := Pack(128, xLoLow, xLoHigh, xHiLow, xHiHigh)
p := Pack(128, pLow, pHigh)
x := Pack(BITSHIFT, xLoLow, xLoHigh, xHiLow, xHiHigh)
p := Pack(BITSHIFT, pLow, pHigh)

xInverseModPBig := new(big.Int).Exp(&x, big.NewInt(-1), &p)

split := func(num big.Int, numBitsShift uint16, length int) []fp.Element {
a := make([]fp.Element, length)
mask := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), uint(numBitsShift)), big.NewInt(1))

for i := 0; i < length; i++ {
a[i] = *new(fp.Element).SetBigInt(new(big.Int).And(&num, mask))
num.Rsh(&num, uint(numBitsShift))
}

return a
}
// split big.Int into two fp.Elements
xInverseModPSplit := make([]fp.Element, 2)
mask := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), uint(BITSHIFT)), big.NewInt(1))

xInverseModPSplit := split(*xInverseModPBig, 128, 2)
xInverseModPSplit[0] = *new(fp.Element).SetBigInt(new(big.Int).And(xInverseModPBig, mask))
xInverseModPBig.Rsh(xInverseModPBig, uint(BITSHIFT))
xInverseModPSplit[1] = *new(fp.Element).SetBigInt(xInverseModPBig)

resAddr, err := xInverseModP.GetAddress(vm)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/hintrunner/zero/zerohint_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func GetUint256ExpandAsFelts(vm *VM.VirtualMachine, ref hinter.ResOperander) ([]
}
}
return uint256Expanded, nil
}

func GetUint512AsFelts(vm *VM.VirtualMachine, ref hinter.ResOperander) (*fp.Element, *fp.Element, *fp.Element, *fp.Element, error) {
var fps [4]*fp.Element
Expand Down

0 comments on commit 1f20df9

Please sign in to comment.