Skip to content

Commit

Permalink
Merge branch 'main' into 490
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh0g0-1758 committed Jul 3, 2024
2 parents 2beab56 + 60deaa1 commit f1cb6de
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 5 deletions.
2 changes: 2 additions & 0 deletions pkg/hintrunner/zero/hintcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ 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)"
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)"

// ------ Dictionaries hints related code ------
dictNewCode string = "if '__dict_manager' not in globals():\n from starkware.cairo.common.dict import DictManager\n __dict_manager = DictManager()\n\nmemory[ap] = __dict_manager.new_dict(segments, initial_dict)\ndel initial_dict"
Expand Down
4 changes: 4 additions & 0 deletions pkg/hintrunner/zero/zerohint.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64
return createBlockPermutationHinter(resolver)
case compareBytesInWordCode:
return createCompareBytesInWordNondetHinter(resolver)
case splitOutputMidLowHighCode:
return createSplitOutputMidLowHighHinter(resolver)
case SplitNBytesCode:
return createSplitNBytesHinter(resolver)
// Usort hints
case usortEnterScopeCode:
return createUsortEnterScopeHinter()
Expand Down
2 changes: 1 addition & 1 deletion pkg/hintrunner/zero/zerohint_ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func createEcDoubleSlopeV1Hinter(resolver hintReferenceResolver) (hinter.Hinter,
return newEcDoubleSlopeV1Hint(point), nil
}

// ReduceV1 hint reduces a packed value modulo the SECP256R1 prime
// ReduceV1 hint reduces a packed value modulo the SECP256K1 prime
//
// `newReduceV1Hint` takes 1 operander as argument
// - `x` is the packed value to be reduced
Expand Down
185 changes: 181 additions & 4 deletions pkg/hintrunner/zero/zerohint_keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zero
import (
"fmt"
"math"
"math/big"

"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter"
"github.com/NethermindEth/cairo-vm-go/pkg/utils"
Expand Down Expand Up @@ -316,9 +317,8 @@ func createUnsafeKeccakFinalizeHinter(resolver hintReferenceResolver) (hinter.Hi
// - `low` is the low part of the `uint256` argument for the Keccac function
// - `high` is the high part of the `uint256` argument for the Keccac function
func newKeccakWriteArgsHint(inputs, low, high hinter.ResOperander) hinter.Hinter {
name := "KeccakWriteArgs"
return &GenericZeroHinter{
Name: name,
Name: "KeccakWriteArgs",
Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error {
//> segments.write_arg(ids.inputs, [ids.low % 2 ** 64, ids.low // 2 ** 64])
//> segments.write_arg(ids.inputs + 2, [ids.high % 2 ** 64, ids.high // 2 ** 64])
Expand Down Expand Up @@ -459,9 +459,8 @@ func createCompareKeccakFullRateInBytesNondetHinter(resolver hintReferenceResolv
// `newBlockPermutationHint` reads 25 memory cells starting from `keccakPtr - 25`, and writes
// the result of the Keccak block permutation in the next 25 memory cells, starting from `keccakPtr`
func newBlockPermutationHint(keccakPtr hinter.ResOperander) hinter.Hinter {
name := "BlockPermutation"
return &GenericZeroHinter{
Name: name,
Name: "BlockPermutation",
Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error {
//> from starkware.cairo.common.keccak_utils.keccak_utils import keccak_func
//> _keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS)
Expand Down Expand Up @@ -572,3 +571,181 @@ func createCompareBytesInWordNondetHinter(resolver hintReferenceResolver) (hinte

return newCompareBytesInWordHint(nBytes), 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`
// and `ids.output1_mid` respectively.
//
// `newSplitOutputMidLowHighHint` takes 4 operanders as arguments
// - `output1Low` is the variable that will store the remainder of the first division
// - `output1Mid` is the variable that will store the remainder of the second division
// - `output1High` is the variable that will store the quotient of the second division
// - `output1` is the variable that will be divided in the first division
func newSplitOutputMidLowHighHint(output1, output1Low, output1Mid, output1High hinter.ResOperander) hinter.Hinter {
return &GenericZeroHinter{
Name: "SplitOutputMidLowHigh",
Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error {
//> tmp, ids.output1_low = divmod(ids.output1, 256 ** 7)
//> ids.output1_high, ids.output1_mid = divmod(tmp, 2 ** 128)

output1LowAddr, err := output1Low.GetAddress(vm)
if err != nil {
return err
}

output1MidAddr, err := output1Mid.GetAddress(vm)
if err != nil {
return err
}

output1HighAddr, err := output1High.GetAddress(vm)
if err != nil {
return err
}

output1Felt, err := hinter.ResolveAsFelt(vm, output1)
if err != nil {
return err
}

output1BigInt := new(big.Int)
output1Felt.BigInt(output1BigInt)

tmpBigInt := new(big.Int)
output1LowBigInt := new(big.Int)
output1MidBigInt := new(big.Int)
output1HighBigInt := new(big.Int)

divisorOne := new(big.Int).Exp(big.NewInt(256), big.NewInt(7), nil)
divisorTwo := new(big.Int).Exp(big.NewInt(2), big.NewInt(128), nil)

tmpBigInt.DivMod(output1BigInt, divisorOne, output1LowBigInt)
output1HighBigInt.DivMod(tmpBigInt, divisorTwo, output1MidBigInt)

var output1LowFelt fp.Element
output1LowFelt.SetBigInt(output1LowBigInt)
output1LowMv := memory.MemoryValueFromFieldElement(&output1LowFelt)

var output1MidFelt fp.Element
output1MidFelt.SetBigInt(output1MidBigInt)
output1MidMv := memory.MemoryValueFromFieldElement(&output1MidFelt)

var output1HighFelt fp.Element
output1HighFelt.SetBigInt(output1HighBigInt)
output1HighMv := memory.MemoryValueFromFieldElement(&output1HighFelt)

err = vm.Memory.WriteToAddress(&output1LowAddr, &output1LowMv)
if err != nil {
return err
}

err = vm.Memory.WriteToAddress(&output1MidAddr, &output1MidMv)
if err != nil {
return err
}
return vm.Memory.WriteToAddress(&output1HighAddr, &output1HighMv)
},
}
}

func createSplitOutputMidLowHighHinter(resolver hintReferenceResolver) (hinter.Hinter, error) {
output1, err := resolver.GetResOperander("output1")
if err != nil {
return nil, err
}

output1Low, err := resolver.GetResOperander("output1_low")
if err != nil {
return nil, err
}

output1Mid, err := resolver.GetResOperander("output1_mid")
if err != nil {
return nil, err
}

output1High, err := resolver.GetResOperander("output1_high")
if err != nil {
return nil, err
}

return newSplitOutputMidLowHighHint(output1, output1Low, output1Mid, output1High), nil
}

// SplitNBytes hint assigns to `ids.n_words_to_copy` and `ids.n_bytes_left` variables
// the quotient and remainder of the division of `ids.n_bytes` variable by the
// variable `ids.BYTES_IN_WORD`
//
// `newSplitNBytesHint` takes 3 operanders as arguments
// - `nWordsToCopy` is the variable that will store the quotient of the division
// - `nBytesLeft` is the variable that will store the remainder of the division
// - `nBytes` is the variable that will be divided
func newSplitNBytesHint(nBytes, nWordsToCopy, nBytesLeft hinter.ResOperander) hinter.Hinter {
name := "SplitNBytes"
return &GenericZeroHinter{
Name: name,
Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error {
//> ids.n_words_to_copy, ids.n_bytes_left = divmod(ids.n_bytes, ids.BYTES_IN_WORD)

nWordsToCopyAddr, err := nWordsToCopy.GetAddress(vm)
if err != nil {
return err
}

nBytesLeftAddr, err := nBytesLeft.GetAddress(vm)
if err != nil {
return err
}

nBytesFelt, err := hinter.ResolveAsFelt(vm, nBytes)
if err != nil {
return err
}

nBytesBigInt := new(big.Int)
nBytesFelt.BigInt(nBytesBigInt)

bytesInWord := big.NewInt(8)

nWordsToCopyBigInt := new(big.Int)
nBytesLeftBigInt := new(big.Int)

nWordsToCopyBigInt.DivMod(nBytesBigInt, bytesInWord, nBytesLeftBigInt)

var nWordsToCopyFelt fp.Element
nWordsToCopyFelt.SetBigInt(nWordsToCopyBigInt)
nWordsToCopyMv := memory.MemoryValueFromFieldElement(&nWordsToCopyFelt)

var nBytesLeftFelt fp.Element
nBytesLeftFelt.SetBigInt(nBytesLeftBigInt)
nBytesLeftMv := memory.MemoryValueFromFieldElement(&nBytesLeftFelt)

err = vm.Memory.WriteToAddress(&nBytesLeftAddr, &nBytesLeftMv)
if err != nil {
return err
}

return vm.Memory.WriteToAddress(&nWordsToCopyAddr, &nWordsToCopyMv)
},
}
}

func createSplitNBytesHinter(resolver hintReferenceResolver) (hinter.Hinter, error) {
nBytes, err := resolver.GetResOperander("n_bytes")
if err != nil {
return nil, err
}

nWordsToCopy, err := resolver.GetResOperander("n_words_to_copy")
if err != nil {
return nil, err
}

nBytesLeft, err := resolver.GetResOperander("n_bytes_left")
if err != nil {
return nil, err
}

return newSplitNBytesHint(nBytes, nWordsToCopy, nBytesLeft), nil
}
Loading

0 comments on commit f1cb6de

Please sign in to comment.