Try   HackMD

This note aims to detail our thoughts on a particular short post from researchers at Aztec, the team behind the original PLONK zk-SNARKs. Due to the original post's short form, some of the details are not altogether clear to us. It requires some leaps of logic or creativity in certain parts. Therefore, the explainations in this note might be a bit misleading in parts and should be taken with a grain of salt.Nevertheless, we will take the burden of possibility of being mistaken and write out what we understood from the original post.

Further, the methods in these notes (or methods similar to them) are implemented in the following repository, https://github.com/privacy-scaling-explorations/halo2wrong, to create efficient circuits for ECDSA verification algorithm which uses large fields.


Motivation

In some circuits such as DSA, we are frequently interested in representing modulo arithmetic over a large prime field, equations of the sort

ab=r mod p where
p
is a large prime. Typically, a witness
(a,b,r,q,p)
is produced and the integer constraint
ab=qp+r
is included as part of the polynomial commitment. Using non-native arithmetic discussed in this note, we can replace these types of constraints with (multiple) constraints over smaller numbers. Therefore, our goal in this note is to emulate certain expensive constraints in a different way.


Setup

Let us consider the setting and the naive approach. We are given a prime number

p. We are interested in building a circuit for
ab mod p
. The input variables
a,b
must be in the range
[0,p)
. Otherwise, we would simply use the equivalent modulo statement with smaller numbers
(a mod p)(b mod p) mod p
.

We are interested in finding a non-negative integer

r s.t.
ab=r mod p
and
r
is the smallest such number, which means
r
is in the range
[0,p)
as well. Given a candidate for
r
, we wish to build a constraint that ensures
r
matches
(a,b,p)
. This would require us to introduce an advice variable
q
(to show the existence of) that satisfies
ab=qp+r
, which is a unique positive number. Since
ab<p2
and the least value
r
can take is
0
,
abr=qp<p2
. This would imply
q
is in the same range
[0,p)
. Therefore, all the relevant variables aside from
p
itself are in the same range,
a,b,q,r[0,p)
.

If we are willing to deal with values in the range

[0,p2) in our circuit constraints, we can simply ensure that
abqpr=0
. However, this may not be desirable if
p
is a large prime in the order of
2256
for example. In the rest of this note, we build the same constraint in a different way, circumventing the computation of
ab
or
qp
for a given
(a,b,q,p,r)
.


Native Field Constraint

We begin by considering a method to ensure

abqpr=0 mod n for a prime
n<p
without computing
ab
or
qp
. Since the above equality implies
(amodn)(bmodn)(q mod n)(p mod n)(r mod n)=0 mod n
, we can write it with new variables
{an,bn,qn,pn,rn}
s.t.
a=an mod n
etc. respectively. In order to accomplish this, we consider a new set of constraints with advice variables
{va,vb,vq,vp,vr}
,
(1)van+an=a(2)vbn+bn=b(3)vqn+qn=q(4)vpn+pn=p(5)vrn+rn=r
Based on
{an,bn,qn,pn,rn}
, we can simply re-write the additional overall constraint as
anbnqnpnrn=0 mod n
or in an integer constraint form as,
(6)voveralln=anbnqnpnrn.
Note that
anbnqnpnrn
itself does not need to be
0
, unlike
abqpr
. These are all integer constraints over numbers smaller than
n
besides
{a,b,q,p,r}
.
{a,b,q,p,r}
appear in constraints in Eq. 1-5 on their own without any multiplications with other numbers. Therefore, these constraint could be computed efficiently with small numbers.

For a given

(a,b,q,p,r,va,vb,vq,vp,vr,voverall) that follows constraints in Eq. 1-6, it is guaranteed that
abqpr=0 mod n
. However,
abqpr=0
is not guaranteed. In order to satisfy
abqpr=0
, we will need the additional constraints we introduce in the next section.


Constraint decomposition in modulo
2T
ring, where
p<2T

In the following,

p=p mod 2T or more concretely
p=2Tp
. It is easy to see that
abqp=ab+qpmod2T
.

Assume

T is a given integer that is a multiple of
4
. We will write integers such as
a,b
in binary notation as a concatenation of multiple bit subsections, sometimes called limbs, as follows.

Consider limbs of size

B=T/4. Then an integer
a[0,2T1]
could be written as
4
limbs,
a=[a3,a2,a1,a0]
, each of which is a number
ai[0,2B1]
that can be represented as a sequence of
B
bits, with
a=i=0:3ai2iB
. Given
a,b,q,p[0,2T1]
, we can write the following equation for
ab+qp
,
ab+qp mod 2T=[a3,a2,a1,a0][b3,b2,b1,b0]+[q3,q2,q1,q0][p3,p2,p1,p0](7)=(a0b0+q0p0)20B+(8)(a1b0+a0b1+q1p0+q0p1)21B+(9)(a2b0+a0b2+a1b1+q2p0+q0p2+q1p1)22B+(10)(a3b0+a0b3+a1b2+a2b1+q3p0+q0p3+q1p2+q2p1)23B=t020B+t121B+t222B+t323B

Note that we do not have terms that involve

a3b3 in these equations, even though they would appear in
ab+qp
. The reason for this is simple: the term that involves
a3b3
would be
a3b32(3+3)B
and since
3+34
, this number is divisible by
2T=24B
and is zero modulo
2T
. The same is true for any
aibj
and
qipj
with
i+j4
.

This leaves us

