Try   HackMD

Digital signatures are crpytographic schemes that provide message authentication (the receiver can verify the origin of the message), integrity (the receiver can verify that the message has not been modified since it was signed) and non-repudiation (the sender cannot falsely claim that they have not signed the message).

In this short note, we discuss arithmetic circuit constraints that ensures that a witness, a set of inputs and outputs, correctly computes (or are related to each other through) the Digital Signature Algorithm (DSA) and its more advanced version that uses elliptic curves. Both algorithms are based on groups of known order and the discrete logarithm (DL) security assumptions.

A signature scheme has three parts, the setup algorithm which involves private and public key generation, the signature algorithm itself, and a verification algorithm for checking if a signature is authentic.

The verification algorithm is the only part of the scheme that is public and is typically the only part that is of importance for circuit constraints. For example, the verification algorithm for DSA appears in smart contracts executed in the Ethereum Virtual Machine. The veracity of the inputs and outputs of this verification algorithm can be shown by encoding a set of contraints into a circuit used to prove a larger set of computations with a ZK-SNARK.

We will introduce the two algorithms, DSA and Elliptic-curve DSA in turn and briefly discuss the circuit constraints associated with each.


Digital Signature Algorithm (DSA)

Setup Algorithm: The setup algorithm generates a private key known only to the sender (or signee) and a corresponding public key. Since the setup algorithm is privately computed and not really relevant to the verification process, it is possible to skip this section, which only provides background information on why DSA works.

The algorithm goes as follows. Security parameters

(p,q) are chosen and publically shared.
p
is a prime of size
>1024
bits and
q
is a prime of size
>160
bits chosen in such a way that
p1=cq
for a particular
c>0
. The reason for this contraint is as follows.

The signatures created by DSA will be utilizing a cyclic subgroup

GZ/pZ× of prime order
q
with a generator
g
. Now the question of interest is how to find such a generator.

Z/pZ× is the multiplicative group (with non-prime order
p1
) of the prime field
Fp
, which will act as the base field. Any subgroups of this multiplicative group have orders that can divide
p1
due to Lagrange's theorem and are cyclic (all subgroups of a cyclic group are cyclic).

We need

gq=1 mod p. Since
q
is prime and have no divisors except
1
and itself, a
g
that satisfies this equation would be a generator of a subgroup of order
q
or would be the identity element
1
. We can generate such a
g
by considering a random element
hUnif({2,...,q2})
in
Fp
. Then, we can consider the candidate
g~=hc mod p
, as
g~q=hcq=hp1=1 mod p
for any
h
, due to Fermat's little theorem. Therefore,
g~
is either
1
or a generator of the cyclic subgroup
G
and we can find other candidates if we end up with
g~=1
.

(p,q,g) are determined through this process. Once these are determined, we can operate within
G
by raising the generator to
i[0,1,2,...,q1]
to obtain the sequence
[1,g,g2,...,gq1]
, which is the
Z/qZ+
isomorphism. Note that the group operation within the subgroup
G
is
gi mod p
and not
gi mod q
.

Based on all this setup, the sender choses a private key

kpr{2,...,q1} to keep hidden and computes a corresponding public key from the subgroup
G
,
kpub=gkprmodp
, which gets published along with the generator
g
. In addition to these, a cryptographic hash function
H
is chosen and published.

Overall, the setup algorithm executed by the sender publishes

(p,q,kpub,g,H) and keeps hidden
kpr
.


Signature Algorithm: Similar to the setup algorithm, the signature algorithm itself is not publically computed and therefore is not typically a subject of circuit constraints. The section can be skipped, as it only explains how the signatures are created by the sender.

DSA is a stochastic signature scheme and therefore, each time a signature is generated for a particular fixed message

m, the signature is different. This requires a source of randomness,
πUnif({1,...,q1})
.

Then, a one-time random key

r is created by trying a number of
π
until
r0
(few tries are needed) which are related through the following equation.
r=r~ mod q=(gπ mod p) mod q
Note that
r~=gπ mod p
is an element of
G
. The exponentiation in this computation is the most computationally expensive part of the signing operation, but it may be computed before the message is known, which is an advantage of the scheme. We also need to calculate
π1=πq2 mod q
, using the Extended Euclidean algorithm (EEA), which is the second most expensive operation.

In order to generate the signature for a given message

m, which can be of variable length, we first hash it to a fixed-size digest,
H(m)
. This digest, along with the randomness variables
π1
,
r
and the public key
kpr
, is then used to compute the following,
σ=(π1(H(m)+rkpr)) mod q.
Then, the triplet
(m,r,σ)
is published as the signed message. Note that the message-sender pair
(m,kpub)
is now interlocked to the signature pair
(r,σ)
through this signed message.


Verification Algorithm: The verification algorithm is the only publically computed part of the scheme and is of great interest to proof systems. In this section, we will give a description and then discuss the circuit constraints in the next section.

Given a signed message

(m,r,σ) and the public variables
(p,q,g,kpub)
, verification process is centered around checking a relation between these variables that is efficiently computable. As a preliminary, the algorithm first checks
0<r
,
σ<q
, and then computes
σ1=σq2 mod q
(usually using EEA for efficiency).

Based on how

σ is computed in the signature algorithm, we can obtain the following candidate relation that could be useful for verification,
σ=(π1(H(m)+rkpr)) mod qπσ=(H(m)+rkpr) mod qπ=(H(m)σ1+rkprσ1) mod q.
However, the verification algorithm does not know the private key
kpr
to be able to check this relationship. On the other hand, the subgroup encrypted version of this relationship,
gπ mod p=g(H(m)σ1+rkprσ1) mod q mod p
is much more useful, as it replaces the private key
kpr
with the public key
kpub
if the correct set of simplification steps take place (note that
 mod q
can be removed, as the subgroup is known to have an order
q
):
gπ mod p=g(H(m)σ1+rkprσ1) mod q mod p=g(H(m)σ1+rkprσ1) mod p=gH(m)σ1grkprσ1 mod pr~=gH(m)σ1kpubrσ1 mod p.
This could further be improved to,
r=r~ mod q=(gH(m)σ1kpubrσ1 mod p) mod q.
Based on the above,
(m,r,σ1,kpub)
are interlocked into this relationship through the generator
g
, the hash function
H
and the
(p,q)
pair, which is checked by the verification algorithm to confirm that the signature
(r,σ)
matches the message-sender pair
(m,kpub)
.


Circuit constraints: Finally, we discuss the circuit constraints that replicate the verification algorithm in proving that the signature in a signed message

(r,σ) matches the message-sender pair
(m,kpub)
.

As it is the case in most arithmetic circuits, this is accomplished by breaking down the verification check equation into a set of simpler arithmetic equations with the use of auxiliary advice variables, provided as inputs to the circuit, that need to match intermediate computations.

These advice variables are, the output of the hash function

Hm, the inverse of
σ
variable (modulo
q
)
σinv
, quotients for modulo operations
κinv,κp,κq
, and various intermediate computations
α,β,γ,θ
.

Given a witness, inputs + advice variables,

p,q,kpub,g[public variables]m,r,σ[signed message]Hm,σinv,κinv,κp,κq,α,β,γ,θ[advice variables] the following set of circuit constraints can be used to verify the authenticity of a signed message,
Hm=H(m)[Using circuit constraints specific to hash function]σσinv=1+κinvqα=Hmσinvβ=rσinvγ=gα[By decomposing it into log2α intermediate constraints]θ=kpubβ[By decomposing it into log2β intermediate constraints]r=γθ+κpp+κqq
Exponentiations are handled by decomposing the constraint into many smaller constraints that build upon, e.g.
g2=gg
and
g4=g2g2
and so on until some
γ=gα/2gα/2
is reached, similar to the square multiply algorithm used in efficient group exponentiation. There are logarithmic number (in the exponent) of such constraints needed.

In general, doing all this efficiently requires decomposing single constraints with large integers in them, into multiple constraints with smaller integers, as the integers inputted to these constraints can be huge due to security requirements of DSA. This is a topic we explore in another note focused on efficient large number arithmetic circuits.


Elliptic Curve DSA (ECDSA)

Setup Algorithm: Elliptic Curve DSA is identical to DSA, except that it changes the subgroup used. It replaces the prime order

q subgroup of the multiplicative group of the field
Fp
with an elliptic curve
G
constructed on
Fp×Fp
. Much of the three algorithms remain the same. Security parameters are again
(p,q)
and
q
is the order of the elliptic curve and the generator of the curve is
g
.

In the same manner as DSA, the private key is chosen from

kpr{2,...,q1} and the public key is computed using the curve group operation according to
kpub=gkpr
. We will use the multiplicative notation for the elliptic curve to match the earlier discussion of DSA, even though an additive notation is more common for elliptic curves.


Signature Algorithm: The source of randomness,

πUnif({1,...,q1}), is the same as DSA. However,
r~G
is now a curve point calculated as
r~=gπ
.

In DSA, the one-time random key

r was a digest of
r~
with some bits of information lost through a smaller modulo operation, when
r~
was in a larger field of order
p>>q
. Similarly,
r
in ECDSA is calculated as a digest from the curve point
r~
by taking its
x
coordinate
r~x
and then passing it through modulo
q
,
r=r~x mod q=(gπ)x mod q

Again, the group operations (exponentiation) in this computation is the most computationally expensive part of the signing operation, but it may be computed before the message is known.

π needs to be randomly chosen such that
r1
, the identity element of the elliptic curve at infinity.

The inverse

π1 and the signature
σ
are computed exactly in the same way as DSA,
σ=(π1(H(m)+rkpr)) mod q.
The signed message
(m,r,σ)
is then published.


Verification Algorithm: Once again, as a preliminary, the algorithm checks that

0<r,σ<q, and then computes
σ1=σq2 mod q
(usually using EEA for efficiency).

It is still the case that

π=(H(m)σ1+rkprσ1) mod q. Again, the verifier does not know the private key
kpr
to be able to check this. Similarly, since
g
is the generator of
G
with order
q
, the group encrypted version of this relationship
gπ=g(H(m)σ1+rkprσ1) mod q
is the same as
g(H(m)σ1+rkprσ1)
, and is much more useful, as it replaces the private key
kpr
with the public key
kpub
if the correct set of simplification steps take place.
r~=gπ=g(H(m)σ1+rkprσ1) mod q=g(H(m)σ1+rkprσ1)=gH(m)σ1grkprσ1r~=gH(m)σ1kpubrσ1
This could further be improved to,
r=r~x mod q=(gH(m)σ1kpubrσ1)x mod q.
which interlocks
(m,r,σ1,kpub)
to each other. Verifying this relationship is enough to verify that the signature
(r,σ)
matches the message/sender pair
(m,kpub)
, which can be done efficiently.

As we can see in all of these three algorithm, ECDSA is almost exactly the same as DSA except that the subgroup used is changed from a modulo group to an elliptic curve (digest operation to compute the one-time key

r also has a slight change).


Circuit constraints: Again, we discuss the circuit constraints that replicate the verification algorithm in proving that the signature in a signed message

(r,σ) matches the message-sender pair
(m,kpub)
.

The advice variables are, the output of the hash function

Hm, the inverse of
σ
variable (modulo
q
)
σinv
, quotients for modulo operations
κinv,κq
, the coordinates
r~x,r~y
and various intermediate computations
α,β,γ,θ
.

Given a witness, inputs + advice variables,

p,q,kpub,g[public variables]m,r,σ[signed message]Hm,σinv,κinv,κq,r~x,r~y,α,β,γ,θ[advice variables] the following set of circuit constraints can be used to verify the authenticity of a signed message,
Hm=H(m)[Using circuit constraints specific to hash function]σσinv=1+κinvqα=Hmσinvβ=rσinvγ=gα[By decomposing it into log2α intermediate constraints]θ=kpubβ[By decomposing it into log2β intermediate constraints](r~x,r~x)=γθr=r~x+κqq
Now, multiplications in the constraints with
α,β,γ,θ
and
γθ
are no longer integer constraints but elliptic curve constraints, which will have to be expanded to the specific integer computations involved in the group operation of the curve.

Aside from that, the only difference between these constraints and those of DSA is the addition of

(r~x,r~x) as advice variables that represent the coordinates of the elliptic curve point corresponding to
r~
and the removal of the quotients related to modulo
p
.