Try   HackMD

Sin7Y Tech Review (27): Combined Selector

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Customized gate

When designing the zkvm circuit, because of many custom gates determined, there are a lot of binary selectors are introduced. Taking the (field) division gate as an example, we plan to design a gate to verify that the relationship

q=x/y works among three elements
q,x,y
. For convenience, we will not perform the field division operation at the circuit level, instead, we will make it by verifying the following logical relationship:
xinvy=qinvyy=1  //ensure y0

Between the two elements, there is an equal relationship. Therefore, we have the following Trace table:

Step
s
sdiv
w0
w1
w2
w3
k 0 0 1 0 x y q
invy
//division

Define the polynomial

w0(x),w1(x),w2(x),w3(x) for columns
w0,w1,w2,w3
, then the rows corresponding to the division operation shall satisfy:
w0(x)w3(x)w2(x)=0w1(x)w3(x)1=0

To ensure that the relationship above can be satisfied in the corresponding row, a selector is needed to make the corresponding division to constrain the verification. Therefore, we have introduced a new column
sdiv={0,0,...1,...0}
$. When it is transformed to the polynomial
sdiv(x)
, the above equation will be:
sdiv(x)(w0(x)w3(x)w2(x))=0sdiv(x)(w1(x)w3(x)1)=0

Combined selector

From the example above, we can see that when we define a new customized gate, a selector column

s needs to be introduced to correspond to the gate. If we represent the corresponding constraint polynomial of the gate with
t(x)
, we can obtain the following constraints finally:

sadd(x)tadd(x)=0sdiv(x)tadd(x)=0scube(x)tcube(x)=0ssqrt(x)tsqrt(x)=0...
For the prover needs to conduct commitments to all polynomials during the proof generation, overmuch selector polynomials introduced will increase the workload of the prover and the verifier. Therefore, it is necessary to optimize the selector number while the following two conditions must be satisfied:

  1. There is no loss for the meaning of selector polynomial, that is, only specific gates can be used;
  2. Less selector polynomial

In "Plonky2: Fast Recursive Arguments with PLONK and FRI"(https://github.com/mir-protocol/plonky2/blob/main/plonky2/plonky2.pdf), there is an optimization method "Binary-Tree Based Selector", which reduces the number of the select polynomial to

log(k), and
k
is the number of the customized gate. In the Halo2 paper, the zcash team has put forward a new optimization method, which may realize a smaller number of the polynomial (in correlation with the constraint polynomial
t(x)
and the parameters set for the constraint polynomial max_degree).

To understand it easier, let's take a simple example (for specific algorithm, please refer to Selector combining - The halo2 Book)

Step
sadd
sdiv
scube
ssqrt
w0
w1
w2
w3
0 1 0 0 0
a0
b0
c0
d0
//add
1 0 1 0 0
a1
b1
c1
d1
//division
2 0 0 1 0
a2
b2
c2
d2
//cube
3 0 0 0 1
a3
b3
c3
d3
//sqrt

We can see that we have set 4 selector columns for 4 kinds of customized gates, which are not what we want just like the aforementioned. Therefore, this will increase the workload of the prover and the verifier. Here we define a new column

q, satisfying:

q={kif  the selector labelled k is 10if  all the selectors is 0

If we define a set

{sadd,sdiv,scube,ssqrt} (called as disjoint, that is to make there is no common point between possible rows) for selectors
sadd,sdiv,scube,ssqrt
, then based on the definition of column
q
, we have:

Step
sadd
sdiv
scube
ssqrt
q
w0
w1
w2
w3
0 1 0 0 0 1
a0
b0
c0
d0
//add
1 0 1 0 0 2
a1
b1
c1
d1
//division
2 0 0 1 0 3
a2
b2
c2
d2
//cube
3 0 0 0 1 4
a3
b3
c3
d3
//sqrt

Here, define a new selector polynomial form based on column

q, and for the number
k
selector polynomial, we have:
q(x)1hlen(selectors),hk(hq(x))

For example, for the constraint:

saddtadd(x)=0, can be rewritten into:

q(x)1hlen(selectors),h1(hq(x))tadd(x)=0

The above formula can be expanded into:

q(x)(2q(x))(3q(x))(4q(x))tadd(x)=0
Set

combinedq(x)=q(x)(2q(x))(3q(x))(4q(x))

Then, the true value figure is obtained:

x
combinedq(x)
sadd(x)
0 1 1
1 0 0
2 0 0
3 0 0

It can be seen that it does the same thing as the original selector. Therefore, for constraints

sadd(x)tadd(x)=0sdiv(x)tadd(x)=0scube(x)tcube(x)=0ssqrt(x)tsqrt(x)=0...
We can rewrite the constrains into:
q(x)1hlen(selectors),h1(hq(x))tadd(x)=0q(x)1hlen(selectors),h2(hq(x))tadd(x)=0q(x)1hlen(selectors),h3(hq(x))(x)tcube(x)=0q(x)1hlen(selectors),h4(hq(x))tsqrt(x)=0...

For the constraints above, we only need to conduct commitment to the polynomial

q(x). But it is worth noting that, this method has also increased the degree of the constraint.

So far, we have achieved the two points mentioned above:

  1. There is no loss for the meaning of selector polynomial, that is, only specific gates can be used;
  2. Less selector polynomial

Of course, there are limitations to the constrained Degree in the protocol, so the selector number participating in the combine is bounded, that is, the number of the single combined selector depends on the boundary value of the Degree and max_degree of the constraint polynomial

t(x). Therefore, more combined columns are needed, and anyway, the number is still much smaller than the original one.

For more handling cases and algorithm details, please refer to Selector combining - The halo2 Book.

References

  1. "The halo2 Book", https://zcash.github.io/halo2/design/implementation/selector-combining.html
  2. Polygon Zero Team, "Plonky2: Fast Recursive Arguments with PLONK and FRI", https://github.com/mir-protocol/plonky2/blob/main/plonky2/plonky2.pdf, accessed: 2022-02-06