diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index cf8181a6..cdc33536 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -69,6 +69,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any, return memcpy_enter_scope(data.Ids, vm, execScopes) case VM_ENTER_SCOPE: return vm_enter_scope(execScopes) + case UNSAFE_KECCAK: + return unsafeKeccak(data.Ids, vm, *execScopes) default: return errors.Errorf("Unknown Hint: %s", data.Code) } diff --git a/pkg/hints/keccak_hints.go b/pkg/hints/keccak_hints.go index b9bb4527..22e32edd 100644 --- a/pkg/hints/keccak_hints.go +++ b/pkg/hints/keccak_hints.go @@ -58,8 +58,11 @@ func unsafeKeccak(ids IdsManager, vm *VirtualMachine, scopes ExecutionScopes) er hasher := sha3.New256() resBytes := hasher.Sum(keccakInput) - high := FeltFromBeBytes((*[32]byte)(resBytes[:16])) - low := FeltFromBeBytes((*[32]byte)(resBytes[16:32])) + highBytes := append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, resBytes[:16]...) + lowBytes := append([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, resBytes[16:32]...) + + high := FeltFromBeBytes((*[32]byte)(highBytes)) + low := FeltFromBeBytes((*[32]byte)(lowBytes)) err = ids.Insert("high", NewMaybeRelocatableFelt(high), vm) if err != nil { diff --git a/pkg/hints/keccak_hints_test.go b/pkg/hints/keccak_hints_test.go new file mode 100644 index 00000000..1ebd4ae1 --- /dev/null +++ b/pkg/hints/keccak_hints_test.go @@ -0,0 +1,57 @@ +package hints_test + +import ( + "testing" + + . "github.com/lambdaclass/cairo-vm.go/pkg/hints" + . "github.com/lambdaclass/cairo-vm.go/pkg/hints/hint_utils" + . "github.com/lambdaclass/cairo-vm.go/pkg/lambdaworks" + . "github.com/lambdaclass/cairo-vm.go/pkg/types" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm" + . "github.com/lambdaclass/cairo-vm.go/pkg/vm/memory" +) + +func TestUnsafeKeccakOk(t *testing.T) { + vm := NewVirtualMachine() + vm.Segments.AddSegment() + data_ptr := vm.Segments.AddSegment() + idsManager := SetupIdsForTest( + map[string][]*MaybeRelocatable{ + "length": {NewMaybeRelocatableFelt(FeltFromUint64(3))}, + "data": {NewMaybeRelocatableRelocatable(data_ptr)}, + "high": {nil}, + "low": {nil}, + }, + vm, + ) + // Insert data into memory + data := []MaybeRelocatable{ + *NewMaybeRelocatableFelt(FeltOne()), + *NewMaybeRelocatableFelt(FeltOne()), + *NewMaybeRelocatableFelt(FeltOne()), + } + vm.Segments.LoadData(data_ptr, &data) + // Add __keccak_max_size + scopes := NewExecutionScopes() + scopes.AssignOrUpdateVariable("__keccak_max_size", uint16(500)) + hintProcessor := CairoVmHintProcessor{} + hintData := any(HintData{ + Ids: idsManager, + Code: UNSAFE_KECCAK, + }) + err := hintProcessor.ExecuteHint(vm, &hintData, nil, scopes) + if err != nil { + t.Errorf("UNSAFE_KECCAK hint test failed with error %s", err) + } + // Check ids values + high, err := idsManager.GetFelt("high", vm) + expectedHigh := FeltFromDecString("199195598804046335037364682505062700553") + if err != nil || high != expectedHigh { + t.Errorf("Wrong/No ids.high.\n Expected %s, got %s.", expectedHigh.ToHexString(), high.ToHexString()) + } + low, err := idsManager.GetFelt("low", vm) + expectedLow := FeltFromDecString("259413678945892999811634722593932702747") + if err != nil || low != expectedLow { + t.Errorf("Wrong/No ids.low\n Expected %s, got %s.", expectedLow.ToHexString(), low.ToHexString()) + } +}