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

Fix Pow hint #243

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pkg/hints/pow_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
// Implements hint:
// %{ ids.locs.bit = (ids.prev_locs.exp % PRIME) & 1 %}
func pow(ids IdsManager, vm *VirtualMachine) error {
prev_locs_exp_addr, err := ids.GetAddr("prev_locs", vm)
prev_locs_exp, _ := vm.Segments.Memory.GetFelt(prev_locs_exp_addr.AddUint(4))
prev_locs_exp_addr, err := ids.GetRelocatable("prev_locs", vm)
if err != nil {
return err
}

prev_locs_exp, err := vm.Segments.Memory.GetFelt(prev_locs_exp_addr.AddUint(4))
fmoletta marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

ids.Insert("locs", NewMaybeRelocatableFelt(prev_locs_exp.And(FeltOne())), vm)
return nil
return ids.Insert("locs", NewMaybeRelocatableFelt(prev_locs_exp.And(FeltOne())), vm)
}
40 changes: 37 additions & 3 deletions pkg/hints/pow_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"testing"
)

func TestPowHintOk(t *testing.T) {
func TestPowHintOddOk(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
vm.Segments.Memory.Insert(NewRelocatable(0, 4), NewMaybeRelocatableFelt(FeltFromUint64(5)))
vm.Segments.Memory.Insert(NewRelocatable(0, 4), NewMaybeRelocatableFelt(FeltFromUint64(3)))

fmoletta marked this conversation as resolved.
Show resolved Hide resolved
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"prev_locs": {NewMaybeRelocatableRelocatable(NewRelocatable(0, 0))},
Expand All @@ -36,7 +37,40 @@ func TestPowHintOk(t *testing.T) {
t.Errorf("Failed to get locs.bit with error: %s", err)
}

if locs != FeltFromUint64(1) {
if locs != FeltOne() {
t.Errorf("locs.bit: %d != 1", locs)
}
}

func TestPowHintEvenOk(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
vm.Segments.Memory.Insert(NewRelocatable(0, 4), NewMaybeRelocatableFelt(FeltFromUint64(2)))

idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"prev_locs": {NewMaybeRelocatableRelocatable(NewRelocatable(0, 0))},
"locs": {nil},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: POW,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err != nil {
t.Errorf("POW hint test failed with error %s", err)
}

locs, err := idsManager.GetFelt("locs", vm)
if err != nil {
t.Errorf("Failed to get locs.bit with error: %s", err)
}

if locs != FeltZero() {
t.Errorf("locs.bit: %d != 0", locs)
}
}