Path: blob/main/frontier/09-commitments-sigma-protocols/sage/09a-commitment-schemes.ipynb
483 views
Commitment Schemes
Module 09a | Commitments and Sigma Protocols
The cryptographic sealed envelope: bind yourself to a value without revealing it.
Motivating question: Alice and Bob want to play "odds and evens" over the internet. Each picks a number (say 0 or 1). If the sum is even, Alice wins; if odd, Bob wins. But here's the problem: if Alice announces first, Bob sees her choice and picks accordingly. If Bob goes first, Alice cheats the same way. Can they play fairly WITHOUT a trusted third party?
By the end of this notebook, you'll have a complete answer, and you'll see that the tool behind it, the commitment scheme, is one of the most fundamental primitives in modern cryptography.
Objectives
By the end of this notebook you will be able to:
Explain the sealed-envelope analogy and the two phases of a commitment (commit, reveal)
State the two security properties, hiding and binding, and explain why they matter
Implement a hash-based commitment scheme in SageMath
Understand the fundamental duality: why you cannot have both perfect hiding and perfect binding
Apply commitments to solve fair coin-flipping and other protocols
Prerequisites
You should have completed Modules 01–08 (modular arithmetic through pairings). This notebook requires:
Comfort with hash functions (conceptually)
Basic familiarity with SageMath / Python syntax
Bridge from earlier modules: In Modules 01–08, we built encryption (AES, RSA), key exchange (Diffie–Hellman, ECDH), and digital signatures (ECDSA). These primitives protect confidentiality and authenticity. Commitments are a different kind of primitive — they don't encrypt a message or prove who sent it. Instead, they let you bind yourself to a value without revealing it. This seemingly simple idea unlocks an entirely new world: zero-knowledge proofs, fair protocols, and verifiable computation.
The Sealed Envelope Analogy
A commitment scheme is the digital version of a sealed envelope:
| Physical World | Cryptographic World |
|---|---|
| Write message on paper | Choose message |
| Seal it in an opaque envelope | Compute commitment |
| Hand the sealed envelope to a friend | Send to the verifier |
| Later: open the envelope to reveal the message | Reveal and ; verifier checks |
Two phases:
Commit phase: The committer locks in a value and sends the commitment. The value is hidden.
Reveal phase (opening): The committer reveals the original value and any randomness. The verifier checks consistency.
The scheme must satisfy two security properties:
Hiding: The sealed envelope is opaque — seeing reveals nothing about
Binding: The sealed envelope is tamper-proof — the committer cannot open it to a different message
Hash-Based Commitment
The simplest commitment scheme uses a cryptographic hash function:
where is the message and is a random nonce (the "randomness" or "blinding factor").
Commit: Pick random , compute , send
Reveal: Send . The verifier computes and checks it equals
Why does this work?
Hiding comes from the random : even if is known to be one of a few values, the hash output looks uniformly random because adds entropy
Binding comes from collision resistance: the committer cannot find a different such that
Misconception alert: "A commitment scheme is just a hash." No! The hash is ONE implementation. The concept is more general — any scheme satisfying hiding + binding is a commitment. We'll see Pedersen commitments (next notebook) that use discrete logarithms instead of hashes, and they have very different security properties.
Checkpoint: Before running the next cell, predict: if Alice tries to open her commitment to
b'tails'using a different random nonce , will she succeed? Why or why not?
Why Randomness Is Essential
What if we skip the randomness and just compute ? This is a common beginner mistake. Let's see what goes wrong.
The Two Security Properties in Detail
Let's be precise about what hiding and binding mean:
Hiding
Given a commitment , an adversary cannot determine which message was committed to.
Computationally hiding: No efficient algorithm can distinguish commitments to different messages. (The hash commitment achieves this — as long as the adversary can't invert SHA-256.)
Perfectly hiding (information-theoretic): Even with unlimited computational power, the adversary learns nothing about from . (The hash commitment does NOT achieve this — there exists exactly one preimage.)
Binding
The committer cannot find two different messages that produce the same commitment.
Computationally binding: No efficient algorithm can find with . (The hash commitment achieves this via collision resistance.)
Perfectly binding (information-theoretic): It is impossible (not just hard) to find a collision. (The hash commitment achieves this! For a fixed , there is at most one valid opening — well, technically the function is injective on its domain.)
So the hash commitment is: computationally hiding + perfectly binding.
The Fundamental Duality
Here is a deep and surprising fact:
Theorem (informal): No commitment scheme can be both perfectly hiding and perfectly binding.
Why? Suppose a scheme is perfectly hiding. Then for every commitment , every message in the message space could have produced (with some randomness ). But that means the committer can find an alternative opening — so the scheme cannot be perfectly binding!
Conversely, if a scheme is perfectly binding, then each has a unique opening, so an unbounded adversary could (in principle) find it — breaking perfect hiding.
This gives us two families:
| Type | Hiding | Binding | Example |
|---|---|---|---|
| Hash commitment | Computational | Perfect (effectively) | |
| Pedersen commitment | Perfect | Computational | (next notebook) |
The choice depends on your application. For zero-knowledge proofs, we usually want perfect hiding (the Pedersen style), because the prover's secret must remain information-theoretically hidden.
Crypto foreshadowing: Pedersen commitments (next notebook, 09b) achieve perfect hiding using the discrete log assumption. They are additively homomorphic — you can add committed values without opening them. This property is the foundation of Bulletproofs, confidential transactions in blockchains, and the commitment schemes inside SNARKs (Module 10).
Commitment as a Protocol
Let's formalize the two-phase protocol with a complete interactive simulation. Alice commits, sends the commitment to Bob, and later reveals.
Application: Fair Coin Flipping
Now let's solve the motivating question! Alice and Bob can play odds-and-evens fairly using commitments:
Alice picks her bit and sends to Bob
Bob picks his bit and sends to Alice (in the clear)
Alice reveals . Bob verifies the commitment.
The "coin flip" result is (XOR)
Why is this fair?
Bob can't cheat: when he sends in step 2, he doesn't know (hiding)
Alice can't cheat: she committed to before seeing (binding)
Checkpoint: Think about this: what if Alice refuses to reveal in step 3? The protocol stalls, but does anyone learn anything unfair? What about if the commitment were NOT hiding — could Bob cheat in step 2? Make sure you can answer both before proceeding.
Other Applications
Commitments appear everywhere in cryptography:
| Application | How commitments help |
|---|---|
| Sealed-bid auctions | Each bidder commits to their bid. All commitments are collected, then all are opened simultaneously. No one can adjust their bid after seeing others'. |
| Zero-knowledge proofs | The prover commits to intermediate values. The verifier challenges. The commitment ensures the prover can't "backtrack." |
| Coin flipping | As we just saw — commitments enable fair randomness without trusted third parties. |
| Blockchain | Commit-reveal schemes prevent front-running in DeFi, enable private voting, and more. |
The pattern is always the same: commit first, then interact, then reveal. The commitment creates a temporal barrier that prevents cheating.
Exercises
Exercise 1: Sealed-Bid Auction (Worked)
Three bidders each commit to a secret bid. After all commitments are collected, the bids are revealed and the highest bidder wins. Implement this protocol and verify all commitments.
Exercise 2: Broken Commitment — What Goes Wrong? (Guided)
Alice uses a bad commitment scheme: she commits to her message WITHOUT randomness, i.e., instead of .
Bob knows Alice's message is one of: 'yes', 'no', or 'maybe'.
Your task: Write code that lets Bob figure out Alice's message from the commitment alone.
Hints:
Bob can compute for each candidate message
Compare each result to Alice's commitment
Which security property is broken? Hiding or binding?
Exercise 3: Commit-and-Reveal Voting (Independent)
Design and implement a simple yes/no vote among 5 voters using hash commitments. Your protocol should:
Have each voter commit to their vote (
b'yes'orb'no') with proper randomnessCollect all commitments before any reveals
Verify each opening and tally the votes
Print the final result
Challenge questions (answer in comments):
What happens if a voter refuses to reveal? How would you handle this?
Does this scheme protect voter privacy after the reveal phase? Why or why not?
How might you modify this to keep votes private even after tallying? (Hint: think about homomorphic commitments — coming in 09b)
Summary
| Concept | Key idea |
|---|---|
| Two phases | Commit (seal the value), then Reveal (open and verify) |
| Hiding | The commitment reveals nothing about the message |
| Binding | The committer cannot change the message after committing |
| Hash commitment | is simple, computationally hiding, and perfectly binding |
| Randomness is essential | Without , an adversary can brute-force small message spaces and break hiding |
| Fundamental duality | You cannot have both perfect hiding and perfect binding, you must choose one to be computational |
| Applications | Fair coin flipping, sealed-bid auctions, zero-knowledge proofs, and more |
Looking ahead: In the next notebook, we move from hash-based commitments to Pedersen commitments — an algebraic scheme that achieves perfect hiding using the discrete log assumption. Pedersen commitments are additively homomorphic: you can add two commitments and get a commitment to the sum, without opening either one. This property is the foundation of Bulletproofs, confidential transactions, and the commitment schemes inside modern zero-knowledge proof systems (SNARKs, STARKs).
Next: Pedersen Commitments