Path: blob/main/frontier/12-mpc/sage/12c-yaos-garbled-circuits.ipynb
483 views
Notebook 12c: Yao's Garbled Circuits
Module 12: Multi-Party Computation
Motivating Question. Secret sharing lets multiple parties compute additions cheaply, but multiplication requires Beaver triples and communication rounds. Is there a way for two parties to compute any function with just one round of interaction? Yao's Garbled Circuits (1986) achieves this: one party "garbles" a Boolean circuit, the other evaluates it, learning only the output.
Prerequisites. You should be comfortable with:
Secret sharing (Notebooks 12a-12b)
Symmetric encryption (basic concept)
Boolean logic gates (AND, OR, XOR)
Learning objectives. By the end of this notebook you will be able to:
Assign random wire labels to each wire in a Boolean circuit.
Garble a single gate by encrypting output labels.
Evaluate a garbled gate using one label per input wire.
Assemble a complete garbled circuit for a small function.
Understand the role of oblivious transfer in the protocol.
1. The Idea: Encrypt the Circuit Itself
Bridge from Notebook 12b. With secret sharing, parties interact during computation (each gate requires communication). Garbled circuits take a different approach: the garbler (Alice) encrypts the entire circuit in advance, and the evaluator (Bob) processes it in a single pass.
The protocol at a high level:
Alice builds a Boolean circuit for the function
Alice assigns random wire labels, two per wire (one for 0, one for 1)
Alice garbles each gate: encrypts output labels under input labels
Alice sends the garbled circuit + her input labels to Bob
Bob gets his input labels via oblivious transfer (next notebook)
Bob evaluates the garbled circuit, learning only the output
Checkpoint 1. Wire labels are the core idea: replace each bit value (0 or 1) with a random 128-bit string. The evaluator works with labels, never knowing the actual bits. This is why the evaluation reveals nothing about the inputs.
2. Garbling a Gate
For a gate , we have the truth table:
| A | B | C = A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
For each row, we encrypt the output label using the two input labels as the key:
We shuffle the four entries so the order reveals nothing.
3. Evaluating a Garbled Gate
The evaluator has one label per input wire. They try to decrypt each entry, exactly one will produce a valid output label.
Checkpoint 2. The evaluator decrypts exactly one entry per gate. They learn the output label but cannot tell which input combination produced it (because the entries are shuffled and the labels are random).
4. A Complete Garbled Circuit
Let's garble a small circuit that computes .
This is a 1-bit inner product: .
Circuit structure:
5. The Full Yao Protocol
In a real two-party computation:
Alice (garbler) knows her input and builds the garbled circuit
Alice sends Bob: the garbled circuit + her input labels (for her actual bits)
Bob (evaluator) needs his input labels, but Alice can't just send them (she'd learn !)
They use Oblivious Transfer (OT): Bob selects his labels without Alice learning which
Bob evaluates the garbled circuit and gets the output label
The decoding table maps the output label to the actual result
Misconception alert. "The evaluator can figure out the input bits by looking at the wire labels." No! Each label is a random 128-bit string with no relation to 0 or 1. The evaluator cannot distinguish label-for-0 from label-for-1 without the decoding table (which is only provided for the output wire).
Crypto foreshadowing. The missing piece is Oblivious Transfer, how Bob gets his input labels without Alice learning Bob's input bits. The next notebook covers OT in detail.
6. Exercises
Exercise 1 (Worked): Garbled OR Gate
Problem. Garble an OR gate and verify it works for all four input combinations.
Solution:
Exercise 2 (Guided): Garbled Comparison
Problem. Garble a circuit that computes whether for 1-bit inputs. (Hint: iff and , which is .)
Fill in the TODOs:
Exercise 3 (Independent): Cost Analysis
Problem.
How many garbled entries does a circuit with gates require?
For a 32-bit integer comparison circuit, estimate the number of gates and the total size of the garbled circuit.
Why is Yao's protocol called a "constant-round" protocol? How many rounds of communication does it need?
Summary
| Concept | Key Fact |
|---|---|
| Wire labels | Two random labels per wire (one for 0, one for 1) |
| Garbling | Encrypt output labels under input labels; shuffle entries |
| Evaluation | Try decrypting each entry; exactly one succeeds |
| Security | Labels are random, evaluator can't tell 0 from 1 |
| Protocol | Alice garbles, Bob evaluates, OT provides Bob's labels |
| Rounds | Constant (2-3), entire circuit evaluated in one pass |
Yao's garbled circuits turn any Boolean function into a secure two-party protocol. The garbler encrypts the circuit; the evaluator decrypts gate by gate. The missing ingredient, how the evaluator gets their input labels without revealing their input, is Oblivious Transfer.
Next: 12d: Oblivious Transfer