-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c37c85
commit b347dd8
Showing
5 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package zero | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" | ||
VM "github.com/NethermindEth/cairo-vm-go/pkg/vm" | ||
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp" | ||
) | ||
|
||
const ( | ||
P_LOW = "201385395114098847380338600778089168199" | ||
P_HIGH = "64323764613183177041862057485226039389" | ||
) | ||
|
||
func createInvModPUint512Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { | ||
x, err := resolver.GetResOperander("x") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
xInverseModP, err := resolver.GetResOperander("x_inverse_mod_p") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newInvModPUint512Hint(x, xInverseModP), nil | ||
} | ||
|
||
func newInvModPUint512Hint(x, xInverseModP hinter.ResOperander) hinter.Hinter { | ||
return &GenericZeroHinter{ | ||
Name: "InvModPUint512", | ||
Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error { | ||
// def pack_512(u, num_bits_shift: int) -> int: | ||
// limbs = (u.d0, u.d1, u.d2, u.d3) | ||
// return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) | ||
// | ||
// x = pack_512(ids.x, num_bits_shift = 128) | ||
// p = ids.p.low + (ids.p.high << 128) | ||
// x_inverse_mod_p = pow(x,-1, p) | ||
// | ||
// x_inverse_mod_p_split = (x_inverse_mod_p & ((1 << 128) - 1), x_inverse_mod_p >> 128) | ||
// | ||
// ids.x_inverse_mod_p.low = x_inverse_mod_p_split[0] | ||
// ids.x_inverse_mod_p.high = x_inverse_mod_p_split[1] | ||
pack512 := func(lolow, loHigh, hiLow, hiHigh *fp.Element, numBitsShift int) big.Int { | ||
var loLowBig, loHighBig, hiLowBig, hiHighBig big.Int | ||
lolow.BigInt(&loLowBig) | ||
loHigh.BigInt(&loHighBig) | ||
hiLow.BigInt(&hiLowBig) | ||
hiHigh.BigInt(&hiHighBig) | ||
|
||
return *new(big.Int).Add(new(big.Int).Lsh(&hiHighBig, uint(numBitsShift)), &loLowBig).Add(new(big.Int).Lsh(&hiLowBig, uint(numBitsShift)), &loHighBig) | ||
} | ||
pack := func(low, high *fp.Element, numBitsShift int) big.Int { | ||
var lowBig, highBig big.Int | ||
low.BigInt(&lowBig) | ||
high.BigInt(&highBig) | ||
|
||
return *new(big.Int).Add(new(big.Int).Lsh(&highBig, uint(numBitsShift)), &lowBig) | ||
} | ||
|
||
xLoLow, xLoHigh, xHiLow, xHiHigh, err := GetUint512AsFelts(vm, x) | ||
if err != nil { | ||
return err | ||
} | ||
pLow, err := new(fp.Element).SetString(P_LOW) | ||
if err != nil { | ||
return err | ||
} | ||
pHigh, err := new(fp.Element).SetString(P_HIGH) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
x := pack512(xLoLow, xLoHigh, xHiLow, xHiHigh, 128) | ||
p := pack(pLow, pHigh, 128) | ||
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 | ||
} | ||
|
||
xInverseModPSplit := split(*xInverseModPBig, 128, 2) | ||
|
||
resAddr, err := xInverseModP.GetAddress(vm) | ||
if err != nil { | ||
return err | ||
} | ||
return vm.Memory.WriteUint256ToAddress(resAddr, &xInverseModPSplit[0], &xInverseModPSplit[1]) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package zero | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" | ||
"github.com/consensys/gnark-crypto/ecc/stark-curve/fp" | ||
) | ||
|
||
func TestInvModPUint512(t *testing.T) { | ||
runHinterTests(t, map[string][]hintTestCase{ | ||
"InvModPUint512": { | ||
{ | ||
operanders: []*hintOperander{ | ||
{Name: "x.d0", Kind: apRelative, Value: feltUint64(101)}, | ||
{Name: "x.d1", Kind: apRelative, Value: feltUint64(2)}, | ||
{Name: "x.d2", Kind: apRelative, Value: feltUint64(15)}, | ||
{Name: "x.d3", Kind: apRelative, Value: feltUint64(61)}, | ||
{Name: "x_inverse_mod_p.low", Kind: uninitialized}, | ||
{Name: "x_inverse_mod_p.high", Kind: uninitialized}, | ||
}, | ||
makeHinter: func(ctx *hintTestContext) hinter.Hinter { | ||
return newInvModPUint512Hint(ctx.operanders["x.d0"], ctx.operanders["x_inverse_mod_p.low"]) | ||
}, | ||
check: allVarValueEquals(map[string]*fp.Element{ | ||
"x_inverse_mod_p.low": feltString("80275402838848031859800366538378848249"), | ||
"x_inverse_mod_p.high": feltString("5810892639608724280512701676461676039"), | ||
}), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters