Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompareBytesInWord keccak hint #481

Merged
merged 11 commits into from
Jun 28, 2024
5 changes: 3 additions & 2 deletions pkg/hintrunner/zero/hintcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ const (
inp = [0] * _keccak_state_size_felts
padding = (inp + keccak_func(inp)) * _block_size
segments.write_arg(ids.keccak_ptr_end, padding)`
keccakWriteArgsCode string = "segments.write_arg(ids.inputs, [ids.low % 2 ** 64, ids.low // 2 ** 64])\nsegments.write_arg(ids.inputs + 2, [ids.high % 2 ** 64, ids.high // 2 ** 64])"
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)"
keccakWriteArgsCode string = "segments.write_arg(ids.inputs, [ids.low % 2 ** 64, ids.low // 2 ** 64])\nsegments.write_arg(ids.inputs + 2, [ids.high % 2 ** 64, ids.high // 2 ** 64])"
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 = "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
2 changes: 2 additions & 0 deletions pkg/hintrunner/zero/zerohint.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func GetHintFromCode(program *zero.ZeroProgram, rawHint zero.Hint, hintPC uint64
return createUnsafeKeccakFinalizeHinter(resolver)
case blockPermutationCode:
return createBlockPermutationHinter(resolver)
case compareBytesInWordCode:
return createCompareBytesInWordNondetHinter(resolver)
// Usort hints
case usortEnterScopeCode:
return createUsortEnterScopeHinter()
Expand Down
40 changes: 40 additions & 0 deletions pkg/hintrunner/zero/zerohint_keccak.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,43 @@ func createBlockPermutationHinter(resolver hintReferenceResolver) (hinter.Hinter

return newBlockPermutationHint(keccakPtr), nil
}

// CompareBytesInWord hint compares a value to BYTES_IN_WORD constant, i.e., 8
//
// `newCompareBytesInWordHint` takes 1 operander as argument
// - `nBytes` is the value to be compared with BYTES_IN_WORD
//
// `newCompareBytesInWordHint` writes 1 or 0 to `ap` memory address depending on whether
// `n_bytes` is lower than BYTES_IN_WORD or not
func newCompareBytesInWordHint(nBytes hinter.ResOperander) hinter.Hinter {
return &GenericZeroHinter{
Name: "CompareBytesInWordHint",
Op: func(vm *VM.VirtualMachine, _ *hinter.HintRunnerContext) error {
//> ids.n_bytes < ids.BYTES_IN_WORD
nBytesVal, err := hinter.ResolveAsUint64(vm, nBytes)
TAdev0 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

bytesInWord := uint64(8)
apAddr := vm.Context.AddressAp()
var resultMv memory.MemoryValue
if nBytesVal < bytesInWord {
resultMv = memory.MemoryValueFromFieldElement(&utils.FeltOne)
} else {
resultMv = memory.MemoryValueFromFieldElement(&utils.FeltZero)
}

return vm.Memory.WriteToAddress(&apAddr, &resultMv)
},
}
}

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

return newCompareBytesInWordHint(nBytes), nil
}
20 changes: 20 additions & 0 deletions pkg/hintrunner/zero/zerohint_keccak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,5 +556,25 @@ func TestZeroHintKeccak(t *testing.T) {
}),
},
},
"CompareBytesInWordHint": {
{
operanders: []*hintOperander{
{Name: "n_bytes", Kind: fpRelative, Value: feltUint64(5)},
},
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newCompareBytesInWordHint(ctx.operanders["n_bytes"])
},
check: apValueEquals(feltUint64(1)),
},
{
operanders: []*hintOperander{
{Name: "n_bytes", Kind: fpRelative, Value: feltUint64(10)},
},
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newCompareBytesInWordHint(ctx.operanders["n_bytes"])
},
check: apValueEquals(feltUint64(0)),
},
},
})
}
Loading