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 27, 2024
1 parent f1386d6 commit 46f8fc3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
23 changes: 10 additions & 13 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,25 +49,20 @@ 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))
}
// 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))

return a
for i := 0; i < 2; i++ {
xInverseModPSplit[i] = *new(fp.Element).SetBigInt(new(big.Int).And(xInverseModPBig, mask))
xInverseModPBig.Rsh(xInverseModPBig, uint(BITSHIFT))
}

xInverseModPSplit := split(*xInverseModPBig, 128, 2)

resAddr, err := xInverseModP.GetAddress(vm)
if err != nil {
return err
Expand Down

0 comments on commit 46f8fc3

Please sign in to comment.