From cbad665c9de8d17087e17978498e847bd6c916c9 Mon Sep 17 00:00:00 2001 From: "M. Mahdi Khosravi" Date: Fri, 24 Nov 2023 17:38:06 +0300 Subject: [PATCH] Implement AssertLeIsFirstExcluded & AssertLeIsSecondExcluded (#167) * added UpdatePc tests and fixed a bug in UpdatePc * Fixed accessing field value without Read() * fixed failing tests * small refactor for TestUpdatePcJump * added the two AssertLe- hints * fixed compile errors * add benchmarks * fixed missing dereference * remove duplicate funcs * remove unnecessary initializations --- pkg/hintrunner/hint.go | 49 ++++++++++++++++++++++++ pkg/hintrunner/hint_bechmark_test.go | 57 ++++++++++++++++++++++++++++ pkg/hintrunner/hint_test.go | 50 ++++++++++++++++++++++++ 3 files changed, 156 insertions(+) diff --git a/pkg/hintrunner/hint.go b/pkg/hintrunner/hint.go index 2282d1790..588b21f08 100644 --- a/pkg/hintrunner/hint.go +++ b/pkg/hintrunner/hint.go @@ -1229,3 +1229,52 @@ func (hint *AssertLeFindSmallArc) Execute(vm *VM.VirtualMachine, ctx *HintRunner } return nil } + + +type AssertLeIsFirstArcExcluded struct { + skipExcludeAFlag CellRefer +} + +func (hint *AssertLeIsFirstArcExcluded) String() string { + return "AssertLeIsFirstArcExcluded" +} + +func (hint *AssertLeIsFirstArcExcluded) Execute(vm *VM.VirtualMachine, ctx *HintRunnerContext) error { + addr, err := hint.skipExcludeAFlag.Get(vm) + if err != nil { + return fmt.Errorf("get skipExcludeAFlag addr: %v", err) + } + + var writeValue mem.MemoryValue + if ctx.ExcludedArc != 0 { + writeValue = mem.MemoryValueFromInt(1) + } else { + writeValue = mem.MemoryValueFromInt(0) + } + + return vm.Memory.WriteToAddress(&addr, &writeValue) +} + +type AssertLeIsSecondArcExcluded struct { + skipExcludeBMinusA CellRefer +} + +func (hint *AssertLeIsSecondArcExcluded) String() string { + return "AssertLeIsSecondArcExcluded" +} + +func (hint *AssertLeIsSecondArcExcluded) Execute(vm *VM.VirtualMachine, ctx *HintRunnerContext) error { + addr, err := hint.skipExcludeBMinusA.Get(vm) + if err != nil { + return fmt.Errorf("get skipExcludeBMinusA addr: %v", err) + } + + var writeValue mem.MemoryValue + if ctx.ExcludedArc != 1 { + writeValue = mem.MemoryValueFromInt(1) + } else { + writeValue = mem.MemoryValueFromInt(0) + } + + return vm.Memory.WriteToAddress(&addr, &writeValue) +} diff --git a/pkg/hintrunner/hint_bechmark_test.go b/pkg/hintrunner/hint_bechmark_test.go index a0e1fb525..dcc2a9db3 100644 --- a/pkg/hintrunner/hint_bechmark_test.go +++ b/pkg/hintrunner/hint_bechmark_test.go @@ -239,6 +239,63 @@ func BenchmarkUint256SquareRoot(b *testing.B) { } vm.Context.Ap += 5 } +} + +func BenchmarkAssertLeIsFirstArcExcluded(b *testing.B) { + vm := defaultVirtualMachine() + vm.Context.Ap = 0 + vm.Context.Fp = 0 + + ctx := HintRunnerContext{ + ExcludedArc: 0, + } + + var skipExcludeAFlag ApCellRef = 1 + + b.ResetTimer() + for i := 0; i < b.N; i++ { + + hint := AssertLeIsFirstArcExcluded{ + skipExcludeAFlag: skipExcludeAFlag, + } + + err := hint.Execute(vm, &ctx) + if err != nil { + b.Error(err) + break + } + + vm.Context.Ap += 1 + } + +} + +func BenchmarkAssertLeIsSecondArcExcluded(b *testing.B) { + vm := defaultVirtualMachine() + vm.Context.Ap = 0 + vm.Context.Fp = 0 + + ctx := HintRunnerContext{ + ExcludedArc: 0, + } + + var skipExcludeBMinusA ApCellRef = 1 + + b.ResetTimer() + for i := 0; i < b.N; i++ { + + hint := AssertLeIsSecondArcExcluded{ + skipExcludeBMinusA: skipExcludeBMinusA, + } + + err := hint.Execute(vm, &ctx) + if err != nil { + b.Error(err) + break + } + + vm.Context.Ap += 1 + } } diff --git a/pkg/hintrunner/hint_test.go b/pkg/hintrunner/hint_test.go index 01a1dc54b..46b1c55f7 100644 --- a/pkg/hintrunner/hint_test.go +++ b/pkg/hintrunner/hint_test.go @@ -784,3 +784,53 @@ func TestAssertLeFindSmallArc(t *testing.T) { require.Equal(t, tc.expectedExcludedArc, ctx.ExcludedArc) } } + +func TestAssertLeIsFirstArcExcluded(t *testing.T) { + vm := defaultVirtualMachine() + + ctx := HintRunnerContext{ + ExcludedArc: 2, + } + + var flag ApCellRef = 0 + + hint := AssertLeIsFirstArcExcluded{ + skipExcludeAFlag: flag, + } + + err := hint.Execute(vm, &ctx) + + require.NoError(t, err) + + expected := mem.MemoryValueFromInt(1) + + actual := readFrom(vm, VM.ExecutionSegment, 0) + + require.Equal(t, expected, actual) +} + +func TestAssertLeIsSecondArcExcluded(t *testing.T) { + vm := defaultVirtualMachine() + vm.Context.Ap = 0 + vm.Context.Fp = 0 + + ctx := HintRunnerContext{ + ExcludedArc: 1, + } + + var flag ApCellRef = 0 + + hint := AssertLeIsSecondArcExcluded{ + skipExcludeBMinusA: flag, + } + + err := hint.Execute(vm, &ctx) + + require.NoError(t, err) + + expected := mem.MemoryValueFromInt(0) + + actual := readFrom(vm, VM.ExecutionSegment, 0) + + require.Equal(t, expected, actual) +}