Skip to content
reilabsPublic

About

Compiling Noir to R1CS with witness generation and automatic differentiation binaries

Topics

Resources

Contributing

Stars

26 stars

Watchers

1 watching

Forks

Latest commit

 

History

572 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mavros

Mavros is a toolchain for compiling Noir to R1CS with additional functionality for use with Spartan. It provides a complete pipeline from Noir source to the output of the eventual circuit, and is designed with mobile proving workflows in mind.

Building

This project uses Nix flakes to provide a pinned build environment with all the tools you need to build the project. Assuming you have nix installed, simply make shell to drop into your user shell inside the build environment, or run nix develop --command <your-command-here> to run a specific command in the build environment.

You can use one of the two following methods to experiment with Mavros.

  • Running make install will build and install Mavros to your cargo binary directory.
  • Running make release will build Mavros to ./target/release/mavros.

Run make to get an overview of the available utilities and targets. For complete guidance please see our contributing guidelines. See usage below for more information on how to use Mavros.

Usage

To use Mavros, navigate to the root of a Noir project (the directory containing Nargo.toml) in your shell. Then run mavros compile.

This writes one file, target/program.mavros:

Part Contents
Header MAVR1CS magic, format version, compiler version, field tag, artifact digest
Body Witness manifest, R1CS, VM bytecode (witgen and AD entry points), ABI as JSON

The circuit digest covers the R1CS, which hashes its field tag, and the manifest. The artifact digest covers the circuit digest, the bytecode and the ABI, and is checked whenever the file is read. The magic, format version and compiler version are not hashed.

Pass --field to choose the prime field that the whole pipeline uses: bn254 (the default) or goldilocks. The standard library's BN254-specific items (elliptic curves, ECDSA, Poseidon2 and the bignum library) are unavailable on Goldilocks, so a program that uses them fails to compile. See Fields in Mavros for how the fields differ.

Pass --include-debug-info to additionally write target/program.debug.json. This standalone, versioned source map records VM bytecode word offsets, function names, and Noir source locations; the artifact itself remains byte-for-byte unchanged. Source paths are relative to the Noir package root by default; pass --absolute-paths to retain absolute paths.

Inputs

main takes and returns integers (see Wide Integers), bool, str<N>, and arrays, tuples and structs of these types. Field and the data bus are refused anywhere in main's signature, so a program that works with field elements must convert to them inside main.

Running mavros without a subcommand compiles the project, reads its Prover.toml, generates the witness and checks it. Prover.toml holds one value per parameter, keyed by name. An integer is a TOML integer or a quoted one: an optional -, an optional 0x, an optional +, then digits, which may hold _ after the first. A value past i64::MAX must be quoted, and every value must fit into the bounds of its declared type. A boolean is true, false, 0 or 1, and a string is a TOML string of exactly its length in bytes. An optional return entry states the expected return value, which is compared with the computed one.

Verifier Mode

Compute the Spartan matrix contractions (rAw, rBw, rCw) with caller-supplied field vectors:

mavros --root ./my-noir-project --verify --r r.json --w w.json

Both files contain JSON arrays of decimal field strings, for example ["3", "17", "42"]. r has one entry per R1CS constraint. w covers the complete witness layout in order: algebraic witnesses (including column zero), multiplicities, challenges, table data, and lookup data. Vector lengths must match exactly. All entries may be arbitrary, including w[0]; the caller supplies any protocol randomness. Paths to vector files are relative to the current working directory.

Each string must be canonical: unsigned decimal without leading zeros (except "0"), and strictly less than the modulus of the field selected with --field. Values are rejected rather than reduced modulo the field.

Verifier mode requires a single binary package. For a multi-binary workspace, select a member with --root or configure default-member in Nargo.toml; otherwise the command fails before compiling any member or writing a result.

The command compiles the project and prints a JSON object with r_aw, r_bw, and r_cw as decimal strings. It needs no Prover.toml and does not check R1CS satisfaction. This operation is a component of a verifier, not a complete proof verification protocol.

The bytecode in program.mavros holds only the witgen and AD entry points. To additionally emit a separate verifier artifact:

mavros compile ./my-noir-project --verifier-output target/verifier.json

verifier.json contains the verifier bytecode, witness_layout, constraints_layout and the circuit_digest of the program.mavros written alongside it; it contains no matrix rows or prover entry points. --include-debug-info also writes verifier.debug.json. Default compilation emits no verifier code. --verify compiles and runs only the verifier artifact.

