Skip to content

Commit

Permalink
add continue loop hint
Browse files Browse the repository at this point in the history
  • Loading branch information
toni-calvin committed Sep 19, 2023
1 parent 853ee4c commit 65b8ce6
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 33 deletions.
2 changes: 2 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return memset_enter_scope(data.Ids, vm, execScopes)
case MEMCPY_CONTINUE_COPYING:
return memset_step_loop(data.Ids, vm, execScopes, "continue_copying")
case MEMSET_CONTINUE_LOOP:
return memset_step_loop(data.Ids, vm, execScopes, "continue_loop")
default:
return errors.Errorf("Unknown Hint: %s", data.Code)
}
Expand Down
33 changes: 0 additions & 33 deletions pkg/hints/memcpy_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,36 +241,3 @@ func TestMemcpyContinueCopyingInsertError(t *testing.T) {
t.Errorf("TestMemcpyContinueCopyingInsertError should fail with error %s", expected)
}
}

// func TestMemsetContinueCopyingValidEqual5Hint(t *testing.T) {
// vm := NewVirtualMachine()
// vm.RunContext.Fp = NewRelocatable(1, 0)
// vm.Segments.Memory.Data[NewRelocatable(1, 2)] = *NewMaybeRelocatableFelt(FeltFromUint64(5))
// idsManager := SetupIdsForTest(
// map[string][]*MaybeRelocatable{
// "continue_copying": nil,
// },
// vm,
// )
// hintProcessor := CairoVmHintProcessor{}
// hintData := any(HintData{
// Ids: idsManager,
// Code: MEMCPY_CONTINUE_COPYING,
// })

// executionScopes := NewExecutionScopes()
// scope := make(map[string]interface{})
// scope["n"] = FeltFromUint64(5)
// executionScopes.EnterScope(scope)
// err := hintProcessor.ExecuteHint(vm, &hintData, nil, executionScopes)
// if err != nil {
// t.Errorf("TestMemsetContinueCopyingValidEqual5Hint failed with error %s", err)
// }
// val, err := vm.Segments.Memory.GetFelt(NewRelocatable(1, 0))
// if err != nil {
// t.Errorf("TestMemsetContinueCopyingValidEqual5Hint failed with error %s", err)
// }
// if val != FeltZero() {
// t.Errorf("TestMemsetContinueCopyingValidEqual5Hint failed, expected %d, got: %d", FeltZero(), val)
// }
// }
1 change: 1 addition & 0 deletions pkg/hints/memset_hint_codes.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package hints

const MEMSET_ENTER_SCOPE = "vm_enter_scope({'n': ids.n})"
const MEMSET_CONTINUE_LOOP = "n -= 1 ids.continue_loop = 1 if n > 0 else 0"
107 changes: 107 additions & 0 deletions pkg/hints/memset_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,110 @@ func TestMemsetEnterScopeInvalid(t *testing.T) {
}

}

func TestMemsetContinueLoopValidEqual1Hint(t *testing.T) {
vm := NewVirtualMachine()
vm.RunContext.Fp = NewRelocatable(1, 0)
vm.Segments = AddNSegments(vm.Segments, 2)
executionScopes := NewExecutionScopesWithInitValue("n", FeltFromUint64(1))

idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"continue_loop": {NewMaybeRelocatableFeltFromUint64(0)},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: MEMSET_CONTINUE_LOOP,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, executionScopes)
if err != nil {
t.Errorf("failed with error %s", err)
}
val, err := vm.Segments.Memory.GetFelt(NewRelocatable(1, 0))
if err != nil {
t.Errorf("failed with error %s", err)
}
if val != FeltZero() {
t.Errorf("failed, expected %d, got: %d", FeltZero(), val)
}
}

func TestMemsetContinueLoopValidEqual5Hint(t *testing.T) {
vm := NewVirtualMachine()
vm.RunContext.Fp = NewRelocatable(1, 0)
vm.Segments = AddNSegments(vm.Segments, 2)
executionScopes := NewExecutionScopesWithInitValue("n", FeltFromUint64(5))

idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"continue_loop": {NewMaybeRelocatableFeltFromUint64(1)},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: MEMSET_CONTINUE_LOOP,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, executionScopes)
if err != nil {
t.Errorf("failed with error %s", err)
}
val, err := vm.Segments.Memory.GetFelt(NewRelocatable(1, 0))
if err != nil {
t.Errorf("failed with error %s", err)
}
if val != FeltOne() {
t.Errorf("failed, expected %d, got: %d", FeltOne(), val)
}
}

func TestMemsetContinueLoopVarNotInScope(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
executionScopes := NewExecutionScopes()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"continue_loop": {NewMaybeRelocatableFeltFromUint64(1)},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: MEMSET_CONTINUE_LOOP,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, executionScopes)
if err.Error() != ErrVariableNotInScope("n").Error() {
t.Errorf("should fail with error %s", ErrVariableNotInScope("n"))
}
}

func TestMemsetContinueLoopInsertError(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments = AddNSegments(vm.Segments, 2)
executionScopes := NewExecutionScopesWithInitValue("n", FeltOne())
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"continue_loop": {NewMaybeRelocatableFelt(FeltFromUint64(5))},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: MEMSET_CONTINUE_LOOP,
})

err := hintProcessor.ExecuteHint(vm, &hintData, nil, executionScopes)
expected := ErrMemoryWriteOnce(NewRelocatable(0, 0), *NewMaybeRelocatableFeltFromUint64(5), *NewMaybeRelocatableFeltFromUint64(0))
if err.Error() != expected.Error() {
t.Errorf("should fail with error %s", expected)
}
}
7 changes: 7 additions & 0 deletions pkg/types/exec_scope_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

func NewExecutionScopesWithInitValue(key string, val interface{}) *ExecutionScopes {
scopes := NewExecutionScopes()
scopes.EnterScope(map[string]interface{}{key: val})
return scopes
}

0 comments on commit 65b8ce6

Please sign in to comment.