Skip to content

Commit

Permalink
feat: convert felt to uint64 in hints where possible (#484)
Browse files Browse the repository at this point in the history
* convert felt to uint64 where possible

* make it work

* update unit test

* update comment

---------

Co-authored-by: Shourya Goel <shouryagoel10000@gmail.com>
  • Loading branch information
TAdev0 and Sh0g0-1758 committed Jul 3, 2024
1 parent f1df68d commit 707d13f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 6 additions & 7 deletions pkg/hintrunner/zero/zerohint_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ func newMemContinueHint(continueTarget hinter.ResOperander, memset bool) hinter.
return err
}

newN, ok := n.(fp.Element)
newN, ok := n.(uint64)
if !ok {
return fmt.Errorf("casting n into a felt failed")
return fmt.Errorf("casting n into a uint64 failed")
}

newN.Sub(&newN, &utils.FeltOne)
newN = newN - 1

if err := ctx.ScopeManager.AssignVariable("n", newN); err != nil {
return err
Expand All @@ -55,7 +54,7 @@ func newMemContinueHint(continueTarget hinter.ResOperander, memset bool) hinter.
}

var continueTargetMv memory.MemoryValue
if utils.FeltLt(&utils.FeltZero, &newN) {
if newN > 0 {
continueTargetMv = memory.MemoryValueFromFieldElement(&utils.FeltOne)
} else {
continueTargetMv = memory.MemoryValueFromFieldElement(&utils.FeltZero)
Expand Down Expand Up @@ -127,12 +126,12 @@ func newMemEnterScopeHint(value hinter.ResOperander, memset bool) hinter.Hinter
// MemcpyEnterScope
//> vm_enter_scope({'n': ids.len})

value, err := hinter.ResolveAsFelt(vm, value)
value, err := hinter.ResolveAsUint64(vm, value)
if err != nil {
return err
}

ctx.ScopeManager.EnterScope(map[string]any{"n": *value})
ctx.ScopeManager.EnterScope(map[string]any{"n": value})
return nil
},
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/hintrunner/zero/zerohint_others_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestZeroHintOthers(t *testing.T) {
{Name: "continue_copying", Kind: uninitialized},
},
ctxInit: func(ctx *hinter.HintRunnerContext) {
err := ctx.ScopeManager.AssignVariable("n", *feltInt64(1))
err := ctx.ScopeManager.AssignVariable("n", uint64(1))
if err != nil {
t.Fatal(err)
}
Expand All @@ -25,7 +25,7 @@ func TestZeroHintOthers(t *testing.T) {
return newMemContinueHint(ctx.operanders["continue_copying"], false)
},
check: func(t *testing.T, ctx *hintTestContext) {
varValueInScopeEquals("n", *feltInt64(0))(t, ctx)
varValueInScopeEquals("n", uint64(0))(t, ctx)
varValueEquals("continue_copying", feltInt64(0))(t, ctx)
},
},
Expand All @@ -34,7 +34,7 @@ func TestZeroHintOthers(t *testing.T) {
{Name: "continue_copying", Kind: uninitialized},
},
ctxInit: func(ctx *hinter.HintRunnerContext) {
err := ctx.ScopeManager.AssignVariable("n", *feltInt64(5))
err := ctx.ScopeManager.AssignVariable("n", uint64(5))
if err != nil {
t.Fatal(err)
}
Expand All @@ -43,7 +43,7 @@ func TestZeroHintOthers(t *testing.T) {
return newMemContinueHint(ctx.operanders["continue_copying"], false)
},
check: func(t *testing.T, ctx *hintTestContext) {
varValueInScopeEquals("n", *feltInt64(4))(t, ctx)
varValueInScopeEquals("n", uint64(4))(t, ctx)
varValueEquals("continue_copying", feltInt64(1))(t, ctx)
},
},
Expand All @@ -56,7 +56,7 @@ func TestZeroHintOthers(t *testing.T) {
makeHinter: func(ctx *hintTestContext) hinter.Hinter {
return newMemEnterScopeHint(ctx.operanders["len"], false)
},
check: varValueInScopeEquals("n", *feltUint64(1)),
check: varValueInScopeEquals("n", uint64(1)),
},
},
"SearchSortedLower": {
Expand Down

0 comments on commit 707d13f

Please sign in to comment.