Rust callers compile it with mavros_compiler::api::compile_verifier_bytecode(&mut driver, options) and pass that binary to run_verifier_from_binary(&binary, &r, &w, witness_layout, constraints_layout, None). No R1CS matrices are required at runtime.

Both artifacts must come from the same circuit compilation; applications must associate the verifier artifact with their trusted circuit configuration. Comparing the two circuit_digests catches a verifier from another circuit, not a forged one: nothing hashes the verifier bytecode.

The verifier runs forward through its own bytecode entry point, allocating no AD nodes or matrix outputs: witness writes read the next value from w, and every constraint, lookups and tables included, adds to three scalars weighted by r.

The bytecode header names its artifact's field and constraint layouts, and AD and verifier execution reject mismatched layouts. Recompile older artifacts before using the current VM. LLVM and WASM artifacts expose only the witgen and AD entry points; the forward verifier runs through the VM API and CLI.

Per-Function FlameGraphs

Run the full compiler and VM pipeline with --profile to generate interactive per-function FlameGraphs:

mavros --root . --profile

The command writes folded-stack inputs and SVGs under mavros_debug/flamegraphs/ for four dimensions: constraint_size, witness_size, witgen_time, and ad_time. The two time profiles use the VM's deterministic simulated instruction count rather than wall-clock samples, so profiles can be compared across machines and runs. Profiling automatically enables the VM debug mapping needed to recover Noir function names. Shared lookup-table allocations appear under a separate <lookup tables> branch; the cost of each lookup remains attributed to its callsite.

The command also writes witgen_time.cpuprofile and ad_time.cpuprofile. Load either file from the Chrome DevTools Performance panel to inspect it with the DevTools flame chart and call tree. These profiles preserve execution order and use deterministic instruction intervals rather than wall-clock time. Runs of up to 100,000 instructions contain one sample per instruction; larger runs use bounded, deterministic stratified sampling to avoid periodic sampling bias while keeping file size practical.

SVG rendering uses Brendan Gregg's FlameGraph flamegraph.pl. It is pinned and available in nix develop; outside the Nix shell, install FlameGraph and put flamegraph.pl on PATH. When the script is unavailable, Mavros still writes the folded profiles and Chrome .cpuprofile files and skips only the SVGs.

For advanced usage and the CLI options, run mavros --help. To output the witness generation binary for WASM-capable platforms, please see the WASM output section in our contributing docs.

Noir Workspaces

When pointed at a workspace, Mavros honors default-member. Without a default, the CLI runs or compiles every binary member in manifest order; library members remain available as dependencies. Each binary uses its own Prover.toml and mavros_debug directory. A failed member makes the workspace run fail, while the remaining members are still checked.

When mavros compile resolves a package root different from the supplied path, a relative --output (and --verifier-output) is taken relative to that package root, including for a single default member, so the default produces <member>/target/program.mavros. An absolute output path, or one containing .., is rejected for multi-binary compilations. Point Mavros directly at a member directory to compile just that package with the usual output-path behavior.

The functional test runner reports each selected binary workspace member as a separate test, with its own checks, circuit size, and artifact sizes. Libraries are not separate executable tests.

Usage Example

cd my-noir-project
mavros compile
# → target/program.mavros

Noir Feature Support and Completeness

For the current status of Mavros' support for various Noir constructs, see the status doc that is generated by CI on every update. This provides information on how far the Mavros pipeline gets for each Noir test.

Wide Integers

The frontend is based on the provekit-v2 Noir fork. This means that Mavros supports integer widths 2 through 16384 for both signed and unsigned integers for use within programs.

Integers of every width cross main as parameters and returns (Inputs). An integer wider than the widest a field element holds injectively (253 bits on bn254, 63 on Goldilocks) crosses as its limbs, little-endian: 64-bit limbs on bn254 and 16-bit limbs on Goldilocks, one witness column each, each bounded to its width. Prover.toml states such a value, and an expected return, as one integer, quoted past i64::MAX, which the host splits into the same limbs.

A Field can be cast to a wider integer, including in constrained code; the compiler decomposes the canonical field value into limbs.

About

Compiling Noir to R1CS with witness generation and automatic differentiation binaries

Topics

Resources

Contributing

Stars

26 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages