diff --git a/pkg/hints/hint_processor.go b/pkg/hints/hint_processor.go index 9eff95b0..2b222969 100644 --- a/pkg/hints/hint_processor.go +++ b/pkg/hints/hint_processor.go @@ -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) } diff --git a/pkg/hints/math_hint_codes.go b/pkg/hints/math_hint_codes.go index a3a47679..623a8e90 100644 --- a/pkg/hints/math_hint_codes.go +++ b/pkg/hints/math_hint_codes.go @@ -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}.'" diff --git a/pkg/hints/math_hints.go b/pkg/hints/math_hints.go index 2ab24bc4..f2fb2450 100644 --- a/pkg/hints/math_hints.go +++ b/pkg/hints/math_hints.go @@ -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 +} diff --git a/pkg/hints/math_hints_test.go b/pkg/hints/math_hints_test.go index d8d09c52..43aa090d 100644 --- a/pkg/hints/math_hints_test.go +++ b/pkg/hints/math_hints_test.go @@ -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") + } +}