Skip to content
KALANBLON
Show contents

Chapter 2 of 6 · 20 min

Privacy and zero-knowledge proofs

By the end of this chapter, you'll be able to

  • Explain why the pseudonymity of a public blockchain is fragile
  • Apply the common-input heuristic to group addresses
  • Build a cryptographic commitment that hides a value and later reveals it
  • Carry out a Schnorr proof: prove you know a secret without revealing it

Pseudonymous is not anonymous

When a transaction has several inputs, the private key of each one is generally needed to sign it: so all these addresses probably belong to the same person. That is the common-input heuristic, used for more than ten years by chain-analysis tools.

transactions = [
    {"inputs": ["a1", "a2"], "outputs": ["b7"]},
    {"inputs": ["a2", "a3"], "outputs": ["c4", "a9"]},
    {"inputs": ["a9", "a3", "a5"], "outputs": ["d2"]},
    {"inputs": ["x1"], "outputs": ["a1"]},
    {"inputs": ["k1", "k2"], "outputs": ["x1"]},
]

parent = {}
def root(a):
    parent.setdefault(a, a)
    while parent[a] != a:
        parent[a] = parent[parent[a]]
        a = parent[a]
    return a
def union(a, b):
    parent[root(a)] = root(b)

for tx in transactions:                             # all the inputs of a transaction: same owner
    for other in tx["inputs"][1:]:
        union(tx["inputs"][0], other)

groups = {}
for address in sorted({a for tx in transactions for a in tx["inputs"]}):
    groups.setdefault(root(address), set()).add(address)
for g in sorted(groups.values(), key=lambda g: (-len(g), min(g))):
    print(sorted(g))

identified = "a5"                                   # withdrawn to a service that checked the identity
print("one identified address reveals:", sorted(groups[root(identified)]))
['a1', 'a2', 'a3', 'a5', 'a9']
['k1', 'k2']
['x1']
one identified address reveals: ['a1', 'a2', 'a3', 'a5', 'a9']

A single address linked to an identity is enough to reveal the whole group, and therefore all of that group's past payments. Other clues add up: round amounts, timing, change addresses. Some blockchains build in strong protections (Monero, Zcash); on the others, privacy relies on cryptographic tools added on top.

Commitments: hide now, reveal later

A commitment is like a sealed envelope: you publish fingerprint(value + randomness) now, and later reveal value and randomness. Everyone checks that the envelope did contain that value. Two properties:

  • hiding: the fingerprint reveals nothing about the value, thanks to the randomness;
  • binding: it is impossible to find another value that gives the same fingerprint (collision resistance).

Typical use: a sealed-bid auction on a blockchain, where each bid must stay secret until the close.

Schnorr's proof: proving without revealing

A zero-knowledge proof convinces that a statement is true, without revealing anything else. The founding example: proving you know the secret x such that y = g^x mod p (the public key y is known to all), without revealing x.

With the Fiat-Shamir heuristic, a hash function replaces the verifier: the proof becomes a signature.

Step 5 of 5
Text description of the animation
  1. r is a one-time secret that will mask x.
  2. The prover does not know c in advance: no fake answer can be prepared.
  3. The response mixes x with r: without r, it reveals nothing about x.
  4. The verifier checks with the public values only.
  5. With the Fiat-Shamir heuristic, a hash function replaces the verifier: the proof becomes a signature.
import hashlib
import secrets

p, g = 2**127 - 1, 3                         # demonstration group
x = 0x5EC12E7_C0FFEE_2026                    # the secret, which will never be revealed
y = pow(g, x, p)                             # the public value

def h(*values):
    return int(hashlib.sha256("|".join(map(str, values)).encode()).hexdigest(), 16)

def prove(x, message):
    r = secrets.randbelow(p - 1)
    t = pow(g, r, p)
    c = h(y, t, message)                     # Fiat-Shamir: the challenge comes from a fingerprint
    return t, (r + c * x) % (p - 1)

def verify(y, message, proof):
    t, s = proof
    return pow(g, s, p) == t * pow(y, h(y, t, message), p) % p

proof = prove(x, "access to the cooperative's safe")
print("valid proof         :", verify(y, "access to the cooperative's safe", proof))
print("other message       :", verify(y, "access to the bank's safe", proof))
print("without the secret  :", verify(y, "access to the cooperative's safe", prove(x + 1, "access to the cooperative's safe")))
valid proof         : True
other message       : False
without the secret  : False

The verifier is convinced without ever seeing x. Why "zero knowledge"? Because transcripts (t, c, s) that verify can be made without knowing x, provided the challenge is chosen before the commitment: the transcript of a real proof therefore teaches nothing you could not have produced alone. Modern systems (zk-SNARKs, zk-STARKs) generalise this idea to any computation: that is what lets a ZK-rollup prove that thousands of transactions are correct, or Zcash hide amounts and addresses.

Mini-lab: simulating a proof without the secretPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: the sealed auction

Write a two-phase auction. Phase 1: each participant publishes commit(bid), which returns the public commitment and keeps (bid, randomness) secret. Phase 2: each reveals their bid and randomness; the function count(commitments, reveals) rejects any reveal that does not match the published commitment, then names the best valid bid. Test with three participants, one of whom tries to change their bid at reveal time.

Hint 1
randomness = secrets.token_hex(16); the commitment is sha256(f"{bid}|{randomness}").
Hint 2
In count, for each name, recompute the commitment from the reveal and compare it with the one published in phase 1.
Show the solution and common mistakes
import hashlib
import secrets

def commitment(bid, randomness):
    return hashlib.sha256(f"{bid}|{randomness}".encode()).hexdigest()

def commit(bid):
    randomness = secrets.token_hex(16)
    return commitment(bid, randomness), (bid, randomness)

def count(commitments, reveals):
    valid = {}
    for name, (bid, randomness) in reveals.items():
        if commitment(bid, randomness) != commitments.get(name):
            print(f"{name}: reveal rejected")
            continue
        valid[name] = bid
    winner = max(valid, key=valid.get)
    return winner, valid[winner]

kept_secrets, published = {}, {}
for name, bid in [("Awa", 420_000), ("Kofi", 450_000), ("Mei", 400_000)]:
    published[name], kept_secrets[name] = commit(bid)
print("phase 1:", len(published), "commitments published, bids invisible")

reveals = dict(kept_secrets)
mei_bid, mei_randomness = kept_secrets["Mei"]
reveals["Mei"] = (500_000, mei_randomness)           # Mei saw the other bids and tries to outbid

print("winner:", count(published, reveals))
phase 1: 3 commitments published, bids invisible
Mei: reveal rejected
winner: ('Kofi', 450000)

Mei's attempt fails: the commitment published in phase 1 binds Mei to the bid of 400,000. Nobody could see the bids before the reveal, and nobody could change them afterwards.

Common mistakes:

  • Revealing the randomness together with the commitment: the envelope is open from phase 1.
  • Treating a missing reveal as a zero bid: a participant who sees they are losing can choose not to reveal; a rule is needed (penalty, forfeited deposit).
  • Concatenating value and randomness without a separator: "45" + "0000…" and "450" + "000…" give the same bytes.

Summary

  1. 1Addresses are pseudonyms: the history is public and the common-input heuristic groups addresses together.
  2. 2A commitment hides a value until it is revealed; without randomness, it is broken by trial.
  3. 3A Schnorr proof shows knowledge of a secret x such that y = g^x without revealing x.
  4. 4With Fiat-Shamir, the challenge becomes a fingerprint: the proof is non-interactive, like a signature.
  5. 5Generalised zero-knowledge proofs power ZK-rollups and confidential cryptocurrencies.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.