Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add a basic cairo zero runner #53

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4e85939
Initial runner draft
rodrigo-pino Sep 5, 2023
5b745c9
Initial assembler draft
rodrigo-pino Sep 5, 2023
45b3b45
Add assert eq logic
rodrigo-pino Sep 6, 2023
86a3dc7
Add tests for assembler
rodrigo-pino Sep 6, 2023
ca26f65
Complete assembler
rodrigo-pino Sep 7, 2023
2af5c8b
Add golangci config to ignore grammar.go
rodrigo-pino Sep 7, 2023
6b6b059
Rename config file
rodrigo-pino Sep 7, 2023
8595730
Fix many bugs
rodrigo-pino Sep 7, 2023
1dd8e44
Add extra grammar tests
rodrigo-pino Sep 8, 2023
84165f7
Add assembler test
rodrigo-pino Sep 8, 2023
288c8fb
Merge branch 'main' into feat-cairo-runner
rodrigo-pino Sep 11, 2023
afd3138
rework runner
rodrigo-pino Sep 12, 2023
c0133b0
Merge branch 'main' into feat-cairo-runner
rodrigo-pino Sep 14, 2023
220f5ea
Minor improvements to cli
rodrigo-pino Sep 15, 2023
754f8a4
Minor improvements to zero parsing tests
rodrigo-pino Sep 15, 2023
cc17afc
Add loading functions for runner
rodrigo-pino Sep 15, 2023
4f52a25
Add basic testing for runner
rodrigo-pino Sep 15, 2023
c90023d
Simplify ast structure
rodrigo-pino Sep 15, 2023
a421684
Merge branch 'feat-casm-assembler' into feat-cairo-runner
rodrigo-pino Sep 18, 2023
a8812d6
add simple test for runner
rodrigo-pino Sep 18, 2023
401da81
Fix minor bugs in assembler
rodrigo-pino Sep 18, 2023
f4eac83
Overhaul vm erros and minor bug fix
rodrigo-pino Sep 18, 2023
a221efa
Add new int to mem val func
rodrigo-pino Sep 18, 2023
77a68b0
Add string representation to Instruction
rodrigo-pino Sep 18, 2023
0eb5a88
Finish runner simple test
rodrigo-pino Sep 18, 2023
6fdd417
Merge branch 'main' into feat-cairo-runner
rodrigo-pino Sep 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 95 additions & 9 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,106 @@ package main

import (
"fmt"
runnerzero "github.com/NethermindEth/cairo-vm-go/pkg/runners/zero"
"github.com/urfave/cli/v2"
"os"
)

func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: cairo-vm <path_to_file>")
os.Exit(1)
}
var proofmode bool
var entrypoint string
var traceLocation string
var arguments cli.StringSlice

path := os.Args[1]
app := &cli.App{
Name: "cairo-vm",
Usage: "A cairo virtual machine",
EnableBashCompletion: true,
Suggest: true,
DefaultCommand: "help",
Commands: []*cli.Command{
{
Name: "run",
Usage: "runs a cairo file",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "proofmode",
Aliases: []string{"p"},
Value: false,
Usage: "outputs the proof of execution",
Required: false,
Destination: &proofmode,
},
&cli.StringFlag{
Name: "entrypoint",
Aliases: []string{"e"},
Value: "main",
Usage: "name of the function to run",
Required: true,
Destination: &entrypoint,
},
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "path to file to store the trace",
Required: false,
Destination: &traceLocation,
},
&cli.StringSliceFlag{
Name: "args",
Usage: "path to file to store the trace",
Required: false,
Destination: &arguments,
},
},
Action: func(ctx *cli.Context) error {
pathToFile := ctx.Args().Get(0)
if pathToFile == "" {
return fmt.Errorf("path to cairo file not set")
}

runCairoFile(path)
}
fmt.Printf("Loading program at %s\n", pathToFile)
content, err := os.ReadFile(pathToFile)
if err != nil {
return fmt.Errorf("cannot load program: %w", err)
}
program, err := runnerzero.LoadCairoZeroProgram(content)
if err != nil {
return fmt.Errorf("cannot load program: %w", err)
}

fmt.Printf("Running....")
runner, err := runnerzero.NewRunner(program, proofmode)
if err != nil {
return fmt.Errorf("cannot create runner: %w", err)
}

end, err := runner.InitializeMainEntrypoint()
if err != nil {
return fmt.Errorf("cannot create runner: %w", err)
}

