book: Start collecting tips and tricks

This commit is contained in:
Jack Grigg 2020-12-10 20:35:36 +00:00
parent e16946f56b
commit 2a7df99478
2 changed files with 26 additions and 0 deletions

View File

@ -4,4 +4,5 @@
- [Concepts](concepts.md)
- [User Documentation](user.md)
- [Gadgets](user/gadgets.md)
- [Tips and tricks](user/tips-and-tricks.md)
- [Design](design.md)

View File

@ -0,0 +1,25 @@
# Tips and tricks
This section contains various ideas and snippets that you might find useful while writing
halo2 circuits.
## Small range constraints
A common constraint used in R1CS circuits is the boolean constraint: $b * (1 - b) = 0$.
This constraint can only be satisfied by $b = 0$ or $b = 1$.
In halo2 circuits, you can similarly constrain a cell to have one of a small set of
values. For example, to constrain $a$ to the range $[0..5]$, you would create a gate of
the form:
$$a * (1 - a) * (2 - a) * (3 - a) * (4 - a) = 0$$
while to constraint $c$ to be either 7 or 13, you would use:
$$(7 - c) * (13 - c) = 0$$
> The underlying principle here is that we create a polynomial constraint with roots at
> each value in the set of possible values we want to allow. In R1CS circuits, the maximum
> supported polynomial degree is 2 (due to all constraints being of the form $a * b = c$).
> In halo2 circuits, you can use arbitrary-degree polynomials - with the proviso that
> higher-degree constraints are more expensive to use.