LogoLogo
LogoLogo
  • Introduction
    • About Us
    • Notations & Definitions
      • MPC
      • ZK
    • Contribute to this Site!
  • Primitives
    • Multiplication
      • Karatsuba Multiplication
      • Toom-Cook Multiplication
    • NAF (Non-adjacent form)
    • Chinese Remainder Theorem (CRT)
    • Euclidean Algorithm
      • Extended Euclidean Algorithm
      • Binary Euclidean Algorithm
      • Extended Binary Euclidean Algorithm
    • Coding Theory
      • Linear Code
    • Number Theoretic Transform
    • Abstract Algebra
      • Group
        • -Morphisms
        • Batch Inverse
      • Elliptic Curve
        • Weierstrass Curve
          • Coordinate Forms
          • Fast Elliptic Curve Arithmetic and Improved WEIL Pairing Evaluation
        • Edwards Curve
          • Coordinate Forms
          • Twisted Edwards ↔ Short Weierstrass Transformation
        • Batch Inverse for Batch Point Additions
        • Scalar Multiplication
          • Double-and-add
          • GLV Decomposition
        • MSM
          • Pippenger's Algorithm
          • Signed Bucket Index
          • CycloneMSM
          • EdMSM
          • cuZK
        • 2-Chain and 2-Cycle of Elliptic Curves
    • Encryption Scheme
      • ElGamal Encryption
    • Modular Arithmetic
      • Modular Reduction
        • Barrett Reduction
        • Montgomery Reduction
      • Modular Inverse
        • Bernstein-Yang's Inverse
    • Multiset Check
    • Sumcheck
    • Commitment Scheme
      • Fflonk
      • SHPlonk
      • Zeromorph
  • MPC
    • Yao's Garbled Circuits
    • GMW
    • BMR
  • ZK
    • Arithmetization
      • R1CS
      • PLONK
      • AIR
    • Folding
      • LatticeFold
      • Nova
        • Nova over Cycles of Curves
    • Lookup
      • Lasso
      • LogUp-GKR
    • SNARK
      • Groth16
      • HyperPlonk
      • Spartan
        • SPARK
    • STARK
      • Additive NTT
      • Basefold
      • Binius
      • Brakedown
      • CircleSTARK
      • FRI
        • FRI Security Features and Optimizations
      • DEEP FRI
      • STIR
      • WHIR
    • Distributed ZK
      • Ryan's Trick for Distributed Groth16
  • Application
    • zkLogin
    • zkHoldem
    • zkTLS
      • DECO
      • Proxying is enough
  • zkVM
Powered by GitBook
On this page
  • Introduction
  • Flattening to gates
  • Gates to R1CS
  • References
Export as PDF
  1. ZK
  2. Arithmetization

R1CS

PreviousArithmetizationNextPLONK

Last updated 3 months ago

Introduction

The Rank 1 Constraint System (R1CS) is one of the common arithmetization techniques used today. In short, it uses 3 matrices to constrain two inputs to the gate and 1 output of the gate.

Az⋅Bz=?Cz\bm{Az}\cdot \bm{Bz}\stackrel{?}=\bm{Cz}Az⋅Bz=?Cz

If the equation above is valid for a given A,B,C\bm{A},\bm{B},\bm{C}A,B,C constraint matrices, we consider z\bm{z}z a valid witness (also known as trace).

So how do we get these constraint matrices for a target computation? For the sake of simplicity, let's say we want to prove that we know a solution to x3+x+5=35x^3+x+5=35x3+x+5=35.

Flattening to gates

First we convert our x3+x+5x^3 + x + 5x3+x+5 program into simple statements which typically look like x=yx=yx=y or x=y⊕zx=y \oplus zx=y⊕z. So x3+x+5x^3 + x + 5x3+x+5 can be flattened to:

sym_1 = x * x  
y = sym_1 * x  
sym_2 = y + x  
out = sym_2 + 5

Gates to R1CS

In this step we convert the equations above into an R1CS matrix representation. At each row of R1CS matrices, we have 3 row vectors a,b,c\bm{a},\bm{b},\bm{c}a,b,c which we multiply with column vector z\bm{z}z. This solution vector z\bm{z}z should satisfy

index     0    1.   2     3.     4.   5.
varname ~one.  x. ~out  sym_1    y.  sym_2

This gives us the R1CS matrix representation:

A  
[0, 1, 0, 0, 0, 0]  
[0, 0, 0, 1, 0, 0]  
[0, 1, 0, 0, 1, 0]  
[5, 0, 0, 0, 0, 1]
B  
[0, 1, 0, 0, 0, 0]  
[0, 1, 0, 0, 0, 0]  
[1, 0, 0, 0, 0, 0]  
[1, 0, 0, 0, 0, 0]
C  
[0, 0, 0, 1, 0, 0]  
[0, 0, 0, 0, 1, 0]  
[0, 0, 0, 0, 0, 1]  
[0, 0, 1, 0, 0, 0]

References

z⋅a×z⋅b−z⋅c=0z⋅a×z⋅b=z⋅c\bm{z}\cdot \bm{a} \times \bm{z} \cdot \bm{b} - \bm{z} \cdot \bm{c} = 0\\ \bm{z}\cdot \bm{a} \times \bm{z} \cdot \bm{b} = \bm{z}\cdot \bm{c}z⋅a×z⋅b−z⋅c=0z⋅a×z⋅b=z⋅c

Each vector's length is equal to the number of variables in our program plus 1 for constant 1. So in the case above, we have ~one,x,sym1,sym2,sym3,~out\char`\~one,x, sym_1, sym_2, sym_3, \char`\~ out~one,x,sym1​,sym2​,sym3​,~out so we need vectors of size 6. We need the extra constant 111 or identity multiplier to deal with situations involving addition in our scheme as well as to add constants. So in total for our case above we have:

The reason for having three vectors is due to the nature of the constraints R1CS is designed to represent. Remember, in circuit computation, we have conceptual gates. These gates have a left input, a right input, and one output. So, basically, the a\bm{a}a vector represents the left input, the b\bm{b}b vector represents the right input, and the c\bm{c}c vector represents the output.

To express sym1=x⋅xsym_1 = x \cdot xsym1​=x⋅x, what we need to say is input 1 and input 2 is xxx and result is sym1sym_1sym1​

a=(0,1,0,0,0,0)b=(0,1,0,0,0,0)c=(0,0,0,1,0,0)\bm{a} = (0, 1, 0, 0, 0, 0)\\ \bm{b} = (0, 1, 0, 0, 0, 0) \\ \bm{c} = (0, 0, 0, 1, 0, 0)a=(0,1,0,0,0,0)b=(0,1,0,0,0,0)c=(0,0,0,1,0,0)

Similarly, to express y=sym1⋅xy = sym_1\cdot xy=sym1​⋅x:

a=(0,0,0,1,0,0)b=(0,1,0,0,0,0)c=(0,0,0,0,1,0)\bm{a} = (0, 0, 0, 1, 0, 0)\\ \bm{b} = (0, 1, 0, 0, 0, 0)\\ \bm{c} = (0, 0, 0, 0, 1, 0)a=(0,0,0,1,0,0)b=(0,1,0,0,0,0)c=(0,0,0,0,1,0)

For sym2=(x+y)⋅1sym_2 = (x + y) \cdot 1sym2​=(x+y)⋅1, we have:

a=(0,1,0,0,1,0)b=(1,0,0,0,0,0)c=(0,0,0,0,0,1)\bm{a} = (0, 1, 0, 0, 1, 0)\\ \bm{b} = (1, 0, 0, 0, 0, 0)\\ \bm{c} = (0, 0, 0, 0, 0, 1)a=(0,1,0,0,1,0)b=(1,0,0,0,0,0)c=(0,0,0,0,0,1)

And for ~out=(sym2+5)⋅1\char`~out = (sym_2 + 5) \cdot 1~out=(sym2​+5)⋅1, we have:

a=(5,0,0,0,0,1)b=(1,0,0,0,0,0)c=(0,0,1,0,0,0)\bm{a} = (5, 0, 0, 0, 0, 1)\\ \bm{b} = (1, 0, 0, 0, 0, 0)\\ \bm{c} = (0, 0, 1, 0, 0, 0)a=(5,0,0,0,0,1)b=(1,0,0,0,0,0)c=(0,0,1,0,0,0)

So by knowing z=[1,3,35,9,27,30]\bm{z} = [1, 3, 35, 9, 27, 30]z=[1,3,35,9,27,30], you prove that you know the solution since it satisfies all the constraints.

Written by from

https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649
A41
Batzorig Zorigoo