Skip to content

Commit

Permalink
[book] Add swap and multiplexer page
Browse files Browse the repository at this point in the history
  • Loading branch information
ConstanceBeguier committed Nov 25, 2024
1 parent 0ab1218 commit e015d0c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [Optimization for 4/5 bits range check](design/gadgets/decomposition/optimization4_5bits.md)
- [SHA-256](design/gadgets/sha256.md)
- [16-bit table chip](design/gadgets/sha256/table16.md)
- [Swap and Multiplexer](design/gadgets/swap_multiplexer.md)
- [Background Material](background.md)
- [Fields](background/fields.md)
- [Polynomials](background/polynomials.md)
Expand Down
63 changes: 63 additions & 0 deletions book/src/design/gadgets/swap_multiplexer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Swap and Multiplexer

Swap gates are used to check the Merkle path of a commitment note.
Multiplixer are used in ZSA Orchard circuit to evaluate note and value commitments
according to the transaction type (ZSA or native transaction).

## Swap

Given an input pair of field elements $(a,b)$ and a boolean value $swap$,
we would like to return
- $(a, b)$ is $swap = 0$, and
- $(b, a)$ if $swap = 1$.

### Layout

Let $(a_s, b_s) = SWAP(a, b, swap)$.
We set all values $a_s$, $b_s$, $a$, $b$ and $swap$ on the same row in the layout.

$$
\begin{array}{|c|c|c|c|c|c|}
\hline
a_0 & a_1 & a_2 & a_3 & a_4 & q_\texttt{swap} \\\hline
a & b & a_s & b_s & swap & 1 \\\hline
\end{array}
$$

### Constraints

$$
\begin{array}{|c|l|}
\hline
\text{Degree} & \text{Constraint} \\\hline
3 & q_\texttt{swap} \cdot \BoolCheck{swap} = 0 \\\hline
3 & q_\texttt{swap} \cdot (a_s - \Ternary{swap}{b}{a}) = 0 \\\hline
3 & q_\texttt{swap} \cdot (b_s - \Ternary{swap}{a}{b}) = 0 \\\hline
\end{array}
$$

where $\Ternary{swap}{x}{y} = swap \cdot x + (1 - swap) y$.

## Multiplexer

Given two curve points $(x_0, y_0)$ and $(x_1, y_1)$ and a boolean value $choice$,
we would like to return
- $(x_0, y_0)$ if $choice=0$, and
- $(x_1, y_1)$ if $choice=1$.

To perform this $MUX$ operation, we will call twice the $SWAP$ gates once for each coordinate.
Let $(x, y) = MUX((x_0, y_0), (x_1, y_1), choice)$.
We have
$$x=SWAP(x_0, x_1, choice)[0]$$
$$y=SWAP(y_0, y_1, choice)[0]$$

### Layout

$$
\begin{array}{|c|c|c|c|c|c|}
\hline
a_0 & a_1 & a_2 & a_3 & a_4 & q_\texttt{swap} \\\hline
x_0 & x_1 & Swap(x_0, x_1, choice)[0] & Swap(x_0, x_1, choice)[1] & choice & 1 \\\hline
y_0 & y_1 & Swap(y_0, y_1, choice)[0] & Swap(y_0, y_1, choice)[1] & choice & 1 \\\hline
\end{array}
$$

0 comments on commit e015d0c

Please sign in to comment.