Path: blob/main/frontier/10-snarks-starks/sage/10c-qap-construction.ipynb
483 views
Notebook 10c: QAP Construction
Module 10. SNARKs and STARKs
Motivating Question. R1CS gives us separate constraints to check: for each row . Can we compress all checks into a single polynomial equation? Yes, by interpolating the R1CS columns into polynomials and using the fact that a polynomial that vanishes at points must be divisible by a known "vanishing polynomial." This is the Quadratic Arithmetic Program (QAP).
Prerequisites. You should be comfortable with:
R1CS: matrices , , and witness vector (Notebook 10b)
Polynomial interpolation (Lagrange) over finite fields (Module 02)
Learning objectives. By the end of this notebook you will be able to:
Interpolate R1CS columns into polynomials using Lagrange interpolation.
Construct the vanishing polynomial .
Verify the QAP equation: .
Understand why this polynomial identity enables succinct proofs.
1. Recap: Our R1CS
Bridge from Notebook 10b. We built R1CS for with 3 constraints over 5 wires. Now we'll transform those matrices into polynomials, the key step toward Groth16.
2. The Key Idea: Interpolation
We have constraints. Assign each constraint an evaluation point:
Constraint 1 → point
Constraint 2 → point
Constraint 3 → point
For each column of matrix , we interpolate a polynomial such that:
This means the polynomial "encodes" the entire column, evaluating it at point gives back the matrix entry for constraint .
3. Interpolating All Columns
We interpolate every column of , , and into polynomials.
Checkpoint 1. We now have polynomials (3 matrices × 5 wires). Each polynomial has degree at most (interpolated through points). Evaluating all polynomials at point reconstructs the -th row of each matrix.
4. Combining with the Witness
The R1CS check at constraint is: .
Define the combined polynomials:
Then , and similarly for and . So:
This means vanishes at all evaluation points!
5. The Vanishing Polynomial
Since vanishes at , it must be divisible by the vanishing polynomial:
So there exists a polynomial such that:
This is the QAP equation, the heart of SNARKs.
Checkpoint 2. The QAP equation is a polynomial identity. It holds for all (not just the evaluation points). The prover constructs by polynomial division. If the witness is invalid, the division will have a non-zero remainder, and the prover cannot forge .
6. Why QAP Enables Succinct Proofs
The QAP equation can be checked at a single random point (chosen by the verifier or embedded in the trusted setup):
By the Schwartz-Zippel lemma: if two polynomials of degree agree at a random point, they are the same polynomial with probability . For a 256-bit field, this probability is negligibly close to 1.
One check replaces constraint checks!
7. What Happens with a Bad Witness?
If the prover uses an incorrect witness, the polynomial won't vanish at all evaluation points, so it won't be divisible by .
8. Degree Analysis
Understanding the polynomial degrees is important for the proof system.
| Polynomial | Degree | Explanation |
|---|---|---|
| , , | Interpolated through points | |
| , , | Linear combinations of the above | |
| Product of degree polynomials | ||
| Product of linear factors | ||
9. The Full QAP Pipeline
Let's wrap the entire construction into a clean function.
Checkpoint 3. The QAP pipeline:
Setup: Convert R1CS to QAP polynomials (done once per circuit).
Prove: Combine polynomials with witness, compute via polynomial division.
Verify: Check at a random point.
In Groth16 (next notebook), the random is embedded in the trusted setup, and the evaluations are hidden inside elliptic curve points using pairings.
10. From QAP to SNARKs: The Gap
The QAP gives us a polynomial identity to check. But a few problems remain:
| Problem | Solution (Groth16) |
|---|---|
| Verifier must know | is generated and destroyed in trusted setup |
| Prover could evaluate at wrong point | Evaluations are committed via elliptic curve points |
| Witness is revealed | Polynomial evaluations are hidden in curve points |
| Need to check polynomial identity | Use pairing equation: |
Crypto foreshadowing. Groth16 uses the bilinear pairing from Module 07 to check the QAP equation "in the exponent", the verifier never sees , , etc., only elliptic curve points that encode them. This is how zero-knowledge is achieved.
11. Exercises
Exercise 1 (Worked): QAP for
Problem. Build the full QAP for (from Notebook 10b). Verify the polynomial identity.
Solution:
Exercise 2 (Guided): Detect a Bad Witness
Problem. Using the QAP for , try to produce a proof for a wrong output: claim instead of . Show that qap_prove gives a non-zero remainder.
Fill in the TODOs:
Exercise 3 (Independent): QAP from Scratch
Problem.
Write R1CS for (hint: two multiplication gates).
Convert to QAP polynomials.
Compute for the witness with .
Verify the QAP equation at three different random points.
What degree is ? Does it match the formula ?
Summary
| Concept | Key Fact |
|---|---|
| QAP | Polynomial encoding of R1CS constraints |
| Interpolation | Each R1CS column becomes a polynomial via Lagrange interpolation |
| Combined polynomials | ; evaluating at gives |
| Vanishing polynomial | , vanishes at all constraint points |
| QAP equation | |
| Schwartz-Zippel | Check polynomial identity at one random point with overwhelming probability |
The QAP is the mathematical core of pairing-based SNARKs. In the next notebook, we'll see how Groth16 evaluates this polynomial identity "in the exponent" using elliptic curve pairings, hiding the witness while allowing verification.
Next: 10d: Groth16 Overview