Skip to content

Commit

Permalink
Merge branch 'main' into SplitOutput0
Browse files Browse the repository at this point in the history
  • Loading branch information
TAdev0 committed Jul 4, 2024
2 parents 175c850 + 6cab0ef commit 59a02e7
Show file tree
Hide file tree
Showing 4 changed files with 416 additions and 6 deletions.
9 changes: 6 additions & 3 deletions pkg/hintrunner/zero/hintcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -128,9 +129,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\noutput_values = keccak_func(memory.get_range(\nids.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)"
splintInput3Code string = "ids.high3, ids.low3 = divmod(memory[ids.inputs + 3], 256)"
splintInput6Code string = "ids.high6, ids.low6 = divmod(memory[ids.inputs + 6], 256 ** 2)"
splintInput9Code string = "ids.high9, ids.low9 = divmod(memory[ids.inputs + 9], 256 ** 3)"
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)"
splitOutput0Code string = "ids.output0_low = ids.output0 & ((1 << 128) - 1)\nids.output0_high = ids.output0 >> 128"
SplitNBytesCode string = "ids.n_words_to_copy, ids.n_bytes_left = divmod(ids.n_bytes, ids.BYTES_IN_WORD)"
Expand Down
10 changes: 7 additions & 3 deletions pkg/hintrunner/zero/zerohint.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,16 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64
return createBlockPermutationHinter(resolver)
case compareBytesInWordCode:
return createCompareBytesInWordNondetHinter(resolver)
case splintInput3Code:
case splitInput3Code:
return createSplitInput3Hinter(resolver)
case splintInput6Code:
case splitInput6Code:
return createSplitInput6Hinter(resolver)
case splintInput9Code:
case splitInput9Code:
return createSplitInput9Hinter(resolver)
case splitInput12Code:
return createSplitInput12Hinter(resolver)
case splitInput15Code:
return createSplitInput15Hinter(resolver)
case splitOutputMidLowHighCode:
return createSplitOutputMidLowHighHinter(resolver)
case splitOutput0Code:
Expand Down
174 changes: 174 additions & 0 deletions pkg/hintrunner/zero/zerohint_keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Loading

0 comments on commit 59a02e7

Please sign in to comment.