-
Notifications
You must be signed in to change notification settings - Fork 50
/
secp256k1.ts
651 lines (599 loc) · 19.5 KB
/
secp256k1.ts
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* eslint-disable functional/no-conditional-statement, functional/no-expression-statement, functional/no-throw-statement */
import {
CompressionFlag,
ContextFlag,
instantiateSecp256k1Wasm,
instantiateSecp256k1WasmBytes,
Secp256k1Wasm,
} from '../bin/bin';
import { RecoverableSignature, RecoveryId, Secp256k1 } from './secp256k1-types';
export { RecoverableSignature, RecoveryId, Secp256k1 };
const enum ByteLength {
compactSig = 64,
compressedPublicKey = 33,
internalPublicKey = 64,
internalSig = 64,
maxPublicKey = 65,
maxECDSASig = 72,
messageHash = 32,
privateKey = 32,
randomSeed = 32,
recoverableSig = 65,
schnorrSig = 64,
uncompressedPublicKey = 65,
}
/**
* @param secp256k1Wasm - a Secp256k1Wasm object
* @param randomSeed - a 32-byte random seed used to randomize the context after
* creation
*/
const wrapSecp256k1Wasm = (
secp256k1Wasm: Secp256k1Wasm,
randomSeed?: Uint8Array
): Secp256k1 => {
/**
* Currently, this wrapper creates a context with both SIGN and VERIFY
* capabilities. For better initialization performance, consumers could
* re-implement a wrapper with only the capabilities they require.
*/
const contextPtr = secp256k1Wasm.contextCreate(ContextFlag.BOTH);
/**
* Since all of these methods are single-threaded and synchronous, we can
* reuse allocated WebAssembly memory for each method without worrying about
* calls interfering with each other. Likewise, these spaces never need to be
* `free`d, since we will continue using them until this entire object (and
* with it, the entire WebAssembly instance) is garbage collected.
*
* If malicious javascript gained access to this object, it should be
* considered a critical vulnerability in the consumer. However, as a best
* practice, we zero out private keys below when we're finished with them.
*/
const sigScratch = secp256k1Wasm.malloc(ByteLength.maxECDSASig);
const publicKeyScratch = secp256k1Wasm.malloc(ByteLength.maxPublicKey);
const messageHashScratch = secp256k1Wasm.malloc(ByteLength.messageHash);
const internalPublicKeyPtr = secp256k1Wasm.malloc(
ByteLength.internalPublicKey
);
const internalSigPtr = secp256k1Wasm.malloc(ByteLength.internalSig);
const schnorrSigPtr = secp256k1Wasm.malloc(ByteLength.schnorrSig);
const privateKeyPtr = secp256k1Wasm.malloc(ByteLength.privateKey);
const internalRSigPtr = secp256k1Wasm.malloc(ByteLength.recoverableSig);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const recoveryNumPtr = secp256k1Wasm.malloc(4);
// eslint-disable-next-line no-bitwise, @typescript-eslint/no-magic-numbers
const recoveryNumPtrView32 = recoveryNumPtr >> 2;
const getRecoveryNumPtr = () => secp256k1Wasm.heapU32[recoveryNumPtrView32];
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const lengthPtr = secp256k1Wasm.malloc(4);
// eslint-disable-next-line no-bitwise, @typescript-eslint/no-magic-numbers
const lengthPtrView32 = lengthPtr >> 2;
const parsePublicKey = (publicKey: Uint8Array) => {
secp256k1Wasm.heapU8.set(publicKey, publicKeyScratch);
return (
secp256k1Wasm.pubkeyParse(
contextPtr,
internalPublicKeyPtr,
publicKeyScratch,
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
publicKey.length as 33 | 65
) === 1
);
};
const setLengthPtr = (value: number) => {
secp256k1Wasm.heapU32.set([value], lengthPtrView32);
};
const getLengthPtr = () => secp256k1Wasm.heapU32[lengthPtrView32];
const serializePublicKey = (length: number, flag: number) => {
setLengthPtr(length);
secp256k1Wasm.pubkeySerialize(
contextPtr,
publicKeyScratch,
lengthPtr,
internalPublicKeyPtr,
flag
);
return secp256k1Wasm.readHeapU8(publicKeyScratch, getLengthPtr()).slice();
};
const getSerializedPublicKey = (compressed: boolean) =>
compressed
? serializePublicKey(
ByteLength.compressedPublicKey,
CompressionFlag.COMPRESSED
)
: serializePublicKey(
ByteLength.uncompressedPublicKey,
CompressionFlag.UNCOMPRESSED
);
const convertPublicKey = (
compressed: boolean
): ((publicKey: Uint8Array) => Uint8Array) => (publicKey) => {
if (!parsePublicKey(publicKey)) {
throw new Error('Failed to parse public key.');
}
return getSerializedPublicKey(compressed);
};
const parseSignature = (signature: Uint8Array, isDer: boolean) => {
secp256k1Wasm.heapU8.set(signature, sigScratch);
return isDer
? secp256k1Wasm.signatureParseDER(
contextPtr,
internalSigPtr,
sigScratch,
signature.length
) === 1
: secp256k1Wasm.signatureParseCompact(
contextPtr,
internalSigPtr,
sigScratch
) === 1;
};
const parseOrThrow = (signature: Uint8Array, isDer: boolean) => {
if (!parseSignature(signature, isDer)) {
throw new Error('Failed to parse signature.');
}
};
const getCompactSig = () => {
secp256k1Wasm.signatureSerializeCompact(
contextPtr,
sigScratch,
internalSigPtr
);
return secp256k1Wasm.readHeapU8(sigScratch, ByteLength.compactSig).slice();
};
const getDERSig = () => {
setLengthPtr(ByteLength.maxECDSASig);
secp256k1Wasm.signatureSerializeDER(
contextPtr,
sigScratch,
lengthPtr,
internalSigPtr
);
return secp256k1Wasm.readHeapU8(sigScratch, getLengthPtr()).slice();
};
const convertSignature = (
wasDER: boolean
): ((signature: Uint8Array) => Uint8Array) => (signature) => {
parseOrThrow(signature, wasDER);
return wasDER ? getCompactSig() : getDERSig();
};
const fillPrivateKeyPtr = (privateKey: Uint8Array) => {
secp256k1Wasm.heapU8.set(privateKey, privateKeyPtr);
};
const zeroOutPtr = (pointer: number, bytes: number) => {
secp256k1Wasm.heapU8.fill(0, pointer, pointer + bytes);
};
const zeroOutPrivateKeyPtr = () => {
zeroOutPtr(privateKeyPtr, ByteLength.privateKey);
};
const withPrivateKey = <T>(
privateKey: Uint8Array,
instructions: () => T
): T => {
fillPrivateKeyPtr(privateKey);
const ret = instructions();
zeroOutPrivateKeyPtr();
return ret;
};
const derivePublicKey = (
compressed: boolean
): ((privateKey: Uint8Array) => Uint8Array) => (privateKey) => {
const invalid = withPrivateKey<boolean>(
privateKey,
() =>
secp256k1Wasm.pubkeyCreate(
contextPtr,
internalPublicKeyPtr,
privateKeyPtr
) !== 1
);
if (invalid) {
throw new Error('Cannot derive public key from invalid private key.');
}
return getSerializedPublicKey(compressed);
};
const fillMessageHashScratch = (messageHash: Uint8Array) => {
secp256k1Wasm.heapU8.set(messageHash, messageHashScratch);
};
const normalizeSignature = () => {
secp256k1Wasm.signatureNormalize(
contextPtr,
internalSigPtr,
internalSigPtr
);
};
const modifySignature = (
isDer: boolean,
normalize: boolean
): ((signature: Uint8Array) => Uint8Array) => (signature) => {
parseOrThrow(signature, isDer);
if (normalize) {
normalizeSignature();
} else {
secp256k1Wasm.signatureMalleate(
contextPtr,
internalSigPtr,
internalSigPtr
);
}
return isDer ? getDERSig() : getCompactSig();
};
const parseAndNormalizeSignature = (
signature: Uint8Array,
isDer: boolean,
normalize: boolean
) => {
const ret = parseSignature(signature, isDer);
if (normalize) {
normalizeSignature();
}
return ret;
};
const signMessageHash = (isDer: boolean) => (
privateKey: Uint8Array,
messageHash: Uint8Array
) => {
fillMessageHashScratch(messageHash);
return withPrivateKey<Uint8Array>(privateKey, () => {
const failed =
secp256k1Wasm.sign(
contextPtr,
internalSigPtr,
messageHashScratch,
privateKeyPtr
) !== 1;
if (failed) {
throw new Error(
'Failed to sign message hash. The private key is not valid.'
);
}
if (isDer) {
setLengthPtr(ByteLength.maxECDSASig);
secp256k1Wasm.signatureSerializeDER(
contextPtr,
sigScratch,
lengthPtr,
internalSigPtr
);
return secp256k1Wasm.readHeapU8(sigScratch, getLengthPtr()).slice();
}
secp256k1Wasm.signatureSerializeCompact(
contextPtr,
sigScratch,
internalSigPtr
);
return secp256k1Wasm
.readHeapU8(sigScratch, ByteLength.compactSig)
.slice();
});
};
const signMessageHashSchnorr = () => (
privateKey: Uint8Array,
messageHash: Uint8Array
) => {
fillMessageHashScratch(messageHash);
return withPrivateKey<Uint8Array>(privateKey, () => {
const failed =
secp256k1Wasm.schnorrSign(
contextPtr,
schnorrSigPtr,
messageHashScratch,
privateKeyPtr
) !== 1;
if (failed) {
throw new Error(
'Failed to sign message hash. The private key is not valid.'
);
}
return secp256k1Wasm
.readHeapU8(schnorrSigPtr, ByteLength.schnorrSig)
.slice();
});
};
const verifyMessage = (messageHash: Uint8Array) => {
fillMessageHashScratch(messageHash);
return (
secp256k1Wasm.verify(
contextPtr,
internalSigPtr,
messageHashScratch,
internalPublicKeyPtr
) === 1
);
};
const verifySignature = (isDer: boolean, normalize: boolean) => (
signature: Uint8Array,
publicKey: Uint8Array,
messageHash: Uint8Array
) =>
parsePublicKey(publicKey) &&
parseAndNormalizeSignature(signature, isDer, normalize) &&
verifyMessage(messageHash);
const verifyMessageSchnorr = (
messageHash: Uint8Array,
signature: Uint8Array
) => {
fillMessageHashScratch(messageHash);
secp256k1Wasm.heapU8.set(signature, schnorrSigPtr);
return (
secp256k1Wasm.schnorrVerify(
contextPtr,
schnorrSigPtr,
messageHashScratch,
internalPublicKeyPtr
) === 1
);
};
const verifySignatureSchnorr = () => (
signature: Uint8Array,
publicKey: Uint8Array,
messageHash: Uint8Array
) =>
parsePublicKey(publicKey)
? verifyMessageSchnorr(messageHash, signature)
: false;
const signMessageHashRecoverable = (
privateKey: Uint8Array,
messageHash: Uint8Array
): RecoverableSignature => {
fillMessageHashScratch(messageHash);
return withPrivateKey<RecoverableSignature>(privateKey, () => {
if (
secp256k1Wasm.signRecoverable(
contextPtr,
internalRSigPtr,
messageHashScratch,
privateKeyPtr
) !== 1
) {
throw new Error(
'Failed to sign message hash. The private key is not valid.'
);
}
secp256k1Wasm.recoverableSignatureSerialize(
contextPtr,
sigScratch,
recoveryNumPtr,
internalRSigPtr
);
return {
recoveryId: getRecoveryNumPtr() as RecoveryId,
signature: secp256k1Wasm
.readHeapU8(sigScratch, ByteLength.compactSig)
.slice(),
};
});
};
const recoverPublicKey = (compressed: boolean) => (
signature: Uint8Array,
recoveryId: RecoveryId,
messageHash: Uint8Array
) => {
fillMessageHashScratch(messageHash);
secp256k1Wasm.heapU8.set(signature, sigScratch);
if (
secp256k1Wasm.recoverableSignatureParse(
contextPtr,
internalRSigPtr,
sigScratch,
recoveryId
) !== 1
) {
throw new Error(
'Failed to recover public key. Could not parse signature.'
);
}
if (
secp256k1Wasm.recover(
contextPtr,
internalPublicKeyPtr,
internalRSigPtr,
messageHashScratch
) !== 1
) {
throw new Error(
'Failed to recover public key. The compact signature, recovery, or message hash is invalid.'
);
}
return getSerializedPublicKey(compressed);
};
const addTweakPrivateKey = (
privateKey: Uint8Array,
tweakValue: Uint8Array
): Uint8Array => {
fillMessageHashScratch(tweakValue);
return withPrivateKey<Uint8Array>(privateKey, () => {
if (
secp256k1Wasm.privkeyTweakAdd(
contextPtr,
privateKeyPtr,
messageHashScratch
) !== 1
) {
throw new Error('Private key is invalid or adding failed.');
}
return secp256k1Wasm
.readHeapU8(privateKeyPtr, ByteLength.privateKey)
.slice();
});
};
const mulTweakPrivateKey = (
privateKey: Uint8Array,
tweakValue: Uint8Array
): Uint8Array => {
fillMessageHashScratch(tweakValue);
return withPrivateKey<Uint8Array>(privateKey, () => {
if (
secp256k1Wasm.privkeyTweakMul(
contextPtr,
privateKeyPtr,
messageHashScratch
) !== 1
) {
throw new Error('Private key is invalid or multiplying failed.');
}
return secp256k1Wasm
.readHeapU8(privateKeyPtr, ByteLength.privateKey)
.slice();
});
};
const addTweakPublicKey = (compressed: boolean) => (
publicKey: Uint8Array,
tweakValue: Uint8Array
) => {
if (!parsePublicKey(publicKey)) {
throw new Error('Failed to parse public key.');
}
fillMessageHashScratch(tweakValue);
if (
secp256k1Wasm.pubkeyTweakAdd(
contextPtr,
internalPublicKeyPtr,
messageHashScratch
) !== 1
) {
throw new Error('Adding failed');
}
return getSerializedPublicKey(compressed);
};
const mulTweakPublicKey = (compressed: boolean) => (
publicKey: Uint8Array,
tweakValue: Uint8Array
) => {
if (!parsePublicKey(publicKey)) {
throw new Error('Failed to parse public key.');
}
fillMessageHashScratch(tweakValue);
if (
secp256k1Wasm.pubkeyTweakMul(
contextPtr,
internalPublicKeyPtr,
messageHashScratch
) !== 1
) {
throw new Error('Multiplying failed');
}
return getSerializedPublicKey(compressed);
};
/**
* The value of this precaution is debatable, especially in the context of
* javascript and WebAssembly.
*
* In the secp256k1 C library, context randomization is an additional layer of
* security from side-channel attacks which attempt to extract private key
* information by analyzing things like a CPU's emitted radio frequencies or
* power usage.
*
* In this library, these attacks seem even less likely, since the "platform"
* on which this code will be executed (e.g. V8) is likely to obscure any
* such signals.
*
* Still, out of an abundance of caution (and because no one has produced a
* definitive proof indicating that this is not helpful), this library exposes
* the ability to randomize the context like the C library. Depending on the
* intended application, consumers can decide whether or not to randomize.
*/
if (randomSeed !== undefined) {
const randomSeedPtr = messageHashScratch;
secp256k1Wasm.heapU8.set(randomSeed, randomSeedPtr);
secp256k1Wasm.contextRandomize(contextPtr, randomSeedPtr);
zeroOutPtr(randomSeedPtr, ByteLength.randomSeed);
}
return {
addTweakPrivateKey,
addTweakPublicKeyCompressed: addTweakPublicKey(true),
addTweakPublicKeyUncompressed: addTweakPublicKey(false),
compressPublicKey: convertPublicKey(true),
derivePublicKeyCompressed: derivePublicKey(true),
derivePublicKeyUncompressed: derivePublicKey(false),
malleateSignatureCompact: modifySignature(false, false),
malleateSignatureDER: modifySignature(true, false),
mulTweakPrivateKey,
mulTweakPublicKeyCompressed: mulTweakPublicKey(true),
mulTweakPublicKeyUncompressed: mulTweakPublicKey(false),
normalizeSignatureCompact: modifySignature(false, true),
normalizeSignatureDER: modifySignature(true, true),
recoverPublicKeyCompressed: recoverPublicKey(true),
recoverPublicKeyUncompressed: recoverPublicKey(false),
signMessageHashCompact: signMessageHash(false),
signMessageHashDER: signMessageHash(true),
signMessageHashRecoverableCompact: signMessageHashRecoverable,
signMessageHashSchnorr: signMessageHashSchnorr(),
signatureCompactToDER: convertSignature(false),
signatureDERToCompact: convertSignature(true),
uncompressPublicKey: convertPublicKey(false),
validatePrivateKey: (privateKey) =>
withPrivateKey<boolean>(
privateKey,
() => secp256k1Wasm.seckeyVerify(contextPtr, privateKeyPtr) === 1
),
verifySignatureCompact: verifySignature(false, true),
verifySignatureCompactLowS: verifySignature(false, false),
verifySignatureDER: verifySignature(true, true),
verifySignatureDERLowS: verifySignature(true, false),
verifySignatureSchnorr: verifySignatureSchnorr(),
};
};
/**
* This method is like `instantiateSecp256k1`, but requires the consumer to
* `Window.fetch` or `fs.readFile` the `secp256k1.wasm` binary and provide it to
* this method as `webassemblyBytes`. This skips a base64 decoding of an
* embedded binary.
*
* ### Randomizing the Context with `randomSeed`
* This method also accepts an optional, 32-byte `randomSeed`, which is passed
* to the `contextRandomize` method in the underlying WebAssembly.
*
* The value of this precaution is debatable, especially in the context of
* javascript and WebAssembly.
*
* In the secp256k1 C library, context randomization is an additional layer of
* security from side-channel attacks which attempt to extract private key
* information by analyzing things like a CPU's emitted radio frequencies or
* power usage.
*
* In this library, these attacks seem even less likely, since the "platform"
* on which this code will be executed (e.g. V8) is likely to obscure any
* such signals.
*
* Still, out of an abundance of caution (and because no one has produced a
* definitive proof indicating that this is not helpful), this library exposes
* the ability to randomize the context like the C library. Depending on the
* intended application, consumers can decide whether or not to randomize.
*
* @param webassemblyBytes - an ArrayBuffer containing the bytes from Libauth's
* `secp256k1.wasm` binary. Providing this buffer manually may be faster than
* the internal base64 decode which happens in `instantiateSecp256k1`.
* @param randomSeed - a 32-byte random seed used to randomize the secp256k1
* context after creation. See above for details.
*/
export const instantiateSecp256k1Bytes = async (
webassemblyBytes: ArrayBuffer,
randomSeed?: Uint8Array
): Promise<Secp256k1> =>
wrapSecp256k1Wasm(
await instantiateSecp256k1WasmBytes(webassemblyBytes),
randomSeed
);
const cachedSecp256k1: { cache?: Promise<Secp256k1> } = {};
/**
* Create and wrap a Secp256k1 WebAssembly instance to expose a set of
* purely-functional Secp256k1 methods. For slightly faster initialization, use
* `instantiateSecp256k1Bytes`.
*
* @param randomSeed - a 32-byte random seed used to randomize the secp256k1
* context after creation. See the description in `instantiateSecp256k1Bytes`
* for details.
*/
export const instantiateSecp256k1 = async (
randomSeed?: Uint8Array
): Promise<Secp256k1> => {
if (cachedSecp256k1.cache !== undefined) {
return cachedSecp256k1.cache;
}
const result = Promise.resolve(
wrapSecp256k1Wasm(await instantiateSecp256k1Wasm(), randomSeed)
);
// eslint-disable-next-line require-atomic-updates, functional/immutable-data
cachedSecp256k1.cache = result;
return result;
};