-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hintrunner/zero: include the ap tracking data into addr calculations (#…
…198) This patch is aimed to include the ap tracking offsets into the produced ApCellRef. The calculation result is saved as an updated ApCellRef offset. Given the `ApCellRef(-3)` and ap offset of hint `3` and ref `2`, we'll get a new `ApCellRef(-4)` (-3 - (3 - 2) => -4). Fixes #197
- Loading branch information
Showing
4 changed files
with
222 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This test uses the artificially constructed TestAssignCode hint | ||
// to test different hint refs evaluation. | ||
// | ||
// Even if the hint's code is the same in every function below, | ||
// the referenced ids.a always has an address that requires | ||
// a different way of computation (see per-func comments). | ||
// They also usually have different ApTracking values associated with them. | ||
// | ||
// See https://github.com/NethermindEth/cairo-vm-go/issues/197 | ||
|
||
// [cast(fp, felt*)] | ||
func simple_fp_ref() -> felt { | ||
alloc_locals; | ||
local a = 43; | ||
%{ memory[ap] = ids.a %} | ||
return [ap]; | ||
} | ||
|
||
// [cast(ap + (-1), felt*)] | ||
func ap_with_offset() -> felt { | ||
[ap] = 0, ap++; | ||
[ap] = 10, ap++; | ||
tempvar a = 32; | ||
[ap] = 100, ap++; | ||
[ap] = 200, ap++; | ||
%{ memory[ap] = ids.a %} | ||
return [ap]; | ||
} | ||
|
||
// cast([fp + (-4)] + [fp + (-3)], felt) | ||
func fp_args_sum(arg1: felt, arg2: felt) -> felt { | ||
let a = arg1 + arg2; | ||
%{ memory[ap] = ids.a %} | ||
return [ap]; | ||
} | ||
|
||
// cast([ap + (-1)] + [fp + 1], felt) | ||
func ap_plus_fp_deref() -> felt { | ||
alloc_locals; | ||
local l1 = 11; | ||
local l2 = 22; // [fp+1] | ||
local l3 = 33; | ||
tempvar t1 = 111; | ||
tempvar t2 = 222; | ||
tempvar t3 = 333; // [ap-1] | ||
let a = [ap-1] + [fp+1]; | ||
%{ memory[ap] = ids.a %} | ||
return [ap]; // 355 | ||
} | ||
|
||
func main() { | ||
alloc_locals; | ||
local v1 = simple_fp_ref(); | ||
[ap] = v1, ap++; | ||
local v2 = ap_with_offset(); | ||
[ap] = v2, ap++; | ||
local v3 = fp_args_sum(4, 6); | ||
[ap] = v3, ap++; | ||
local v4 = ap_plus_fp_deref(); | ||
[ap] = v4, ap++; | ||
ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package zero | ||
|
||
import ( | ||
"github.com/NethermindEth/cairo-vm-go/pkg/hintrunner/hinter" | ||
) | ||
|
||
type hintReferenceResolver struct { | ||
refs []hintReference | ||
|
||
numCellRefer int | ||
numResOperander int | ||
} | ||
|
||
type hintReference struct { | ||
name string | ||
|
||
cell hinter.CellRefer | ||
operander hinter.ResOperander | ||
} | ||
|
||
func (m *hintReferenceResolver) NumCellRefers() int { return m.numCellRefer } | ||
|
||
func (m *hintReferenceResolver) NumResOperanders() int { return m.numResOperander } | ||
|
||
func (m *hintReferenceResolver) AddCellRefer(name string, v hinter.CellRefer) { | ||
m.refs = append(m.refs, hintReference{ | ||
name: name, | ||
cell: v, | ||
}) | ||
m.numCellRefer++ | ||
} | ||
|
||
func (m *hintReferenceResolver) AddResOperander(name string, v hinter.ResOperander) { | ||
m.refs = append(m.refs, hintReference{ | ||
name: name, | ||
operander: v, | ||
}) | ||
m.numResOperander++ | ||
} | ||
|
||
func (m *hintReferenceResolver) GetResOperander(name string) hinter.ResOperander { | ||
ref := m.find(name) | ||
if ref != nil { | ||
return ref.operander | ||
} | ||
return nil | ||
} | ||
|
||
func (m *hintReferenceResolver) find(name string) *hintReference { | ||
for i, ref := range m.refs { | ||
if ref.name == name { | ||
return &m.refs[i] | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters