Learn / Math / Linear Algebra over Finite Fields
22. Linear Algebra over Finite Fields
Linear Algebra · Lesson 23 of 26 · 13:00
How vectors and matrices work over a finite field — the dot product, matrix times vector, solving a system, and building an inverse — and how a computation's claimed values get packed into a vector and checked with exactly the dot products that give R1CS its shape.

The last two lessons worked with one number at a time. This lesson puts many numbers together, in lists and in grids — linear algebra, but every number is reduced mod a prime. A ZK proof starts life as a computation, and that computation gets turned into a pile of checks written with exactly this machinery. One small example carries the whole lesson: the prime 77, small enough that every step is easy to check by hand.

Vectors and the dot product

A list of numbers is called a vector. Vectors add and scale entry by entry, just like ordinary numbers — and one more operation, the dot product, turns two vectors into a single number.

vectors and the dot product What can we do with lists of numbers mod 7? STEP 1 — add and scale, entry by entry (2, 3, 5) + (4, 1, 6) = (6, 4, 11) = (6, 4, 4) 3 × (2, 3, 5) = (6, 9, 15) = (6, 2, 1) 11 − 7 = 4, 9 − 7 = 2, 15 − 14 = 1 STEP 2 — the dot product (2, 3, 5) • (4, 1, 6) = 2×4 + 3×1 + 5×6 = 8 + 3 + 30 = 41 41 = 5 × 7 + 6 = 6 a dot product gives one number reduce first: 8 = 1 and 30 = 2, so 1 + 3 + 2 = 6 ✓ TWO LISTS IN, ONE NUMBER OUT Every step is ordinary arithmetic, then reduce mod 7.
Fig. 1 — adding and scaling (2, 3, 5) entry by entry, then taking its dot product with (4, 1, 6): multiply matching entries, add, and reduce mod 7.
The dot product

(a1,a2,a3)⋅(b1,b2,b3)=a1b1+a2b2+a3b3(a_1, a_2, a_3) \cdot (b_1, b_2, b_3) = a_1 b_1 + a_2 b_2 + a_3 b_3

Addition and scaling work entry by entry, with the usual reduction mod 77 at the end. The dot product is different: it multiplies matching entries and adds the results, collapsing two lists into one number. Every other operation in this lesson — matrices, systems, constraints — is built entirely out of dot products.

Matrix times vector

A matrix is a grid of numbers, and multiplying it by a vector means taking the dot product of each row with that vector.

matrix times vector What does a matrix do to a vector? A = 1 2 3 4 v = (5, 6) A × v STEP 1 — row 1 • v 1 × 5 + 2 × 6 = 5 + 12 = 17 = 3 STEP 2 — row 2 • v 3 × 5 + 4 × 6 = 15 + 24 = 39 = 4 A × v = (3, 4) 17 = 2 × 7 + 3 and 39 = 5 × 7 + 4 ✓ ONE ROW, ONE DOT PRODUCT A matrix is a machine that turns one vector into another.
Fig. 2 — A = [[1, 2], [3, 4]] times v = (5, 6): row 1 dotted with v gives 3, row 2 dotted with v gives 4, so A × v = (3, 4).
Matrix times vector

(Av)i=rowi(A)⋅v(Av)_i = \text{row}_i(A) \cdot v

Each output entry is one row of AA dotted with vv — nothing more. A matrix, seen this way, is just a stack of dot products: a machine that turns one vector into another. Keep AA, v=(5,6)v = (5, 6), and the output (3,4)(3, 4) in mind; they carry through the rest of this lesson.

Solving a system

Running the machine backwards — given the output, find the input — is where division mod 77 earns its keep.

solving a system Which vector v = (x, y) does A turn into (3, 4)? SETUP x + 2y = 3 3x + 4y = 4 STEP 1 — clear x from row 2 subtract 3 × row 1 from row 2: (3x + 4y) − 3(x + 2y) = 4 − 3 × 3 −2y = −5 −2 = 5 and −5 = 2, so 5y = 2 STEP 2 — divide 5 × 3 = 15 = 1, so 1 ÷ 5 = 3, and y = 2 × 3 = 6 STEP 3 — back-substitute x = 3 − 2y = 3 − 12 = −9 = 5 (x, y) = (5, 6) 1 × 5 + 2 × 6 = 17 = 3 ✓ and 3 × 5 + 4 × 6 = 39 = 4 ✓ ELIMINATE, DIVIDE, BACK-SUBSTITUTE Same steps as with real numbers, but division means multiplying by the inverse mod 7.
Fig. 3 — recovering v from A × v = (3, 4): eliminate x from the second equation, divide by 5 mod 7, then back-substitute to get v = (5, 6) again.
Elimination mod p

eliminate→divide by the inverse mod p→back-substitute\text{eliminate} \to \text{divide by the inverse mod } p \to \text{back-substitute}

The two rows of AA give two equations in xx and yy. Eliminating xx leaves one equation in yy alone, solved by multiplying by 55‘s inverse mod 77 rather than dividing. Substituting yy back into the first equation recovers xx. The answer, (5,6)(5, 6), is exactly the vector Fig. 2 started from — the system undoes the matrix.

The inverse matrix

Solving from scratch every time is wasteful. A single matrix, A−1A^{-1}, can undo AA in one multiplication — as long as a number called the determinant isn’t 00.

the inverse matrix Can we undo A in one step? STEP 1 — the determinant for the grid 1 2 3 4 : 1 × 4 − 2 × 3 = −2 = 5 STEP 2 — invert it 5 × 3 = 15 = 1, so 1 ÷ 5 = 3 STEP 3 — swap, negate, scale 3 × 4 −2 −3 1 = 3 × 4 5 4 1 = 12 15 12 3 = 5 1 5 3 −2 = 5 and −3 = 4 A−1 = 5 1 5 3 A−1× (3, 4) = (5•3 + 1•4, 5•3 + 3•4) = (19, 27) = (5, 6) ✓, the same answer as before if the determinant is 0, there is no inverse: 1 2 2 4 has 1 × 4 − 2 × 2 = 0 A NON-ZERO DETERMINANT MEANS YOU CAN UNDO IT Zero determinant means two rows carry the same information.
Fig. 4 — building A⁻¹ from the determinant of A, then checking A⁻¹ × (3, 4) = (5, 6); a determinant of 0, as in [[1, 2], [2, 4]], means no inverse exists.
The 2×2 inverse

det⁡(abcd)=ad−bc,A−1=1det⁡A(d−b−ca)\det \begin{pmatrix} a & b \\ c & d \end{pmatrix} = ad - bc, \qquad A^{-1} = \frac{1}{\det A}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}

The determinant of AA is ad−bcad - bc; for this AA, that’s 55 mod 77. Swap the diagonal entries, negate the other two, and scale everything by the determinant’s inverse to get A−1A^{-1}. A determinant of 00 means the rows carry the same information — one is a multiple of the other — so the matrix loses information and can’t be undone.

Constraints as dot products

Here’s the payoff. A claimed computation — a handful of numbers satisfying some equations — packs into one vector, and each equation becomes a check built entirely from dot products.

constraints as dot products Can a computation be checked with dot products? SETUP claim: x = 3, y = x × x = 9 = 2, w = y × x = 6 (mod 7) pack everything into one vector z = (1, x, y, w) = (1, 3, 2, 6) STEP 1 — constraint y = x × x (row A)•z = (0, 1, 0, 0)•z = 3 (row B)•z = (0, 1, 0, 0)•z = 3 (row C)•z = (0, 0, 1, 0)•z = 2 3 × 3 = 9 = 2 ✓ STEP 2 — constraint w = y × x (row A)•z = (0, 0, 1, 0)•z = 2 (row B)•z = (0, 1, 0, 0)•z = 3 (row C)•z = (0, 0, 0, 1)•z = 6 2 × 3 = 6 ✓ every constraint: (row A • z) × (row B • z) = (row C • z) ONE MULTIPLY PER CONSTRAINT This is the shape of R1CS, a constraint system many ZK proofs use.
Fig. 5 — packing the claim x = 3, y = x², w = yx into z = (1, 3, 2, 6), then checking each multiplication step as (row A · z) × (row B · z) = (row C · z).
R1CS, one constraint

(rowA⋅z)×(rowB⋅z)=rowC⋅z(\text{row}_A \cdot z) \times (\text{row}_B \cdot z) = \text{row}_C \cdot z

Pack the claim’s values — including a leading 11 for constants — into a vector zz. Three carefully chosen rows, AA, BB, and CC, each pick out the right entries of zz by dotting with it, and the single real multiplication checks that the computation’s claimed step actually happened. Stack many such rows into matrices AA, BB, CC, and this becomes Az×Bz=CzAz \times Bz = Cz entry by entry — the shape of R1CS, a constraint system many ZK proofs use.

Where this shows up

Three ideas, each resting on the last: the dot product, matrices built from dot products, and constraints that are nothing but dot products wearing a disguise.

where this shows up THE DOT PRODUCT multiply matching entries, add, reduce mod 7 Two lists, one number. MATRICES AND SOLVING matrix times vector, row reduction, inverses Undo a matrix. CONSTRAINTS each check is three dot products and one multiply The shape of R1CS. DOT PRODUCTS ARE THE BUILDING BLOCK Next up: FFT and NTT.
Fig. 6 — the dot product, matrices and solving, and constraints, and what each one is for.

A dot product turns two lists into one number. A matrix is a stack of dot products, and it can be undone — by solving directly, or by building its inverse once — whenever its determinant isn’t 00. And a ZK constraint is exactly three dot products and one multiplication. But real proofs work with lists of millions of entries, and multiplying that many numbers the slow way is too expensive. The next lesson meets the trick that makes it fast.

Key takeaways
  • A dot product multiplies matching entries of two vectors and adds the results, reducing mod pp — two lists in, one number out.
  • A matrix times a vector is one dot product per row; the matrix is a machine that turns one vector into another.
  • Solving Av=bAv = b for vv works by eliminating variables and dividing by the inverse mod pp, the same elimination used with ordinary numbers.
  • A matrix has an inverse exactly when its determinant ad−bcad - bc isn’t 00; a zero determinant means two rows carry the same information and the matrix can’t be undone.
  • A computation’s claimed values pack into a vector zz, and each step becomes a constraint (rowA⋅z)×(rowB⋅z)=rowC⋅z(\text{row}_A \cdot z) \times (\text{row}_B \cdot z) = \text{row}_C \cdot z — the shape of R1CS.