# Permutation argument

Given that gates in halo2 circuits operate "locally" (on cells in the current row or defined relative rows), it is common to need to copy a value from some arbitrary cell into the current row for use in a gate. This is performed with an equality constraint, which enforces that the source and destination cells contain the same value.

We implement these equality constraints by constructing a permutation that represents the constraints, and then using a permutation argument within the proof to enforce them.

## Notation

A permutation is a one-to-one and onto mapping of a set onto itself. A permutation can be factored uniquely into a composition of cycles (up to ordering of cycles, and rotation of each cycle).

We sometimes use cycle notation to write permutations. Let $(abc)$ denote a cycle where $a$ maps to $b,$ $b$ maps to $c,$ and $c$ maps to $a$ (with the obvious generalization to arbitrary-sized cycles). Writing two or more cycles next to each other denotes a composition of the corresponding permutations. For example, $(ab)(cd)$ denotes the permutation that maps $a$ to $b,$ $b$ to $a,$ $c$ to $d,$ and $d$ to $c.$

## Constructing the permutation

### Goal

We want to construct a permutation in which each subset of variables that are in a equality-constraint set form a cycle. For example, suppose that we have a circuit that defines the following equality constraints:

- $a≡b$
- $a≡c$
- $d≡e$

From this we have the equality-constraint sets ${a,b,c}$ and ${d,e}.$ We want to construct the permutation:

$(abc)(de)$

which defines the mapping of $[a,b,c,d,e]$ to $[b,c,a,e,d].$

### Algorithm

We need to keep track of the set of cycles, which is a set of disjoint sets. Efficient data structures for this problem are known; for the sake of simplicity we choose one that is not asymptotically optimal but is easy to implement.

We represent the current state as:

- an array $mapping$ for the permutation itself;
- an auxiliary array $aux$ that keeps track of a distinguished element of each cycle;
- another array $sizes$ that keeps track of the size of each cycle.

We have the invariant that for each element $x$ in a given cycle $C,$ $aux(x)$ points to the same element $c∈C.$ This allows us to quickly decide whether two given elements $x$ and $y$ are in the same cycle, by checking whether $aux(x)=aux(y).$ Also, $sizes(aux(x))$ gives the size of the cycle containing $x.$ (This is guaranteed only for $sizes(aux(x)),$ not for $sizes(x).$)

The algorithm starts with a representation of the identity permutation: for all $x,$ we set $mapping(x)=x,$ $aux(x)=x,$ and $sizes(x)=1.$

To add an equality constraint $left≡right$:

- Check whether $left$ and $right$ are already in the same cycle, i.e. whether $aux(left)=aux(right).$ If so, there is nothing to do.
- Otherwise, $left$ and $right$ belong to different cycles. Make $left$ the larger cycle and $right$ the smaller one, by swapping them iff $sizes(aux(left))<sizes(aux(right)).$
- Set $sizes(aux(left)):=sizes(aux(left))+sizes(aux(right)).$
- Following the mapping around the right (smaller) cycle, for each element $x$ set $aux(x):=aux(left).$
- Splice the smaller cycle into the larger one by swapping $mapping(left)$ with $mapping(right).$

For example, given two disjoint cycles $(ABCD)$ and $(EFGH)$:

```
A +---> B
^ +
| |
+ v
D <---+ C E +---> F
^ +
| |
+ v
H <---+ G
```

After adding constraint $B≡E$ the above algorithm produces the cycle:

```
A +---> B +-------------+
^ |
| |
+ v
D <---+ C <---+ E F
^ +
| |
+ v
H <---+ G
```

### Broken alternatives

If we did not check whether $left$ and $right$ were already in the same cycle, then we could end up undoing an equality constraint. For example, if we have the following constraints:

- $a≡b$
- $b≡c$
- $c≡d$
- $b≡d$

and we tried to implement adding an equality constraint just using step 5 of the above algorithm, then we would end up constructing the cycle $(ab)(cd),$ rather than the correct $(abcd).$

## Argument specification

We need to check a permutation of cells in $m$ columns, represented in Lagrange basis by polynomials $v_{0},…,v_{m−1}.$

We will label *each cell* in those $m$ columns with a unique element of $F_{×}.$

Suppose that we have a permutation on these labels, $σ(column:i,row:j)=(column:i_{′},row:j_{′}).$ in which the cycles correspond to equality-constraint sets.

If we consider the set of pairs ${(label,value)}$, then the values within each cycle are equal if and only if permuting the label in each pair by $σ$ yields the same set:

Since the labels are distinct, set equality is the same as multiset equality, which we can check using a product argument.

Let $ω$ be a $2_{k}$ root of unity and let $δ$ be a $T$ root of unity, where $T⋅2_{S}+1=p$ with $T$ odd and $k≤S.$ We will use $δ_{i}⋅ω_{j}∈F_{×}$ as the label for the cell in the $j$th row of the $i$th column of the permutation argument.

We represent $σ$ by a vector of $m$ polynomials $s_{i}(X)$ such that $s_{i}(ω_{j})=δ_{i_{′}}⋅ω_{j_{′}}.$

Notice that the identity permutation can be represented by the vector of $m$ polynomials $ID_{i}(ω_{j})$ such that $ID_{i}(ω_{j})=δ_{i}⋅ω_{j}.$

We will use a challenge $β$ to compress each $(label,value)$ pair to $value+β⋅label.$ Just as in the product argument we used for lookups, we also use a challenge $γ$ to randomize each term of the product.

