From 1f20df99a726991e95b4dc2636dde9b30977d185 Mon Sep 17 00:00:00 2001 From: Valentin Dosimont Date: Sat, 27 Jul 2024 07:33:55 +0200 Subject: [PATCH] fix: inline split fn + fix integration tests --- ...12.cairo => inv_mod_p_uint512.small.cairo} | 0 pkg/hintrunner/zero/zerohint_uint512.go | 24 ++++++++----------- pkg/hintrunner/zero/zerohint_utils.go | 1 + 3 files changed, 11 insertions(+), 14 deletions(-) rename integration_tests/cairo_zero_hint_tests/{inv_mod_p_uint512.cairo => inv_mod_p_uint512.small.cairo} (100%) diff --git a/integration_tests/cairo_zero_hint_tests/inv_mod_p_uint512.cairo b/integration_tests/cairo_zero_hint_tests/inv_mod_p_uint512.small.cairo similarity index 100% rename from integration_tests/cairo_zero_hint_tests/inv_mod_p_uint512.cairo rename to integration_tests/cairo_zero_hint_tests/inv_mod_p_uint512.small.cairo diff --git a/pkg/hintrunner/zero/zerohint_uint512.go b/pkg/hintrunner/zero/zerohint_uint512.go index 711fcd104..58da52bf1 100644 --- a/pkg/hintrunner/zero/zerohint_uint512.go +++ b/pkg/hintrunner/zero/zerohint_uint512.go @@ -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 @@ -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 { diff --git a/pkg/hintrunner/zero/zerohint_utils.go b/pkg/hintrunner/zero/zerohint_utils.go index 5b15748c5..e581952d5 100644 --- a/pkg/hintrunner/zero/zerohint_utils.go +++ b/pkg/hintrunner/zero/zerohint_utils.go @@ -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