From 9dfdd78c4adcc312190343c8a5641c8d2bd5d209 Mon Sep 17 00:00:00 2001 From: Juan-M-V <102986292+Juan-M-V@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:08:00 -0300 Subject: [PATCH] Add test and use GetRelocatable instead of GetAddr (#243) Co-authored-by: juan.mv --- pkg/hints/pow_hints.go | 10 ++++++---- pkg/hints/pow_hints_test.go | 40 ++++++++++++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/pkg/hints/pow_hints.go b/pkg/hints/pow_hints.go index 5d60c510..bccc9786 100644 --- a/pkg/hints/pow_hints.go +++ b/pkg/hints/pow_hints.go @@ -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) } diff --git a/pkg/hints/pow_hints_test.go b/pkg/hints/pow_hints_test.go index 9a652e0c..0146e7c4 100644 --- a/pkg/hints/pow_hints_test.go +++ b/pkg/hints/pow_hints_test.go @@ -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))}, @@ -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) + } +}