Try   HackMD

Aztec emulated field and group operations

Disclaimer: This documentation was written on September, 2021. It is intended to give readers a high-level understanding. The codebase is the canonical source of truth, and over time this document might fall behind the implementation details of the code.

Emulated Field

We want to evaluate a modular multiplication of

Fp elements a, b, using constraints over
Fn
(
p,n
are prime). In particular we allow for the case where
n<p
.
We will choose integer
t
such that
2tn>p2
. And also leverage operations mod
2t
together with the CRT.

In more detail:
We will check that as positive integers

ab=qp+r
The way we do this at a high level is

  1. check that the equation holds both mod
    2t
    and mod
    n
  2. check that each side of the equation is smaller than
    M:=2tn
    .

The first, using CRT, implies the equation hold mod

M. Then the second implies the equation hold over the integers since both sides are smaller than
M
. This in turn implies
ab=r
mod
p
, which means
r
is the correct multiplication result in
Fp
.

Notation summary:

  • p = non-native field modulus
  • n = native circuit modulus
  • 2t
    = extra CRT modulus
  • p
    =
    p
    mod
    2t
  • a0,a1,a2,a3
    are the 4 limbs of a.
  • b = maximum size of a limb (e.g. b = 2^68 in the code)
  • s<t
    - number of bits of
    p
    (having seprate
    s,t
    parameters will be helpful for "lazy reductions" e.g. for long addition chains, see Aztec bigfield class for details).

Unless specified, all arithmetic is modulo n.

When we say 'compute' a variable we mean also insert constraints to enforce that value.

Multiplication algorithm:

Assume for simplicity that

b=t/4 below.

  1. Compute witnesses
    q
    and
    r
    such that
    abqpr=0
    .
  2. Apply range constraints on the limbs of
    q,r
    such that they are all
    <2b
    .
  3. Apply multiplication gates and addition gates to evaluate the following intermediate products mod
    2t
    (note that other intermediate products that arise in
    abqp
    are zero mod
    2t
    ):
  • (a0b0+q0p0)
    - bits
    [0,t/2+1]
  • (a0b1+a1b0+q0p1+q1p0)
    -bits
    [t/4,3t/4+2]
  • (a0b2+a1b1+a2b0+q0p2+q1p1+q2p0)
    - bits
    [t/2,t+3]
    .
  • (a0b3+a1b2+a2b1+a3b0+q0p3+q1p2+q2p1+q3p0)
    - bits
    [3t/4,5t/4+3]

Let's call these 4 (size ~2b) limbs

t0,t1,t2,t3.
We can create them using 10 mult-add gates (the terms
qipj
can be added to the multiplication
aibj
cause
p
is fixed)

Note that the range of each

ti follows from the range checks on the limbs of
a,b,q
.

  1. Compute
    u0=t0+2bt1r02br1
    and
    u1=t2+2bt3r2br3
    .

Both

u0 and
u1
should be in the range [0, 3b] (plus some overflow bits). Moreover, the first
2b
bits of
u0
should be zero because we have subtracted from them the low 2b-bits of the remainder term using
r0,r1
. Same holds for the first
2b
bits of
u0/(22b)+u1
.

  1. Compute

    v0 such that
    u0=v022b

  2. Validate

    v0 is in the range [0, b].

  3. Compute

    v1 such that
    u1+v0=v122b
    .

  4. Validate

    v1 is in the range [0,b] plus some overflow bits.

  5. Finally, to use the CRT, check also that

    abpqr=0
    (modn)
    . Since
    n
    is the native modulus this can be done very efficiently.

  6. Range constrain

    q so that
    qp+r<2tn
    . The reason this range constraint is needed is that the CRT allows us to verify the desired equality
    ab=pq+r
    as integers only when both side of the equation are smaller than
    2tn
    .
    Note that for this to be possible we need in partciular that
    t
    is large enough so that
    2tn>p2+p
    .

acknowledgements: Thanks to Onur Kilic & Xin for pointing out necessity of step 10