Liam Zebedee
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
      • No invitee
    • Publish Note

      Publish Note

      Everyone on the web can find and read all notes of this public team.
      Once published, notes can be searched and viewed by anyone online.
      See published notes
      Please check the box to agree to the Community Guidelines.
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
No invitee
Publish Note

Publish Note

Everyone on the web can find and read all notes of this public team.
Once published, notes can be searched and viewed by anyone online.
See published notes
Please check the box to agree to the Community Guidelines.
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
4
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# A dumb primer on recursive ZK proofs How do I understand recursive ZK proofs? / A better way. By **Liam Zebedee ([@liamzebedee](https://twitter.com/liamzebedee))** and **XYZ** **Acknowledgements**. This came directly out of convos with Lord from Biblioteca, Francesco from Apibara, Guiltygyoza, in the entire StarkNet hacker house at ETHLisbon 2022. Thank you to them, and the organisers of that house. PS: Don't feel scared by the length - the sections are designed to be skippable. ## Part 1: memoization. In JavaScript, we have a common optimization pattern called [memoization](https://en.wikipedia.org/wiki/Memoization). For those not familiar, memoization is kind of like caching the results of calling a function, so when you call it again with the same inputs, you get the result from the cache. Memoization is usually implemented as a higher-order function, meaning it takes a function as input and returns another function. Here's an idiot's implementation of `memoize(x)` in Python: ```py cache = {} def memoize(f): # The memoized version of f(x). def f2(x): if not x in cache: cache[x] = f(x) return cache[x] return f2 ``` **Learning**: memoization is wrapping a function with a results cache. ## Part 2: ZK proofs - a practical walkthrough in Cairo. Now let's recap ZK proofs - a shorthand for ZK-STARK's and ZK-SNARK's. A ZK proof is a way to prove computation, in such a way that verifying that proof takes less time than running it. For a program that takes $N$ computational steps, we can prove it and verify that proof in $O(log^2 N)$ steps using STARK's - an exponential speedup. I've written a fairly good summary of this on [this Twitter thread](https://twitter.com/liamzebedee/status/1516241618919374851)). What does ZK proofing look like? Usually you write a "provable" program using a language like [Cairo](https://www.cairo-lang.org/), which compiles to bytecode, which is executed in a VM that generates a "trace" - basically, a dump of the values of the VM's CPU registers over time. This trace is then proven using a prover (see [Giza](https://github.com/maxgillett/giza), an open-source one I worked on), and out the other side, you get a binary blob. For example: ```cairo! # program.cairo func fib(n) { if n == 0 { return 0 } if n == 1 { return 1 } return fib(n-1) + fib(n-2) } ``` Compile, run and generate the trace: ```shell! # Compile. cairo-compile ./program.cairo --output ./program.json # Run. cairo-run --program=program.json --layout=all --memory_file=memory.bin --trace_file=trace.bin ``` Now prove the trace: ```shell! giza prove --trace=trace.bin --memory=memory.bin --program=program.json --output=proof.bin ``` The proof is generated as a file, `proof.bin`. The proving step will take longer than running the program, but the verification will be much shorter. We can verify it like so: ```shell! giza verify --proof=proof.bin ``` Now, how do we know what we're actually verifying? Obviously it could be a proof of anything. In some ZK systems like [circom](https://github.com/iden3/circom), we have two separate compilation outputs - the proving circuit and the verification circuit, the latter of which we would use to authoritively verify a program run. In Starkware, they have a primitive higher up the stack called the "bootloader", which a **known program** that will execute any program given to it. How do we know we're running the bootloader? The program hash is exposed as public memory input into the program run, and because it's public memory, we can access it as part of the proof in `proof.bin`. This is what it looks like, conceptually: ```cairo! func bootloader(code: felt*, program_hash: felt, input: felt*) { # communicate with the verifier that we are executing this program. assert hash(code) == program_hash # write program_hash to public_memory # execute push input to memory jump code # equivalent to f(x), where f=code, x=input } ``` To illustrate how this works, we might have `bootloader.py` which will setup the bootloader to run our program. Then proving looks the same: ```py! # This will write the program.json and the input data (fib(20)) to the bootloader's memory slot. python3 bootloader.py load --program program.json --entrypoint "fib(20)" --output-memory memory.bin # Now we run using the bootloader, which will run fib(20). cairo-run --program=bootloader.json --layout=all --memory_file=memory.bin --trace_file=trace.bin # Same steps for prove. giza prove --trace=trace.bin --memory=memory.bin --program=program.json --output=proof.bin # Verify. # a. Verify we are running program.json, by checking the program_hash == H(program.json) inside proof.bin. python3 bootloader.py verify-invocation --proof proof.bin --program program.json --entrypoint "fib(20)" # b. Verify the proof. giza verify --proof=proof.bin ``` And that's basically the full runthrough of a real ZK-STARK system, minus the bootloader. **Learning**: We write a program in Cairo, compile it to bytecode, run it to generate a trace, prove the trace, and then verify the proof. To verify what we're verifying, we use something called a bootloader, which communicates the program hash in the proof data via public memory. ## Part 3: Recursive ZK proofs. So what the fuck is recursive ZK proofing? It's alien technology, that's what it is. We like communicating with aliens. But like most aliens, you can't understand what the fuck they're talking about at first. The first lesson - recursion in proofing does not look like recursion **in any other language you're familiar with**. There is no "call stack" that is growing in size. There is no "calling the same function". So I think the analogy is completely stupid, tbh. Recursive proofing is this idea of "verifying proofs within proofs". Basically, you have `giza prove`, but this program is actually implemented in Cairo. So it's an API you can call within your ZK programs, which allows you to verify proofs from other program invocations. This is fucking magic. Because you can verify a proof that verifies other proofs, which verifies other proofs...and eventually you get a single 20kB file which succinctly verifies an entire blockchain's history. See [ZeroSync for a genuine attempt to do this on Bitcoin](https://github.com/lucidLuckylee/zerosync). **But**, it's a terrible analogy. The recursion occurs conceptually, not inside of the programming language. It actually just looks like invoking a pure function. The only language that implements recursive proofing right now is Mina. I've [written an example which uses recursion in ZK proofs](https://gist.github.com/liamzebedee/8d2efbb105d3b474dbe752d526cc9d27), and this is what it looks like using their API: ```ts # From https://gist.github.com/liamzebedee/8d2efbb105d3b474dbe752d526cc9d27 transfer: { privateInputs: [SelfProof], method(t1: Transaction, t0: SelfProof<Transaction>) { // Verify the state at t=0 was computed correctly. t0.verify(); // State: utxo signature validation. const t1_sighash = t1.sighash(); const msg = [t1_sighash]; ``` t0 is a `SelfProof`, basically a class which is a proof. `verify()` is a method. Does that look like recursion to you? No. **Learning**: Recursive ZK proofs are extremely powerful. But the recursion occurs conceptually, not like it looks like in normal programming (no call stacks, no calling the same function). ## Part 4: A better analogy for recursive proofs. So here's the meat - I think memoization is a much better way to imagine recursion in ZK proofing. Coming back to the beginning of our article, this is `memoize(x)`: ```py cache = {} def memoize(f): # The memoized version of f(x). def f2(x): if not x in cache: cache[x] = f(x) return cache[x] return f2 ``` The funny thing is, a ZK proof is kind of like a secure cache. If I compute `fib(n=100)`, and give you a number, you can't trust me without running the `fib(n=100)` which is $O(N)$ steps. But what if I prove `fib(n=100)` and give you the proof? Then it costs you $O(log^2 N)$ steps to trust me, by verifying the proof. What might this look like applied to our memoize helper? ```py cache = {} def memoize(f): # The "securely" memoized version of f(x). def f2(x): if not x in cache: cache[x] = prove(f(x)) return verify(cache[x]) return f2 ``` **Learning**: Another way to understand recursive proofs is through the idea of a "secure cache". The cache values are checked automatically using the `verify` function from Starkware. ## Part 5: A better API for recursive proofs. Recursion is a fitting analogy conceptually, but when programming, we might think of a more ergonomic API design. What if we could annotate any function as `provable`? e.g. ```cairo! # program2.cairo #[provable] func fib(n) { if n == 0 { return 0 } if n == 1 { return 1 } return fib(n-1) + fib(n-2) } ``` And in other places, we could interact as so: ```cairo! # program2.cairo func run_1() { proof = fib.prove() } func run_2(proof: felt*) { // Verify the proof. val = fib.verify(proof) } ``` But why have `prove`/`verify` splattered throughout our code? Could we go more ergonomic? Why not have this built into the runner? Maybe we could have a shared `cache` for all function invocations, and the runtime would automatically memoize and check the cache. Here's how this might work: * a public area of memory dedicated to the cache. * cairo-run and giza are modified to accept "secure cache entries" - proofs. * the Cairo runtime automatically rewrites functions annotated as "provable" to check this cache for results, thus removing the need for the programmer to write "verify". The potential workflow: ```cairo! # program3.cairo #[provable] func fib(n) { if n == 0 { return 0 } if n == 1 { return 1 } return fib(n-1) + fib(n-2) } func main1() { val = fib(100) } func main2() { val = fib(100) } ``` There are no verify API calls here. The verification occurs automatically inside the Cairo runtime - `cairo-run`. We begin by running the program: ```shell! # Compile. cairo-compile ./program3.cairo --output ./program3.json # Run main1. cairo-run --program=program3.json --entrypoint "main1" --layout=all --memory_file=memory.bin --trace_file=trace1.bin --cache_file=cache1.json # Prove main1. giza prove --trace=trace1.bin --memory=memory.bin --program=program3.json --output=proof1.bin ``` This might generate `cache1.json` looking like: ```json [ { "name": "fib", "code": "00a00e0fd0bca...", "hash": "010e0f0dd0bca...", "cache": { "100": "PROOF_DATA" } } ] ``` The second invocation, `main2`, we run the same function but we supply the cache of function invocations. ```shell! # Run main1. cairo-run --program=program3.json --entrypoint "main2" --layout=all --memory_file=memory.bin --trace_file=trace2.bin --cache_file=cache1.json # Prove main1. giza prove --trace=trace2.bin --memory=memory.bin --program=program3.json --output=proof1.bin ``` And there you have it - an API for recursive proofs that doesn't litter our code. Comparing this against the Mina code, where we have to be **manually passing around proofs** and intermixing our code with these verify functions, this is much more ergonomic. `SelfProof<Transaction>.verify()` vs. `#[provable]`. It feels very clean.

Import from clipboard

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template is not available.
Upgrade
All
  • All
  • Team
No template found.

Create custom template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

How to use Slide mode

API Docs

Edit in VSCode

Install browser extension

Get in Touch

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Upgrade to Prime Plan

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

No updates to save
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Upgrade

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Upgrade

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully