Skip to content

Commit

Permalink
Add hint + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta committed Sep 19, 2023
1 parent 8707c24 commit 789fce2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/hints/hint_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (p *CairoVmHintProcessor) ExecuteHint(vm *vm.VirtualMachine, hintData *any,
return assertLeFeltExcluded1(vm, execScopes)
case ASSERT_LE_FELT_EXCLUDED_2:
return assertLeFeltExcluded2(vm, execScopes)
case ASSERT_LT_FELT:
return assertLtFelt(data.Ids, vm)
default:
return errors.Errorf("Unknown Hint: %s", data.Code)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/hints/math_hint_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ const ASSERT_LE_FELT_EXCLUDED_0 = "memory[ap] = 1 if excluded != 0 else 0"
const ASSERT_LE_FELT_EXCLUDED_1 = "memory[ap] = 1 if excluded != 1 else 0"

const ASSERT_LE_FELT_EXCLUDED_2 = "assert excluded == 2"

const ASSERT_LT_FELT = "from starkware.cairo.common.math_utils import assert_integer\nassert_integer(ids.a)\nassert_integer(ids.b)\nassert (ids.a % PRIME) < (ids.b % PRIME), \\\n f'a = {ids.a % PRIME} is not less than b = {ids.b % PRIME}.'"
16 changes: 16 additions & 0 deletions pkg/hints/math_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,19 @@ func assertLeFeltExcluded2(vm *VirtualMachine, scopes *ExecutionScopes) error {
}
return nil
}

func assertLtFelt(ids IdsManager, vm *VirtualMachine) error {
// Fetch ids variables
a, err := ids.GetFelt("a", vm)
if err != nil {
return err
}
b, err := ids.GetFelt("b", vm)
if err != nil {
return err
}
if a.Cmp(b) != -1 {
return errors.Errorf("Assertion failed, a = %s %% PRIME is not less than b = %s %% PRIME", a.ToHexString(), b.ToHexString())
}
return nil
}
42 changes: 42 additions & 0 deletions pkg/hints/math_hints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,45 @@ func TestAssertLeFeltExcluded2Err(t *testing.T) {
t.Errorf("ASSERT_LE_FELT_EXCLUDED_2 hint test should have failed")
}
}

func TestAssertLeFeltHintOk(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"a": {NewMaybeRelocatableFelt(FeltFromUint64(17))},
"b": {NewMaybeRelocatableFelt(FeltFromUint64(18))},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: ASSERT_LT_FELT,
})
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err != nil {
t.Errorf("ASSERT_LT_FELT hint test failed with error %s", err)
}
}

func TestAssertLeFeltHintErr(t *testing.T) {
vm := NewVirtualMachine()
vm.Segments.AddSegment()
idsManager := SetupIdsForTest(
map[string][]*MaybeRelocatable{
"a": {NewMaybeRelocatableFelt(FeltFromUint64(17))},
"b": {NewMaybeRelocatableFelt(FeltFromUint64(16))},
},
vm,
)
hintProcessor := CairoVmHintProcessor{}
hintData := any(HintData{
Ids: idsManager,
Code: ASSERT_LT_FELT,
})
err := hintProcessor.ExecuteHint(vm, &hintData, nil, nil)
if err != nil {
t.Errorf("ASSERT_LT_FELT hint test should have failed")
}
}

0 comments on commit 789fce2

Please sign in to comment.