DEV Community

Arush
Arush

Posted on Edited on

What does it take to write your first zero-knowledge proof?

TL;DR

  • You'll write a four-line Noir circuit, generate a real zero-knowledge proof, and verify it.
  • Along the way you'll learn the difference between public and private inputs, and what "circuit", "witness" and "backend" actually mean.

Three posts of theory. It's time to make an actual proof.

We'll use Noir, a Rust-flavored language for writing ZK circuits. I picked it for the start of this series because it reads like normal code, which makes it the gentlest on-ramp I've found.

Intuition: a circuit is a checklist, not a program

A normal program computes an answer. A ZK circuit checks an answer. You don't ask it "what's the result?". Instead you hand it a filled-in form and it says ✅ or ❌.

Think of a customs officer with a checklist. You (the prover) show up with your paperwork already filled in. The officer doesn't fill anything in for you; they just tick boxes. The ZK magic is that they can tick every box while some of your paperwork stays in a sealed envelope.

The vocabulary, mapped to code

  • Circuit: your Noir program. It's a set of constraints that must all hold.
  • Private input: part of the witness. Only the prover knows it.
  • Public input: known to the prover and the verifier. Part of the statement.
  • Witness: all the values (private inputs plus intermediate values) that make the constraints true.
  • Backend: the proving system that turns "circuit + witness" into a proof. Noir's default backend is Barretenberg, run with the bb command.

Hands-on

1. Install

curl -L https://raw.githubusercontent.com/noir-lang/noirup/refs/heads/main/install | bash
noirup
curl -L https://raw.githubusercontent.com/AztecProtocol/aztec-packages/refs/heads/next/barretenberg/bbup/install | bash
bbup
Enter fullscreen mode Exit fullscreen mode

(Tooling moves fast. It's nargo 1.0.0 while I'm writing this blog. If a command below fails, check the Noir quick start for the latest.)

2. The circuit

nargo new hello && cd hello
Enter fullscreen mode Exit fullscreen mode

Replace src/main.nr with this (full project: code/04-noir-hello):

// "I know a secret x that is NOT equal to your public y."
fn main(x: Field, y: pub Field) {
    assert(x != y);
}

#[test]
fn different_values_pass() {
    main(1, 2);
}

#[test(should_fail)]
fn equal_values_fail() {
    main(7, 7);
}
Enter fullscreen mode Exit fullscreen mode

That's it. Let's read it slowly:

  • x: Field: a private input. No keyword needed: in Noir, everything is private unless you say otherwise.
  • y: pub Field: a public input. The verifier will see it.
  • Field: the basic number type of ZK. Think "a really big integer that wraps around". 6th post is all about it.
  • assert(x != y): the one constraint. If it fails, there is no valid witness, so no proof can be made.

Yes, the statement is silly. "I know a number that isn't 2" isn't exactly a secret worth protecting. But every real circuit has exactly this shape: some private inputs, some public inputs, and asserts tying them together.

3. Test it

$ nargo test
[hello] Running 2 test functions
[hello] Testing different_values_pass ... ok
[hello] Testing equal_values_fail ... ok
[hello] 2 tests passed
Enter fullscreen mode Exit fullscreen mode

Notice the second test: we check that a bad witness fails. Keep that habit, it'll save us in later post.

4. Provide inputs and execute

nargo check creates a Prover.toml. Fill it in:

x = "1"
y = "2"
Enter fullscreen mode Exit fullscreen mode
$ nargo execute
[hello] Circuit witness successfully solved
[hello] Witness saved to target/hello.gz
Enter fullscreen mode Exit fullscreen mode

execute runs the circuit with your inputs and saves the witness. No proof yet, just the solved form.

5. Prove and verify

bb prove -b ./target/hello.json -w ./target/hello.gz --write_vk -o target
bb verify -p ./target/proof -k ./target/vk
Enter fullscreen mode Exit fullscreen mode

bb prove takes the compiled circuit (hello.json) and the witness, and outputs a proof plus a verification key (vk). bb verify checks the proof using only the vk and the public inputs. It never sees x = 1.

That's a real zero-knowledge proof. 🎉

Gotcha: I forgot pub and my "public" input wasn't public

My first non-toy circuit was "I know the preimage of this hash":

fn main(secret: Field, expected_hash: Field) { ... }   // oops
Enter fullscreen mode Exit fullscreen mode

It compiled. It proved. It verified. And it was useless, because I forgot pub on expected_hash. That made it private, so the statement became "I know some secret and some hash that match", which anyone can satisfy by hashing any number they like. The verifier had no way to know which hash I'd proven against.

Rule of thumb: for every input, ask "does the verifier need to know this value for the proof to mean anything?" If yes, it must be pub.

Recap

  • A circuit checks a filled-in witness; it doesn't compute an answer for you.
  • Noir inputs are private by default; add pub for anything the verifier must see.
  • The workflow is: nargo test → nargo execute (witness) → bb prove (proof + vk) → bb verify.

Next up: same circuit, different universe. In next post, we'll rebuild this in Circom with snarkjs and compare the two languages side by side.

Further reading

  • Noir docs: the quick start and the language guide.
  • Cyfrin Updraft ZK courses: video course that covers Noir. (Well, it will get you started; They're still working on full ZK content.)
  • Awesome Noir: libraries and example projects.

Series codebase

All the code from posts in this series will be in this GitHub repo: Zero-Knowledge from zero knowledge

Top comments (0)