Noir’s module and package system follows the same conventions as Rust’s newer module system. Modules help you organize code across multiple files, and packages group crates that are managed together viaDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/noir-lang/noir/llms.txt
Use this file to discover all available pages before exploring further.
Nargo.toml.
Modules
Declaring a module
The compiler does not automatically scan files for modules. You must explicitly declare each module with themod keyword in the file that uses it.
src/main.nr
src/foo.nr
mod foo and looks for either src/foo.nr or src/foo/mod.nr.
It is an error to have both
src/foo.nr and src/foo/mod.nr in the same project.Sub-modules
A module can declare its own sub-modules. The compiler looks for sub-module files relative to the parent module’s directory:src/main.nr
src/foo.nr
src/foo/bar.nr
Referencing modules
All modules are accessible from thecrate:: namespace. Use crate::path to reference any module from anywhere in the project:
super:: to refer to the parent module:
Visibility
Modules are private by default. Usepub or pub(crate) to control visibility:
use statements
Import names from other modules withuse:
Re-exporting with pub use
use declarations are private to their containing module by default. Mark them pub or pub(crate) to re-export names:
Crates and packages
Crate types
A crate is the smallest unit the Noir compiler processes. There are three kinds:Binary crates
Binary crates
Binary crates compile to ACIR circuits that you can prove. They must have a
main function.The crate root file must be named src/main.nr.Library crates
Library crates
Library crates define reusable functionality without a
main function. They do not compile to ACIR directly.The crate root file must be named src/lib.nr.Contract crates
Contract crates
Contract crates compile to ACIR and are deployed to the Aztec network. They expose a collection of functions rather than a single
main.Packages
A package is a collection of one or more crates, described by aNargo.toml file. A package must contain either a library crate or a binary crate, but not both.
A minimal Nargo.toml for a binary:
Dependencies
Git dependencies
Specify a git dependency with atag for reproducible builds:
directory:
Local (path) dependencies
Reference a local library withpath:
Importing dependencies
After declaring a dependency inNargo.toml, import it in your Noir code:
Transitive dependencies
When you import a dependency, you also get access to all of that package’s dependencies:The standard library
The Noir standard library (std) is always available. Import items from it with use std::...:
| Module | Contents |
|---|---|
std::hash | Hash functions: blake2s, blake3, sha256, Pedersen, Poseidon |
std::field | Field utilities: to_le_bits, bn254 curve-specific functions |
std::ops | Operator traits: Add, Sub, Mul, wrapping variants |
std::collections | Data structures: BoundedVec, HashMap, BTreeSet |
std::meta | Compile-time metaprogramming API |
std::mem | Memory utilities: zeroed |
std::option | The Option<T> type |
Workspaces
A workspace manages multiple related Noir packages in a single repository. Create a top-levelNargo.toml with a [workspace] section:
members— which packages are included in the workspace.default-member— the package processed by commands when no--packageflag is given.