Hello, World
Now that we have installed Nargo, it is time to make our first hello world program!
Create a Project Directory
Noir code can live anywhere on your computer. Let us create a projects folder in the home directory to house our Noir programs.
For Linux, macOS, and Windows PowerShell, create the directory and change directory into it by running:
$ mkdir ~/projects
$ cd ~/projects
For Windwows CMD, run:
> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
Create Our First Nargo Project
Now that we are in the projects directory, create a new Nargo project by running:
$ nargo new hello_world
Note:
hello_world
can be any arbitrary project name, we are simply usinghello_world
for demonstration.In production, the common practice is to name the project folder as
circuits
for better identifiability when sitting alongside other folders in the codebase (e.g.contracts
,scripts
,test
).
A hello_world
folder would be created. Similar to Rust, the folder houses src/main.nr and Nargo.toml that contains the source code and environmental options of your Noir program respectively.
Intro to Noir Syntax
Let us take a closer look at main.nr. The default main.nr generated should look like this:
fn main(x : Field, y : pub Field) { constrain x != y; }
The first line of the program specifies the program's inputs:
#![allow(unused)] fn main() { x : Field, y : pub Field }
Program inputs in Noir are private by default (e.g. x
), but can be labeled public using the keyword pub
(e.g. y
). To learn more about private and public values, check the Data Types section.
The next line of the program specifies its body:
#![allow(unused)] fn main() { constrain x != y; }
The Noir syntax constrain
can be interpreted as something similar to assert
in other languages.
For more Noir syntax, check the Language Concepts chapter.
Build In/Output Files
Change directory into hello_world and build in/output files for your Noir program by running:
$ cd hello_world
$ nargo check
Two additional files would be generated in your project directory:
Prover.toml houses input values, and Verifier.toml houses public values.
Prove Our Noir Program
Now that the project is set up, we can create a proof of correct execution on our Noir program.
Fill in input values for execution in the Prover.toml file. For example:
x = "1"
y = "2"
Prove the valid execution of your Noir program with your preferred proof name, for example p
:
$ nargo prove p
A new folder proofs would then be generated in your project directory, containing the proof file p.proof
.
The Verifier.toml file would also be updated with the public values computed from program execution (in this case the value of y
):
y = "0x0000000000000000000000000000000000000000000000000000000000000002"
Note: Values in Verifier.toml are computed as 32-byte hex values.
Verify Our Noir Program
Once a proof is generated, we can verify correct execution of our Noir program by verifying the proof file.
Verify your proof of name p
by running:
$ nargo verify p
The verification will complete in silence if it is successful. If it fails, it will log the corresponding error instead.
Congratulations, you have now created and verified a proof for your very first Noir program!
In the next section, we will go into more detail on each step performed.