err = runner.RunUntilPc(end)
if err != nil {
return fmt.Errorf("cannot create runner: %w", err)
}

func runCairoFile(path string) {
fmt.Printf("Running Cairo file at path: %s\n", path)
if proofmode {
err = runner.BuildProof()
if err != nil {
return err
}
}

fmt.Printf("Success!")
return nil
},
},
},
}

if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ go 1.21

require (
github.com/bits-and-blooms/bitset v1.8.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand All @@ -19,5 +22,6 @@ require (
github.com/consensys/gnark-crypto v0.11.1
github.com/go-playground/validator/v10 v10.4.1
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/bits-and-blooms/bitset v1.8.0 h1:FD+XqgOZDUxxZ8hzoBFuV9+cGWY9CslN6d5M
github.com/bits-and-blooms/bitset v1.8.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/consensys/gnark-crypto v0.11.1 h1:pt2nLbntYZA5IXnSw21vcQgoUCRPn6J/xylWQpK8gtM=
github.com/consensys/gnark-crypto v0.11.1/go.mod h1:Iq/P3HHl0ElSjsg2E1gsMwhAyxnxoKK5nVyZKd+/KhU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -27,10 +29,16 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa h1:zuSxTR4o9y82ebqCUJYNGJbGPo6sKVl54f/TVDObg1c=
Expand Down
2 changes: 1 addition & 1 deletion pkg/assembler/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestAssertEqRegister(t *testing.T) {
encode := parseSingleInstruction("[ap + 0] = [fp + 0], ap++;")
encode := parseSingleInstruction("[ap] = [fp + 0], ap++;")

// verify offsets
dstOffset := uint16(encode)
Expand Down
6 changes: 3 additions & 3 deletions pkg/assembler/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Grammar and AST

type CasmProgram struct {
Instructions []Instruction `@@`
Instructions []Instruction `@@*`
}

type Instruction struct {
Expand Down Expand Up @@ -150,14 +150,14 @@ func (deref *Deref) IsFp() bool {

func (deref *Deref) BiasedOffset() (uint16, error) {
if deref.Offset == nil {
return 0, nil
return biasedZero, nil
}
return biasedOffset(deref.Offset.Sign == "-", *deref.Offset.Value)
}

func (dderef *DoubleDeref) BiasedOffset() (uint16, error) {
if dderef.Offset == nil {
return 0, nil
return biasedZero, nil
}
return biasedOffset(dderef.Offset.Sign == "-", *dderef.Offset.Value)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/hintrunner/hintrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type HintRunner struct {
hints map[uint64]Hinter
}

func CreateHintRunner(hints map[uint64]Hinter) HintRunner {
func NewHintRunner(hints map[uint64]Hinter) HintRunner {
return HintRunner{hints}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/hintrunner/hintrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestExistingHint(t *testing.T) {
var ap ApCellRef = 5
allocHint := AllocSegment{ap}

hr := CreateHintRunner(map[uint64]Hinter{
hr := NewHintRunner(map[uint64]Hinter{
10: allocHint,
})

Expand All @@ -36,7 +36,7 @@ func TestNoHint(t *testing.T) {
var ap ApCellRef = 5
allocHint := AllocSegment{ap}

hr := CreateHintRunner(map[uint64]Hinter{
hr := NewHintRunner(map[uint64]Hinter{
10: allocHint,
})

Expand Down
10 changes: 10 additions & 0 deletions pkg/parsers/zero/zero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ func TestIdentifiers(t *testing.T) {
content := []byte(`
{
"identifiers": {
"__main__.fib": {
"decorators": [],
"pc": 9,
"type": "function"
},
"__main__.BitwiseBuiltin": {
"destination": "starkware.cairo.common.cairo_builtins.BitwiseBuiltin",
"type": "alias"
Expand Down Expand Up @@ -216,6 +221,11 @@ func TestIdentifiers(t *testing.T) {
t,
&ZeroProgram{
Identifiers: map[string]any{
"__main__.fib": map[string]any{
"decorators": make([]any, 0),
"pc": float64(9),
"type": "function",
},
"__main__.BitwiseBuiltin": map[string]any{
"destination": "starkware.cairo.common.cairo_builtins.BitwiseBuiltin",
"type": "alias",
Expand Down
1 change: 0 additions & 1 deletion pkg/runner/runner.go

This file was deleted.

Loading