Now given our permutation represented by $s_{0},…,s_{m−1}$ over columns represented by $v_{0},…,v_{m−1},$ we want to ensure that: $i=0∏m−1 j=0∏n−1 (v_{i}(ω_{j})+β⋅s_{i}(ω_{j})+γv_{i}(ω_{j})+β⋅δ_{i}⋅ω_{j}+γ )=1$

Here $v_{i}(ω_{j})+β⋅δ_{i}⋅ω_{j}$ represents the unpermuted $(label,value)$ pair, and $v_{i}(ω_{j})+β⋅s_{i}(ω_{j})$ represents the permuted $(σ(label),value)$ pair.

Let $Z_{P}$ be such that $Z_{P}(ω_{0})=Z_{P}(ω_{n})=1$ and for $0≤j<n$: $Z_{P}(ω_{j+1}) =h=0∏j i=0∏m−1 v_{i}(ω_{h})+β⋅s_{i}(ω_{h})+γv_{i}(ω_{h})+β⋅δ_{i}⋅ω_{h}+γ =Z_{P}(ω_{j})i=0∏m−1 v_{i}(ω_{j})+β⋅s_{i}(ω_{j})+γv_{i}(ω_{j})+β⋅δ_{i}⋅ω_{j}+γ $

Then it is sufficient to enforce the rules: $Z_{P}(ωX)⋅i=0∏m−1 (v_{i}(X)+β⋅s_{i}(X)+γ)−Z_{P}(X)⋅i=0∏m−1 (v_{i}(X)+β⋅δ_{i}⋅X+γ)=0ℓ_{0}⋅(1−Z_{P}(X))=0$

This assumes that the number of columns $m$ is such that the polynomial in the first rule above fits within the degree bound of the PLONK configuration. We will see below how to handle a larger number of columns.

The optimization used to obtain the simple representation of the identity permutation was suggested by Vitalik Buterin for PLONK, and is described at the end of section 8 of the PLONK paper. Note that the $δ_{i}$ are all distinct quadratic non-residues, provided that the number of columns that are enabled for equality is no more than $T$, which always holds in practice for the curves used in Halo 2.

## Zero-knowledge adjustment

Similarly to the lookup argument, we need an adjustment to the above argument to account for the last $t$ rows of each column being filled with random values.

We limit the number of usable rows to $u=2_{k}−t−1.$ We add two selectors, defined in the same way as for the lookup argument:

- $q_{blind}$ is set to $1$ on the last $t$ rows, and $0$ elsewhere;
- $q_{last}$ is set to $1$ only on row $u,$ and $0$ elsewhere (i.e. it is set on the row in between the usable rows and the blinding rows).

We enable the product rule from above only for the usable rows:

$(1−(q_{last}(X)+q_{blind}(X)))⋅$ $(Z_{P}(ωX)⋅i=0∏m−1 (v_{i}(X)+β⋅s_{i}(X)+γ)−Z_{P}(X)⋅i=0∏m−1 (v_{i}(X)+β⋅δ_{i}⋅X+γ))=0$

The rule that is enabled on row $0$ remains the same:

$ℓ_{0}(X)⋅(1−Z_{P}(X))=0$

Since we can no longer rely on the wraparound to ensure that each product $Z_{P}$ becomes $1$ again at $ω_{2_{k}},$ we would instead need to constrain $Z(ω_{u})=1.$ This raises the same problem that was described for the lookup argument. So we allow $Z(ω_{u})$ to be either zero or one:

$q_{last}(X)⋅(Z_{P}(X)_{2}−Z_{P}(X))=0$

which gives perfect completeness and zero knowledge.

## Spanning a large number of columns

The halo2 implementation does not in practice limit the number of columns for which equality constraints can be enabled. Therefore, it must solve the problem that the above approach might yield a product rule with a polynomial that exceeds the PLONK configuration's degree bound. The degree bound could be raised, but this would be inefficient if no other rules require a larger degree.

Instead, we split the product across $b$ sets of $m$ columns, using product columns $Z_{P,0},…Z_{P,b−1},$ and we use another rule to copy the product from the end of one column set to the beginning of the next.

That is, for $0≤a<b$ we have:

$(1−(q_{last}(X)+q_{blind}(X)))⋅$ $(Z_{P,a}(ωX)⋅i=am∏(a+1)m−1 (v_{i}(X)+β⋅s_{i}(X)+γ)−Z_{P}(X)⋅i=am∏(a+1)m−1 (v_{i}(X)+β⋅δ_{i}⋅X+γ))$ $=0$

For simplicity this is written assuming that the number of columns enabled for equality constraints is a multiple of $m$; if not then the products for the last column set will have fewer than $m$ terms.

For the first column set we have:

$ℓ_{0}⋅(1−Z_{P,0}(X))=0$

For each subsequent column set, $0<a<b,$ we use the following rule to copy $Z_{P,a−1}(ω_{u})$ to the start of the next column set, $Z_{P,a}(ω_{0})$:

$ℓ_{0}⋅(Z_{P,a}(X)−Z_{P,a−1}(ω_{u}X))=0$

For the last column set, we allow $Z_{P,b−1}(ω_{u})$ to be either zero or one:

$q_{last}(X)⋅(Z_{P,b−1}(X)_{2}−Z_{P,b−1}(X))=0$

which gives perfect completeness and zero knowledge as before.