Skip to content

Commit 73f0cbd

Browse files
Merge #157: Add description of MuSig signing to musig-spec.md
69b392f musig: move explanation for aggnonce=inf to spec (Jonas Nick) 4824220 musig-spec: describe NonceGen, NonceAgg, Sign,PartialSig{Verify,Agg} (Jonas Nick) 3c122d0 musig-spec: improve definition of lift_x (Jonas Nick) e0bb2d7 musig-spec: improve KeyAgg description (Jonas Nick) b8f4e75 musig-spec: move to doc directory (Jonas Nick) Pull request description: Will wait before adding tweaking until #151 is merged. ACKs for top commit: robot-dreams: ACK 69b392f based on: real-or-random: ACK 69b392f I haven't looked at every detail but it's certainly ready to be merged as draft spec Tree-SHA512: e3aa0265a9d7a7648e03ca42575397100edd5af43f0224937af51aa5c77efc451d7938149bdc711f69e24fb9291438453b8cd762affaa1a2e7bcc89f121485df
2 parents 8fd97d8 + 69b392f commit 73f0cbd

File tree

3 files changed

+256
-117
lines changed

3 files changed

+256
-117
lines changed

doc/musig-spec.mediawiki

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
<pre>
2+
Title: MuSig Key Aggregation
3+
Author:
4+
Status: Draft
5+
License: BSD-2-Clause
6+
Created: 2020-01-19
7+
</pre>
8+
9+
== Introduction ==
10+
11+
=== Abstract ===
12+
13+
This document describes MuSig Key Aggregation in libsecp256k1-zkp.
14+
15+
=== Copyright ===
16+
17+
This document is licensed under the 2-clause BSD license.
18+
19+
=== Motivation ===
20+
21+
== Description ==
22+
23+
=== Design ===
24+
25+
* The output of the ''KeyAgg'' algorithm depends on the order of the input public keys.
26+
* It is possible to sort the public keys with the ''KeySort'' algorithm before key aggregation to ensure the same output, independent of the (initial) order.
27+
* The KeyAgg coefficient is computed by hashing the key instead of key index. Otherwise, if the pubkey list gets sorted, the signer needs to translate between key indices pre- and post-sorting.
28+
* The second unique key in the pubkey list given to ''KeyAgg'' (as well as any keys identical to this key) gets the constant KeyAgg coefficient 1 which saves an exponentiation (see the MuSig2* appendix in the [https://eprint.iacr.org/2020/1261 MuSig2 paper]).
29+
* The public key inputs are serialized using x-only (32 byte) instead of compressed (33 byte) serialization. The reason for this is that as x-only keys are becoming more common, the full key may not be available.
30+
* The public nonces are serialized in compressed format (33 bytes). We accept the small overhead compared to x-only serialization to avoid complicating the specification.
31+
32+
=== Specification ===
33+
34+
The following conventions are used, with constants as defined for [https://www.secg.org/sec2-v2.pdf secp256k1]. We note that adapting this specification to other elliptic curves is not straightforward and can result in an insecure scheme<ref>Among other pitfalls, using the specification with a curve whose order is not close to the size of the range of the nonce derivation function is insecure.</ref>.
35+
* Lowercase variables represent integers or byte arrays.
36+
** The constant ''p'' refers to the field size, ''0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F''.
37+
** The constant ''n'' refers to the curve order, ''0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141''.
38+
* Uppercase variables refer to points on the curve with equation ''y<sup>2</sup> = x<sup>3</sup> + 7'' over the integers modulo ''p''.
39+
** ''is_infinite(P)'' returns whether or not ''P'' is the point at infinity.
40+
** ''x(P)'' and ''y(P)'' are integers in the range ''0..p-1'' and refer to the X and Y coordinates of a point ''P'' (assuming it is not infinity).
41+
** The constant ''G'' refers to the base point, for which ''x(G) = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798'' and ''y(G) = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8''.
42+
** Addition of points refers to the usual [https://en.wikipedia.org/wiki/Elliptic_curve#The_group_law elliptic curve group operation].
43+
** [https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication Multiplication (⋅) of an integer and a point] refers to the repeated application of the group operation.
44+
* Functions and operations:
45+
** ''||'' refers to byte array concatenation.
46+
** The function ''x[i:j]'', where ''x'' is a byte array and ''i, j &ge; 0'', returns a ''(j - i)''-byte array with a copy of the ''i''-th byte (inclusive) to the ''j''-th byte (exclusive) of ''x''.
47+
** The function ''bytes(x)'', where ''x'' is an integer, returns the 32-byte encoding of ''x'', most significant byte first.
48+
** The function ''bytes(P)'', where ''P'' is a point, returns ''bytes(x(P))''.
49+
** The function ''has_even_y(P)'', where ''P'' is a point for which ''not is_infinite(P)'', returns ''y(P) mod 2 = 0''.
50+
** The function ''cbytes(P)'', where ''P'' is a point, returns ''a || bytes(P)'' where ''a'' is a byte that is ''2'' if ''has_even_y(P)'' and ''3'' otherwise.
51+
** The function ''int(x)'', where ''x'' is a 32-byte array, returns the 256-bit unsigned integer whose most significant byte first encoding is ''x''.
52+
** The function ''lift_x(x)'', where ''x'' is an integer in range ''0..2<sup>256</sup>-1'', returns the point ''P'' for which ''x(P) = x''<ref>
53+
Given a candidate X coordinate ''x'' in the range ''0..p-1'', there exist either exactly two or exactly zero valid Y coordinates. If no valid Y coordinate exists, then ''x'' is not a valid X coordinate either, i.e., no point ''P'' exists for which ''x(P) = x''. The valid Y coordinates for a given candidate ''x'' are the square roots of ''c = x<sup>3</sup> + 7 mod p'' and they can be computed as ''y = &plusmn;c<sup>(p+1)/4</sup> mod p'' (see [https://en.wikipedia.org/wiki/Quadratic_residue#Prime_or_prime_power_modulus Quadratic residue]) if they exist, which can be checked by squaring and comparing with ''c''.</ref> and ''has_even_y(P)'', or fails if ''x'' is greater than ''p-1'' or no such point exists. The function ''lift_x(x)'' is equivalent to the following pseudocode:
54+
*** Fail if ''x &gt; p-1''.
55+
*** Let ''c = x<sup>3</sup> + 7 mod p''.
56+
*** Let ''y' = c<sup>(p+1)/4</sup> mod p''.
57+
*** Fail if ''c &ne; y'<sup>2</sup> mod p''.
58+
*** Let ''y = y' '' if ''y' mod 2 = 0'', otherwise let ''y = p - y' ''.
59+
*** Return the unique point ''P'' such that ''x(P) = x'' and ''y(P) = y''.
60+
** The function ''point(x)'', where ''x'' is a 32-byte array ("x-only" serialization), returns ''lift_x(int(x))''. Fail if ''lift_x'' fails.
61+
** The function ''pointc(x)'', where ''x'' is a 33-byte array (compressed serialization), sets ''P = lift_x(int(x[1:33]))'' and fails if that fails. If ''x[0] = 2'' it returns ''P'' and if ''x[0] = 3'' it returns ''-P''. Otherwise, it fails.
62+
** The function ''hash<sub>tag</sub>(x)'' where ''tag'' is a UTF-8 encoded tag name and ''x'' is a byte array returns the 32-byte hash ''SHA256(SHA256(tag) || SHA256(tag) || x)''.
63+
64+
65+
==== Key Sorting ====
66+
67+
Input:
68+
* The number ''u'' of signatures with ''0 < u < 2^32''
69+
* The public keys ''pk<sub>1..u</sub>'': ''u'' 32-byte arrays
70+
71+
The algorithm ''KeySort(pk<sub>1..u</sub>)'' is defined as:
72+
* Return ''pk<sub>1..u</sub>'' sorted in lexicographical order.
73+
74+
==== Key Aggregation ====
75+
76+
Input:
77+
* The number ''u'' of public keys with ''0 < u < 2^32''
78+
* The public keys ''pk<sub>1..u</sub>'': ''u'' 32-byte arrays
79+
80+
The algorithm ''KeyAgg(pk<sub>1..u</sub>)'' is defined as:
81+
* Let ''Q = KeyAggInternal(pk<sub>1..u</sub>)''; fail if that fails.
82+
* Return ''bytes(Q)''.
83+
84+
The algorithm ''KeyAggInternal(pk<sub>1..u</sub>)'' is defined as:
85+
* For ''i = 1 .. u'':
86+
** Let ''a<sub>i</sub> = KeyAggCoeff(pk<sub>1..u</sub>, pk<sub>i</sub>)''.
87+
** Let ''P<sub>i</sub> = point(pk<sub>i</sub>)''; fail if that fails.
88+
* Let ''Q = a<sub>1</sub>⋅P<sub>1</sub> + a<sub>2</sub>⋅P<sub>1</sub> + ... + a<sub>u</sub>⋅P<sub>u</sub>''
89+
* Fail if ''is_infinite(Q)''.
90+
* Return ''Q''.
91+
92+
The algorithm ''HashKeys(pk<sub>1..u</sub>)'' is defined as:
93+
* Return ''hash<sub>KeyAgg list</sub>(pk<sub>1</sub> || pk<sub>2</sub> || ... || pk<sub>u</sub>)''
94+
95+
The algorithm ''IsSecond(pk<sub>1..u</sub>, pk')'' is defined as:
96+
* For ''j = 1 .. u'':
97+
** If ''pk<sub>j</sub> &ne; pk<sub>1</sub>'':
98+
*** Return ''true'' if ''pk<sub>j</sub> = pk' '', otherwise return ''false''.
99+
* Return ''false''
100+
101+
The algorithm ''KeyAggCoeff(pk<sub>1..u</sub>, pk')'' is defined as:
102+
* Let ''L = HashKeys(pk<sub>1..u</sub>)''.
103+
* If ''IsSecond(pk<sub>1..u</sub>, pk')'':
104+
** Return 1
105+
* Return ''int(hash<sub>KeyAgg coefficient</sub>(L || pk')) mod n''
106+
107+
==== Nonce Generation ====
108+
109+
The algorithm ''NonceGen()'' is defined as:
110+
* Generate two random integers ''k<sub>1</sub>, k<sub>2</sub>'' in the range ''1...n-1''
111+
* Let ''R<sup>*</sup><sub>1</sub> = k<sub>1</sub>⋅G, R<sup>*</sup><sub>2</sub> = k<sub>2</sub>⋅G''
112+
* Let ''pubnonce = cbytes(R<sup>*</sup><sub>1</sub>) || cbytes(R<sup>*</sup><sub>2</sub>)''
113+
* Let ''secnonce = bytes(k<sub>1</sub>) || bytes(k<sub>2</sub>)''
114+
* Return ''secnonce'' and ''pubnonce''
115+
116+
==== Nonce Aggregation ====
117+
118+
* The number ''u'' of ''pubnonces'' with ''0 < u < 2^32''
119+
* The public nonces ''pubnonce<sub>1..u</sub>'': ''u'' 66-byte arrays
120+
121+
The algorithm ''NonceAgg(pubnonce<sub>1..u</sub>)'' is defined as:
122+
* For ''i = 1 .. 2'':
123+
** For ''j = 1 .. u'':
124+
*** Let ''R<sub>i,j</sub> = pointc(pubnonce<sub>j</sub>[(i-1)*33:i*33])''; fail if that fails
125+
** Let ''R'<sub>i</sub> = R<sub>i,1</sub> + R<sub>i,2</sub> + ... + R<sub>i,u</sub>''
126+
** Let ''R<sub>i</sub> = R'<sub>i</sub>'' if not ''is_infinite(R'<sub>i</sub>)'', otherwise let R<sub>i</sub> = G''
127+
* Return ''aggnonce = cbytes(R<sub>1</sub>) || cbytes(R<sub>2</sub>)''
128+
129+
===== Note on ''is_infinite(R'<sub>i</sub>)'' =====
130+
131+
If ''is_infinite(R'<sub>i</sub>)'' there is at least one dishonest signer (except with negligible probability).
132+
If we would fail here, we will never be able to determine who it is.
133+
Therefore, we should continue such that the culprit is revealed when collecting and verifying partial signatures.
134+
However, dealing with the point at infinity requires defining a serialization and may require extra code complexity in implementations.
135+
Instead, we set the aggregate nonce to some arbitrary point, the generator.
136+
137+
This modification does not affect the security of the scheme.
138+
''NonceAgg'' (both the original and modified version) only depends on publicly available data (the set of public pre-nonces from every signer).
139+
Thus in the multi-signature security game (EUF-CMA), we can consider ''NonceAgg'' to be performed by the adversary (rather than the challenger) without loss of generality.
140+
The modification changes neither the behavior of the EUF-CMA challenger nor the condition required to win the security game (the adversary still has to output a valid forgery according to the unmodified MuSig2* scheme). Since we've already proved that MuSig2* is secure against an arbitrary adversary, we can conclude that the modified scheme is still secure.
141+
142+
==== Signing ====
143+
144+
Input:
145+
* The secret nonce ''secnonce'' that has never been used as input to ''Sign'' before: a 64-byte array
146+
* The secret key ''sk'': a 32-byte array
147+
* The aggregate public nonce ''aggnonce'': a 66-byte array
148+
* The number ''u'' of public keys with ''0 < u < 2^32''
149+
* The public keys ''pk<sub>1..u</sub>'': ''u'' 32-byte arrays
150+
* The message ''m'': a 32-byte array
151+
152+
The algorithm ''Sign(secnonce, sk, aggnonce, pk<sub>1..u</sub>, m)'' is defined as:
153+
* Let ''R<sub>1</sub> = pointc(aggnonce[0:33]), R<sub>2</sub> = pointc(aggnonce[33:66])''; fail if that fails
154+
* Let ''Q = KeyAggInternal(pk<sub>1..u</sub>)''; fail if that fails
155+
* Let ''b = int(hash<sub>MuSig/noncecoef</sub>(aggnonce || bytes(Q) || m)) mod n''
156+
* Let ''R = R<sub>1</sub> + b⋅R<sub>2</sub>''
157+
* Fail if ''is_infinite(R)''
158+
* Let ''k'<sub>1</sub> = int(secnonce[0:32]), k'<sub>2</sub> = int(secnonce[32:64])''
159+
* Fail if ''k'<sub>i</sub> = 0'' or ''k'<sub>i</sub> &ge; n'' for ''i = 1..2''
160+
* Let ''k<sub>1</sub> = k'<sub>1</sub>, k<sub>2</sub> = k'<sub>2</sub> '' if ''has_even_y(R)'', otherwise let ''k<sub>1</sub> = n - k'<sub>1</sub>, k<sub>2</sub> = n - k<sub>2</sub>''
161+
* Let ''d' = int(sk)''
162+
* Fail if ''d' = 0'' or ''d' &ge; n''
163+
* Let ''P = d'⋅G''
164+
* Let ''d = n - d' '' if ''has_even_y(P) `XOR` has_even_y(Q)'', otherwise let ''d = d' ''
165+
* Let ''e = int(hash<sub>BIP0340/challenge</sub>(bytes(R) || bytes(Q) || m)) mod n''
166+
* Let ''mu = KeyAggCoeff(pk<sub>1..u</sub>, bytes(P))''
167+
* Let ''s = (k<sub>1</sub> + b⋅k<sub>2</sub> + e⋅mu⋅d) mod n''
168+
* Let ''psig = bytes(s)''
169+
* Let ''pubnonce = cbytes(k'<sub>1</sub>⋅G) || cbytes(k'<sub>2</sub>⋅G)''
170+
* If ''PartialSigVerifyInternal(psig, pubnonce, aggnonce, pk<sub>1..u</sub>, bytes(P), m)'' (see below) returns failure, abort<ref>Verifying the signature before leaving the signer prevents random or attacker provoked computation errors. This prevents publishing invalid signatures which may leak information about the secret key. It is recommended, but can be omitted if the computation cost is prohibitive.</ref>.
171+
* Return partial signature ''psig
172+
173+
==== Partial Signature Verification ====
174+
175+
Input:
176+
* The partial signature ''psig'': a 32-byte array
177+
* The number ''u'' of public nonces and public keys with ''0 < u < 2^32''
178+
* The public nonces ''pubnonce<sub>1..u</sub>'': ''u'' 66-byte arrays
179+
* The public keys ''pk<sub>1..u</sub>'': ''u'' 32-byte arrays
180+
* The message ''m'': a 32-byte array
181+
* The index of the signer ''i'' in the public nonces and public keys with ''0 < i <= u''
182+
183+
The algorithm ''PartialSigVerify(psig, pubnonce<sub>1..u</sub>, pk<sub>1..u</sub>, m, i)'' is defined as:
184+
* Let ''aggnonce = NonceAgg(pubnonce<sub>1..u</sub>)''; fail if that fails
185+
* Run ''PartialSigVerifyInternal(psig, pubnonce<sub>i</sub>, aggnonce, pk<sub>1..u</sub>, pk<sub>i</sub>, m)''
186+
* Return success iff no failure occurred before reaching this point.
187+
188+
===== PartialSigVerifyInternal =====
189+
190+
Input:
191+
* The partial signature ''psig'': a 32-byte array
192+
* The public nonce of the signer ''pubnonce'': a 66-byte array
193+
* The aggregate public nonce ''aggnonce'': a 66-byte array
194+
* The number ''u'' of public keys with ''0 < u < 2^32''
195+
* The public keys ''pk<sub>1..u</sub>'': ''u'' 32-byte arrays
196+
* The public key of the signer ''pk<sup>*</sup>'' (in ''pk<sub>1..u</sub>''): a 32-byte array
197+
* The message ''m'': a 32-byte array
198+
199+
The algorithm ''PartialSigVerifyInternal(psig, pubnonce, aggnonce, pk<sub>1..u</sub>, pk<sup>*</sup>, m)'' is defined as:
200+
* Let ''s = int(psig)''; fail if ''s &ge; n''
201+
* Let ''R<sub>1</sub> = pointc(aggnonce[0:33]), R<sub>2</sub> = pointc(aggnonce[33:66])''; fail if that fails
202+
* Let ''Q = KeyAggInternal(pk<sub>1..u</sub>)''; fail if that fails
203+
* Let ''b = int(hash<sub>MuSig/noncecoef</sub>(aggnonce || bytes(Q) || m)) mod n''
204+
* Let ''R = R<sub>1</sub> + b⋅R<sub>2</sub>''
205+
* Let ''R<sup>*</sup><sub>1</sub> = pointc(pubnonce[0:33]), R<sup>*</sup><sub>2</sub> = pointc(pubnonce[33:66])''
206+
* Let ''R<sup>*</sup>' = R<sup>*</sup><sub>1</sub> + b⋅R<sup>*</sup><sub>2</sub>''
207+
* Let ''R<sup>*</sup> = R<sup>*</sup>' '' if ''has_even_y(R)'', otherwise let ''R<sup>*</sup> = -R<sup>*</sup>' ''
208+
* Let ''e = int(hash<sub>BIP0340/challenge</sub>(bytes(R) || bytes(Q) || m)) mod n''
209+
* Let ''mu = KeyAggCoeff(pk<sub>1..u</sub>, pk<sup>*</sup>)''
210+
* Let ''P' = point(pk<sup>*</sup>)''; fail if that fails
211+
* Let ''P = P' '' if ''has_even_y(Q)'', otherwise let ''P = -P' ''
212+
* Fail if ''s⋅G &ne; R<sup>*</sup> + e⋅mu⋅P''
213+
* Return success iff no failure occurred before reaching this point.
214+
215+
==== Partial Signature Aggregation ====
216+
217+
Input:
218+
* The final nonce ''R'' as created during ''Sign'' or ''PartialSigVerify'': a point
219+
* The number ''u'' of signatures with ''0 < u < 2^32''
220+
* The partial signatures ''sig<sub>1..u</sub>'': ''u'' 32-byte arrays
221+
222+
The algorithm ''SigAgg(R, sig<sub>1..u</sub>)'' is defined as:
223+
* For ''i = 1 .. u'':
224+
** Let ''s<sub>i</sub> = int(sig<sub>i</sub>)''; fail if ''s<sub>i</sub> &ge; n''.
225+
* Let ''s = s<sub>1</sub> + ... + s<sub>u</sub> mod n''
226+
* Return ''sig = ''bytes(R) || bytes(s)''
227+
228+
=== Signing Flow ===
229+
230+
Note that this specification unnecessarily recomputes intermediary values (such as the aggregate public key) that can be cached in real implementations.
231+
232+
There are multiple ways to use above algorithms and arrive at a final Schnorr signature.
233+
One of them can be described as follows:
234+
The signers ''1'' to ''n'' each run ''NonceGen'' to compute ''secnonce'' and ''pubnonce''.
235+
Every signer sends its public key and ''pubnonce'' to every other signer and all signers agree on a single message to sign.
236+
Then, the signers run ''NonceAgg'' and ''Sign'' with their secret signing key and ''secnonce''.
237+
They send the resulting partial signature to every other signer and combine them with the ''SigAgg'' algorithm.
238+
239+
''IMPORTANT'': The ''Sign'' algorithm must '''not''' be executed twice with the same ''secnonce''.
240+
Otherwise, it is possible to extract the secret signing key from the partial signatures.
241+
An implementation may invalidate the secnonce argument after ''Sign'' to avoid any reuse.
242+
Avoiding reuse also implies that the ''NonceGen'' algorithm must compute unbiased, uniformly random values ''k<sub>1</sub>'' and ''k<sub>2</sub>''.
243+
244+
== Applications ==
245+
246+
== Test Vectors and Reference Code ==
247+
248+
There are some vectors in libsecp256k1's [https://github.com/ElementsProject/secp256k1-zkp/blob/master/src/modules/musig/tests_impl.h MuSig test file].
249+
Search for the ''musig_test_vectors_keyagg'' and ''musig_test_vectors_sign'' functions.
250+
251+
== Footnotes ==
252+
253+
<references />
254+
255+
== Acknowledgements ==

0 commit comments

Comments
 (0)