All ideas credited to Axiom.
Here we suggest an implementation of multiplication in a large field, under the assumption that lookup tables work very fast.
Suppose we want to emulate multiplication modulo
over
In practice,
Lookup tables can have at most
The classic: Aztec
The original implementation by Aztec uses Chinese Remainder Theorem. We find a large number
- Find
$q$ ,$r$ , such that$ab = qp + r$ ; - Range check
$0 \le ab < M\cdot n$ ; - Range check
$0 \le qp + r < M\cdot n$ ; - Native operation
$ab - qp - r = 0 \mod n$ ; -
$ab - qp - r = 0 \mod M$ .
The final check is the tricky one. In the original implementation by Aztec, this check was handled as follows:
In reality, all steps are slightly more complicated, since the numbers are too large for the lookup tables.
Here is a way to perform non-native multiplication using a lookup table: we can find
Now in order to check that
For this to work, we would need the number of rows
First, we chose
- All
$m_i$ are relatively prime; -
$m_i < 2^{14}$ for all$i$ ; -
$Mn > p^2 + p$ . (I suspect, we want$t \ge 20$ ).
Now we want to pre-compute lookup tables
- Find
$q$ ,$r$ , such that$ab = qp + r$ ; - Range check
$0 \le ab < M\cdot n$ : this is done by checking$0\le a < p$ and$0 \le b < p$ ; - Range check
$0 \le qp + r < M\cdot n$ : this is done by checking$0\le q < p$ and$0 \le r < p$ ; - Check that
$a \cdot b = q \cdot p + r \mod n$ ; -
$a$ ,$b$ ,$q$ ,$r$ are converted into the CRT form:$a \mapsto (a_1, a_2, \ldots)$ where$a_i = a \mod m_i$ ; - For each modulus
$m_i$ , check that$a_i \cdot b_i = q_i \cdot p_i + r_i \mod m_i$ by using a lookup table.
This bit is inspired by Aztec implementation, as well as this paper.
Traditionally, computations with big integers use limbs:
The algorithm is realized by the function crt_mul. The inputs are a: &BigUint
, b: &BigUint
, crt_p: &CRTint<F>
, moduli: &Vec<BigUint>
. Computes and constrains r
$ = a\cdot b \mod p$. Returns bits_r
.
-
Preparation
- Finding witnesses
$q$ and$r$ , such that$ab = pq+r$ .
- Finding witnesses
-
biguint_into_crtint_fe_modulus
-
$a$ ,$b$ ,$q$ ,$r$ are converted into the CRT form:$a \mapsto (a_1, a_2, \ldots)$ where$a_i = a = a^0 + 2^{128}a^1\mod m_i$ ; - Loading witnesses: pairs
$(a^0, a^1), \ldots$ such that$a^0 + 2^{128}a^1 = a$ .
-
-
check_big_less_than_p: LHS range checks.
- Checking that
$a < p$ . We need to see that$a^1 < p^1 + 1$ and$a^0 \cdot \delta_{a^1, p^1} < p^0$ . - Same for
$b$ . We get$LHS \le (p-1)(p-1) = p^2-2p+1$ .
- Checking that
-
check_big_less_than_p: RHS range checks.
- Checking that
$q < p-1$ and$r < p$ . We get$RHS \le p(p-2) + (p-1) = p^2 -p + 1$ (note that this needs to be true if $ LHS = RHS$).
- Checking that
-
mod_r_mul: Checking that
$a \cdot b = q \cdot p + r \mod n$ ; -
crt_lookup_division_with_remainder CRT operations (to simplify the notation, let us fix a modulus
$m_i$ ).-
crt_lookup_mul Finding
$(a_i, b_i, a_ib_i )$ ,$(p_i, q_i, p_iq_i )$ in the lookup table. -
crt_lookup_add For addition: then checking that $$p_iq_i + r_i = (p_iq_i + r_i ){\mod m_i}$$ for
$$p_iq_i + r_i = (p_iq_i + r_i ){\mod m_i} + m_i.$$
Note that we verify that
$r_i < m_i$ in 6.3. -
bits_to_crt_check Proving the CRT-reperesentation:
$$(a^0 _{\mod m_i} + a^1 _{\mod m_i} \cdot 2^{128} _{\mod m_i}) \mod m_i = a_i$$ and$a_i < m_i$ , for$a$ ,$b$ ,$q$ and$r$ .
-
crt_lookup_mul Finding
CRTint struct, containing all the CRT information: residues modulo each
CQLookupGateChip trait, implementing addition and multiplication modulo
BITStoCRT trait, implementing methods that constrain