|
| 1 | +# 🧾 Age Range Zero-Knowledge Proof (Noir) |
| 2 | + |
| 3 | +This project implements a zero-knowledge proof circuit in [Noir](https://noir-lang.org/) that allows a user to prove their age lies within a valid range (e.g. 18 to 60) **without revealing their actual age**. |
| 4 | + |
| 5 | +It uses the `bb` backend with the `ultra_honk` scheme and supports exporting a Solidity verifier contract for on-chain use. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🚀 Quickstart |
| 10 | +### Prerequisites |
| 11 | + |
| 12 | +- [Rust](https://www.rust-lang.org/tools/install) |
| 13 | +- [nargo](https://noir-lang.org/docs/getting_started/quick_start#installation) |
| 14 | +- [bb](https://github.com/AztecProtocol/aztec-packages/tree/master/barretenberg#installation) |
| 15 | + |
| 16 | +Install `bb` and `nargo`: |
| 17 | +```bash |
| 18 | +cargo install --locked nargo |
| 19 | +cargo install --locked bb |
| 20 | + |
| 21 | +curl https://sh.rustup.rs -sSf | sh -s -- -y |
| 22 | +source $HOME/.cargo/env |
| 23 | +cargo install --locked nargo bb |
| 24 | +``` |
| 25 | +Clone the repo and make all scripts executable: |
| 26 | + |
| 27 | +```bash |
| 28 | +git clone https://github.com/noir-lang/noir-examples.git |
| 29 | +cd noir-examples |
| 30 | +cd age_range_proof |
| 31 | +``` |
| 32 | +Make all scripts executable: |
| 33 | +```bash |
| 34 | +chmod +x scripts/*.sh |
| 35 | +``` |
| 36 | +## 📁 Directory Structure |
| 37 | +```bash |
| 38 | +├── .github/ # GitHub Actions workflows for CI/CD |
| 39 | +│ └── workflows/ # YAML files defining automated workflows (e.g., test on push) |
| 40 | +├── onchain/ # Smart contract outputs |
| 41 | +│ └── Verifier.sol # Auto-generated Solidity verifier for onchain use |
| 42 | +├── scripts/ # Automation scripts for your zk pipeline |
| 43 | +│ ├── build.sh # Full build pipeline (compile, prove, verify, export verifier) |
| 44 | +│ ├── clean.sh # Cleans target/ for fresh builds |
| 45 | +│ ├── export_solidity_verifier.sh # Optional manual verifier export |
| 46 | +│ ├── keygen.sh # VK generation script (optional if build.sh handles it) |
| 47 | +│ ├── prove.sh # Standalone proof creation (optional modular use) |
| 48 | +│ ├── test.sh # Test multiple inputs end-to-end |
| 49 | +│ └── verify.sh # Standalone verifier runner |
| 50 | +├── src/ # Noir circuit logic |
| 51 | +│ └── main.nr # Your actual Noir circuit and tests |
| 52 | +├── target/ # Build artifacts (proofs, keys, verifier contract) |
| 53 | +├── .gitignore # To exclude target/, secrets, etc. |
| 54 | +├── input.json # Example input to your circuit |
| 55 | +├── Nargo.toml # Noir project config |
| 56 | +├── Prover.toml # Additional config for `bb` prover |
| 57 | +├── README.md # Project documentation |
| 58 | +``` |
| 59 | +## 🔧 Circuit Logic |
| 60 | +The circuit takes a **private** `age` **input** and checks it falls in the valid range: |
| 61 | +```rust |
| 62 | +assert(age >= min_age, "Too young"); |
| 63 | +assert(age <= max_age, "Too old"); |
| 64 | +``` |
| 65 | +It outputs a proof that the prover knows a valid age without revealing it. |
| 66 | +## 🛠️ Using the Build and Test Scripts |
| 67 | +This project includes several handy scripts to automate building, testing, and verifying zero-knowledge proofs using the `bb` and `nargo` toolchains. |
| 68 | +`build.sh` – **Full build pipeline** |
| 69 | +You can run the build script with or without an argument: |
| 70 | +```bash |
| 71 | +./scripts/build.sh 42 # Uses age = 42 |
| 72 | +./scripts/build.sh # Uses age = 35 by default |
| 73 | +``` |
| 74 | +This behavior is enabled by the following line in `build.sh`: |
| 75 | +```bash |
| 76 | +AGE=${1:-35} |
| 77 | +``` |
| 78 | +***Explanation*: |
| 79 | +- If you provide an argument (e.g., `42`), that value is used as the input age. |
| 80 | +- If you omit the argument, it defaults to `35`. |
| 81 | +This allows you to quickly test a default run, or pass a specific age as needed. |
| 82 | + |
| 83 | +`build.sh`: |
| 84 | +- Compiles the circuit |
| 85 | +- Generates the witness and proof |
| 86 | +- Creates or reuses a verification key |
| 87 | +- Verifies the proof |
| 88 | +- Exports the Solidity verifier contract |
| 89 | + |
| 90 | +`clean.sh` – **Clean output artifacts** |
| 91 | +```bash |
| 92 | +./scripts/clean.sh |
| 93 | +``` |
| 94 | +Cleans the build artifacts by deleting the `target` directory. Use this to start fresh. |
| 95 | + |
| 96 | +`keygen.sh` |
| 97 | +Generates the verification key separately (usually called automatically by `build.sh` if needed). |
| 98 | +```bash |
| 99 | +./scripts/keygen.sh |
| 100 | +``` |
| 101 | + |
| 102 | +`test.sh` – **Run tests on various ages** |
| 103 | +To test for multiple ages automatically, you should use your test.sh script which runs build.sh for a set of ages. You run: |
| 104 | +```bash |
| 105 | +./scripts/test.sh |
| 106 | +``` |
| 107 | +This script loops over predefined ages (e.g., 17, 18, 35, 60, 61) and runs the full build pipeline on each, reporting success or failure. |
| 108 | + |
| 109 | +To test for a single age like 42, you run: |
| 110 | +```bash |
| 111 | +./scripts/build.sh 42 |
| 112 | +``` |
| 113 | +## 🧾 Export Solidity Verifier |
| 114 | +To export a Solidity verifier contract: |
| 115 | +```bash |
| 116 | +./scripts/export_verifier.sh |
| 117 | +``` |
| 118 | +This uses: |
| 119 | +```bash |
| 120 | +bb write_solidity_verifier -k ./target/vk -o ./target/Verifier.sol |
| 121 | +``` |
| 122 | +## Example Output (Valid Proof) |
| 123 | +```bash |
| 124 | +🚧 Starting full build pipeline... |
| 125 | +✅ Proof verified successfully |
| 126 | +✅ Verifier exported to ./target/Verifier.sol |
| 127 | +🎉 Build pipeline completed successfully |
| 128 | +``` |
| 129 | +`verify.sh` |
| 130 | +Verifies the generated proof using the verification key. Automatically runs `keygen.sh` if the verification key is missing. |
| 131 | +```bash |
| 132 | +./scripts/verify.sh |
| 133 | +``` |
| 134 | +`prove.sh <age>` |
| 135 | +Compiles the circuit, generates the witness, and creates a proof for the given age (lower-level script). You can run the build script with or without an argument: |
| 136 | +```bash |
| 137 | +./scripts/build.sh 42 # Uses age = 42 |
| 138 | +./scripts/build.sh # Defaults to age = 35 |
| 139 | +``` |
| 140 | +This allows you to quickly test a default run, or pass a specific age as needed. |
| 141 | +## 📦 GitHub Actions (CI Example) |
| 142 | +To enable automatic build and testing on each push, use this `.github/workflows/ci.yml`: |
| 143 | +```yaml |
| 144 | +name: CI |
| 145 | + |
| 146 | +on: |
| 147 | + push: |
| 148 | + branches: |
| 149 | + - main |
| 150 | + |
| 151 | +jobs: |
| 152 | + build-and-test: |
| 153 | + runs-on: ubuntu-latest |
| 154 | + steps: |
| 155 | + - uses: actions/checkout@v3 |
| 156 | + |
| 157 | + - name: Install dependencies |
| 158 | + run: | |
| 159 | + curl https://sh.rustup.rs -sSf | sh -s -- -y |
| 160 | + source $HOME/.cargo/env |
| 161 | + cargo install --locked nargo bb |
| 162 | +
|
| 163 | + - name: Run tests |
| 164 | + run: ./scripts/test.sh |
| 165 | +``` |
| 166 | +## 📝 License |
| 167 | +MIT License. Feel free to use and adapt this project. |
| 168 | +## 🤝 Contributing |
| 169 | +Contributions and suggestions welcome! Please open an issue or pull request. |
0 commit comments