/
schnorr-mu-sig.spec.js
285 lines (252 loc) · 12.3 KB
/
schnorr-mu-sig.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* global describe, it, beforeEach */
const assert = require('assert');
const Buffer = require('safe-buffer').Buffer;
const BigInteger = require('bigi');
const convert = require('../src/convert');
const muSig = require('../src/mu-sig');
const schnorr = require('../src/schnorr');
const randomBytes = require('random-bytes');
const testVectors = require('./test-vectors-mu-sig.json');
const randomBuffer = (len) => Buffer.from(randomBytes.sync(len));
describe('muSig', () => {
describe('pubKeyCombine', () => {
testVectors.forEach(vec => {
it('can combine public keys into ' + vec.pubKeyCombined, () => {
// given
const pubKeys = vec.pubKeys.map(pk => Buffer.from(pk, 'hex'));
// when
const result = muSig.pubKeyCombine(pubKeys);
// then
assert.strictEqual(result.toString('hex'), vec.pubKeyCombined);
});
});
});
describe('sessionInitialize', () => {
testVectors
.forEach(vec => {
it('can initialize session for combined key ' + vec.pubKeyCombined, () => {
// given
const pubKeys = vec.pubKeys.map(pk => Buffer.from(pk, 'hex'));
const pubKeyCombined = Buffer.from(vec.pubKeyCombined, 'hex');
const ell = muSig.computeEll(pubKeys);
const message = Buffer.from(vec.message, 'hex');
for (let i = 0; i < vec.privKeys.length; i++) {
const sessionId = Buffer.from(vec.sessionIds[i], 'hex');
const privateKey = BigInteger.fromHex(vec.privKeys[i]);
// when
const session = muSig.sessionInitialize(sessionId, privateKey, message, pubKeyCombined, ell, i);
// then
assert.strictEqual(session.commitment.toString('hex'), vec.commitments[i]);
assert.strictEqual(convert.intToBuffer(session.secretKey).toString('hex'), vec.secretKeys[i]);
assert.strictEqual(convert.intToBuffer(session.secretNonce).toString('hex'), vec.secretNonces[i]);
}
});
});
});
describe('sessionNonceCombine', () => {
testVectors.forEach(vec => {
it('can combine nonces into ' + vec.nonceCombined, () => {
// given
const pubKeys = vec.pubKeys.map(pk => Buffer.from(pk, 'hex'));
const pubKeyCombined = Buffer.from(vec.pubKeyCombined, 'hex');
const ell = muSig.computeEll(pubKeys);
const message = Buffer.from(vec.message, 'hex');
const sessions = [];
for (let i = 0; i < vec.privKeys.length; i++) {
const sessionId = Buffer.from(vec.sessionIds[i], 'hex');
const privateKey = BigInteger.fromHex(vec.privKeys[i]);
sessions[i] = muSig.sessionInitialize(sessionId, privateKey, message, pubKeyCombined, ell, i);
}
// when
const result = muSig.sessionNonceCombine(sessions[0], sessions.map(s => s.nonce));
// then
assert.strictEqual(result.toString('hex'), vec.nonceCombined);
});
});
});
describe('partialSign', () => {
testVectors.forEach((vec, index) => {
it('can create partial signatures #' + (index + 1), () => {
// given
const pubKeys = vec.pubKeys.map(pk => Buffer.from(pk, 'hex'));
const pubKeyCombined = Buffer.from(vec.pubKeyCombined, 'hex');
const ell = muSig.computeEll(pubKeys);
const message = Buffer.from(vec.message, 'hex');
const sessions = [];
for (let i = 0; i < vec.privKeys.length; i++) {
const sessionId = Buffer.from(vec.sessionIds[i], 'hex');
const privateKey = BigInteger.fromHex(vec.privKeys[i]);
sessions[i] = muSig.sessionInitialize(sessionId, privateKey, message, pubKeyCombined, ell, i);
}
const signerSession = sessions[0];
const nonceCombined = muSig.sessionNonceCombine(signerSession, sessions.map(s => s.nonce));
for (let i = 0; i < sessions.length; i++) {
// when
sessions[i].nonceIsNegated = signerSession.nonceIsNegated;
const result = muSig.partialSign(sessions[i], message, nonceCombined, pubKeyCombined);
// then
assert.strictEqual(convert.intToBuffer(result).toString('hex'), vec.partialSigs[i]);
}
});
});
});
describe('partialSigCombine', () => {
testVectors.forEach(vec => {
it('can combine partial signatures into ' + vec.signature, () => {
// given
const pubKeys = vec.pubKeys.map(pk => Buffer.from(pk, 'hex'));
const pubKeyCombined = Buffer.from(vec.pubKeyCombined, 'hex');
const ell = muSig.computeEll(pubKeys);
const message = Buffer.from(vec.message, 'hex');
const sessions = [];
for (let i = 0; i < vec.privKeys.length; i++) {
const sessionId = Buffer.from(vec.sessionIds[i], 'hex');
const privateKey = BigInteger.fromHex(vec.privKeys[i]);
sessions[i] = muSig.sessionInitialize(sessionId, privateKey, message, pubKeyCombined, ell, i);
}
const signerSession = sessions[0];
const nonceCombined = muSig.sessionNonceCombine(signerSession, sessions.map(s => s.nonce));
const partialSignatures = sessions.map(s => {
s.nonceIsNegated = signerSession.nonceIsNegated;
return muSig.partialSign(s, message, nonceCombined, pubKeyCombined)
});
// when
const result = muSig.partialSigCombine(nonceCombined, partialSignatures);
// then
assert.strictEqual(result.toString('hex'), vec.signature);
});
});
});
describe('full example', () => {
it('can sign and verify example in README', () => {
// data known to every participant
const publicData = {
pubKeys: [
Buffer.from('03846f34fdb2345f4bf932cb4b7d278fb3af24f44224fb52ae551781c3a3cad68a', 'hex'),
Buffer.from('02cd836b1d42c51d80cef695a14502c21d2c3c644bc82f6a7052eb29247cf61f4f', 'hex'),
Buffer.from('03b8c1765111002f09ba35c468fab273798a9058d1f8a4e276f45a1f1481dd0bdb', 'hex'),
],
message: convert.hash(Buffer.from('muSig is awesome!', 'utf8')),
pubKeyHash: null,
pubKeyCombined: null,
commitments: [],
nonces: [],
nonceCombined: null,
partialSignatures: [],
signature: null,
};
// data only known by the individual party, these values are never shared
// between the signers!
const signerPrivateData = [
// signer 1
{
privateKey: BigInteger.fromHex('add2b25e2d356bec3770305391cbc80cab3a40057ad836bcb49ef3eed74a3fee'),
session: null,
},
// signer 2
{
privateKey: BigInteger.fromHex('0a1645eef5a10e1f5011269abba9fd85c4f0cc70820d6f102fb7137f2988ad78'),
session: null,
},
// signer 3
{
privateKey: BigInteger.fromHex('2031e7fed15c770519707bb092a6337215530e921ccea42030c15d86e8eaf0b8'),
session: null,
}
];
// -----------------------------------------------------------------------
// Step 1: Combine the public keys
// The public keys P_i are combined into the combined public key P.
// This can be done by every signer individually or by the initializing
// party and then be distributed to every participant.
// -----------------------------------------------------------------------
publicData.pubKeyHash = muSig.computeEll(publicData.pubKeys);
publicData.pubKeyCombined = muSig.pubKeyCombine(publicData.pubKeys, publicData.pubKeyHash);
// -----------------------------------------------------------------------
// Step 2: Create the private signing session
// Each signing party does this in private. The session ID *must* be
// unique for every call to sessionInitialize, otherwise it's trivial for
// an attacker to extract the secret key!
// -----------------------------------------------------------------------
signerPrivateData.forEach((data, idx) => {
const sessionId = randomBuffer(32); // must never be reused between sessions!
data.session = muSig.sessionInitialize(
sessionId,
data.privateKey,
publicData.message,
publicData.pubKeyCombined,
publicData.pubKeyHash,
idx
);
});
const signerSession = signerPrivateData[0].session;
// -----------------------------------------------------------------------
// Step 3: Exchange commitments (communication round 1)
// The signers now exchange the commitments H(R_i). This is simulated here
// by copying the values from the private data to public data array.
// -----------------------------------------------------------------------
for (let i = 0; i < publicData.pubKeys.length; i++) {
publicData.commitments[i] = signerPrivateData[i].session.commitment;
}
// -----------------------------------------------------------------------
// Step 4: Get nonces (communication round 2)
// Now that everybody has commited to the session, the nonces (R_i) can be
// exchanged. Again, this is simulated by copying.
// -----------------------------------------------------------------------
for (let i = 0; i < publicData.pubKeys.length; i++) {
publicData.nonces[i] = signerPrivateData[i].session.nonce;
}
// -----------------------------------------------------------------------
// Step 5: Combine nonces
// The nonces can now be combined into R. Each participant should do this
// and keep track of whether the nonce was negated or not. This is needed
// for the later steps.
// -----------------------------------------------------------------------
publicData.nonceCombined = muSig.sessionNonceCombine(signerSession, publicData.nonces);
signerPrivateData.forEach(data => (data.session.nonceIsNegated = signerSession.nonceIsNegated));
// -----------------------------------------------------------------------
// Step 6: Generate partial signatures
// Every participant can now create their partial signature s_i over the
// given message.
// -----------------------------------------------------------------------
signerPrivateData.forEach(data => {
data.session.partialSignature = muSig.partialSign(data.session, publicData.message, publicData.nonceCombined, publicData.pubKeyCombined);
});
// -----------------------------------------------------------------------
// Step 7: Exchange partial signatures (communication round 3)
// The partial signature of each signer is exchanged with the other
// participants. Simulated here by copying.
// -----------------------------------------------------------------------
for (let i = 0; i < publicData.pubKeys.length; i++) {
publicData.partialSignatures[i] = signerPrivateData[i].session.partialSignature;
}
// -----------------------------------------------------------------------
// Step 8: Verify individual partial signatures
// Every participant should verify the partial signatures received by the
// other participants.
// -----------------------------------------------------------------------
for (let i = 0; i < publicData.pubKeys.length; i++) {
muSig.partialSigVerify(
signerSession,
publicData.partialSignatures[i],
publicData.nonceCombined,
i,
publicData.pubKeys[i],
publicData.nonces[i]
);
}
// -----------------------------------------------------------------------
// Step 9: Combine partial signatures
// Finally, the partial signatures can be combined into the full signature
// (s, R) that can be verified against combined public key P.
// -----------------------------------------------------------------------
publicData.signature = muSig.partialSigCombine(publicData.nonceCombined, publicData.partialSignatures);
// -----------------------------------------------------------------------
// Step 10: Verify signature
// The resulting signature can now be verified as a normal Schnorr
// signature (s, R) over the message m and public key P.
// -----------------------------------------------------------------------
schnorr.verify(publicData.pubKeyCombined, publicData.message, publicData.signature);
});
});
});