Skip to content

Commit

Permalink
Add test and use GetRelocatable instead of GetAddr (#243)
Browse files Browse the repository at this point in the history
Co-authored-by: juan.mv <[email protected]>
  • Loading branch information
Juan-M-V and juan.mv authored Sep 19, 2023
1 parent e6ab48a commit 9dfdd78
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
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))
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 @@ -10,10 +10,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)))

idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"prev_locs": {NewMaybeRelocatableRelocatable(NewRelocatable(0, 0))},
Expand All @@ -37,7 +38,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)
}
}

0 comments on commit 9dfdd78

Please sign in to comment.