-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add zero parser and test (#23)
- Loading branch information
Showing
4 changed files
with
175 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package zero | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type ZeroProgram struct { | ||
Attributes []string `json:"attributes"` | ||
Builtins map[string]int64 `json:"builtins"` | ||
Code string `json:"code"` | ||
DebugInfo DebugInfo `json:"debug_info"` | ||
Prime string `json:"prime"` | ||
Version string `json:"version"` | ||
Hints map[string][]Hint `json:"hints"` | ||
} | ||
|
||
type DebugInfo struct { | ||
InstructionOffsets []int64 `json:"instruction_offsets"` | ||
SourceCode string `json:"source_code"` | ||
SourcePath string `json:"source_path"` | ||
} | ||
|
||
type Hint struct { | ||
AccessibleScopes []string `json:"accessible_scopes"` | ||
Code string `json:"code"` | ||
FlowTrackingData FlowTrackingData `json:"flow_tracking_data"` | ||
} | ||
|
||
type FlowTrackingData struct { | ||
ApTracking ApTracking `json:"ap_tracking"` | ||
ReferenceIds map[string]interface{} `json:"reference_ids"` | ||
} | ||
|
||
type ApTracking struct { | ||
Group int `json:"group"` | ||
Offset int `json:"offset"` | ||
} | ||
|
||
func (z ZeroProgram) MarshalToFile(filepath string) error { | ||
// Marshal Output struct into JSON bytes | ||
data, err := json.MarshalIndent(z, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write JSON bytes to file | ||
err = os.WriteFile(filepath, data, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ZeroProgramFromFile(filepath string) (zero *ZeroProgram, err error) { | ||
content, err := os.ReadFile(filepath) | ||
if err != nil { | ||
return | ||
} | ||
return ZeroProgramFromJSON(content) | ||
} | ||
|
||
func ZeroProgramFromJSON(content json.RawMessage) (*ZeroProgram, error) { | ||
var zero ZeroProgram | ||
return &zero, json.Unmarshal(content, &zero) | ||
} |
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,105 @@ | ||
package zero | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestZeroParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
jsonData string | ||
expected ZeroProgram | ||
}{ | ||
{ | ||
name: "Attributes field", | ||
jsonData: ` | ||
{ | ||
"attributes": ["attr1", "attr2"] | ||
}`, | ||
expected: ZeroProgram{ | ||
Attributes: []string{"attr1", "attr2"}, | ||
}, | ||
}, | ||
{ | ||
name: "Builtins field", | ||
jsonData: ` | ||
{ | ||
"builtins": {"builtin1": 1, "builtin2": 2} | ||
}`, | ||
expected: ZeroProgram{ | ||
Builtins: map[string]int64{"builtin1": 1, "builtin2": 2}, | ||
}, | ||
}, | ||
{ | ||
name: "Code field", | ||
jsonData: ` | ||
{ | ||
"code": "some code sample" | ||
}`, | ||
expected: ZeroProgram{ | ||
Code: "some code sample", | ||
}, | ||
}, | ||
{ | ||
name: "Version field", | ||
jsonData: ` | ||
{ | ||
"version": "0.12.2" | ||
}`, | ||
expected: ZeroProgram{ | ||
Version: "0.12.2", | ||
}, | ||
}, | ||
{ | ||
name: "Prime field", | ||
jsonData: ` | ||
{ | ||
"prime": "0x800000000000011000000000000000000000000000000000000000000000001" | ||
}`, | ||
expected: ZeroProgram{ | ||
Prime: "0x800000000000011000000000000000000000000000000000000000000000001", | ||
}, | ||
}, | ||
{ | ||
name: "Hints field", | ||
jsonData: ` | ||
{ | ||
"hints": { | ||
"0": [ | ||
{ | ||
"accessible_scopes": [ | ||
"starkware.cairo.common.math", | ||
"starkware.cairo.common.math.assert_not_equal" | ||
], | ||
"code": "from starkware.cairo.lang.vm.relocatable import RelocatableValue\nboth_ints = isinstance(ids.a, int) and isinstance(ids.b, int)\nboth_relocatable = (\n isinstance(ids.a, RelocatableValue) and isinstance(ids.b, RelocatableValue) and\n ids.a.segment_index == ids.b.segment_index)\nassert both_ints or both_relocatable, \\\n f'assert_not_equal failed: non-comparable values: {ids.a}, {ids.b}.'\nassert (ids.a - ids.b) % PRIME != 0, f'assert_not_equal failed: {ids.a} = {ids.b}.'" | ||
} | ||
] | ||
} | ||
}`, | ||
expected: ZeroProgram{ | ||
Hints: map[string][]Hint{ | ||
"0": {{ | ||
AccessibleScopes: []string{"starkware.cairo.common.math", "starkware.cairo.common.math.assert_not_equal"}, | ||
Code: "from starkware.cairo.lang.vm.relocatable import RelocatableValue\nboth_ints = isinstance(ids.a, int) and isinstance(ids.b, int)\nboth_relocatable = (\n isinstance(ids.a, RelocatableValue) and isinstance(ids.b, RelocatableValue) and\n ids.a.segment_index == ids.b.segment_index)\nassert both_ints or both_relocatable, \\\n f'assert_not_equal failed: non-comparable values: {ids.a}, {ids.b}.'\nassert (ids.a - ids.b) % PRIME != 0, f'assert_not_equal failed: {ids.a} = {ids.b}.'", | ||
}}, | ||
}, | ||
}, | ||
}, | ||
// TODO: Add debug-info test case | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var z ZeroProgram | ||
err := json.Unmarshal([]byte(tt.jsonData), &z) | ||
if err != nil { | ||
t.Errorf("Failed to unmarshal JSON: %s", err) | ||
} | ||
|
||
assert.Equal(t, tt.expected, z, "Field does not match") | ||
}) | ||
} | ||
} |