Skip to content

Commit

Permalink
Implement AssertLeIsFirstExcluded & AssertLeIsSecondExcluded (#167)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mmk-1 authored Nov 24, 2023
1 parent a474b0c commit cbad665
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/hintrunner/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
57 changes: 57 additions & 0 deletions pkg/hintrunner/hint_bechmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

Expand Down
50 changes: 50 additions & 0 deletions pkg/hintrunner/hint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit cbad665

Please sign in to comment.