10 different terms for
ab
(and
10
terms for
qp
) that are grouped together by the index sum
i+j{0,1,2,3}
in each line of Eq. 7-10 respectively. Once we group them in this way, we can factor out
2(i+j)B
in each line, according to the position
(i,j)
of the limbs of the original numbers involved in the multiplications. Based on this grouping, we can create intermediate variables
ti+j{t0,t1,t2,t3}
,
ab+qp mod 2T=t323B+t222B+t121B+t020B
which can take values in the following ranges:
t0[0,22B+11]t1[0,22B+21]t2[0,22B+31]t3[0,22B+31]
If the number of elements in each sum
ti
is represented by
vi
, the formula for the range for each sum can be given as
ti[0,22B+log2vi1]
. The exponent
2B+log2vi
for the bound above will be explained through
t2
.

t2=a2b0+a0b2+a1b1+q2p0+q0p2+q1p1As we can see, it consists of a summation of
6
terms where each element is the multiplication of two numbers in the range
[0,2B1]
. When we multiply two such numbers, we end up with a number in the range
[0,22B+11]
.
+1
in
22B+1
comes from the fact that there might be an additional carry bit after the multiplication. Further, when we add
6
such numbers, we end up with a number in the range
[0,6(22B+11)]
. This range can be (minimally) expanded above to a bit friendly range
[0,2log2622B6+5]=[0,22B+log261]
that encompasses
[0,6(22B+11)]
.

Let

t=t323B+t222B+t121B+t020B. Note that
t
itself does not need to be less than
2T=24B
. Its calculation only throws away some parts of
ab+qp
that do not contribute to the modulo result but not all. Therefore, if
ab+qp=rmod2T
with
r<2T
, it is not guaranteed that
t=r
. However, the last
T
bits of
t
are certainly going to equal
r
(the reason a power of two such as
2T
is convenient).

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Further, if we consider the

B-bit limb decomposition,
r=[r3,r2,r1,r0]
,
r0
is the last
B
bits of
y0=t0+0
, i.e.
y0r0=z02B
for some non-negative integer
z0
. Similarly,
r1
is the last
B
bits of
y1=t1+z0
with
y1r1=z12B
. We can write the all the limbs of
r
in a similar fashion:
y0=t0+0,y0r0=z02By1=t1+z0,y1r1=z12By2=t2+z1,y2r2=z22By2=t3+z2,y3r3=z32B
Removing the intermediate variables
y
, we get
(11)t0+0r0=z02B(12)t1+z0r1=z12B(13)t2+z1r2=z22B(14)t3+z2r3=z32B.

In this section, we have demonstrated a sets of integer constraints in Eq. 11-14 that can be used to ensure

ab+qp=rmod2T, which also ensures
abqpr=0mod2T
. Note that we accomplish this without ever computing
ab
or
qp
but by working directly with their
B
-bit limbs in the constraints.

To summarize, for a given

([a3,a2,a1,a0],[b3,b2,b1,b0],[q3,q2,q1,q0],[p3,p2,p1,p0],
[r3,r2,r1,r0],t3,t2,t1,t0,z3,z2,z1,z0)
that satisfies the constraints in Eq. 11-14, along with constraints that ensures
p=2Tp
,
(15)p0+p0+0=2B(16)p1+p1+1=2B(17)p2+p2+1=2B(18)p3+p3+1=2B

it is guaranteed that

abqpr=0mod2T (but not
abqpr=0
on their own).

We finally note that constraints from the previous section in Eq. 1-6 can be written in the

B-bit limb form as well, to match the constraints in this section.


A different version from the original post: The above is our version of how we would do this constraint decomposition.

A different version of this that combines a pair of limbs

[r3,r2] and
[r1,r0]
into the constraints is given below (the original formulation in the blog post that this note is based on).

u0=(t12B+t0)(r12B+r0)u0+0=v022Bu1=(t32B+t2)(r32B+r2)u1+v0=v122B

In the image below, we demostrate this idea.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

It is hard to tell what we gain from this particular formulation, which was given in the original post instead of our formulation above. There are

4 constraints in each case and in the latter, there are multiplications with
22B
which makes the numbers such as
u0
and
u1
quite a bit larger. We believe our formutation should be more efficient but it is difficult to be certain.


Applying the Chinese Remainder Theorem

We have shown constraints that allow us to restrict

(a,b,q,p,r) such that
abqpr=0modn
and
abqpr=0mod2T
. However, these do not guarantee that
abqpr=0
without the modulo as
abqpr=0modn
is valid when
abqpr
is
n
and a multiple of
n
. Similarly,
abqpr2T
and is a multiple of
2T
is allowed (this is a distinct possibility since
ab[0,p22252)
). Therefore, individually, these constraints do not ensure
abqpr=0
.

However, since

n and
2T
are coprimes (
n
is a prime
>2
), i.e.
gcd(n,2T)=1
, we can invoke the constant-case CRT, which states,
abqpr=0modnabqpr=0mod2Tabqpr=0mod(n2T)
Therefore, satisfying these constraints jointly means that
abqpr=0mod(n2T)
is also satisfied. Does this guarantee that
abqpr=0
? Not necessarily, as it could be that
abqprn2T
and is a multiple of
n2T
. However, if we chose
(n,T)
large enough s.t. this is not possible, then satisfying these constraints jointly will mean that
abqpr=0
.

In order to accomplish this, we need to enforce

abqpr<n2T by choosing
(n,T)
s.t.
p2<n2T
, as we already know the allowed ranges of variables,
a,b,q,r[0,p)
. We know that
n<p
. Therefore, it must be true that
2T>p
. Therefore,
T
must be large enough to satisfy
p2<n2T
but at the same time, it needs to be small enough that
B=T/4
is sufficiently small to justify the multitude of constraints that involve numbers in the range
[0,2B)
.