Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mmk-1 committed Sep 26, 2023
2 parents 1e1cfe7 + b4be588 commit 39f71f6
Show file tree
Hide file tree
Showing 30 changed files with 1,649 additions and 148 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Integration Tests

on:
push:
branches:
- main
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install cairo-lang
run: pip install cairo-lang==0.11

- name: Build
run: make build

- name: Test
run: make integration
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Unit Tests

on:
push:
Expand All @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Set up go
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.20"
Expand All @@ -21,5 +21,5 @@ jobs:
run: make build

- name: Test
run: make test
run: make unit

30 changes: 23 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ default: help

help:
@echo "This makefile allos the following commands"
@echo " make build - compile the source code"
@echo " make clean - remove binary files"
@echo " make test - run tests"
@echo " make help - show this help message"
@echo " make build - compile the source code"
@echo " make clean - remove binary files"
@echo " make unit - run unit tests"
@echo " make integration - run integration tests"
@echo " make testall - run all tests"
@echo " make help - show this help message"

build:
@echo "Building..."
Expand All @@ -27,8 +29,22 @@ clean:
@echo "Cleaning up..."
@rm -rf $(BINARY_DIR)

test:
@echo "Running tests..."
unit:
@echo "Running unit tests..."
@go test ./pkg/...

integration:
@echo "Running integration tests..."
@$(MAKE) build
@if [ $$? -eq 0 ]; then \
go test ./integration_tests/... -v; \
else \
echo "Integration tests were not run"; \
exit 1; \
fi

testall:
@echo "Running all tests..."
@go test ./...

format:
Expand All @@ -37,4 +53,4 @@ format:
staticcheck:
@staticcheck ./...

pre-commit: format staticcheck build test clean
pre-commit: format staticcheck build test clean
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,39 @@ Currently, it is only possible to use it by building it from source by following
3. Execute on the root folder of the repo: `make build`.
4. Make sure everything is running smoothly by executing: `make test`.

After completing these steps, you can find the compiled VM in `bin/cairo-vm`. It is worth noting that this binary, in the current state of the project, is still non-functional.
After completing these steps, you can find the compiled VM in `bin/cairo-vm`.

### Run The VM

To run the VM you need first compile the cairo file using [cairo-lang](https://github.com/starkware-libs/cairo-lang).
Install it with the following command:

```bash
pip install cairo-lang==0.11
```

The next step is to compile a cairo file, an example would be:

```bash
cairo-compile ./integration_tests/cairo_files/factorial.cairo --proof_mode --output ./factorial_compiled.json
```

This will compile `factorial.cairo` and store the compilation result in `factorial_compiled.json`. The `--proof_mode` flag makes the compilation output special identifiers that allow to get proof of execution later on. Finally, let's execute `factorial_compiled.json` with the next command:

```bash
./bin/cairo-vm run factorial_compiled.json --proofmode --tracefile factorial_trace --memoryfile factorial_memory
```

When this command finishes, `factorial.cairo` has run correctly starting from the `main` function. The `--proofmode` flag indicates that a proof of execution should be generated. The location where this proof is stored is determined by both `--tracefile` and `--memoryfile` flags accordingly.

To test the correct output of the VM compared to the result of the Python VM, you just have to run:

```bash
make integration
```

This will take all Cairo files inside _./integration_tests/cairo_files/_ and run both VMs comparing that both proofs of executions are equal.


### Useful Commands

Expand Down
97 changes: 88 additions & 9 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,98 @@ package main
import (
"fmt"
"os"

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

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

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 zero compiled file",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "proofmode",
Usage: "runs the cairo vm in proof mode",
Required: false,
Destination: &proofmode,
},
&cli.StringFlag{
Name: "tracefile",
Usage: "location to store the relocated trace",
Required: false,
Destination: &traceLocation,
},
&cli.StringFlag{
Name: "memoryfile",
Usage: "location to store the relocated memory",
Required: false,
Destination: &memoryLocation,
},
},
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.Println("Running....")
runner, err := runnerzero.NewRunner(program, proofmode)
if err != nil {
return fmt.Errorf("cannot create runner: %w", err)
}

if err := runner.Run(); err != nil {
return fmt.Errorf("runtime error: %w", err)
}

func runCairoFile(path string) {
fmt.Printf("Running Cairo file at path: %s\n", path)
if proofmode {
trace, memory, err := runner.BuildProof()
if err != nil {
return fmt.Errorf("cannot build proof: %w", err)
}
if traceLocation != "" {
if err := os.WriteFile(traceLocation, trace, 0644); err != nil {
return fmt.Errorf("cannot write relocated trace: %w", err)
}
}
if memoryLocation != "" {
if err := os.WriteFile(memoryLocation, memory, 0644); err != nil {
return fmt.Errorf("cannot write relocated memory: %w", err)
}
}
}

fmt.Println("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
12 changes: 12 additions & 0 deletions integration_tests/cairo_files/factorial.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func factorial(n) -> (result: felt) {
if (n == 1) {
return (n,);
}
let (a) = factorial(n - 1);
return (n * a,);
}

func main() {
factorial(2000);
return ();
}
15 changes: 15 additions & 0 deletions integration_tests/cairo_files/fib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func main() {
fib(1, 1, 100);

ret;
}

func fib(first_element, second_element, n) -> (res: felt) {
if (n == 0) {
return (second_element,);
}

tempvar y = first_element + second_element;
return fib(second_element, y, n - 1);
}

6 changes: 6 additions & 0 deletions integration_tests/cairo_files/simple.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func main() {
[ap] = 5, ap++;
[ap] = 6;
ret;
}

Loading

0 comments on commit 39f71f6

Please sign in to comment.