Oskar
    • 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
      • 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
    • 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
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
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
--- marp: true title: "Taipei ZK Workshop: Programming ZKPs: From Zero to Hero" paginate: true _paginate: false --- ## Taipei ZK Workshop - Second session: Intro to programming (11-13) - Stop for exercises twice - Short break in middle Note: --- ## Programming ZKPs: From Zero to Hero ![02_combined](https://hackmd.io/_uploads/HyT5oTJa1x.png =60%x) Based on [Programming ZKPs: From Zero to Hero](https://zkintro.com/articles/programming-zkps-from-zero-to-hero) --- ## Programming ZKPs: From Zero to Hero - Zebras: stripes for camouflage in a herd - Humans: group pseudonyms (Publius, Bourbaki) - Digital: prove membership without revealing which member Note: - Motivates group signatures. --- ## Programming ZKPs: From Zero to Hero - Circom + Groth16 focus - Goal: hands-on coding & proof generation - Edit-Build-Run cycle; work iteratively Note: - Emphasize iterative flow: build → prove → verify. - Setup env etc, then improve --- ## Proving you sent a message ![image](https://hackmd.io/_uploads/ByrMa61akl.png =80%x) Note: - Signature scheme, proving msg - Grok code and why, able to support group sigs --- ## Pre-requisites (1.1) - Software engineer; OK with command line - Familiarity with digital signatures, public-key cryptography, hash functions - Previous session intro to ZK --- ## Recap of ZKPs (1.2) - Zero Knowledge: prove correctness, hide data - Succinctness: small proofs, no matter complexity - Circom: a DSL for constraints - Groth16: proving system needing a trusted setup Note: - If needed, see “A Friendly Intro to Zero Knowledge.” - Assume everyone in previous session - Blockchain state transition, digital id, sudoku... --- ## Overview (2) 1. Write circuit (`.circom`) 2. Build → `.r1cs` + `.wasm` 3. Trusted setup → proving & verification keys 4. Generate proof (private inputs + proving key) 5. Verify proof (verification key + public data) Note: - We’ll do simple examples first, then build up. --- ## Preparation (2.1) - See https://github.com/oskarth/zkintro-tutorial - Pre-reqs: rust, just, npm - ZKP tools: circom, snarkjs, just tasks - Alternative: https://zkrepl.dev Note: See README, recommend installing but not comf zkrepl Better mental model; also see book Feel free to do now or wait for break --- ## First iteration (3) - Equivalent of "Hello World" - Program to prove knowledge of two secret numbers - without revealing secret numbers - E.g. $3 \cdot 11 = 33$ Note: - Stepping stone towards digital signatures - Think of secret numbers as private keys, product public - Diff way of thinking --- ## Write a special program (3.1) (example1) ``` pragma circom 2.0.0; template Multiplier2 () { signal input a; signal input b; signal output c; c <== a * b; } component main = Multiplier2(); ``` - `a`, `b`: private; `c`: public Note: - “Hello World” of Circom; line by line - Minimal circuit; set of constraints --- ## Constraints (3.2) - How do constraints work? - In Sudoku: number between 1..9, not a single constraint - Circom: Express using equality `===` constraints, e.g. `a * b === c` - Under hood: polynomials Note: - More to be aware of, more fundamental operations - (constants/add/mul, check equality) --- ## Constraints (3.2) ![02_example1_circuit](https://hackmd.io/_uploads/Hk34SR1Tkx.png =80%x) Note: - Illustrated here - Under hood, polynomials, fundamental operations --- ## Building our circuit (3.3) - `just build example1` → produces `.r1cs` + `.wasm` - thin wrapper around `circom` Note: - Solution; also Circom docs - `.r1cs` describes constraints; `.wasm` used for the witness generation (WebAssembly) - New way of doing things --- ## Building our circuit (3.3) Output: ```shell template instances: 1 non-linear constraints: 1 linear constraints: 0 public inputs: 0 private inputs: 2 public outputs: 1 wires: 4 labels: 4 Written successfully: example/target/example1.r1cs Written successfully: example/target/example1_js/example1.wasm ``` Note: - 2 priv 1 pub 1 non-linear --- ## Building our circuit (3.3) ![02_example1_build](https://hackmd.io/_uploads/SkHVLAyTyg.png =80%x) --- ## Trusted setup (3.4) - `just trusted_setup example1` → single-participant ceremony - Generates Common Reference String (CRS) - Consists of proving & verification keys - Needed for proving and verifying; not a "private key" Note: - Preprocessing; create cryptographic secret --- ## Trusted setup (3.4) ![02_example1_setup1](https://hackmd.io/_uploads/SygI5A161l.png =80%x) ![02_example1_setup2](https://hackmd.io/_uploads/r1G8cRkTkl.png =80%x) --- ## Trusted setup (3.4) - In production: N participants, trust $\frac{1}{n}$ - Different ZKP systems different properties - Groth16 two phases: phase1 and phase2 (circuit specific) Note: - Groth16 small proofs so two trusted setups - Most modern universal or single trusted setup --- ## Generate proof (Example 1) (3.5) - `input.json`: `{"a":"3","b":"11"}` - `just generate_proof example1` → outputs proof + public `["33"]` Note: - Demonstrates privacy (hiding `a`,`b`) + succinctness (small proof). --- ## Proof output (3.5) ```json { "pi_a": ["15932[...]3948", "66284[...]7222", "1"], "pi_b": [ ["17667[...]0525", "13094[...]1600"], ["12020[...]5738", "10182[...]7650"], ["1", "0"] ], "pi_c": ["18501[...]3969", "13175[...]3552", "1"], "protocol": "groth16", "curve": "bn128" } ``` Note: - Details not important, these are EC elemn - Short concise proof regardless; succinctness --- ## Visualization (3.5) ![02_example1_generate_proof](https://hackmd.io/_uploads/SJ1HfKchJx.png) Note: - Summarizes the pipeline in one diagram. --- ## Verify proof (3.6) - `just verify_proof example1` → checks correctness - If we tamper the output to `34`, verification fails --- ## Visualization (3.7) ![02_example1_verify_proof](https://hackmd.io/_uploads/HJDJk1xpkl.png =80%x) --- ## Exercises (Example 1) (3.7) - What are two key properties of ZKPs and what mean? - Role of prover and what input needed? Verifier? - What does `c <== a * b;` do? - Why a trusted setup? How use artifacts? - (Coding) Generate & verify a proof in `example1`. Note: - Group discussion after we cover all examples. --- ## Code & Break - Break up into smaller groups - Run setup and solve exercises above (15-20m) - Then take brief break (10-15m) --- (Break) --- ## Second iteration (4) - Integer factorization - Prime factorization: hard (multiplication: easy) - Big problem with current approach right now, what is it? Note: Just integer factorization but related --- ## Second iteration (4) - Change input to be "1" and "33" - Want constraint neither `a=1` or `b=1` - How to add this? Can't just write `!=` - How to express "something is not?" Note: - Different paradigm, not intuitive --- ## Updating our circuit (4.1) - Idea: Use `IsZero()` template (in stdlib) - Truth table: ``` | in | out | | --- | --- | | 0 | 1 | | n | 0 | ``` --- ## Updating our circuit (4.1) ``` include "circomlib/circuits/comparators.circom"; ... component isZeroCheck = IsZero(); isZeroCheck.in <== (a - 1) * (b - 1); isZeroCheck.out === 0; ``` - Forces `a,b ≠ 1` Note: - Classic Circom trick to encode “≠ 1.” - See example2; npm project around --- ## Build & Setup (Example 2) (4.1-4.2) - Update circuit, then: - `just build example2` - `just trusted_setup_phase2 example2` (reuse .ptau) - `just generate_proof example2` / `just verify_proof example2` - `1 * 33` fails → better constraints Note: - Re-running phase2 each time we alter circuit. --- ## Re-running our trusted setup (4.2) ![02_example2_setup_both](https://hackmd.io/_uploads/HJbkh-bpye.png) ![02_example2_setup2](https://hackmd.io/_uploads/SktjiWZayg.png) Note: - First generic; shared with some capacity for constraints - Groth16 need circuit-specific, annoying; modern ZKPs don't have this --- ## Testing our changes (4.3) - `just generate_proof example2` - `just verify_proof example2` - Changing `input.json` to be `1 * 33` fails Note: --- ## Complete flow diagram (4.4) ![02_example2_complete_flow](https://hackmd.io/_uploads/B1o04Kq3yg.png =50%x) Note: - Gone through twice, hopefully starting to make sense - Let's make our circuit more useful --- ## Third iteration (5) - Proven know product of two secret values - not very useful - Useful: digitial signature scheme - prove you wrote a specific message - Usually implememented with public-key crypto --- ## Digital signatures (5.1) - Key generation - Signing - Signature verification --- ## Trapdoor function (5.1) ![02_example3_trapdoor](https://hackmd.io/_uploads/rkGKwZGWayg.png =60%x) --- ## Digital signatures (5.1) - Public key crypto (implemented with ECC) - We don't want: ![02_example3_sigverify](https://hackmd.io/_uploads/HJFSxM-T1g.png =50%x) - Want to write program, generate and verify proof Note: - Not clever math --- ## Hash functions and commitments (5.2) - Hash functions: e.g. SHA256 - Commitments: - Commit and reveal - Two properties: hiding and binding Note: - Use these simpler tools; common primitive - Meatgrinder; not a trapdoor --- ## Lockbox (5.1) ![02_example3_lockbox](https://hackmd.io/_uploads/rkyVffb6ke.png =40%x) Note: Can't change after fact, give key to friend --- ## Digital signature scheme (5.1) - Key generation: hash(secret) to create commitment - Signing: Hash secret with message - Verification: Verify proof using commitment, message, signature (public output) --- ## Pseudocode (5.1) ```python identity_secret # private identity_commitment = hash(secret) signature = hash(secret, message) ``` Note: - Qs: Why ZKP? why not public key crypto? - Programmable; easier to extend logic here - Mimics public-key logic but purely via ZK constraints --- ## Back to the code (5.3) `just generate_identity` ```shell identity_secret: 43047[...]2270 identity_commitment: 21618[...]0684 ``` Note: - Big random number - What hash function? using Poseidon --- ## Poseidon: ZK-Friendly Hash (5.3) - SHA-256 is large in constraints - Poseidon = fewer multiplications, faster to prove - Example: ``` component hasher = Poseidon(2); hasher.inputs[0] <== identity_secret hasher.inputs[1] <== message; signature <== hasher.out; ``` Note: - Big difference in performance vs. standard crypto hashes. - Has to do with operating on binary diff arch --- ## Back to the code (5.3) - New syntax: ``` component main {public [identity_commitment, message]} = SignMessage(); ``` - Input private by default, this makes it part of public output Note: - Input private default, now public output --- ## Signature Circuit (5.3) ![02_example3_circuit](https://hackmd.io/_uploads/H1OiHz-ayg.png =90%x) Note: - Another diagram: how signals flow in the circuit. --- ## Build & Setup (Example 3) (5.3) - `just build example3` - `just trusted_setup_phase2 example3` Note: - Public output: `[signature, identity_commitment, message]`. --- ## Testing our circuit (5.4) - Example input: ``` {"identity_secret":"...", "identity_commitment":"...", "message":"42"} ``` - Then generate & verify proof as before - Public output: ```json ["48968[...]5499", "48269[...]7915", "42"] ``` Note: - Public output: `[signature, identity_commitment, message - Have to quote message, numbers - Output: sig, commitment, message --- ## Next steps (6) - We have all tools for group signatures - See example4, 5-10 lines of code - Group sig: sign message, prove 1/3 people, not reveal which one - Extensions: `n` participants, `reveal`, `deny` - Use cases: Anonymous voting, Private membership, Sybil-resistance Note: - Doing this with code, not just math special case - Not specialized cryptography, few hours Note: - A bit tricky, but there's one big aha moment, one line - For loops --- ## Exercises (4.5) 6. Why do we have to run phase 2 but not phase 1 of our trusted setup for `example2`? 7. What was the main problem with the previous example and how did we fix it? 8. Code: Finish `example2` until you failed to generate a proof. --- ### Exercises (5.5) 9. Three parts of a digital signature scheme? 10. Purpose of using a "ZK-Friendly hash function" like Poseidon? 11. What are commitments? How can we use them for a digital signature scheme? 12. Why do we mark the identity commitment and message as public? 13. Why do we need the identity commitment and signature constraints? 14. Code: Finish `example3`, generate/verify proof. --- ## Exercises (6.1) 15. What do group signatures do over normal signatures? How can they be used? --- ## Problems (7) - See booklet - IsZero, group signatures, ZK Identity, Layer 2s --- ## Code & Break - Break up into smaller groups - Solve exercises above (20-30m) --- (Coding) --- ## Conclusion (8) - Write and modify ZKPs from scratch - Programming environment, setup, generate and verify proofs - Multiplier circuit, improvements -> signature scheme -> group signatures - Mental model for ZKP dev and edit-run-debug cycle - Easy to extend, move to different toolstack, etc Note: - We turned custom crypto logic into straightforward code. --- ## Conclusion (8) - ZKPs allow for “programmable cryptography” - No specialized math for every new use case - Different programmming paradigm, new skill (constraints, commitments, ZK-friendly hashes) Note: - Encourages creative new solutions; see stdlib etc --- ## References - **Programming ZKPs: From Zero to Hero** <https://zkintro.com/articles/programming-zkps-from-zero-to-hero> - Circom Docs <https://docs.circom.io> Note: --- ## Thank you - Questions - Lunch break Note: - Wrap-up & Q&A

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 with
    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