|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +This is a collection of Aztec smart contract examples written in Noir, designed for learning Aztec development hands-on. The repository contains multiple example projects showcasing different aspects of Aztec's zero-knowledge smart contract platform. |
| 8 | + |
| 9 | +## Key Technologies |
| 10 | + |
| 11 | +- **Aztec**: A privacy-first Layer 2 on Ethereum using zero-knowledge proofs |
| 12 | +- **Noir**: A domain-specific language for writing zero-knowledge circuits |
| 13 | +- **aztec-nargo**: Aztec's fork of the Noir compiler for compiling Aztec contracts |
| 14 | +- **aztec-wallet**: CLI tool for interacting with Aztec contracts |
| 15 | +- **Bun**: Fast JavaScript runtime (used in recursive_verification example) |
| 16 | +- **Node.js/npm**: JavaScript runtime (used in starter-token example) |
| 17 | + |
| 18 | +## Project Structure |
| 19 | + |
| 20 | +``` |
| 21 | +aztec-examples/ |
| 22 | +├── recursive_verification/ # Noir proof verification in Aztec contracts example |
| 23 | +│ ├── circuit/ # Noir circuit that generates proofs (proves x ≠ y) |
| 24 | +│ ├── contract/ # Aztec contract that verifies Noir proofs |
| 25 | +│ ├── scripts/ # TypeScript utilities for proof generation and deployment |
| 26 | +│ ├── tests/ # Integration test suite |
| 27 | +│ ├── data.json # Generated proof data (created by `bun data`) |
| 28 | +│ ├── README.md # Comprehensive documentation |
| 29 | +│ ├── CLAUDE.md # Project-specific AI guidance |
| 30 | +│ ├── EXPLAINER.md # Technical deep-dive explanation |
| 31 | +│ └── run-tests.sh # Local test runner script |
| 32 | +├── starter-token/ # Token contract example with start-here and reference implementations |
| 33 | +│ ├── start-here/ # Template for implementing a token |
| 34 | +│ │ ├── contract/ # Noir contract code |
| 35 | +│ │ ├── external-call-contract/ # Cross-contract calls |
| 36 | +│ │ └── ts/ # TypeScript client code |
| 37 | +│ └── reference/ # Complete reference implementation |
| 38 | +│ ├── contract/ # Full token implementation |
| 39 | +│ ├── external-call-contract/ # Cross-contract example |
| 40 | +│ └── ts/ # TypeScript client |
| 41 | +└── .github/ # CI/CD configuration |
| 42 | + └── workflows/ |
| 43 | + └── recursive-verification-tests.yml # Automated testing workflow |
| 44 | +``` |
| 45 | + |
| 46 | +## Development Commands |
| 47 | + |
| 48 | +### Prerequisites |
| 49 | +```bash |
| 50 | +# Install Aztec tools (required) |
| 51 | +bash -i <(curl -s https://install.aztec.network) |
| 52 | + |
| 53 | +# Set specific version (examples may require different versions) |
| 54 | +aztec-up 2.0.3 # For recursive_verification |
| 55 | +aztec-up 2.0.2 # For starter-token |
| 56 | +``` |
| 57 | + |
| 58 | +### Building Contracts |
| 59 | + |
| 60 | +From a contract directory containing `Nargo.toml`: |
| 61 | +```bash |
| 62 | +# Compile an Aztec contract |
| 63 | +aztec-nargo compile |
| 64 | + |
| 65 | +# For recursive_verification example (using Bun) |
| 66 | +bun ccc # Compiles contract, post-processes, and generates TypeScript bindings |
| 67 | +``` |
| 68 | + |
| 69 | +### Running Local Development Environment |
| 70 | + |
| 71 | +```bash |
| 72 | +# Start Aztec sandbox (node + PXE) |
| 73 | +aztec start --sandbox |
| 74 | + |
| 75 | +# Start without PXE (when using aztec-wallet) |
| 76 | +NO_PXE=true aztec start --sandbox |
| 77 | + |
| 78 | +# Import test accounts to aztec-wallet |
| 79 | +aztec-wallet import-test-accounts |
| 80 | +``` |
| 81 | + |
| 82 | +### Testing |
| 83 | + |
| 84 | +```bash |
| 85 | +# Run tests (starts TXE automatically) |
| 86 | +aztec test |
| 87 | + |
| 88 | +# Or manually with TXE for debug output: |
| 89 | +# Terminal 1: Start Testing Execution Environment |
| 90 | +aztec start --txe --port=8081 |
| 91 | + |
| 92 | +# Terminal 2: Run tests with output |
| 93 | +nargo test --oracle-resolver http://127.0.0.1:8081 --show-output |
| 94 | + |
| 95 | +# Run integration tests (recursive_verification) |
| 96 | +cd recursive_verification |
| 97 | +bun test |
| 98 | + |
| 99 | +# Run full test suite with compilation |
| 100 | +./run-tests.sh |
| 101 | +``` |
| 102 | + |
| 103 | +### Deploying and Interacting with Contracts |
| 104 | + |
| 105 | +```bash |
| 106 | +# Deploy a contract (without constructor) |
| 107 | +aztec-wallet deploy --no-init target/<contract-name>.json --from test0 --alias <alias> |
| 108 | + |
| 109 | +# Call a contract function |
| 110 | +aztec-wallet send <function_name> --args <args...> --contract-address <alias> -f test0 |
| 111 | + |
| 112 | +# Simulate a function call (read-only) |
| 113 | +aztec-wallet simulate <function_name> --args <args...> --contract-address <alias> -f test0 |
| 114 | + |
| 115 | +# Profile gas/gates for a function |
| 116 | +aztec-wallet profile <function_name> --args <args...> --contract-address <alias> -f test0 |
| 117 | +``` |
| 118 | + |
| 119 | +### Performance Analysis |
| 120 | + |
| 121 | +```bash |
| 122 | +# Generate gate flamegraph for private functions |
| 123 | +SERVE=1 aztec flamegraph target/<contract>.json <function_name> |
| 124 | + |
| 125 | +# Profile gate count for deployed contract |
| 126 | +aztec-wallet profile <function_name> --args <args...> --contract-address <alias> -f test0 |
| 127 | +``` |
| 128 | + |
| 129 | +## Example-Specific Workflows |
| 130 | + |
| 131 | +### Recursive Verification Example |
| 132 | + |
| 133 | +Complete workflow for the proof verification example: |
| 134 | + |
| 135 | +```bash |
| 136 | +# 1. Install dependencies (requires Bun) |
| 137 | +cd recursive_verification |
| 138 | +bun install |
| 139 | + |
| 140 | +# 2. Compile the Noir circuit |
| 141 | +cd circuit && aztec-nargo compile && cd .. |
| 142 | + |
| 143 | +# 3. Compile the Aztec contract |
| 144 | +bun ccc # Runs: aztec-nargo compile && aztec-postprocess-contract && aztec codegen |
| 145 | + |
| 146 | +# 4. Generate proof data (UltraHonk proof, verification key, public inputs) |
| 147 | +bun data # Creates data.json with proof for x=1, y=2 |
| 148 | + |
| 149 | +# 5. Start Aztec sandbox (in separate terminal) |
| 150 | +aztec start --sandbox |
| 151 | + |
| 152 | +# 6. Deploy contract and verify proof on-chain |
| 153 | +bun recursion # Deploys ValueNotEqual contract and verifies proof |
| 154 | + |
| 155 | +# 7. Run tests |
| 156 | +bun test |
| 157 | + |
| 158 | +# Optional: Run circuit tests |
| 159 | +cd circuit && nargo test |
| 160 | +``` |
| 161 | + |
| 162 | +### Starter Token Example |
| 163 | + |
| 164 | +```bash |
| 165 | +# Navigate to reference implementation |
| 166 | +cd starter-token/reference |
| 167 | + |
| 168 | +# Build the contract |
| 169 | +cd contract && aztec-nargo compile && cd .. |
| 170 | + |
| 171 | +# Build and run TypeScript client |
| 172 | +cd ts |
| 173 | +npm install |
| 174 | +npm run build |
| 175 | +npm start |
| 176 | +``` |
| 177 | + |
| 178 | +## Contract Architecture |
| 179 | + |
| 180 | +### Aztec Contract Structure |
| 181 | + |
| 182 | +Aztec contracts use the `#[aztec]` macro and define functions as either: |
| 183 | +- `#[private]`: Executed client-side with zero-knowledge proofs |
| 184 | +- `#[public]`: Executed on-chain by the protocol |
| 185 | +- `#[initializer]`: Constructor-like functions for setup |
| 186 | +- `#[unconstrained]`: View functions that don't modify state |
| 187 | + |
| 188 | +Key considerations: |
| 189 | +- **Private functions**: Optimize for circuit size (gates), unconstrained functions don't add gates |
| 190 | +- **Public functions**: Optimize for gas cost, unconstrained functions do add cost |
| 191 | +- **Unconstrained functions**: Used for computation that doesn't need proving, must verify results in constrained context |
| 192 | + |
| 193 | +### Proof Verification Pattern (recursive_verification) |
| 194 | + |
| 195 | +The recursive verification example demonstrates: |
| 196 | +- **Off-chain proof generation**: Noir circuits compiled and executed with Barretenberg |
| 197 | +- **On-chain verification**: Using `std::verify_proof_with_type` in Aztec contracts |
| 198 | +- **UltraHonk proving system**: Generates proofs with 457 field elements, verification keys with 115 fields |
| 199 | +- **Private state management**: Using `EasyPrivateUint` for private counters |
| 200 | + |
| 201 | +### Token Pattern (starter-token) |
| 202 | + |
| 203 | +The token example showcases: |
| 204 | +- **Dual balance system**: Public and private token balances |
| 205 | +- **State management**: Using `PublicMutable` and `Map` for storage |
| 206 | +- **Access control**: Owner-based permissions for minting |
| 207 | +- **Cross-contract calls**: External contract interactions |
| 208 | + |
| 209 | +### Testing Pattern |
| 210 | + |
| 211 | +Tests use the Testing Execution Environment (TXE): |
| 212 | +```noir |
| 213 | +use dep::aztec::test::helpers::test_environment::TestEnvironment; |
| 214 | +
|
| 215 | +#[test] |
| 216 | +unconstrained fn test_function() { |
| 217 | + let mut env = TestEnvironment::new(); |
| 218 | + let user = env.create_account_contract(1); |
| 219 | + env.impersonate(user); |
| 220 | +
|
| 221 | + // Deploy and interact with contracts |
| 222 | + let contract = env.deploy_self("ContractName").without_initializer(); |
| 223 | + // Test contract functions |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +## Dependencies |
| 228 | + |
| 229 | +### Aztec Contract Dependencies |
| 230 | + |
| 231 | +Aztec contracts specify dependencies in `Nargo.toml`: |
| 232 | +```toml |
| 233 | +[dependencies] |
| 234 | +aztec = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "vX.X.X", directory = "noir-projects/aztec-nr/aztec" } |
| 235 | +easy_private_state = { git = "https://github.com/AztecProtocol/aztec-packages/", tag = "vX.X.X", directory = "noir-projects/aztec-nr/easy-private-state" } |
| 236 | +``` |
| 237 | + |
| 238 | +**Version Compatibility**: Different examples may use different Aztec versions: |
| 239 | +- `recursive_verification`: v2.0.3 |
| 240 | +- `starter-token`: v2.0.2 |
| 241 | + |
| 242 | +### JavaScript/TypeScript Dependencies |
| 243 | + |
| 244 | +TypeScript projects use: |
| 245 | +- `@aztec/aztec.js`: Aztec SDK for contract deployment and interaction |
| 246 | +- `@aztec/accounts`: Account management for Aztec |
| 247 | +- `@aztec/bb.js`: Barretenberg backend for proof generation (recursive_verification) |
| 248 | +- `@aztec/noir-noir_js`: Noir.js for circuit execution (recursive_verification) |
| 249 | + |
| 250 | +### Runtime Requirements |
| 251 | + |
| 252 | +- **Node.js/npm**: For starter-token TypeScript examples (v20+) |
| 253 | +- **Bun**: Required for recursive_verification example (faster alternative to Node.js) |
| 254 | +- **Docker**: Required for running Aztec sandbox |
| 255 | +- **Memory**: 8GB+ RAM recommended for proof generation |
| 256 | + |
| 257 | +## CI/CD |
| 258 | + |
| 259 | +The repository includes GitHub Actions workflows for automated testing: |
| 260 | + |
| 261 | +### recursive-verification-tests.yml |
| 262 | + |
| 263 | +Runs on: |
| 264 | +- Push to main branch |
| 265 | +- Pull requests modifying `recursive_verification/**` |
| 266 | +- Manual workflow dispatch |
| 267 | + |
| 268 | +Steps: |
| 269 | +1. Sets up Node.js (v22) and Bun |
| 270 | +2. Installs Aztec CLI |
| 271 | +3. Starts Aztec sandbox |
| 272 | +4. Compiles circuits and contracts |
| 273 | +5. Generates proof data |
| 274 | +6. Runs integration tests |
| 275 | +7. Uploads test artifacts on failure |
| 276 | + |
| 277 | +## Common Issues and Solutions |
| 278 | + |
| 279 | +### Issue: "Cannot find module './contract/artifacts/'" |
| 280 | +**Solution**: Run `bun ccc` or `aztec-nargo compile` to generate contract artifacts |
| 281 | + |
| 282 | +### Issue: "Failed to connect to PXE" |
| 283 | +**Solution**: Ensure Aztec sandbox is running with `aztec start --sandbox` |
| 284 | + |
| 285 | +### Issue: "Proof verification failed" |
| 286 | +**Solution**: Regenerate proof data after circuit changes with `bun data` |
| 287 | + |
| 288 | +### Issue: Memory issues during proof generation |
| 289 | +**Solution**: Close other applications or use a machine with more RAM (8GB+ recommended) |
| 290 | + |
| 291 | +### Issue: Version compatibility errors |
| 292 | +**Solution**: Check the Aztec version required for each example and set with `aztec-up <version>` |
| 293 | + |
| 294 | +## Best Practices |
| 295 | + |
| 296 | +1. **Version Management**: Always check and set the correct Aztec version for each example |
| 297 | +2. **Testing**: Run tests locally before pushing changes |
| 298 | +3. **Documentation**: Update READMEs when modifying examples |
| 299 | +4. **Clean Builds**: When encountering issues, try removing `target/`, `artifacts/`, and `node_modules/` directories |
| 300 | +5. **Sandbox Management**: Always ensure sandbox is running when deploying/testing contracts |
0 commit comments