diff --git a/pkg/hintrunner/utils/math_utils.go b/pkg/hintrunner/utils/math_utils.go index 6a112587..9b789b6f 100644 --- a/pkg/hintrunner/utils/math_utils.go +++ b/pkg/hintrunner/utils/math_utils.go @@ -129,10 +129,45 @@ func sign(n *big.Int) (int, big.Int) { func SafeDiv(x, y *big.Int) (big.Int, error) { if y.Cmp(big.NewInt(0)) == 0 { - return *big.NewInt(0), fmt.Errorf("Division by zero.") + return *big.NewInt(0), fmt.Errorf("division by zero") } if new(big.Int).Mod(x, y).Cmp(big.NewInt(0)) != 0 { - return *big.NewInt(0), fmt.Errorf("%v is not divisible by %v.", x, y) + return *big.NewInt(0), fmt.Errorf("%v is not divisible by %v", x, y) } return *new(big.Int).Div(x, y), nil } + +func IsQuadResidue(x *fp.Element) bool { + // Implementation adapted from sympy implementation which can be found here : + // https://github.com/sympy/sympy/blob/d91b8ad6d36a59a879cc70e5f4b379da5fdd46ce/sympy/ntheory/residue_ntheory.py#L689 + // We have omitted the prime as it will be CAIRO_PRIME + + return x.IsZero() || x.IsOne() || x.Legendre() == 1 +} + +func YSquaredFromX(x, beta, fieldPrime *big.Int) *big.Int { + // Computes y^2 using the curve equation: + // y^2 = x^3 + alpha * x + beta (mod field_prime) + // We ignore alpha as it is a constant with a value of 1 + + ySquaredBigInt := new(big.Int).Set(x) + ySquaredBigInt.Mul(ySquaredBigInt, x).Mod(ySquaredBigInt, fieldPrime) + ySquaredBigInt.Mul(ySquaredBigInt, x).Mod(ySquaredBigInt, fieldPrime) + ySquaredBigInt.Add(ySquaredBigInt, x).Mod(ySquaredBigInt, fieldPrime) + ySquaredBigInt.Add(ySquaredBigInt, beta).Mod(ySquaredBigInt, fieldPrime) + + return ySquaredBigInt +} + +func Sqrt(x, p *big.Int) *big.Int { + // Finds the minimum non-negative integer m such that (m*m) % p == x. + + halfPrimeBigInt := new(big.Int).Rsh(p, 1) + m := new(big.Int).ModSqrt(x, p) + + if m.Cmp(halfPrimeBigInt) > 0 { + m.Sub(p, m) + } + + return m +} diff --git a/pkg/hintrunner/zero/hintcode.go b/pkg/hintrunner/zero/hintcode.go index ed15e7cd..8cd34c4f 100644 --- a/pkg/hintrunner/zero/hintcode.go +++ b/pkg/hintrunner/zero/hintcode.go @@ -50,6 +50,7 @@ const ( // is_quad_residue() hint isQuadResidueCode string = "from starkware.crypto.signature.signature import FIELD_PRIME\nfrom starkware.python.math_utils import div_mod, is_quad_residue, sqrt\n\nx = ids.x\nif is_quad_residue(x, FIELD_PRIME):\n ids.y = sqrt(x, FIELD_PRIME)\nelse:\n ids.y = sqrt(div_mod(x, 3, FIELD_PRIME), FIELD_PRIME)" + // ------ Uint256 hints related code ------ uint256AddCode string = "sum_low = ids.a.low + ids.b.low\nids.carry_low = 1 if sum_low >= ids.SHIFT else 0\nsum_high = ids.a.high + ids.b.high + ids.carry_low\nids.carry_high = 1 if sum_high >= ids.SHIFT else 0" split64Code string = "ids.low = ids.a & ((1<<64) - 1)\nids.high = ids.a >> 64" @@ -97,6 +98,7 @@ ids.multiplicities = segments.gen_arg([len(positions_dict[k]) for k in output])` isZeroNondetCode string = "memory[ap] = to_felt_or_relocatable(x == 0)" isZeroPackCode string = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P, pack\n\nx = pack(ids.x, PRIME) % SECP_P" isZeroDivModCode string = "from starkware.cairo.common.cairo_secp.secp_utils import SECP_P\nfrom starkware.python.math_utils import div_mod\n\nvalue = x_inv = div_mod(1, x, SECP_P)" + recoverYCode string = "from starkware.crypto.signature.signature import ALPHA, BETA, FIELD_PRIME\nfrom starkware.python.math_utils import recover_y\nids.p.x = ids.x\n# This raises an exception if `x` is not on the curve.\nids.p.y = recover_y(ids.x, ALPHA, BETA, FIELD_PRIME)" // ------ Signature hints related code ------ verifyECDSASignatureCode string = "ecdsa_builtin.add_signature(ids.ecdsa_ptr.address_, (ids.signature_r, ids.signature_s))" @@ -120,6 +122,11 @@ ids.multiplicities = segments.gen_arg([len(positions_dict[k]) for k in output])` blockPermutationCode string = "from starkware.cairo.common.keccak_utils.keccak_utils import keccak_func\n_keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS)\nassert 0 <= _keccak_state_size_felts < 100\n\noutput_values = keccak_func(memory.get_range(\n ids.keccak_ptr - _keccak_state_size_felts, _keccak_state_size_felts))\nsegments.write_arg(ids.keccak_ptr, output_values)" compareBytesInWordCode string = "memory[ap] = to_felt_or_relocatable(ids.n_bytes < ids.BYTES_IN_WORD)" compareKeccakFullRateInBytesCode string = "memory[ap] = to_felt_or_relocatable(ids.n_bytes >= ids.KECCAK_FULL_RATE_IN_BYTES)" + splitInput3Code string = "ids.high3, ids.low3 = divmod(memory[ids.inputs + 3], 256)" + splitInput6Code string = "ids.high6, ids.low6 = divmod(memory[ids.inputs + 6], 256 ** 2)" + splitInput9Code string = "ids.high9, ids.low9 = divmod(memory[ids.inputs + 9], 256 ** 3)" + splitInput12Code string = "ids.high12, ids.low12 = divmod(memory[ids.inputs + 12], 256 ** 4)" + splitInput15Code string = "ids.high15, ids.low15 = divmod(memory[ids.inputs + 15], 256 ** 5)" splitOutputMidLowHighCode string = "tmp, ids.output1_low = divmod(ids.output1, 256 ** 7)\nids.output1_high, ids.output1_mid = divmod(tmp, 2 ** 128)" SplitNBytesCode string = "ids.n_words_to_copy, ids.n_bytes_left = divmod(ids.n_bytes, ids.BYTES_IN_WORD)" diff --git a/pkg/hintrunner/zero/zerohint.go b/pkg/hintrunner/zero/zerohint.go index f4fdc214..89a157a4 100644 --- a/pkg/hintrunner/zero/zerohint.go +++ b/pkg/hintrunner/zero/zerohint.go @@ -149,6 +149,8 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64 return createIsZeroPackHinter(resolver) case isZeroDivModCode: return createIsZeroDivModHinter() + case recoverYCode: + return createRecoverYHinter(resolver) // Blake hints case blake2sAddUint256BigendCode: return createBlake2sAddUint256Hinter(resolver, true) @@ -173,6 +175,16 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64 return createBlockPermutationHinter(resolver) case compareBytesInWordCode: return createCompareBytesInWordNondetHinter(resolver) + case splitInput3Code: + return createSplitInput3Hinter(resolver) + case splitInput6Code: + return createSplitInput6Hinter(resolver) + case splitInput9Code: + return createSplitInput9Hinter(resolver) + case splitInput12Code: + return createSplitInput12Hinter(resolver) + case splitInput15Code: + return createSplitInput15Hinter(resolver) case splitOutputMidLowHighCode: return createSplitOutputMidLowHighHinter(resolver) case SplitNBytesCode: diff --git a/pkg/hintrunner/zero/zerohint_ec.go b/pkg/hintrunner/zero/zerohint_ec.go index 3424ccd3..02838a13 100644 --- a/pkg/hintrunner/zero/zerohint_ec.go +++ b/pkg/hintrunner/zero/zerohint_ec.go @@ -862,3 +862,86 @@ func newIsZeroDivModHint() hinter.Hinter { func createIsZeroDivModHinter() (hinter.Hinter, error) { return newIsZeroDivModHint(), nil } + +// RecoverY hint Recovers the y coordinate of a point on the elliptic curve +// y^2 = x^3 + alpha * x + beta (mod field_prime) of a given x coordinate. +// +// `newRecoverYHint` takes 2 operanders as arguments +// - `x` is the x coordinate of an elliptic curve point +// - `p` is one of the two EC points with the given x coordinate (x, y) +func newRecoverYHint(x, p hinter.ResOperander) hinter.Hinter { + return &GenericZeroHinter{ + Name: "RecoverY", + Op: func(vm *VM.VirtualMachine, ctx *hinter.HintRunnerContext) error { + //> from starkware.crypto.signature.signature import ALPHA, BETA, FIELD_PRIME + //> from starkware.python.math_utils import recover_y + //> ids.p.x = ids.x + //> # This raises an exception if `x` is not on the curve. + //> ids.p.y = recover_y(ids.x, ALPHA, BETA, FIELD_PRIME) + + pXAddr, err := p.GetAddress(vm) + if err != nil { + return err + } + + pYAddr, err := pXAddr.AddOffset(1) + if err != nil { + return err + } + + xFelt, err := hinter.ResolveAsFelt(vm, x) + if err != nil { + return err + } + + valueX := mem.MemoryValueFromFieldElement(xFelt) + + err = vm.Memory.WriteToAddress(&pXAddr, &valueX) + if err != nil { + return err + } + + const betaString = "3141592653589793238462643383279502884197169399375105820974944592307816406665" + betaBigInt, ok := new(big.Int).SetString(betaString, 10) + if !ok { + panic("failed to convert BETA string to big.Int") + } + + const fieldPrimeString = "3618502788666131213697322783095070105623107215331596699973092056135872020481" + fieldPrimeBigInt, ok := new(big.Int).SetString(fieldPrimeString, 10) + if !ok { + panic("failed to convert FIELD_PRIME string to big.Int") + } + + xBigInt := new(big.Int) + xFelt.BigInt(xBigInt) + + // y^2 = x^3 + alpha * x + beta (mod field_prime) + ySquaredBigInt := secp_utils.YSquaredFromX(xBigInt, betaBigInt, fieldPrimeBigInt) + ySquaredFelt := new(fp.Element).SetBigInt(ySquaredBigInt) + + if secp_utils.IsQuadResidue(ySquaredFelt) { + result := new(fp.Element).SetBigInt(secp_utils.Sqrt(ySquaredBigInt, fieldPrimeBigInt)) + value := mem.MemoryValueFromFieldElement(result) + return vm.Memory.WriteToAddress(&pYAddr, &value) + } else { + ySquaredString := ySquaredBigInt.String() + return fmt.Errorf("%s does not represent the x coordinate of a point on the curve", ySquaredString) + } + }, + } +} + +func createRecoverYHinter(resolver hintReferenceResolver) (hinter.Hinter, error) { + x, err := resolver.GetResOperander("x") + if err != nil { + return nil, err + } + + p, err := resolver.GetResOperander("p") + if err != nil { + return nil, err + } + + return newRecoverYHint(x, p), nil +} diff --git a/pkg/hintrunner/zero/zerohint_ec_test.go b/pkg/hintrunner/zero/zerohint_ec_test.go index 5e5abb76..07f366c8 100644 --- a/pkg/hintrunner/zero/zerohint_ec_test.go +++ b/pkg/hintrunner/zero/zerohint_ec_test.go @@ -974,6 +974,86 @@ func TestZeroHintEc(t *testing.T) { check: varValueInScopeEquals("value", bigIntString("4", 10)), }, }, + "RecoverY": { + { + operanders: []*hintOperander{ + {Name: "x", Kind: apRelative, Value: feltString("2497468900767850684421727063357792717599762502387246235265616708902555305129")}, + {Name: "p.x", Kind: uninitialized}, + {Name: "p.y", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newRecoverYHint(ctx.operanders["x"], ctx.operanders["p.x"]) + }, + check: allVarValueEquals(map[string]*fp.Element{ + "p.x": feltString("2497468900767850684421727063357792717599762502387246235265616708902555305129"), + "p.y": feltString("205857351767627712295703269674687767888261140702556021834663354704341414042"), + }), + }, + { + operanders: []*hintOperander{ + {Name: "x", Kind: apRelative, Value: feltString("205857351767627712295703269674687767888261140702556021834663354704341414042")}, + {Name: "p.x", Kind: uninitialized}, + {Name: "p.y", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newRecoverYHint(ctx.operanders["x"], ctx.operanders["p.x"]) + }, + errCheck: errorTextContains("does not represent the x coordinate of a point on the curve"), + }, + { + operanders: []*hintOperander{ + {Name: "x", Kind: apRelative, Value: feltString("3004956058830981475544150447242655232275382685012344776588097793621230049020")}, + {Name: "p.x", Kind: uninitialized}, + {Name: "p.y", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newRecoverYHint(ctx.operanders["x"], ctx.operanders["p.x"]) + }, + check: allVarValueEquals(map[string]*fp.Element{ + "p.x": feltString("3004956058830981475544150447242655232275382685012344776588097793621230049020"), + "p.y": feltString("386236054595386575795345623791920124827519018828430310912260655089307618738"), + }), + }, + { + operanders: []*hintOperander{ + {Name: "x", Kind: apRelative, Value: feltString("138597138396302485058562442936200017709939129389766076747102238692717075504")}, + {Name: "p.x", Kind: uninitialized}, + {Name: "p.y", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newRecoverYHint(ctx.operanders["x"], ctx.operanders["p.x"]) + }, + check: allVarValueEquals(map[string]*fp.Element{ + "p.x": feltString("138597138396302485058562442936200017709939129389766076747102238692717075504"), + "p.y": feltString("1116947097676727397390632683964789044871379304271794004325353078455954290524"), + }), + }, + { + operanders: []*hintOperander{ + {Name: "x", Kind: apRelative, Value: feltString("71635783675677659163985681365816684268526846280467284682674852685628658265882465826464572245")}, + {Name: "p.x", Kind: uninitialized}, + {Name: "p.y", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newRecoverYHint(ctx.operanders["x"], ctx.operanders["p.x"]) + }, + check: allVarValueEquals(map[string]*fp.Element{ + "p.x": feltString("71635783675677659163985681365816684268526846280467284682674852685628658265882465826464572245"), + "p.y": feltString("903372048565605391120071143811887302063650776015287438589675702929494830362"), + }), + }, + { + operanders: []*hintOperander{ + {Name: "x", Kind: apRelative, Value: feltString("42424242424242424242")}, + {Name: "p.x", Kind: uninitialized}, + {Name: "p.y", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newRecoverYHint(ctx.operanders["x"], ctx.operanders["p.x"]) + }, + errCheck: errorTextContains("does not represent the x coordinate of a point on the curve"), + }, + }, }, ) } diff --git a/pkg/hintrunner/zero/zerohint_keccak.go b/pkg/hintrunner/zero/zerohint_keccak.go index 782adc8e..36169e43 100644 --- a/pkg/hintrunner/zero/zerohint_keccak.go +++ b/pkg/hintrunner/zero/zerohint_keccak.go @@ -572,6 +572,180 @@ func createCompareBytesInWordNondetHinter(resolver hintReferenceResolver) (hinte return newCompareBytesInWordHint(nBytes), nil } +// SplitInput12 hint assigns to `ids.high12` and `ids.low12` variables +// the quotient and remainder of the division of the value at memory address +// `ids.inputs + 12` by 256 ** 4 +// +// `newSplitInput12Hint` takes 3 operanders as arguments +// - `high12` is the variable that will store the quotient of the division +// - `low12` is the variable that will store the remainder of the division +// - `inputs` is the address in memory to which we add an offset of 12 and read that value +func newSplitInput12Hint(high12, low12, inputs hinter.ResOperander) hinter.Hinter { + return &GenericZeroHinter{ + Name: "SplitInput12", + Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error { + //> ids.high12, ids.low12 = divmod(memory[ids.inputs + 12], 256 ** 4) + + high12Addr, err := high12.GetAddress(vm) + if err != nil { + return err + } + + low12Addr, err := low12.GetAddress(vm) + if err != nil { + return err + } + + inputsAddr, err := hinter.ResolveAsAddress(vm, inputs) + if err != nil { + return err + } + + *inputsAddr, err = inputsAddr.AddOffset(12) + if err != nil { + return err + } + + inputValue, err := vm.Memory.ReadFromAddress(inputsAddr) + if err != nil { + return err + } + + var inputBigInt big.Int + inputValue.Felt.BigInt(&inputBigInt) + + // 256 ** 4 + divisor := big.NewInt(4294967296) + + high12BigInt := new(big.Int) + low12BigInt := new(big.Int) + + high12BigInt.DivMod(&inputBigInt, divisor, low12BigInt) + + var high12Felt fp.Element + high12Felt.SetBigInt(high12BigInt) + high12Mv := memory.MemoryValueFromFieldElement(&high12Felt) + + var low12Felt fp.Element + low12Felt.SetBigInt(low12BigInt) + low12Mv := memory.MemoryValueFromFieldElement(&low12Felt) + + err = vm.Memory.WriteToAddress(&low12Addr, &low12Mv) + if err != nil { + return err + } + + return vm.Memory.WriteToAddress(&high12Addr, &high12Mv) + }, + } +} + +func createSplitInput12Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { + high12, err := resolver.GetResOperander("high12") + if err != nil { + return nil, err + } + + low12, err := resolver.GetResOperander("low12") + if err != nil { + return nil, err + } + + inputs, err := resolver.GetResOperander("inputs") + if err != nil { + return nil, err + } + + return newSplitInput12Hint(high12, low12, inputs), nil +} + +// SplitInput15 hint assigns to `ids.high15` and `ids.low15` variables +// the quotient and remainder of the division of the value at memory address +// `ids.inputs + 15` by 256 ** 5 +// +// `newSplitInput9Hint` takes 3 operanders as arguments +// - `high15` is the variable that will store the quotient of the division +// - `low15` is the variable that will store the remainder of the division +// - `inputs` is the address in memory to which we add an offset of 15 and read that value +func newSplitInput15Hint(high15, low15, inputs hinter.ResOperander) hinter.Hinter { + return &GenericZeroHinter{ + Name: "SplitInput15", + Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error { + //> ids.high15, ids.low15 = divmod(memory[ids.inputs + 15], 256 ** 5) + + high15Addr, err := high15.GetAddress(vm) + if err != nil { + return err + } + + low15Addr, err := low15.GetAddress(vm) + if err != nil { + return err + } + + inputsAddr, err := hinter.ResolveAsAddress(vm, inputs) + if err != nil { + return err + } + + *inputsAddr, err = inputsAddr.AddOffset(15) + if err != nil { + return err + } + + inputValue, err := vm.Memory.ReadFromAddress(inputsAddr) + if err != nil { + return err + } + + var inputBigInt big.Int + inputValue.Felt.BigInt(&inputBigInt) + + // 256 ** 5 + divisor := big.NewInt(1099511627776) + + high15BigInt := new(big.Int) + low15BigInt := new(big.Int) + + high15BigInt.DivMod(&inputBigInt, divisor, low15BigInt) + + var high15Felt fp.Element + high15Felt.SetBigInt(high15BigInt) + high15Mv := memory.MemoryValueFromFieldElement(&high15Felt) + + var low15Felt fp.Element + low15Felt.SetBigInt(low15BigInt) + low15Mv := memory.MemoryValueFromFieldElement(&low15Felt) + + err = vm.Memory.WriteToAddress(&low15Addr, &low15Mv) + if err != nil { + return err + } + + return vm.Memory.WriteToAddress(&high15Addr, &high15Mv) + }, + } +} + +func createSplitInput15Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { + high15, err := resolver.GetResOperander("high15") + if err != nil { + return nil, err + } + + low15, err := resolver.GetResOperander("low15") + if err != nil { + return nil, err + } + + inputs, err := resolver.GetResOperander("inputs") + if err != nil { + return nil, err + } + + return newSplitInput15Hint(high15, low15, inputs), nil +} + // SplitOutputMidLowHigh hint assigns to `ids.output1_low` the remainder of the division // of `ids.output1` variable by 256 ** 7 and uses its quotient as a variable which is // divided by 2 ** 128, the quotient and remainder of which are then assigned to `ids.output1_high` @@ -749,3 +923,263 @@ func createSplitNBytesHinter(resolver hintReferenceResolver) (hinter.Hinter, err return newSplitNBytesHint(nBytes, nWordsToCopy, nBytesLeft), nil } + +// SplitInput3 hint assigns to `ids.high3` and `ids.low3` variables +// the quotient and remainder of the division of the value at memory address +// `ids.inputs + 3` by 256 +// +// `newSplitInput3Hint` takes 3 operanders as arguments +// - `high3` is the variable that will store the quotient of the division +// - `low3` is the variable that will store the remainder of the division +// - `inputs` is the address in memory to which we add an offset of 3 and read that value +func newSplitInput3Hint(high3, low3, inputs hinter.ResOperander) hinter.Hinter { + return &GenericZeroHinter{ + Name: "SplitInput3", + Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error { + //> ids.high3, ids.low3 = divmod(memory[ids.inputs + 3], 256) + + high3Addr, err := high3.GetAddress(vm) + if err != nil { + return err + } + + low3Addr, err := low3.GetAddress(vm) + if err != nil { + return err + } + + inputsAddr, err := hinter.ResolveAsAddress(vm, inputs) + if err != nil { + return err + } + + *inputsAddr, err = inputsAddr.AddOffset(3) + if err != nil { + return err + } + + inputValue, err := vm.Memory.ReadFromAddress(inputsAddr) + if err != nil { + return err + } + + var inputBigInt big.Int + inputValue.Felt.BigInt(&inputBigInt) + + divisor := big.NewInt(256) + + high3BigInt := new(big.Int) + low3BigInt := new(big.Int) + + high3BigInt.DivMod(&inputBigInt, divisor, low3BigInt) + + var high3Felt fp.Element + high3Felt.SetBigInt(high3BigInt) + high3Mv := memory.MemoryValueFromFieldElement(&high3Felt) + + var low3Felt fp.Element + low3Felt.SetBigInt(low3BigInt) + low3Mv := memory.MemoryValueFromFieldElement(&low3Felt) + + err = vm.Memory.WriteToAddress(&low3Addr, &low3Mv) + if err != nil { + return err + } + + return vm.Memory.WriteToAddress(&high3Addr, &high3Mv) + }, + } +} + +func createSplitInput3Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { + high3, err := resolver.GetResOperander("high3") + if err != nil { + return nil, err + } + + low3, err := resolver.GetResOperander("low3") + if err != nil { + return nil, err + } + + inputs, err := resolver.GetResOperander("inputs") + if err != nil { + return nil, err + } + + return newSplitInput3Hint(high3, low3, inputs), nil +} + +// SplitInput6 hint assigns to `ids.high6` and `ids.low6` variables +// the quotient and remainder of the division of the value at memory address +// `ids.inputs + 6` by 256 ** 2 +// +// `newSplitInput6Hint` takes 3 operanders as arguments +// - `high6` is the variable that will store the quotient of the division +// - `low6` is the variable that will store the remainder of the division +// - `inputs` is the address in memory to which we add an offset of 6 and read that value +func newSplitInput6Hint(high6, low6, inputs hinter.ResOperander) hinter.Hinter { + return &GenericZeroHinter{ + Name: "SplitInput6", + Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error { + //> ids.high6, ids.low6 = divmod(memory[ids.inputs + 6], 256 ** 2) + + high6Addr, err := high6.GetAddress(vm) + if err != nil { + return err + } + + low6Addr, err := low6.GetAddress(vm) + if err != nil { + return err + } + + inputsAddr, err := hinter.ResolveAsAddress(vm, inputs) + if err != nil { + return err + } + + *inputsAddr, err = inputsAddr.AddOffset(6) + if err != nil { + return err + } + + inputValue, err := vm.Memory.ReadFromAddress(inputsAddr) + if err != nil { + return err + } + + var inputBigInt big.Int + inputValue.Felt.BigInt(&inputBigInt) + + // 256 ** 2 + divisor := big.NewInt(65536) + + high6BigInt := new(big.Int) + low6BigInt := new(big.Int) + + high6BigInt.DivMod(&inputBigInt, divisor, low6BigInt) + + var high6Felt fp.Element + high6Felt.SetBigInt(high6BigInt) + high6Mv := memory.MemoryValueFromFieldElement(&high6Felt) + + var low6Felt fp.Element + low6Felt.SetBigInt(low6BigInt) + low6Mv := memory.MemoryValueFromFieldElement(&low6Felt) + + err = vm.Memory.WriteToAddress(&low6Addr, &low6Mv) + if err != nil { + return err + } + + return vm.Memory.WriteToAddress(&high6Addr, &high6Mv) + }, + } +} + +func createSplitInput6Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { + high6, err := resolver.GetResOperander("high6") + if err != nil { + return nil, err + } + + low6, err := resolver.GetResOperander("low6") + if err != nil { + return nil, err + } + + inputs, err := resolver.GetResOperander("inputs") + if err != nil { + return nil, err + } + + return newSplitInput6Hint(high6, low6, inputs), nil +} + +// SplitInput9 hint assigns to `ids.high9` and `ids.low9` variables +// the quotient and remainder of the division of the value at memory address +// `ids.inputs + 9` by 256 ** 3 +// +// `newSplitInput9Hint` takes 3 operanders as arguments +// - `high9` is the variable that will store the quotient of the division +// - `low9` is the variable that will store the remainder of the division +// - `inputs` is the address in memory to which we add an offset of 9 and read that value +func newSplitInput9Hint(high9, low9, inputs hinter.ResOperander) hinter.Hinter { + return &GenericZeroHinter{ + Name: "SplitInput9", + Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error { + //> ids.high9, ids.low9 = divmod(memory[ids.inputs + 9], 256 ** 3) + + high9Addr, err := high9.GetAddress(vm) + if err != nil { + return err + } + + low9Addr, err := low9.GetAddress(vm) + if err != nil { + return err + } + + inputsAddr, err := hinter.ResolveAsAddress(vm, inputs) + if err != nil { + return err + } + + *inputsAddr, err = inputsAddr.AddOffset(9) + if err != nil { + return err + } + + inputValue, err := vm.Memory.ReadFromAddress(inputsAddr) + if err != nil { + return err + } + + var inputBigInt big.Int + inputValue.Felt.BigInt(&inputBigInt) + + // 256 ** 3 + divisor := big.NewInt(16777216) + + high9BigInt := new(big.Int) + low9BigInt := new(big.Int) + + high9BigInt.DivMod(&inputBigInt, divisor, low9BigInt) + + var high9Felt fp.Element + high9Felt.SetBigInt(high9BigInt) + high9Mv := memory.MemoryValueFromFieldElement(&high9Felt) + + var low9Felt fp.Element + low9Felt.SetBigInt(low9BigInt) + low9Mv := memory.MemoryValueFromFieldElement(&low9Felt) + + err = vm.Memory.WriteToAddress(&low9Addr, &low9Mv) + if err != nil { + return err + } + + return vm.Memory.WriteToAddress(&high9Addr, &high9Mv) + }, + } +} + +func createSplitInput9Hinter(resolver hintReferenceResolver) (hinter.Hinter, error) { + high9, err := resolver.GetResOperander("high9") + if err != nil { + return nil, err + } + + low9, err := resolver.GetResOperander("low9") + if err != nil { + return nil, err + } + + inputs, err := resolver.GetResOperander("inputs") + if err != nil { + return nil, err + } + + return newSplitInput9Hint(high9, low9, inputs), nil +} diff --git a/pkg/hintrunner/zero/zerohint_keccak_test.go b/pkg/hintrunner/zero/zerohint_keccak_test.go index 0db02368..247a299d 100644 --- a/pkg/hintrunner/zero/zerohint_keccak_test.go +++ b/pkg/hintrunner/zero/zerohint_keccak_test.go @@ -605,6 +605,235 @@ func TestZeroHintKeccak(t *testing.T) { check: apValueEquals(feltUint64(0)), }, }, + "SplitInput12": { + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(0)}, + {Name: "high12", Kind: uninitialized}, + {Name: "low12", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput12Hint(ctx.operanders["high12"], ctx.operanders["low12"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high12": feltUint64(0), "low12": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(100)}, + {Name: "high12", Kind: uninitialized}, + {Name: "low12", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput12Hint(ctx.operanders["high12"], ctx.operanders["low12"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high12": feltUint64(0), "low12": feltUint64(100)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(4294967296)}, + {Name: "high12", Kind: uninitialized}, + {Name: "low12", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput12Hint(ctx.operanders["high12"], ctx.operanders["low12"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high12": feltUint64(1), "low12": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(4294967297)}, + {Name: "high12", Kind: uninitialized}, + {Name: "low12", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput12Hint(ctx.operanders["high12"], ctx.operanders["low12"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high12": feltUint64(1), "low12": feltUint64(1)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(42949672961)}, + {Name: "high12", Kind: uninitialized}, + {Name: "low12", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput12Hint(ctx.operanders["high12"], ctx.operanders["low12"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high12": feltUint64(10), "low12": feltUint64(1)}), + }, + }, + "SplitInput15": { + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(0)}, + {Name: "high15", Kind: uninitialized}, + {Name: "low15", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput15Hint(ctx.operanders["high15"], ctx.operanders["low15"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high15": feltUint64(0), "low15": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(100)}, + {Name: "high15", Kind: uninitialized}, + {Name: "low15", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput15Hint(ctx.operanders["high15"], ctx.operanders["low15"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high15": feltUint64(0), "low15": feltUint64(100)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(1099511627776)}, + {Name: "high15", Kind: uninitialized}, + {Name: "low15", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput15Hint(ctx.operanders["high15"], ctx.operanders["low15"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high15": feltUint64(1), "low15": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(1099511627777)}, + {Name: "high15", Kind: uninitialized}, + {Name: "low15", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput15Hint(ctx.operanders["high15"], ctx.operanders["low15"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high15": feltUint64(1), "low15": feltUint64(1)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(10995116277761)}, + {Name: "high15", Kind: uninitialized}, + {Name: "low15", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput15Hint(ctx.operanders["high15"], ctx.operanders["low15"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high15": feltUint64(10), "low15": feltUint64(1)}), + }, + }, "SplitOutputMidLowHigh": { { operanders: []*hintOperander{ @@ -760,5 +989,236 @@ func TestZeroHintKeccak(t *testing.T) { check: allVarValueEquals(map[string]*fp.Element{"n_words_to_copy": feltUint64(940459082211961), "n_bytes_left": feltUint64(3)}), }, }, + "SplitInput3": { + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(0)}, + {Name: "high3", Kind: uninitialized}, + {Name: "low3", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput3Hint(ctx.operanders["high3"], ctx.operanders["low3"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high3": feltUint64(0), "low3": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(100)}, + {Name: "high3", Kind: uninitialized}, + {Name: "low3", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput3Hint(ctx.operanders["high3"], ctx.operanders["low3"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high3": feltUint64(0), "low3": feltUint64(100)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(256)}, + {Name: "high3", Kind: uninitialized}, + {Name: "low3", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput3Hint(ctx.operanders["high3"], ctx.operanders["low3"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high3": feltUint64(1), "low3": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(257)}, + {Name: "high3", Kind: uninitialized}, + {Name: "low3", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput3Hint(ctx.operanders["high3"], ctx.operanders["low3"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high3": feltUint64(1), "low3": feltUint64(1)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(2561)}, + {Name: "high3", Kind: uninitialized}, + {Name: "low3", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput3Hint(ctx.operanders["high3"], ctx.operanders["low3"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high3": feltUint64(10), "low3": feltUint64(1)}), + }, + }, + "SplitInput6": { + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(0)}, + {Name: "high6", Kind: uninitialized}, + {Name: "low6", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput6Hint(ctx.operanders["high6"], ctx.operanders["low6"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high6": feltUint64(0), "low6": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(100)}, + {Name: "high6", Kind: uninitialized}, + {Name: "low6", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput6Hint(ctx.operanders["high6"], ctx.operanders["low6"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high6": feltUint64(0), "low6": feltUint64(100)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(65536)}, + {Name: "high6", Kind: uninitialized}, + {Name: "low6", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput6Hint(ctx.operanders["high6"], ctx.operanders["low6"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high6": feltUint64(1), "low6": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(65537)}, + {Name: "high6", Kind: uninitialized}, + {Name: "low6", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput6Hint(ctx.operanders["high6"], ctx.operanders["low6"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high6": feltUint64(1), "low6": feltUint64(1)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(655361)}, + {Name: "high6", Kind: uninitialized}, + {Name: "low6", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput6Hint(ctx.operanders["high6"], ctx.operanders["low6"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high6": feltUint64(10), "low6": feltUint64(1)}), + }, + }, + "SplitInput9": { + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(0)}, + {Name: "high9", Kind: uninitialized}, + {Name: "low9", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput9Hint(ctx.operanders["high9"], ctx.operanders["low9"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high9": feltUint64(0), "low9": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(100)}, + {Name: "high9", Kind: uninitialized}, + {Name: "low9", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput9Hint(ctx.operanders["high9"], ctx.operanders["low9"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high9": feltUint64(0), "low9": feltUint64(100)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(16777216)}, + {Name: "high9", Kind: uninitialized}, + {Name: "low9", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput9Hint(ctx.operanders["high9"], ctx.operanders["low9"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high9": feltUint64(1), "low9": feltUint64(0)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(16777217)}, + {Name: "high9", Kind: uninitialized}, + {Name: "low9", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput9Hint(ctx.operanders["high9"], ctx.operanders["low9"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high9": feltUint64(1), "low9": feltUint64(1)}), + }, + { + operanders: []*hintOperander{ + {Name: "inputs", Kind: fpRelative, Value: addr(2)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "random_value", Kind: apRelative, Value: feltUint64(0)}, + {Name: "real_input", Kind: apRelative, Value: feltUint64(167772161)}, + {Name: "high9", Kind: uninitialized}, + {Name: "low9", Kind: uninitialized}, + }, + makeHinter: func(ctx *hintTestContext) hinter.Hinter { + return newSplitInput9Hint(ctx.operanders["high9"], ctx.operanders["low9"], ctx.operanders["inputs"]) + }, + check: allVarValueEquals(map[string]*fp.Element{"high9": feltUint64(10), "low9": feltUint64(1)}), + }, + }, }) } diff --git a/pkg/hintrunner/zero/zerohint_math.go b/pkg/hintrunner/zero/zerohint_math.go index 058edcff..3817411b 100644 --- a/pkg/hintrunner/zero/zerohint_math.go +++ b/pkg/hintrunner/zero/zerohint_math.go @@ -1152,41 +1152,27 @@ func newIsQuadResidueHint(x, y hinter.ResOperander) hinter.Hinter { xBigInt := math_utils.AsInt(x) var value = memory.MemoryValue{} + var result *fp.Element = new(fp.Element) - if x.IsZero() || x.IsOne() { - value = memory.MemoryValueFromFieldElement(x) + const primeString = "3618502788666131213697322783095070105623107215331596699973092056135872020481" + primeBigInt, ok := new(big.Int).SetString(primeString, 10) + if !ok { + panic("failed to convert prime string to big.Int") + } + if math_utils.IsQuadResidue(x) { + result.SetBigInt(math_utils.Sqrt(&xBigInt, primeBigInt)) } else { - var result *fp.Element = new(fp.Element) - - if x.Legendre() == 1 { - // result = x.Sqrt(x) - - const primeString = "3618502788666131213697322783095070105623107215331596699973092056135872020481" - primeBigInt, ok := new(big.Int).SetString(primeString, 10) - if !ok { - panic("failed to convert prime string to big.Int") - } - - // divide primeBigInt by 2 - halfPrimeBigInt := new(big.Int).Rsh(primeBigInt, 1) - - tempResult := new(big.Int).ModSqrt(&xBigInt, primeBigInt) - - // ensures that tempResult is the smaller of the two possible square roots in the prime field. - if tempResult.Cmp(halfPrimeBigInt) > 0 { - tempResult.Sub(primeBigInt, tempResult) - } - - result.SetBigInt(tempResult) - - } else { - result = x.Sqrt(new(fp.Element).Div(x, new(fp.Element).SetUint64(3))) + y, err := math_utils.Divmod(&xBigInt, big.NewInt(3), primeBigInt) + if err != nil { + return err } - value = memory.MemoryValueFromFieldElement(result) + result.SetBigInt(math_utils.Sqrt(&y, primeBigInt)) } + value = memory.MemoryValueFromFieldElement(result) + return vm.Memory.WriteToAddress(&yAddr, &value) }, } diff --git a/pkg/vm/builtins/ecdsa.go b/pkg/vm/builtins/ecdsa.go index 8a1f6686..9b11e6f2 100644 --- a/pkg/vm/builtins/ecdsa.go +++ b/pkg/vm/builtins/ecdsa.go @@ -60,7 +60,7 @@ func (e *ECDSA) CheckWrite(segment *memory.Segment, offset uint64, value *memory pubKey := &ecdsa.PublicKey{A: key} sig, ok := e.signatures[pubOffset] if !ok { - return fmt.Errorf("signature is missing form ECDA builtin") + return fmt.Errorf("signature is missing from ECDSA builtin") } msgBytes := msgField.Bytes()