Skip to content

Commit 36a93c0

Browse files
committed
Private function example
1 parent fa80654 commit 36a93c0

File tree

11 files changed

+332
-1
lines changed

11 files changed

+332
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"noir.nargoPath": "~/.nargo/bin/nargo"
3+
}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# aztec-examples
2-
A place for example Aztec projects
2+
3+
A place for example Aztec contracts to learn in a hands-on way.
4+
5+
## Documentation
6+
7+
See raw markdown [here](./docs/index.md), or serve locally.
8+
9+
- [Install mkdocs](https://www.mkdocs.org/getting-started/#installation)
10+
- Serve locally: `mkdocs serve-docs`

docs/docs/hello.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../hello/hello.md

docs/docs/index.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Contents
2+
3+
## Using examples
4+
5+
To see all commands from within an example directory use: `make help`
6+
7+
On linux, if you do not have `make`, you will need the package `build-essential`.
8+
9+
Note: Examples assume you have aztec tools installed: `bash -i <(curl -s https://install.aztec.network)`
10+
11+
Some commands require having a local network running (without its PXE): `NO_PXE=true aztec start --sandbox`
12+
The Private eXecution Environment (PXE) within the `aztec-wallet` will be used.
13+
14+
15+
## Contributions
16+
17+
Example in this repo are heavily curated to showcase and explain atomic functionality of Aztec, as well as slightly larger examples for broader concepts.
18+
19+
Developers: Pull requests contining examples with code AND documentation that fit the brief will be considered.

docs/mkdocs.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
site_name: My Docs
2+
nav:
3+
- Home: index.md
4+
- Hello: hello.md
5+
theme: readthedocs

hello/Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.PHONY: all build gate-flamegraph clean
2+
3+
# Default target
4+
all: build
5+
6+
# Build the project
7+
build:
8+
aztec-nargo compile
9+
10+
# Generate gate flamegraph of a private function
11+
gate-flamegraph:
12+
@echo "Building gates flamegraph of $(filter-out $@,$(MAKECMDGOALS)) ..."
13+
SERVE=1 aztec flamegraph target/hello-Hello.json $(filter-out $@,$(MAKECMDGOALS))
14+
15+
# Clean build artifacts
16+
clean:
17+
rm -rf target/
18+
19+
# Help command
20+
help:
21+
@echo "Available commands:"
22+
@echo " make build - Compile the project"
23+
@echo " make clean - Remove build artifacts"
24+
@echo " make gate-flamegraph <function_name> - Generate gate flamegraph for the given private function"
25+
@echo " make help - Show this help message"

hello/Nargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "hello"
3+
type = "contract"
4+
authors = [""]
5+
6+
[dependencies]
7+
aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v0.87.8", directory = "noir-projects/aztec-nr/aztec" }

hello/hello.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Coding for Execution vs Proving
2+
3+
In this intro example you will:
4+
5+
- Create an Aztec contract from scratch
6+
- Add Aztec dependencies and macros
7+
- Appreciate different function types/considerations
8+
- Measure proving cost
9+
- Run a local node, deploy/interact with contract
10+
11+
## Self Setup
12+
13+
The steps in this section explain how to recreate a project like this from scratch.
14+
15+
### Create project files and first contract
16+
17+
The flow with Noir is much like that of Rust.
18+
19+
```bash
20+
# Create project dir and boilerplate files
21+
aztec-nargo new --contract hello
22+
```
23+
24+
```bash
25+
# Add Aztec dependency to `hello/Nargo.toml`
26+
cd hello
27+
echo 'aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "v0.87.8", directory = "noir-projects/aztec-nr/aztec" }' >> Nargo.toml
28+
```
29+
30+
```bash
31+
# Replace src/main.nr with empty Aztec contract
32+
echo 'use dep::aztec::macros::aztec; // import aztec macro for use
33+
34+
#[aztec] // use macro for contract
35+
pub contract Hello {
36+
// imports, storage struct, and functions go here
37+
}' > src/main.nr
38+
```
39+
40+
### Add a private function
41+
42+
Inside the empty contract, add the following functions and their requisite macro imports:
43+
44+
```rust
45+
// ...
46+
// import function types
47+
use dep::aztec::macros::functions::{private, public};
48+
49+
#[private] // use macro to wrap for private execution
50+
fn mul_8_prv(num: u32) -> u32 {
51+
num * 8 // more efficient for proving
52+
}
53+
54+
#[public] // use macro to wrap for public execution
55+
fn mul_8_exe(num: u32) -> u32 {
56+
num << 3 // more efficient for execution
57+
}
58+
59+
// ...
60+
```
61+
62+
## Understanding performance
63+
64+
Since private functions are executed client side and the proof of execution is used to advance private state, the size of the circuit is important. The size of a circuit is measured in gates.
65+
66+
Public functions are executed as part of the protocol, so the total cost of operations is important, which is measured in gas.
67+
68+
We can compile and then calculate the gates required for this private function.
69+
70+
## Building the project
71+
### Compile the contract
72+
73+
For simplicity this project uses make, where `make build` calls `aztec-nargo compile`. Build is the default make target so for convenience simply compile the contract with:
74+
75+
```bash
76+
# From directory with Markefile & Nargo.toml
77+
make
78+
```
79+
80+
### Showing gate counts in a flamegraph
81+
82+
The gate flamegraph of a specific function can be calculate and presented by passing the function name to:
83+
84+
```bash
85+
make gate-flamegraph <function-name>
86+
```
87+
88+
eg: `make gate-flamegraph mul_8_prv`
89+
90+
Go to the URL in the command output to see the gate count total, and of each sub-section. The exported .svg file is in the `target` directory.
91+
92+
## Using the project
93+
### Deploy contract to Aztec dev node
94+
95+
First we'll run a local dev environment, aka "sandbox", which includes:
96+
97+
- an Aztec dev node
98+
- a Private eXecution Environment (PXE)
99+
100+
Both can be started together in a terminal with: `aztec start --sandbox`
101+
102+
We will now use the `aztec-wallet` to interact with them.
103+
104+
```bash
105+
# Register test account contract addresses from sandbox, into the pxe
106+
aztec-wallet import-test-accounts
107+
aztec-wallet get-alias accounts # show account contract aliases -> addresses
108+
```
109+
110+
Use the test account aliased to, `test0`, to deploy the compiled contract:
111+
112+
```bash
113+
aztec-wallet deploy --no-init target/hello-Hello.json --from test0 --alias hello
114+
```
115+
116+
Note:
117+
118+
- `no-init` is specified because we do not have or need a constractor/`initializer` for this example
119+
- The last param requests the deployed contract address be aliased to `hello`
120+
121+
### Command summary script
122+
123+
For convenience/reference, these commands are consolidated in a script. To see them:
124+
```bash
125+
./run.sh --help
126+
```
127+
128+
### Profile gate count
129+
130+
To see a breakdown of proving times and gate counts per inner-circuit:
131+
132+
```bash
133+
./run.sh gate-profile mul_8_prv 8
134+
```
135+
136+
This command expects the contract in `hello.nr` to be deployed, and the contract address aliased to `hello`.
137+
138+
This uses the deployed contract to provide a breakdown of time and gates for each inner function. This will become useful when comparing
139+
140+
## Compare implementations
141+
142+
143+
144+
## Calling private functions
145+
146+
147+
148+
## Further reading
149+
150+
- For more about optimisation, see [Thinking in Circuits](https://noir-lang.org/docs/explainers/explainer-writing-noir), and [aztec-examples]().
151+
- To see a side-by-side comparison with Rust, see [noir_by_example](https://github.com/noir-lang/noir-examples/tree/master/noir_by_example#readme).

hello/run.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bash
2+
3+
usage() {
4+
cat <<EOF
5+
Usage: $0 <command> [options]
6+
7+
Commands:
8+
Setup:
9+
sandbox Run Aztec node + pxe locally
10+
import-test-accounts Import test accounts into aztec-wallet
11+
deploy-hello Deploy the hello contract without initialization
12+
gate-flamegraph <function_name> Generate gate flamegraph for the specified private function
13+
gate-profile <function_name> [args...] Show total gates for the specified private function with optional arguments
14+
-h, --help Show this help message
15+
16+
Examples:
17+
$0 sandbox
18+
$0 import-test-accounts
19+
$0 deploy-hello
20+
$0 gate-flamegraph myFunction
21+
$0 gate-profile myFunction 1 2 3
22+
EOF
23+
}
24+
25+
if [[ $# -lt 1 ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
26+
usage
27+
exit 0
28+
fi
29+
30+
COMMAND="$1"
31+
shift
32+
33+
case "$COMMAND" in
34+
sandbox)
35+
aztec start --sandbox
36+
;;
37+
import-test-accounts)
38+
aztec-wallet import-test-accounts
39+
;;
40+
deploy-hello)
41+
aztec-wallet deploy --no-init target/hello-Hello.json --from test0 --alias hello
42+
;;
43+
gate-flamegraph)
44+
if [[ $# -lt 1 ]]; then
45+
echo "Error: function_name required for flamegraph"
46+
usage
47+
exit 1
48+
fi
49+
SERVE=1 aztec flamegraph target/hello-Hello.json "$1"
50+
;;
51+
gate-profile)
52+
if [[ $# -lt 1 ]]; then
53+
echo "Error: function_name required for profile"
54+
usage
55+
exit 1
56+
fi
57+
fn="$1"; shift
58+
aztec-wallet profile "$fn" --args "$@" --contract-address hello -f test0
59+
;;
60+
*)
61+
echo "Unknown command: $COMMAND"
62+
usage
63+
exit 1
64+
;;
65+
esac

0 commit comments

Comments
 (0)