1
0
mirror of https://github.com/bitcoinbook/bitcoinbook synced 2024-11-22 08:08:11 +00:00
bitcoinbook/ch08_signatures.adoc

1035 lines
54 KiB
Plaintext
Raw Normal View History

2023-09-12 01:22:28 +00:00
[[c_signatures]]
== Digital Signatures
Two ((("digital signatures", "schnorr signature algorithm")))((("schnorr signature algorithm")))((("digital signatures", "ECDSA")))((("ECDSA (Elliptic Curve Digital Signature Algorithm)")))((("transactions", "signatures", see="digital signatures")))signature algorithms are currently
2023-09-12 01:22:28 +00:00
used in Bitcoin, the _schnorr signature algorithm_ and the _Elliptic
Curve Digital Signature Algorithm_ (_ECDSA_).
These algorithms are used for digital signatures based on elliptic
curve private/public key pairs, as described in <<elliptic_curve>>.
They are used for spending segwit v0 P2WPKH outputs, segwit v1 P2TR
keypath spending, and by the script functions +OP_CHECKSIG+,
+OP_CHECKSIGVERIFY+, +OP_CHECKMULTISIG+, [.keep-together]#+OP_CHECKMULTISIGVERIFY+,# and
2023-09-12 01:22:28 +00:00
+OP_CHECKSIGADD+.
Any time one of those is executed, a signature must be
provided.
A digital signature((("digital signatures", "purpose of"))) serves
2023-09-12 01:22:28 +00:00
three purposes in Bitcoin. First, the
signature proves that the controller of a private key, who is by
implication the owner of the funds, has _authorized_ the spending of
those funds. Secondly, the proof of authorization is _undeniable_
(nonrepudiation). Thirdly, that the authorized transaction cannot be
changed by unauthenticated third parties--that its _integrity_ is
intact.
[NOTE]
====
Each transaction input and any signatures it may contain is _completely_
independent of any other input or signature. Multiple parties can
collaborate to construct transactions and sign only one input each.
2023-10-03 13:25:59 +00:00
Several protocols use this fact to create multiparty transactions for
2023-09-12 01:22:28 +00:00
privacy.
====
In this chapter we look at how digital signatures work and how they can
present proof of control of a private key without revealing that private
key.
[role="less_space pagebreak-before"]
2023-09-12 01:22:28 +00:00
=== How Digital Signatures Work
A digital signature
consists of two parts. The first part is an algorithm for creating a
signature for a message (the transaction) using a private key (the
signing key). The second part is an algorithm
that allows anyone to verify the signature, given also the message and the corresponding
public key.
2023-10-03 13:25:59 +00:00
==== Creating a Digital Signature
2023-09-12 01:22:28 +00:00
In Bitcoin's((("digital signatures", "creating"))) use of digital signature algorithms, the "message" being
2023-09-12 01:22:28 +00:00
signed is the transaction, or more accurately a hash of a specific
subset of the data in the transaction, ((("commitment hash")))called the _commitment hash_ (see
2023-09-12 01:22:28 +00:00
<<sighash_types>>). The
signing key is the user's private key. The result is the signature:
2023-11-01 16:17:32 +00:00
[latexmath]
++++
\begin{equation}
2023-11-01 17:29:40 +00:00
Sig = F_{sig}(F_{hash}(m), x)
2023-11-01 16:17:32 +00:00
\end{equation}
++++
2023-09-12 01:22:28 +00:00
where:
* _x_ is the signing private key
* _m_ is the message to sign, the commitment hash (such as parts of a transaction)
* _F_~_hash_~ is the hashing function
* _F_~_sig_~ is the signing algorithm
* _Sig_ is the resulting signature
2023-10-03 13:25:59 +00:00
You can find more details on the mathematics of schnorr and ECDSA signatures in <<schnorr_signatures>>
2023-09-12 01:22:28 +00:00
and <<ecdsa_signatures>>.
In both schnorr and ECDSA signatures, the function _F_~_sig_~ produces a signature +Sig+ that is composed of
two values. There are differences between the two values in the
different algorithms, which we'll explore later. After the two values
2023-10-03 13:25:59 +00:00
are calculated, they are serialized into a byte stream. For ECDSA
2023-09-12 01:22:28 +00:00
signatures, the encoding uses an international standard encoding scheme
called the
_Distinguished Encoding Rules_, or _DER_. For schnorr signatures, a
simpler serialization format is used.
==== Verifying the Signature
The((("digital signatures", "verifying")))((("verifying", "digital signatures"))) signature verification algorithm takes the message (a hash of parts of the transaction and related data), the signer's public key and the signature, and returns ++TRUE++ if the signature is valid for this message and public key.
2023-09-12 01:22:28 +00:00
To verify the signature, one must have the signature, the serialized
transaction, some data about the output being spent, and the public key
that corresponds to the private key used to create the signature.
Essentially, verification of a signature means "Only the controller of
the private key that generated this public key could have produced this
signature on this transaction."
[[sighash_types]]
==== Signature Hash Types (SIGHASH)
Digital signatures((("digital signatures", "SIGHASH flags", id="digital-signature-sighash")))((("SIGHASH flags", id="sighash"))) apply to messages,
2023-09-12 01:22:28 +00:00
which in the case of Bitcoin, are the transactions themselves. The
2023-10-23 15:18:30 +00:00
signature proves a _commitment_ by the signer to specific transaction
2023-09-12 01:22:28 +00:00
data. In the simplest form, the signature applies to almost the entire
transaction, thereby committing to all the inputs, outputs, and other
transaction fields. However, a signature can commit to only a subset of
the data in a transaction, which is useful for a number of scenarios as
we will see in this section.
Bitcoin signatures have a way of indicating which
part of a transaction's data is included in the hash signed by the
private key using a +SIGHASH+ flag. The +SIGHASH+ flag is a single byte
that is appended to the signature. Every signature has either an
2023-10-23 15:18:30 +00:00
explicit or implicit +SIGHASH+ flag,
2023-09-12 01:22:28 +00:00
and the flag can be different from input to input. A transaction with
three signed inputs may have three signatures with different +SIGHASH+
flags, each signature signing (committing) to different parts of the
transaction.
Remember, each input may contain one or more signatures. As
a result, an input may have signatures
with different +SIGHASH+ flags that commit to different parts of the
transaction. Note also that Bitcoin transactions
may contain inputs from different "owners," who may sign only one input
in a partially constructed transaction, collaborating with
others to gather all the necessary signatures to make a valid
transaction. Many of the +SIGHASH+ flag types only make sense if you
think of multiple participants collaborating outside the Bitcoin network
and updating a partially signed transaction.
There are three +SIGHASH+ flags: +ALL+, +NONE+, and +SINGLE+, as shown
in <<sighash_types_and_their>>.
2023-10-11 19:29:37 +00:00
++++
<table id="sighash_types_and_their">
<caption>
<span class="plain"><code>SIGHASH</code></span> types and their meanings</caption>
<thead>
<tr>
<th><code>SIGHASH</code> flag</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><p><code>ALL</code></p></td>
<td><p><code>0x01</code></p></td>
<td><p>Signature applies to all inputs and outputs</p></td>
</tr>
<tr>
<td><p><code>NONE</code></p></td>
<td><p><code>0x02</code></p></td>
<td><p>Signature applies to all inputs, none of the outputs</p></td>
</tr>
<tr>
<td><p><code>SINGLE</code></p></td>
<td><p><code>0x03</code></p></td>
<td><p>Signature applies to all inputs but only the one output with the same index number as the signed input</p></td>
</tr>
</tbody>
</table>
++++
2023-09-12 01:22:28 +00:00
2023-10-03 13:25:59 +00:00
In addition, there is a modifier flag, +SIGHASH_ANYONECANPAY+, which can
2023-09-12 01:22:28 +00:00
be combined with each of the preceding flags. When +ANYONECANPAY+ is
set, only one input is signed, leaving the rest (and their sequence
numbers) open for modification. The +ANYONECANPAY+ has the value +0x80+
and is applied by bitwise OR, resulting in the combined flags as shown
in <<sighash_types_with_modifiers>>.
[role="less_space pagebreak-before"]
2023-10-11 19:29:37 +00:00
++++
<table id="sighash_types_with_modifiers">
<caption>
<span class="plain"><code>SIGHASH</code></span> types with modifiers and their meanings</caption>
<thead>
<tr>
<th><code>SIGHASH</code> flag</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><p><code>ALL|ANYONECANPAY</code></p></td>
<td><p><code>0x81</code></p></td>
<td><p>Signature applies to one input and all outputs</p></td>
</tr>
<tr>
<td><p><code>NONE|ANYONECANPAY</code></p></td>
<td><p><code>0x82</code></p></td>
<td><p>Signature applies to one input, none of the outputs</p></td>
</tr>
<tr>
<td><p><code>SINGLE|ANYONECANPAY</code></p></td>
<td><p><code>0x83</code></p></td>
<td><p>Signature applies to one input and the output with the same index number</p></td>
</tr>
</tbody>
</table>
++++
2023-09-12 01:22:28 +00:00
The way +SIGHASH+ flags are applied during signing and verification is
that a copy of the transaction is made and certain fields within are
either omitted or truncated (set to zero length and emptied). The resulting transaction is
serialized. The +SIGHASH+ flag is included in the serialized
transaction data and the result is hashed. The hash digest itself is the "message"
that is signed. Depending on which +SIGHASH+ flag is used, different
parts of the transaction are included.
By including the
+SIGHASH+ flag itself, the signature commits the
+SIGHASH+ type as well, so it can't be changed (e.g., by a miner).
In
<<serialization_of_signatures_der>>, we will see that the last part of the
DER-encoded signature was +01+, which is the +SIGHASH_ALL+ flag for ECDSA signatures. This
locks the transaction data, so Alice's signature is committing to the state
of all inputs and outputs. This is the most common signature form.
Let's look at some of the other +SIGHASH+ types and how they can be used
in practice:
+ALL|ANYONECANPAY+ :: This ((("crowdfunding")))construction can be used to make a
2023-09-12 01:22:28 +00:00
"crowdfunding&#x201d;-style transaction. Someone attempting to raise
funds can construct a transaction with a single output. The single
output pays the "goal" amount to the fundraiser. Such a transaction is
obviously not valid, as it has no inputs. However, others can now amend
2023-10-23 15:18:30 +00:00
it by adding an input of their own as a donation. They sign their own
2023-09-12 01:22:28 +00:00
input with +ALL|ANYONECANPAY+. Unless enough inputs are gathered to
reach the value of the output, the transaction is invalid. Each donation
is a "pledge," which cannot be collected by the fundraiser until the
entire goal amount is raised. Unfortunately, this protocol can be
circumvented by the fundraiser adding an input of their own (or from
someone who lends them funds), allowing them to collect the donations
even if they haven't reached the specified value.
+NONE+ :: This construction can be used to create a "bearer check" or
2023-10-23 15:18:30 +00:00
"blank check" of a specific amount. It commits to all inputs but allows
2023-09-12 01:22:28 +00:00
the outputs to be changed. Anyone can write their own
Bitcoin address into the output script.
By itself, this allows any miner to change
the output destination and claim the funds for themselves, but if other
required signatures in the transaction use +SIGHASH_ALL+ or another type
that commits to the output, it allows those spenders to change the
2023-10-03 13:25:59 +00:00
destination without allowing any third parties (like miners) to modify
2023-09-12 01:22:28 +00:00
the outputs.
+NONE|ANYONECANPAY+ :: This construction can be used to build a "dust
collector." Users who have tiny UTXOs in their wallets can't spend these
2023-10-03 13:25:59 +00:00
without the cost in fees exceeding the value of the UTXO; see
2023-09-12 01:22:28 +00:00
<<uneconomical_outputs>>. With this type
of signature, the uneconomical UTXOs can be donated for anyone to aggregate and
spend whenever they want.
There are some proposals to modify or
expand the +SIGHASH+ system. The most widely discussed proposal as of
this writing is ((("BIP118 SIGHASH flags")))BIP118, which proposes to add two
2023-09-12 01:22:28 +00:00
new sighash flags. A signature using +SIGHASH_ANYPREVOUT+ would not
commit to an input's outpoint field, allowing it to be used to spend any
previous output for a particular witness program. For example, if Alice
receives two outputs for the same amount to the same witness program
2023-10-03 13:25:59 +00:00
(e.g., requiring a single signature from her wallet), a
2023-09-12 01:22:28 +00:00
+SIGHASH_ANYPREVOUT+ signature for spending either one of those outputs
could be copied and used to spend the other output to the same
destination.
A signature using +SIGHASH_ANYPREVOUTANYSCRIPT+ would not
commit to the outpoint, the amount, the witness program, or the
2023-10-03 13:25:59 +00:00
specific leaf in the taproot merkle tree (script tree), allowing it to spend any previous output that the signature could satisfy. For example, if Alice received two
outputs for different amounts and different witness programs (e.g., one
2023-09-12 01:22:28 +00:00
requiring a single signature and another requiring her signature plus some
other data), a +SIGHASH_ANYPREVOUTANYSCRIPT+ signature for spending
either one of those outputs could be copied and used to spend the other
output to the same destination (assuming the extra data for the second
output was known).
2023-10-03 13:25:59 +00:00
The main expected use for the two ++SIGHASH_ANYPREVOUT++ opcodes is improved
2023-10-25 12:26:17 +00:00
payment channels, such as those used in the Lightning Network (LN), although
2023-09-12 01:22:28 +00:00
several other uses have been described.
[NOTE]
====
You will not often see +SIGHASH+ flags presented as an option in a user's
wallet application. Simple wallet applications
2023-10-03 13:25:59 +00:00
sign with [.keep-together]#+SIGHASH_ALL+# flags. More sophisticated applications, such as
2023-10-25 12:26:17 +00:00
LN nodes, may use alternative +SIGHASH+ flags, but they
2023-09-12 01:22:28 +00:00
use protocols that have been extensively reviewed to understand the
influence of the alternative ((("digital signatures", "SIGHASH flags", startref="digital-signature-sighash")))((("SIGHASH flags", startref="sighash")))flags.
2023-09-12 01:22:28 +00:00
====
[[schnorr_signatures]]
2023-10-03 13:25:59 +00:00
=== Schnorr Signatures
2023-09-12 01:22:28 +00:00
In 1989, ((("digital signatures", "schnorr signature algorithm", id="digital-sigs-schnorr")))((("schnorr signature algorithm", id="schnorr")))Claus Schnorr published a paper describing the signature
2023-09-12 01:22:28 +00:00
algorithm that's become eponymous with him. The algorithm isn't
2023-10-03 13:25:59 +00:00
specific to the elliptic curve cryptography (ECC) that Bitcoin and many
2023-09-12 01:22:28 +00:00
other applications use, although it is perhaps most strongly associated
with ECC today. Schnorr signatures have a number of nice properties:
Provable security::
A mathematical ((("digital signatures", "schnorr signature algorithm", "properties of")))((("schnorr signature algorithm", "properties of")))proof of the security of schnorr signatures depends on
2023-09-12 01:22:28 +00:00
only the difficulty of solving the Discrete Logarithm Problem (DLP),
2023-10-03 13:25:59 +00:00
particularly for elliptic curves (EC) for Bitcoin, and the ability of
2023-09-12 01:22:28 +00:00
a hash function (like the SHA256 function used in Bitcoin) to produce
2023-10-03 13:25:59 +00:00
unpredictable values, called the random oracle model (ROM). Other
2023-09-12 01:22:28 +00:00
signature algorithms have additional dependencies or require much
larger public keys or signatures for equivalent security to
ECC-Schnorr (when the threat is defined as classical computers; other
algorithms may provide more efficient security against quantum
computers).
Linearity::
Schnorr signatures have a property that mathematicians ((("linearity")))call
2023-09-12 01:22:28 +00:00
_linearity_, which applies to functions with two particular
properties. The first property is that summing together two or more
variables and then running a function on that sum will produce the
same value as running the function on each of the variables
2023-10-03 13:25:59 +00:00
independently and then summing together the results, e.g.,
2023-10-25 14:20:58 +00:00
_f(x_ + _y_ + _z)_ == _f(x)_ + _f(y)_ + _f(z)_; this property is((("additivity"))) called
2023-09-12 01:22:28 +00:00
_additivity_. The second property is that multiplying a variable and
then running a function on that product will produce the same value as
running the function on the variable and then multiplying it by the
2023-10-25 14:20:58 +00:00
same amount, e.g., _f(a_ × _x)_ == _a_ × _f(x)_; this property is ((("homogeneity of degree 1")))called
2023-09-12 01:22:28 +00:00
_homogeneity of degree 1_.
+
In cryptographic operations, some functions may be private (such
as functions involving private keys or secret nonces), so being able
to get the same result whether performing an operation inside or
outside of a function makes it easy for multiple parties to coordinate
and cooperate without sharing their secrets. We'll see some of the
specific benefits of linearity in schnorr signatures in
<<schnorr_multisignatures>> and <<schnorr_threshold_signatures>>.
2023-10-03 13:25:59 +00:00
Batch verification::
When used((("batch verification of digital signatures"))) in a certain way (which Bitcoin does), one consequence of
2023-09-12 01:22:28 +00:00
schnorr's linearity is that it's relatively straightforward to verify
more than one schnorr signature at the same time in less time than it
would take to verify each signature independently. The more
signatures that are verified in a batch, the greater the speed up.
For the typical number of signatures in a block, it's possible to
batch verify them in about half the amount of time it would take to
verify each signature independently.
Later in this chapter, we'll describe the schnorr signature algorithm
exactly as it's used in Bitcoin, but we're going to start with a
2023-10-03 13:25:59 +00:00
simplified version of it and work our way toward the actual protocol in
2023-09-12 01:22:28 +00:00
stages.
[role="less_space pagebreak-before"]
2023-10-25 14:20:58 +00:00
Alice((("digital signatures", "schnorr signature algorithm", "examples of usage")))((("schnorr signature algorithm", "examples of usage"))) starts by choosing a large random number (_x_), which we call her
2023-10-03 13:25:59 +00:00
_private key_. She also knows a public point on Bitcoin's elliptic
2023-10-25 14:20:58 +00:00
curve called the Generator (_G_) (see <<public_key_derivation>>). Alice uses EC
multiplication to multiply _G_ by her private key _x_, in which case _x_
is called a _scalar_ because it scales up _G_. The result is _xG_,
2023-09-12 01:22:28 +00:00
which we call Alice's _public key_. Alice gives her public key to Bob.
2023-10-25 14:20:58 +00:00
Even though Bob also knows _G_, the DLP prevents Bob from being able to divide _xG_ by _G_ to derive Alice's
2023-09-12 01:22:28 +00:00
private key.
At some later time, Bob wants Alice to identify herself by proving
2023-10-25 14:20:58 +00:00
that she knows the scalar _x_ for the public key (_xG_) that Bob
received earlier. Alice can't give Bob _x_ directly because that would
2023-09-12 01:22:28 +00:00
allow him to identify as her to other people, so she needs to prove
2023-10-25 14:20:58 +00:00
her knowledge of _x_ without revealing _x_ to Bob,((("zero-knowledge proof"))) called a
2023-09-12 01:22:28 +00:00
_zero-knowledge proof_. For that, we begin the schnorr identity
process:
2023-10-25 14:20:58 +00:00
1. Alice chooses another large random number (_k_), which we call the
_private nonce_. Again she uses it as a scalar, multiplying it by _G_
to produce _kG_, which we call the _public nonce_. She gives the
2023-09-12 01:22:28 +00:00
public nonce to Bob.
2023-10-25 14:20:58 +00:00
2. Bob chooses a large random number of his own, _e_, which we call the
2023-09-12 01:22:28 +00:00
_challenge scalar_. We say "challenge" because it's used to challenge
2023-10-25 14:20:58 +00:00
Alice to prove that she knows the private key (_x_) for the public key
(_xG_) she previously gave Bob; we say "scalar" because it will later
2023-09-12 01:22:28 +00:00
be used to multiply an EC point.
2023-10-25 14:20:58 +00:00
3. Alice now has the numbers (scalars) _x_, _k_, and _e_. She combines
them together to produce a final scalar _s_ using the formula
_s_ = _k_ + _ex_. She gives _s_ to Bob.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
4. Bob now knows the scalars _s_ and _e_, but not _x_ or _k_. However,
Bob does know _xG_ and _kG_, and he can compute for himself _sG_ and
_exG_. That means he can check the equality of a scaled-up version of
the operation Alice performed: [.keep-together]#_sG_ == _kG_ + _exG_.# If that is equal,
2023-10-25 14:20:58 +00:00
then Bob can be sure that Alice knew _x_ when she generated _s_.
2023-09-12 01:22:28 +00:00
2023-10-03 13:25:59 +00:00
.Schnorr Identity Protocol with Integers Instead of Points
2023-09-12 01:22:28 +00:00
****
It might be easier to understand the interactive schnorr identity
2023-10-25 14:20:58 +00:00
protocol if we create an insecure oversimplification by substituting each of the preceding values (including _G_) with simple integers instead of points on an elliptic curve.
2023-09-12 01:22:28 +00:00
For example, we'll use the prime numbers starting with 3:
2023-10-25 14:20:58 +00:00
Setup: Alice chooses _x_ = 3 as her private key. She multiplies it by the
generator _G_ = 5 to get her public key _xG_ = 15. She gives Bob 15.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
1. Alice chooses the private nonce _k_ = 7 and generates the public nonce
_kG_ = 35. She gives Bob 35.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
2. Bob chooses _e_ = 11 and gives it to Alice.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
3. Alice generates _s_ = 40 = 7 + 11 × 3. She gives Bob 40.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
4. Bob derives _sG_ = 200 = 40 × 5 and _exG_ = 165 = 11 × 15. He then
verifies that [.keep-together]#200 == 35 + 165.# Note that this is the same operation
2023-10-25 14:20:58 +00:00
that Alice performed, but all of the values have been scaled up by 5
(the value of _G_).
2023-09-12 01:22:28 +00:00
Of course, this is an oversimplified example. When working with simple
2023-10-25 14:20:58 +00:00
integers, we can divide products by the generator _G_ to get the
2023-09-12 01:22:28 +00:00
underlying scalar, which isn't secure. This is why a critical property
2023-10-03 13:25:59 +00:00
of the elliptic curve cryptography used in Bitcoin is that
2023-09-12 01:22:28 +00:00
multiplication is easy but division by a point on the curve is impractical. Also, with numbers
this small, finding underlying values (or valid substitutes) through
brute force is easy; the numbers used in Bitcoin are much larger.
****
Let's discuss some of the features of the interactive schnorr
identity protocol that make it secure:
2023-10-25 17:40:02 +00:00
The nonce (k)::
In step 1, ((("digital signatures", "schnorr signature algorithm", "security features")))((("schnorr signature algorithm", "security features")))Alice chooses a number that Bob doesn't
2023-09-12 01:22:28 +00:00
know and can't guess and gives him the scaled form of that number,
2023-10-25 14:20:58 +00:00
_kG_. At that point, Bob also already has her public key (_xG_),
2023-11-01 15:02:42 +00:00
which is the scaled form of _x_, her private key. That means when Bob is working on
2023-10-25 14:20:58 +00:00
the final equation (_sG_ = _kG_ + _exG_), there are two independent
variables that Bob doesn't know (_x_ and _k_). It's possible to use
2023-09-12 01:22:28 +00:00
simple algebra to solve an equation with one unknown variable but not
two independent unknown variables, so the presence of Alice's nonce
prevents Bob from being able to derive her private key. It's critical
to note that this protection depends on nonces being unguessable in
any way. If there's anything predictable about Alice's nonce, Bob may
be able to leverage that into figuring out Alice's private key. See
<<nonce_warning>> for more details.
2023-10-25 17:40:02 +00:00
The challenge scalar (e)::
2023-10-03 13:25:59 +00:00
Bob waits to receive Alice's public nonce
2023-09-12 01:22:28 +00:00
and then proceeds in step 2 to give her a number (the challenge
scalar) that Alice didn't previously know and couldn't have guessed.
It's critical that Bob only give her the challenge scalar after she
commits to her public nonce. Consider what could happen if someone
2023-10-25 14:20:58 +00:00
who didn't know _x_ wanted to impersonate Alice, and Bob accidentally
gave them the challenge scalar _e_ before they told him the public
nonce _kG_. This allows the impersonator to change parameters on both sides of
the equation that Bob will use for verification, _sG_ == _kG_ + _exG_;
specifically, they can change both _sG_ and _kG_. Think about a
simplified form of that expression: _x_ = _y_ + _a_. If you can change both
2023-10-25 18:59:03 +00:00
_x_ and _y_, you can cancel out _a_ using _x_++'++ = (_x_ _a_) + _a_. Any
2023-10-25 14:20:58 +00:00
value you choose for _x_ will now satisfy the equation. For the
actual equation the impersonator simply chooses a random number for _s_, generates
_sG_, and then uses EC subtraction to select a _kG_ that equals _kG_ =
2023-10-25 18:59:03 +00:00
_sG_ _exG_. They give Bob their calculated _kG_ and later their random
_sG_, and Bob thinks that's valid because [.keep-together]#_sG_ == (_sG_ _exG_)# + _exG_.
2023-09-12 01:22:28 +00:00
This explains why the order of operations in the protocol is
essential: Bob must only give Alice the challenge scalar after Alice
has committed to her public nonce.
++++
<p class="fix_tracking">
2023-09-12 01:22:28 +00:00
The interactive identity protocol described here matches part of Claus
Schnorr's original description, but it lacks two essential features we
need for the decentralized Bitcoin network. The first of these is that
it relies on Bob waiting for Alice to commit to her public nonce and
then Bob giving her a random challenge scalar. In Bitcoin, the spender
of every transaction needs to be authenticated by thousands of Bitcoin
full nodes—including future nodes that haven't been started yet but
2023-09-12 01:22:28 +00:00
whose operators will one day want to ensure the bitcoins they receive
came from a chain of transfers where every transaction was valid. Any
Bitcoin node that is unable to communicate with Alice, today or in the
future, will be unable to authenticate her transaction and will be in
disagreement with every other node that did authenticate it. That's not
acceptable for a consensus system like Bitcoin. For Bitcoin to work, we
need a protocol that doesn't require interaction between Alice and each
node that wants to authenticate her.
</p>
++++
2023-09-12 01:22:28 +00:00
A simple technique, known as the Fiat-Shamir transform after its
discoverers, can turn the schnorr interactive identity protocol
2023-10-03 13:25:59 +00:00
into a noninteractive digital signature scheme. Recall the importance
2023-09-12 01:22:28 +00:00
of steps 1 and 2--including that they be performed in order. Alice must
commit to an unpredictable nonce; Bob must give Alice an unpredictable
challenge scalar only after he has received her commitment. Recall also
the properties of secure cryptographic hash functions we've used
elsewhere in this book: it will always produce the same output when
given the same input but it will produce a value indistinguishable from
random data when given a different input.
This allows Alice to choose her private nonce, derive her public nonce,
and then hash the public nonce to get the challenge scalar. Because
Alice can't predict the output of the hash function (the challenge), and
because it's always the same for the same input (the nonce), this
ensures that Alice gets a random challenge even though she chooses the nonce
and hashes it herself. We no longer need interaction from Bob. She can
2023-10-25 14:20:58 +00:00
simply publish her public nonce _kG_ and the scalar _s_, and each of the
thousands of full nodes (past and future) can hash _kG_ to produce _e_,
use that to produce _exG_, and then verify _sG_ == _kG_ + _exG_. Written
explicitly, the verification equation becomes [.keep-together]#_sG_ == _kG_ + _hash_(_kG_) × _xG_.#
2023-09-12 01:22:28 +00:00
We need one other thing to finish converting the interactive schnorr
identity protocol into a digital signature protocol useful for
Bitcoin. We don't just want Alice to prove that she knows her private
key; we also want to give her the ability to commit to a message. Specifically,
we want her to commit to the data related to the Bitcoin transaction she
wants to send. With the Fiat-Shamir transform in place, we already
have a commitment, so we can simply have it additionally commit to the
2023-10-25 18:59:03 +00:00
message. Instead of _hash_(_kG_), we now also commit to the message
_m_ using _hash_(_kG_ || _m_), where || stands for concatenation.
2023-09-12 01:22:28 +00:00
We've now defined a version of the schnorr signature protocol, but
there's one more thing we need to do to address a Bitcoin-specific
concern. In BIP32 key derivation, as described in
<<public_child_key_derivation>>, the algorithm for unhardened derivation
2023-10-03 13:25:59 +00:00
takes a public key and adds to it a nonsecret value to produce a
2023-09-12 01:22:28 +00:00
derived public key. That means it's also possible to add that
2023-10-03 13:25:59 +00:00
nonsecret value to a valid signature for one key to produce a signature
2023-09-12 01:22:28 +00:00
for a related key. That related signature is valid but it wasn't
authorized by the person possessing the private key, which is a major
security failure. To protect BIP32 unhardened derivation and
also support several protocols people wanted to build on top of schnorr
signatures, Bitcoin's version of schnorr signatures, called _BIP340
schnorr signatures for secp256k1_, also commits to the public key being
used in addition to the public nonce and the message. That makes the
2023-10-25 18:59:03 +00:00
full commitment _hash_(_kG_ || _xG_ || _m_).
2023-09-12 01:22:28 +00:00
Now that we've described each part of the BIP340 schnorr signature
algorithm and explained what it does for us, we can define the protocol.
Multiplication of integers are performed _modulus p_, indicating that the
2023-11-01 15:02:42 +00:00
result of the operation is divided by the number _p_ (as defined in the
2023-09-12 01:22:28 +00:00
secp256k1 standard) and the remainder is used. The number _p_ is very
large, but if it was 3 and the result of an operation was 5, the actual
2023-11-01 15:02:42 +00:00
number we would use is 2 (i.e., 5 divided by 3 has a remainder of 2).
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
Setup: Alice chooses a large random number (_x_) as her private key
2023-09-12 01:22:28 +00:00
(either directly or by using a protocol like BIP32 to deterministically
generate a private key from a large random seed value). She uses the
parameters defined in secp256k1 (see <<elliptic_curve>>) to multiply the
2023-10-25 14:20:58 +00:00
generator _G_ by her scalar _x_, producing _xG_ (her public key). She
2023-09-12 01:22:28 +00:00
gives her public key to everyone who will later authenticate her Bitcoin
2023-10-25 14:20:58 +00:00
transactions (e.g., by having _xG_ included in a transaction output). When
2023-09-12 01:22:28 +00:00
she's ready to spend, she begins generating her signature:
2023-10-25 14:20:58 +00:00
1. Alice chooses a large random private nonce _k_ and derives the public
nonce _kG_.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
2. She chooses her message _m_ (e.g., transaction data) and generates the
2023-10-25 18:59:03 +00:00
challenge scalar _e_ = _hash_(_kG_ || _xG_ || _m_).
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
3. She produces the scalar _s_ = _k_ + _ex_. The two values _kG_ and _s_
2023-09-12 01:22:28 +00:00
are her signature. She gives this signature to everyone who wants to
verify that signature; she also needs to ensure everyone receives her
2023-10-25 14:20:58 +00:00
message _m_. In Bitcoin, this is done by including her signature in
2023-09-12 01:22:28 +00:00
the witness structure of her spending transaction and then relaying that
transaction to full nodes.
2023-10-25 14:20:58 +00:00
4. The verifiers (e.g., full nodes) use _s_ to derive _sG_ and then
2023-10-25 18:59:03 +00:00
verify that _sG_ == _kG_ + _hash_(_kG_ || _xG_ || _m_) × _xG_. If the equation is
2023-10-25 14:20:58 +00:00
valid, Alice proved that she knows her private key _x_ (without
revealing it) and committed to the message _m_ (containing the
2023-09-12 01:22:28 +00:00
transaction data).
2023-10-03 13:25:59 +00:00
==== Serialization of Schnorr Signatures
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
A schnorr signature ((("digital signatures", "schnorr signature algorithm", "serialization")))((("schnorr signature algorithm", "serialization")))((("serialization", "of schnorr signature algorithm", secondary-sortas="schnorr")))consists of two values, _kG_ and _s_. The value
_kG_ is a point on Bitcoin's elliptic curve (called secp256k1) and would normally be represented by two 32-byte coordinates, e.g., (_x_, _y_).
2023-10-03 13:25:59 +00:00
However, only the _x_ coordinate is needed, so only that value is
2023-10-25 14:20:58 +00:00
included. When you see _kG_ in schnorr signatures for Bitcoin, note that it's only that point's _x_
2023-09-12 01:22:28 +00:00
coordinate.
2023-10-25 14:20:58 +00:00
The value _s_ is a scalar (a number meant to multiply other numbers). For
2023-09-12 01:22:28 +00:00
Bitcoin's secp256k1 curve, it can never be more than 32 bytes long.
2023-10-25 14:20:58 +00:00
Although both _kG_ and _s_ can sometimes be values that can be
2023-09-12 01:22:28 +00:00
represented with fewer than 32 bytes, it's improbable that they'd be
2023-10-23 15:18:30 +00:00
much smaller than 32 bytes, so they're serialized as two 32-byte
2023-10-03 13:25:59 +00:00
values (i.e., values smaller than 32 bytes have leading zeros).
2023-10-25 14:20:58 +00:00
They're serialized in the order of _kG_ and then _s_, producing exactly
2023-09-12 01:22:28 +00:00
64 bytes.
The taproot soft fork, also called v1 segwit, introduced schnorr signatures
to Bitcoin and is the only way they are used in Bitcoin as of this writing. When
used with either taproot keypath or scriptpath spending, a 64-byte
schnorr signature is considered to use a default signature hash (sighash)
that is +SIGHASH_ALL+. If an alternative sighash is used, or if the
spender wants to waste space to explicitly specify +SIGHASH_ALL+, a
single additional byte is appended to the signature that specifies the
signature hash, making the signature 65 bytes.
As we'll see, either 64 or 65 bytes is considerably more efficient that
the serialization used for ECDSA signatures described in
<<serialization_of_signatures_der>>.
[[schnorr_multisignatures]]
2023-10-03 13:25:59 +00:00
==== Schnorr-based Scriptless Multisignatures
2023-09-12 01:22:28 +00:00
In the((("digital signatures", "schnorr signature algorithm", "scriptless multisignatures", id="digital-sigs-schnorr-multisig")))((("schnorr signature algorithm", "scriptless multisignatures", id="schnorr-multisig")))((("scriptless multisignatures", "in schnorr signature algorithm", secondary-sortas="schnorr", id="scriptless-multi-schnorr")))((("multisignature scripts", "in schnorr signature algorithm", secondary-sortas="schnorr", id="multi-script-schnorr"))) single-signature schnorr protocol described in <<schnorr_signatures>>, Alice
2023-10-25 14:20:58 +00:00
uses a signature (_kG_, _s_) to publicly prove her knowledge of her
private key, which in this case we'll call _y_. Imagine if Bob also has
a private key (_z_) and he's willing to work with Alice to prove that
together they know _x_ = _y_ + _z_ without either of them revealing their
2023-09-12 01:22:28 +00:00
private key to each other or anyone else. Let's go through the BIP340
schnorr signature protocol again.
[WARNING]
====
The simple protocol we are about to describe is not secure for the
reasons we will explain shortly. We use it only to demonstrate the
mechanics of schnorr multisignatures before describing related protocols
that are believed to be secure.
====
2023-10-25 14:20:58 +00:00
Alice and Bob need to derive the public key for _x_, which is _xG_.
2023-10-03 13:25:59 +00:00
Since it's possible to use elliptic curve operations to add two EC
2023-10-25 14:20:58 +00:00
points together, they start by Alice deriving _yG_ and Bob deriving
_zG_. They then add them together to create [.keep-together]#_xG_ = _yG_ + _zG_.# The point
2023-10-25 14:20:58 +00:00
_xG_ is ((("aggregated public keys")))((("public keys", "aggregated")))their _aggregated public key_. To create a signature, they begin the
2023-09-12 01:22:28 +00:00
simple multisignature protocol:
2023-10-25 14:20:58 +00:00
1. They each individually choose a large random private nonce, _a_ for
Alice and _b_ for Bob. They also individually derive the corresponding
public nonce _aG_ and _bG_. Together, they produce an aggregated
public nonce _kG_ = _aG_ + _bG_.
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
2. They agree on the message to sign, _m_ (e.g., a transaction), and
2023-10-25 18:59:03 +00:00
each generates a copy of the challenge scalar: _e_ = _hash_(_kG_ || _xG_ || _m_).
2023-09-12 01:22:28 +00:00
2023-10-25 14:20:58 +00:00
3. Alice produces the scalar _q_ = _a_ + _ey_. Bob produces the scalar
_r_ = _b_ + _ez_. They add the scalars together to produce
2023-11-01 15:02:42 +00:00
_s_ = _q_ + _r_. Their signature is the two values _kG_ [.keep-together]#and _s_.#
2023-09-12 01:22:28 +00:00
4. The verifiers check their public key and signature using the normal
2023-11-01 15:02:42 +00:00
equation: [.keep-together]#_sG_ ==# _kG_ + _hash_(_kG_ || _xG_ || _m_) × _xG_.
2023-09-12 01:22:28 +00:00
Alice and Bob have proven that they know the sum of their private keys without
either one of them revealing their private key to the other or anyone
2023-10-23 15:18:30 +00:00
else. The protocol can be extended to any number of participants (e.g.,
2023-09-12 01:22:28 +00:00
a million people could prove they knew the sum of their million
2023-10-23 15:18:30 +00:00
different keys).
2023-09-12 01:22:28 +00:00
2023-10-03 13:25:59 +00:00
The preceding protocol has several security problems. Most notable is that one
2023-09-12 01:22:28 +00:00
party might learn the public keys of the other parties before committing
to their own public key. For example, Alice generates her public key
2023-10-25 14:20:58 +00:00
_yG_ honestly and shares it with Bob. Bob generates his public key
using _zG_ _yG_. When their two keys are combined [.keep-together]#(_yG_ + _zG_ _yG_),# the
2023-10-25 14:20:58 +00:00
positive and negative _yG_ terms cancel out so the public key only represents
the private key for _z_ (i.e., Bob's private key). Now Bob can create a
valid signature without any assistance from Alice. This is ((("key cancellation attacks")))called a
2023-09-12 01:22:28 +00:00
_key cancellation attack_.
There are various ways to solve the key cancellation attack. The
simplest scheme would be to require each participant commit to their
part of the public key before sharing anything about that key with all
of the other participants. For example, Alice and Bob each individually
hash their public keys and share their digests with each other. When
they both have the other's digest, they can share their keys. They
individually check that the other's key hashes to the previously
provided digest and then proceed with the protocol normally. This prevents
either one of them from choosing a public key that cancels out the keys
of the other participants. However, it's easy to fail to implement this
scheme correctly, such as using it in a naive way with unhardened
BIP32 public key derivation. Additionally, it adds an extra step for
communication between the participants, which may be undesirable in many
cases. More complex schemes have been proposed that address these
shortcomings.
In addition to the key cancellation attack, there are a number of
attacks possible against ((("nonce attacks")))nonces. Recall that the purpose of the nonce
2023-09-12 01:22:28 +00:00
is to prevent anyone from being able to use their knowledge of other values
in the signature verification equation to solve for your private key,
determining its value. To effectively accomplish that, you must use a
different nonce every time you sign a different message or change other
signature parameters. The different nonces must not be related in any
way. For a multisignature, every participant must follow these rules or
it could compromise the security of other participants. In addition,
cancellation and other attacks need to be prevented. Different
2023-10-03 13:25:59 +00:00
protocols that accomplish these aims make different trade-offs, so
2023-09-12 01:22:28 +00:00
there's no single multisignature protocol to recommend in all cases.
Instead, we'll note three from the MuSig family of protocols:
MuSig::
Also called _MuSig1_, this protocol((("MuSig protocol"))) requires three rounds of
2023-09-12 01:22:28 +00:00
communication during the signing process, making it similar to the
2023-10-03 13:25:59 +00:00
process we just described. MuSig1's greatest advantage is its
2023-09-12 01:22:28 +00:00
simplicity.
MuSig2::
This only ((("MuSig2 protocol")))requires two rounds of communication and can sometimes allow
2023-09-12 01:22:28 +00:00
one of the rounds to be combined with key exchange. This can
significantly speed up signing for certain protocols, such as how
2023-10-25 17:40:02 +00:00
scriptless multisignatures are planned to be used in the LN. MuSig2 is specified in BIP327 (the only scriptless
2023-09-12 01:22:28 +00:00
multisignature protocol that has a BIP as of this writing).
MuSig-DN::
DN stands ((("MuSig-DN protocol")))((("repeated session attacks")))for Deterministic Nonce, which eliminates as a concern a
2023-09-12 01:22:28 +00:00
problem known as the _repeated session attack_. It can't be combined
with key exchange and it's significantly more complex to implement
2023-10-03 13:25:59 +00:00
than MuSig or MuSig2.
2023-09-12 01:22:28 +00:00
For most applications, MuSig2 is the best multisignature protocol
available at the time((("digital signatures", "schnorr signature algorithm", "scriptless multisignatures", startref="digital-sigs-schnorr-multisig")))((("schnorr signature algorithm", "scriptless multisignatures", startref="schnorr-multisig")))((("scriptless multisignatures", "in schnorr signature algorithm", secondary-sortas="schnorr", startref="scriptless-multi-schnorr")))((("multisignature scripts", "in schnorr signature algorithm", secondary-sortas="schnorr", startref="multi-script-schnorr"))) of writing.
2023-09-12 01:22:28 +00:00
[[schnorr_threshold_signatures]]
2023-10-03 13:25:59 +00:00
==== Schnorr-based Scriptless Threshold Signatures
2023-09-12 01:22:28 +00:00
Scriptless ((("digital signatures", "schnorr signature algorithm", "scriptless threshold signatures", id="digital-sigs-schnorr-threshold")))((("schnorr signature algorithm", "scriptless threshold signatures", id="schnorr-threshold")))((("scriptless threshold signatures", id="scriptless-threshold-schnorr")))((("threshold signatures", "in schnorr signature algorithm", secondary-sortas="schnorr", id="threshold-schnorr")))multisignature protocols only work for _k_-of-_k_ signing.
2023-09-12 01:22:28 +00:00
Everyone with a partial public key that becomes part of the aggregated
public key must contribute a partial signature and partial nonce to the
final signature. Sometimes, though, the participants want to allow a
subset of them to sign, such as _t_-of-_k_ where a threshold (_t_) number of participants can sign for
a key constructed by _k_ participants. That type of signature is called a
2023-09-12 01:22:28 +00:00
_threshold signature_.
We saw script-based threshold signatures in
<<multisig>>. But just as
scriptless multisignatures save space and increase privacy compared to
2023-10-03 13:25:59 +00:00
scripted multisignatures, _scriptless threshold signatures_ save space and
2023-09-12 01:22:28 +00:00
increase privacy compared to _scripted threshold signatures_. To anyone
2023-10-03 13:25:59 +00:00
not involved in the signing, a _scriptless threshold signature_ looks
like any other signature that could've been created by a single-sig
2023-09-12 01:22:28 +00:00
user or through a scriptless multisignature protocol.
Various methods are known for generating scriptless threshold
signatures, with the simplest being a slight modification of how we
created scriptless multisignatures previously. This protocol also
depends on verifiable secret sharing (which itself depends on secure
secret sharing).
Basic secret sharing can work through simple splitting. Alice has a
secret number that she splits into three equal-length parts and shares
with Bob, Carol, and Dan. Those three can combine the partial numbers
they received (called _shares_) in the correct order to reconstruct
Alice's secret. A more sophisticated scheme would involve Alice adding
on some additional information to each share, called a correction code,
that allows any two of them to recover the number. This scheme is not
secure because each share gives its holder partial knowledge of Alice's
secret, making it easier for the participant to guess Alice's secret
2023-10-03 13:25:59 +00:00
than a nonparticipant who didn't have a share.
2023-09-12 01:22:28 +00:00
A secure secret sharing scheme prevents participants from learning
anything about the secret unless they combine the minimum threshold
number of shares. For example, Alice can choose a threshold of
2023-10-25 14:20:58 +00:00
2 if she wants any two of Bob, Carol, and Dan to be able to
2023-09-12 01:22:28 +00:00
reconstruct her secret. The best known secure secret sharing algorithm
is _Shamir's Secret Sharing Scheme_, commonly abbreviated SSSS and named
after its discoverer, one of the same discoverers of the Fiat-Shamir
transform we saw in <<schnorr_signatures>>.
In some cryptographic protocols, such as the scriptless threshold signature
2023-10-03 13:25:59 +00:00
schemes we're working toward, it's critical for Bob, Carol, and Dan to
2023-09-12 01:22:28 +00:00
know that Alice followed her side of the protocol correctly. They need to
know that the shares she creates all derive from the same secret, that
she used the threshold value she claims, and that she gave each one of
them a different share. A protocol that can accomplish all of that,
and still be a secure secret sharing scheme, is a _verifiable secret
sharing scheme_.
2023-10-03 13:25:59 +00:00
To see how multisignatures and verifiable secret sharing work for
2023-09-12 01:22:28 +00:00
Alice, Bob, and Carol, imagine they each wish to receive funds that can
be spent by any two of them. They collaborate as described in
<<schnorr_multisignatures>> to produce a regular multisignature public
key to accept the funds (k-of-k). Then each participant derives two
secret shares from their private key--one for each of two the other
participants. The shares allow any two of them to reconstruct the
originating partial private key for the multisignature. Each participant
distributes one of their secret shares to the other two participants,
resulting in each participant storing their own partial private key and
one share for every other participant. Subsequently, each participant
verifies the authenticity and uniqueness of the shares they received
compared to the shares given to the other participants.
Later on, when (for example) Alice and Bob want to generate a scriptless
threshold signature without Carol's involvement, they exchange the two
shares they possess for Carol. This enables them to reconstruct Carol's
partial private key. Alice and Bob also have their private keys,
allowing them to create a scriptless multisignature with all three
necessary keys.
2023-10-03 13:25:59 +00:00
In other words, the scriptless threshold signature scheme just described
is the same as a scriptless multisignature scheme except that
2023-09-12 01:22:28 +00:00
a threshold number of participants have the ability to reconstruct the
partial private keys of any other participants who are unable or
unwilling to sign.
This does point to a few things to be aware about when considering a
scriptless threshold signature protocol:
2023-10-03 13:25:59 +00:00
No accountability::
Because Alice and Bob reconstruct Carol's partial
2023-09-12 01:22:28 +00:00
private key, there can be no fundamental difference between a scriptless
multisignature produced by a process that involved Carol and one that
didn't. Even if Alice, Bob, or Carol claim that they didn't sign,
there's no guaranteed way for them to prove that they didn't
help produce the signature. If it's important to know which members of
the group signed, you will need to use a script.
2023-10-03 13:25:59 +00:00
Manipulation attacks::
Imagine that Bob tells Alice that Carol is
2023-09-12 01:22:28 +00:00
unavailable, so they work together to reconstruct Carol's partial
private key. Then Bob tells Carol that Alice is unavailable, so they
work together to reconstruct Alice's partial private key. Now Bob has
his own partial private key plus the keys of Alice and Carol, allowing
him to spend the funds himself without their involvement. This attack can
be addressed if all of the participants agree to only communicate using a
2023-10-23 15:18:30 +00:00
scheme that allows any one of them to see all of the other's messages
(e.g., if Bob tells Alice that Carol is unavailable, Carol is able to see
that message before she begins working with Bob). Other solutions,
2023-09-12 01:22:28 +00:00
possibly more robust solutions, to this problem were being researched at
the time of writing.
No scriptless threshold signature protocol has been proposed as a BIP
yet, although significant research into the subject has been performed
by multiple Bitcoin contributors and we expect peer-reviewed solutions
will become available after the publication of this((("digital signatures", "schnorr signature algorithm", startref="digital-sigs-schnorr")))((("schnorr signature algorithm", startref="schnorr")))((("digital signatures", "schnorr signature algorithm", "scriptless threshold signatures", startref="digital-sigs-schnorr-threshold")))((("schnorr signature algorithm", "scriptless threshold signatures", startref="schnorr-threshold")))((("scriptless threshold signatures", startref="scriptless-threshold-schnorr")))((("threshold signatures", "in schnorr signature algorithm", secondary-sortas="schnorr", startref="threshold-schnorr"))) book.
2023-09-12 01:22:28 +00:00
[[ecdsa_signatures]]
2023-10-03 13:25:59 +00:00
=== ECDSA Signatures
2023-09-12 01:22:28 +00:00
Unfortunately ((("digital signatures", "ECDSA", id="digital-signature-ecdsa")))((("ECDSA (Elliptic Curve Digital Signature Algorithm)", id="ecdsa")))for the future development of Bitcoin and many other
2023-09-12 01:22:28 +00:00
applications, Claus Schnorr patented the algorithm he discovered and
prevented its use in open standards and open source software for almost
two decades. Cryptographers in the early 1990s who were blocked from
using the schnorr signature scheme developed an alternative construction
called the _Digital Signature Algorithm_ (DSA), with a version adapted
2023-10-03 13:25:59 +00:00
to elliptic curves called ECDSA.
2023-09-12 01:22:28 +00:00
The ECDSA scheme and standardized parameters for suggested curves it could be used
with were widely implemented in cryptographic libraries by the time
development on Bitcoin began in 2007. This was almost certainly the
reason why ECDSA was the only digital signature protocol that Bitcoin
supported from its first release version until the activation of the
taproot soft fork in 2021. ECDSA remains supported today for all
non-taproot transactions. Some of the differences compared to schnorr
signatures include:
More complex::
As we'll see, ECDSA requires more operations to create or verify a
signature than the schnorr signature protocol. It's not significantly
more complex from an implementation standpoint, but that extra
complexity makes ECDSA less flexible, less performant, and harder to
prove secure.
Less provable security::
The interactive schnorr signature identification protocol depends only
2023-10-03 13:25:59 +00:00
on the strength of the elliptic curve Discrete Logarithm Problem
2023-09-12 01:22:28 +00:00
(ECDLP). The non-interactive authentication protocol used in Bitcoin
2023-10-03 13:25:59 +00:00
also relies on the random oracle model (ROM). However, ECDSA's extra
2023-09-12 01:22:28 +00:00
complexity has prevented a complete proof of its security being
published (to the best of our knowledge). We are not experts in
proving cryptographic algorithms, but it seems unlikely after 30 years
that ECDSA will be proven to only require the same two assumptions as
schnorr.
2023-10-03 13:25:59 +00:00
Nonlinear::
2023-09-12 01:22:28 +00:00
ECDSA signatures cannot be easily combined to create scriptless
2023-10-23 15:18:30 +00:00
multisignatures or used in related advanced applications, such as
2023-10-03 13:25:59 +00:00
multiparty signature adaptors. There are workarounds for this
problem, but they involve additional extra complexity that
2023-09-12 01:22:28 +00:00
significantly slows down operations and which, in some cases, has
resulted in software accidentally leaking private keys.
2023-10-25 18:17:02 +00:00
==== ECDSA Algorithm
Let's look at the math of ECDSA.
Signatures are created by a mathematical function _F_~_sig_~
2023-09-12 01:22:28 +00:00
that produces a signature composed of two values. In ECDSA, those two
values are _R_ and _s_.
The signature
algorithm first generates a private nonce (_k_) and derives from it a public
2023-10-03 13:25:59 +00:00
nonce (_K_). The _R_ value of the digital signature is then the _x_
2023-09-12 01:22:28 +00:00
coordinate of the nonce _K_.
2023-10-03 13:25:59 +00:00
From there, the algorithm calculates the _s_ value of the signature. Like we did with schnorr signatures, operations involving
integers are modulus p:
2023-09-12 01:22:28 +00:00
2023-11-01 16:17:32 +00:00
[latexmath]
++++
\begin{equation}
2023-11-01 16:27:14 +00:00
s = k^{-1} (Hash(m) + x \times R)
2023-11-01 16:17:32 +00:00
\end{equation}
++++
2023-09-12 01:22:28 +00:00
2023-11-01 16:17:32 +00:00
2023-09-12 01:22:28 +00:00
where:
* _k_ is the private nonce
2023-10-03 13:25:59 +00:00
* _R_ is the _x_ coordinate of the public nonce
2023-09-12 01:22:28 +00:00
* _x_ is the Alice's private key
* _m_ is the message (transaction data)
Verification is the inverse of the signature generation function, using
the _R_, _s_ values and the public key to calculate a value _K_, which
is a point on the elliptic curve (the public nonce used in
signature creation):
2023-11-01 16:27:14 +00:00
[latexmath]
++++
\begin{equation}
K = s^{-1} \times Hash(m) \times G + s^{-1} \times R \times X
\end{equation}
++++
2023-09-12 01:22:28 +00:00
where:
- _R_ and _s_ are the signature values
- _X_ is Alice's public key
- _m_ is the message (the transaction data that was signed)
- _G_ is the elliptic curve generator point
2023-10-03 13:25:59 +00:00
If the _x_ coordinate of the calculated point _K_ is equal to _R_, then
2023-09-12 01:22:28 +00:00
the verifier can conclude that the signature is valid.
[TIP]
====
ECDSA is necessarily a fairly complicated piece of math; a full
explanation is beyond the scope of this book. A number of great guides
2023-10-03 13:25:59 +00:00
online take you through it step by step: search for "ECDSA explained."
2023-09-12 01:22:28 +00:00
====
[[serialization_of_signatures_der]]
2023-10-03 13:25:59 +00:00
==== Serialization of ECDSA Signatures (DER)
2023-09-12 01:22:28 +00:00
Let's ((("serialization", "ECDSA signatures")))look at
2023-09-12 01:22:28 +00:00
the following DER-encoded signature:
----
2023-10-09 18:17:59 +00:00
3045022100884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb02204
b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e381301
2023-09-12 01:22:28 +00:00
----
2023-10-25 17:40:02 +00:00
That signature is a serialized byte stream of the _R_ and _s_ values
2023-10-03 13:25:59 +00:00
produced by the signer to prove control of the private key authorized
2023-09-12 01:22:28 +00:00
to spend an output. The serialization format consists of nine elements
as follows:
2023-10-03 13:25:59 +00:00
* +0x30+, indicating the start of a DER sequence
* +0x45+, the length of the sequence (69 bytes)
* +0x02+, an integer value follows
* +0x21+, the length of the integer (33 bytes)
* +R+, ++00884d142d86652a3f47ba4746ec719bbfbd040a570b1deccbb6498c75c4ae24cb++
* +0x02+, another integer follows
* +0x20+, the length of the integer (32 bytes)
* +S+, ++4b9f039ff08df09cbe9f6addac960298cad530a863ea8f53982c09db8f6e3813++
* A suffix (+0x01+) indicating the type of hash((("digital signatures", "ECDSA", startref="digital-signature-ecdsa")))((("ECDSA (Elliptic Curve Digital Signature Algorithm)", startref="ecdsa"))) used (+SIGHASH_ALL+)
2023-09-12 01:22:28 +00:00
[[nonce_warning]]
=== The Importance of Randomness in Signatures
As we((("digital signatures", "randomness, importance of", id="digital-signature-random")))((("randomness, importance in digital signatures", id="random-digital-signature"))) saw in <<schnorr_signatures>> and <<ecdsa_signatures>>,
2023-10-23 15:18:30 +00:00
the signature generation algorithm uses a random number _k_ as the basis
2023-09-12 01:22:28 +00:00
for a private/public nonce pair. The value of _k_ is not
important, _as long as it is random_. If signatures from the same
private key use the private nonce _k_ with different messages
(transactions), then the
signing _private key_ can be calculated by anyone. Reuse of the same
value for _k_ in a signature algorithm leads to exposure of the private
key!
[WARNING]
====
If the same value _k_
is used in the signing algorithm on two different transactions, the
private key can be calculated and exposed to the world!
====
This is not just a theoretical possibility. We have seen this issue lead
to exposure of private keys in a few different implementations of
transaction-signing algorithms in Bitcoin. People have had funds stolen
because of inadvertent reuse of a _k_ value. The most common reason for
reuse of a _k_ value is an improperly initialized random-number
generator.
To avoid this
vulnerability, the industry best practice is to not generate _k_ with a
random-number generator seeded only with entropy, but instead to use a
process seeded in part with the transaction data itself plus the
private key being used to sign.
This ensures that each transaction produces a different _k_. The
industry-standard algorithm for deterministic initialization of _k_ for
2023-10-10 17:53:41 +00:00
ECDSA is defined in https://oreil.ly/yuabl[RFC6979], published by
2023-09-12 01:22:28 +00:00
the Internet Engineering Task Force. For schnorr signatures, BIP340
recommends a default signing algorithm.
BIP340 and RFC6979 can generate _k_ entirely deterministically, meaning the same
transaction data will always produce the same _k_. Many wallets do this
because it makes it easy to write tests to verify their safety-critical
2023-10-24 14:58:16 +00:00
signing code is producing _k_ values correctly. BIP340 and RFC6979 both also allow
2023-09-12 01:22:28 +00:00
including additional data in the calculation. If that data is entropy,
then a different _k_ will be produced even if the exact same transaction
data is signed. This can increase protection against sidechannel and
fault-injection attacks.
If you are implementing an algorithm to sign transactions in Bitcoin,
you _must_ use BIP340, RFC6979, or a similar algorithm to
ensure you generate a different _k_ for each ((("digital signatures", "randomness, importance of", startref="digital-signature-random")))((("randomness, importance in digital signatures", startref="random-digital-signature")))transaction.
2023-09-12 01:22:28 +00:00
=== Segregated Witness's New Signing Algorithm
Signatures in((("digital signatures", "segregated witness and")))((("segregated witness (segwit)", "digital signatures and")))((("commitment hash"))) Bitcoin transactions are applied on a _commitment hash_,
2023-09-12 01:22:28 +00:00
which is calculated from the transaction data, locking specific parts of
the data indicating the signer's commitment to those values. For
example, in a simple +SIGHASH_ALL+ type signature, the commitment hash
includes all inputs and outputs.
Unfortunately, the way the legacy commitment hashes were calculated introduced the
possibility that a node verifying a signature can be forced to perform
a significant number of hash computations. Specifically, the hash
operations increase roughly quadratically with respect to the number of
inputs in the transaction. An attacker could therefore create a
transaction with a very large number of signature operations, causing
the entire Bitcoin network to have to perform hundreds or thousands of
hash operations to verify the transaction.
Segwit represented an opportunity to address this problem by changing
the way the commitment hash is calculated. For segwit version 0 witness
programs, signature verification occurs using an improved commitment
hash algorithm as specified in BIP143.
The new algorithm allows the number of
hash operations to increase by a much more gradual O(n) to the number of
signature operations, reducing the opportunity to create
denial-of-service attacks with overly complex transactions.
In this chapter, we learned about schnorr and ECDSA signatures for
Bitcoin. This explains how full nodes authenticate transactions to
ensure that only someone controlling the key to which bitcoins were
received can spend those bitcoins. We also examined several advanced
applications of signatures, such as scriptless multisignatures and
scriptless threshold signatures that can be used to improve the
efficiency and privacy of Bitcoin. In the past few chapters, we've
learned how to create transactions, how to secure them with
authorization and authentication, and how to sign them. We will next
learn how to encourage miners to confirm them by adding fees to the
transactions we create.
//FIXME: mention segwit v0 and v1 coverage of values to aid hardware
//wallets