@@ -20,36 +20,36 @@ primitive. The reason for this is that importing everything from a single file w
20
20
avoided through tree-shaking, but the possibility of it not working properly
21
21
on one of [ the supported bundlers] ( #browser-usage ) is too high.
22
22
23
- * [ Usage] ( #usage )
24
- * [ Dependencies] ( #dependencies )
25
- * [ hashes: sha256, sha512, keccak, ripemd160, blake2b] ( #hashes-sha256-sha512-keccak-ripemd160-blake2b )
26
- * [ kdfs: pbkdf2, scrypt] ( #kdfs-pbkdf2-scrypt )
27
- * [ random: secure randomness] ( #random-secure-randomness )
28
- * [ secp256k1: curve operations] ( #secp256k1-curve-operations )
29
- * [ bn: pairing-friendly curve] ( #bn-pairing-friendly-curve )
30
- * [ bls: pairing-friendly curve] ( #bls-pairing-friendly-curve )
31
- * [ aes: encryption] ( #aes-encryption )
32
- * [ hdkey: bip32 HD wallets] ( #hdkey-bip32-hd-wallets )
33
- * [ bip39: mnemonic phrases] ( #bip39-mnemonic-phrases )
34
- * [ math: utilities] ( #math-utilities )
35
- * [ utils: generic utilities] ( #utils-generic-utilities )
36
- * [ secp256k1-compat: compatibility layer with other libraries] ( #secp256k1-compat-compatibility-layer-with-other-libraries )
37
- * [ All imports] ( #all-imports )
38
- * [ Caveats] ( #caveats )
39
- * [ Browser usage: Rollup setup] ( #browser-usage-rollup-setup )
40
- * [ AES] ( #aes )
41
- * [ Encrypting with passwords] ( #encrypting-with-passwords )
42
- * [ Operation modes] ( #operation-modes )
43
- * [ Padding plaintext messages] ( #padding-plaintext-messages )
44
- * [ How to use the IV parameter] ( #how-to-use-the-iv-parameter )
45
- * [ How to handle errors with this module] ( #how-to-handle-errors-with-this-module )
46
- * [ Upgrading] ( #upgrading )
47
- * [ Changelog] ( #changelog )
48
- * [ From v2 to v3] ( #from-v2-to-v3 )
49
- * [ From v1 to v2] ( #from-v1-to-v2 )
50
- * [ From v0.1 to v1] ( #from-v01-to-v1 )
51
- * [ Security] ( #security )
52
- * [ License] ( #license )
23
+ - [ Usage] ( #usage )
24
+ - [ Dependencies] ( #dependencies )
25
+ - [ hashes: sha256, sha512, keccak, ripemd160, blake2b] ( #hashes-sha256-sha512-keccak-ripemd160-blake2b )
26
+ - [ kdfs: pbkdf2, scrypt] ( #kdfs-pbkdf2-scrypt )
27
+ - [ random: secure randomness] ( #random-secure-randomness )
28
+ - [ secp256k1: curve operations] ( #secp256k1-curve-operations )
29
+ - [ bn: pairing-friendly curve] ( #bn-pairing-friendly-curve )
30
+ - [ bls: pairing-friendly curve] ( #bls-pairing-friendly-curve )
31
+ - [ aes: encryption] ( #aes-encryption )
32
+ - [ hdkey: bip32 HD wallets] ( #hdkey-bip32-hd-wallets )
33
+ - [ bip39: mnemonic phrases] ( #bip39-mnemonic-phrases )
34
+ - [ math: utilities] ( #math-utilities )
35
+ - [ utils: generic utilities] ( #utils-generic-utilities )
36
+ - [ secp256k1-compat: compatibility layer with other libraries] ( #secp256k1-compat-compatibility-layer-with-other-libraries )
37
+ - [ All imports] ( #all-imports )
38
+ - [ Caveats] ( #caveats )
39
+ - [ Browser usage: Rollup setup] ( #browser-usage-rollup-setup )
40
+ - [ AES] ( #aes )
41
+ - [ Encrypting with passwords] ( #encrypting-with-passwords )
42
+ - [ Operation modes] ( #operation-modes )
43
+ - [ Padding plaintext messages] ( #padding-plaintext-messages )
44
+ - [ How to use the IV parameter] ( #how-to-use-the-iv-parameter )
45
+ - [ How to handle errors with this module] ( #how-to-handle-errors-with-this-module )
46
+ - [ Upgrading] ( #upgrading )
47
+ - [ Changelog] ( #changelog )
48
+ - [ From v2 to v3] ( #from-v2-to-v3 )
49
+ - [ From v1 to v2] ( #from-v1-to-v2 )
50
+ - [ From v0.1 to v1] ( #from-v01-to-v1 )
51
+ - [ Security] ( #security )
52
+ - [ License] ( #license )
53
53
54
54
### Dependencies
55
55
@@ -61,23 +61,28 @@ re-export of 6 audited [noble & scure libraries](https://paulmillr.com/noble/):
61
61
62
62
ethereum-cryptography pins versions of the libraries to ensure good
63
63
protection against supply chain attacks. Ideally, your app would also
64
- pin version of ethereum-cryptography. That means, no ` ^3.0 .0 ` - use ` 3.0 .0 ` instead.
64
+ pin version of ethereum-cryptography. That means, no ` ^3.1 .0 ` - use ` 3.1 .0 ` instead.
65
65
66
66
### hashes: sha256, sha512, keccak, ripemd160, blake2b
67
67
68
68
``` js
69
69
import { sha256 } from " ethereum-cryptography/sha256.js" ;
70
70
import { sha512 } from " ethereum-cryptography/sha512.js" ;
71
- import { keccak256 , keccak224 , keccak384 , keccak512 } from " ethereum-cryptography/keccak.js" ;
71
+ import {
72
+ keccak256 ,
73
+ keccak224 ,
74
+ keccak384 ,
75
+ keccak512 ,
76
+ } from " ethereum-cryptography/keccak.js" ;
72
77
import { ripemd160 } from " ethereum-cryptography/ripemd160.js" ;
73
78
import { blake2b } from " ethereum-cryptography/blake2b.js" ;
74
- sha256 (Uint8Array .from ([1 , 2 , 3 ])) // A: buffers
79
+ sha256 (Uint8Array .from ([1 , 2 , 3 ])); // A: buffers
75
80
76
81
import { utf8ToBytes } from " ethereum-cryptography/utils.js" ;
77
- sha256 (utf8ToBytes (" abc" )) // B: strings
82
+ sha256 (utf8ToBytes (" abc" )); // B: strings
78
83
79
84
import { bytesToHex as toHex } from " ethereum-cryptography/utils.js" ;
80
- toHex (sha256 (utf8ToBytes (" abc" ))) // C: hex
85
+ toHex (sha256 (utf8ToBytes (" abc" ))); // C: hex
81
86
```
82
87
83
88
### kdfs: pbkdf2, scrypt
@@ -88,8 +93,8 @@ import { scrypt, scryptSync } from "ethereum-cryptography/scrypt.js";
88
93
import { utf8ToBytes } from " ethereum-cryptography/utils.js" ;
89
94
90
95
// Pass Uint8Array, or convert strings to Uint8Array
91
- const pass = utf8ToBytes (" password" )
92
- const salt = utf8ToBytes (" salt" )
96
+ const pass = utf8ToBytes (" password" );
97
+ const salt = utf8ToBytes (" salt" );
93
98
const iters = 131072 ;
94
99
const outLength = 32 ;
95
100
console .log (await pbkdf2 (pass, salt, iters, outLength, " sha256" ));
@@ -130,8 +135,10 @@ pseudo-random data in synchronous and asynchronous ways. Backed by [`crypto.getR
130
135
``` js
131
136
import { secp256k1 } from " ethereum-cryptography/secp256k1.js" ;
132
137
// You pass either a hex string, or Uint8Array
133
- const privateKey = " 6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e" ;
134
- const messageHash = " a33321f98e4ff1c283c76998f14f57447545d339b3db534c6d886decb4209f28" ;
138
+ const privateKey =
139
+ " 6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e" ;
140
+ const messageHash =
141
+ " a33321f98e4ff1c283c76998f14f57447545d339b3db534c6d886decb4209f28" ;
135
142
const publicKey = secp256k1 .getPublicKey (privateKey);
136
143
const signature = secp256k1 .sign (messageHash, privateKey);
137
144
const isSigned = secp256k1 .verify (signature, messageHash, publicKey);
@@ -148,11 +155,7 @@ compromised.
148
155
``` js
149
156
import { bn } from " ethereum-cryptography/bls.js" ;
150
157
151
- console .log (
152
- bn254 .G1 ,
153
- bn254 .G2 ,
154
- bn254 .pairing
155
- )
158
+ console .log (bn254 .G1 , bn254 .G2 , bn254 .pairing );
156
159
```
157
160
158
161
For example usage, check out [ the implementation of bn254 EVM precompiles] ( https://github.com/paulmillr/noble-curves/blob/3ed792f8ad9932765b84d1064afea8663a255457/test/bn254.test.js#L697 ) .
@@ -163,8 +166,9 @@ For example usage, check out [the implementation of bn254 EVM precompiles](https
163
166
import { bls12_381 as bls } from " ethereum-cryptography/bls.js" ;
164
167
165
168
// G1 keys, G2 signatures
166
- const privateKey = ' 67d53f170b908cabb9eb326c3c337762d59289a8fec79f7bc9254b584b73265c' ;
167
- const message = ' 64726e3da8' ;
169
+ const privateKey =
170
+ " 67d53f170b908cabb9eb326c3c337762d59289a8fec79f7bc9254b584b73265c" ;
171
+ const message = " 64726e3da8" ;
168
172
const publicKey = bls .getPublicKey (privateKey);
169
173
const signature = bls .sign (message, privateKey);
170
174
const isValid = bls .verify (signature, message, publicKey);
@@ -177,12 +181,15 @@ console.log({ publicKey, signature, isValid });
177
181
// aggregateShortSignatures(signatures)
178
182
179
183
// Custom DST
180
- const htfEthereum = { DST : ' BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_' };
184
+ const htfEthereum = { DST : " BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_" };
181
185
const signatureEth = bls .sign (message, privateKey, htfEthereum);
182
186
const isValidEth = bls .verify (signature, message, publicKey, htfEthereum);
183
187
184
188
// Aggregation
185
- const aggregatedKey = bls .aggregatePublicKeys ([bls .utils .randomPrivateKey (), bls .utils .randomPrivateKey ()])
189
+ const aggregatedKey = bls .aggregatePublicKeys ([
190
+ bls .utils .randomPrivateKey (),
191
+ bls .utils .randomPrivateKey (),
192
+ ]);
186
193
// const aggregatedSig = bls.aggregateSignatures(sigs)
187
194
188
195
// Pairings, with and without final exponentiation
@@ -256,7 +263,7 @@ const mn = bip39.generateMnemonic(wordlist);
256
263
console .log (mn);
257
264
258
265
// Reversible: Converts mnemonic string to raw entropy in form of byte array.
259
- const ent = bip39 .mnemonicToEntropy (mn, wordlist)
266
+ const ent = bip39 .mnemonicToEntropy (mn, wordlist);
260
267
261
268
// Reversible: Converts raw entropy in form of byte array to mnemonic string.
262
269
bip39 .entropyToMnemonic (ent, wordlist);
@@ -265,8 +272,8 @@ bip39.entropyToMnemonic(ent, wordlist);
265
272
bip39 .validateMnemonic (mn, wordlist);
266
273
267
274
// Irreversible: Uses KDF to derive 64 bytes of key data from mnemonic + optional password.
268
- await bip39 .mnemonicToSeed (mn, ' password' );
269
- bip39 .mnemonicToSeedSync (mn, ' password' );
275
+ await bip39 .mnemonicToSeed (mn, " password" );
276
+ bip39 .mnemonicToSeedSync (mn, " password" );
270
277
```
271
278
272
279
The ` bip39 ` submodule provides functions to generate, validate and use seed
@@ -292,7 +299,10 @@ import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils.js";
292
299
### secp256k1-compat: compatibility layer with other libraries
293
300
294
301
``` js
295
- import { createPrivateKeySync , ecdsaSign } from " ethereum-cryptography/secp256k1-compat" ;
302
+ import {
303
+ createPrivateKeySync ,
304
+ ecdsaSign ,
305
+ } from " ethereum-cryptography/secp256k1-compat" ;
296
306
const msgHash = Uint8Array .from (
297
307
" 82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" ,
298
308
" hex"
@@ -311,7 +321,12 @@ The API of `secp256k1-compat` is the same as [secp256k1-node](https://github.com
311
321
``` js
312
322
import { sha256 } from " ethereum-cryptography/sha256.js" ;
313
323
import { sha512 } from " ethereum-cryptography/sha512.js" ;
314
- import { keccak256 , keccak224 , keccak384 , keccak512 } from " ethereum-cryptography/keccak.js" ;
324
+ import {
325
+ keccak256 ,
326
+ keccak224 ,
327
+ keccak384 ,
328
+ keccak512 ,
329
+ } from " ethereum-cryptography/keccak.js" ;
315
330
import { ripemd160 } from " ethereum-cryptography/ripemd160.js" ;
316
331
import { blake2b } from " ethereum-cryptography/blake2b.js" ;
317
332
@@ -341,19 +356,19 @@ import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils.js";
341
356
342
357
Using this library with Rollup requires the following plugins:
343
358
344
- * [ ` @rollup/plugin-commonjs ` ] ( https://www.npmjs.com/package/@rollup/plugin-commonjs )
345
- * [ ` @rollup/plugin-node-resolve ` ] ( https://www.npmjs.com/package/@rollup/plugin-node-resolve )
359
+ - [ ` @rollup/plugin-commonjs ` ] ( https://www.npmjs.com/package/@rollup/plugin-commonjs )
360
+ - [ ` @rollup/plugin-node-resolve ` ] ( https://www.npmjs.com/package/@rollup/plugin-node-resolve )
346
361
347
362
These can be used by setting your ` plugins ` array like this:
348
363
349
364
``` js
350
- plugins: [
351
- commonjs (),
352
- resolve ({
353
- browser: true ,
354
- preferBuiltins: false ,
355
- }),
356
- ]
365
+ plugins: [
366
+ commonjs (),
367
+ resolve ({
368
+ browser: true ,
369
+ preferBuiltins: false ,
370
+ }),
371
+ ];
357
372
```
358
373
359
374
### AES
@@ -424,17 +439,17 @@ exception.
424
439
425
440
### Changelog
426
441
427
- * v3.0 (Sep 2024): new modules ` bls ` , ` bn ` , ` math `
428
- change async AES to non-native sync,
429
- improve typescript compatibility, new dependency [ noble-ciphers] ( https://github.com/paulmillr/noble-ciphers )
430
- * v2.0 (Apr 2023): switched
431
- [ noble-secp256k1] ( https://github.com/paulmillr/noble-secp256k1 ) to
432
- [ noble-curves] ( https://github.com/paulmillr/noble-curves ) ,
433
- which changes re-exported api of ` secp256k1 ` submodule.
434
- * v1.0 (Jan 2022): rewritten the library from
435
- scratch and [ audited] ( #security ) it. It became ** 6x smaller:** ~ 5,000 lines of
436
- code instead of ~ 24,000 (with all deps); 650KB instead of 10.2MB.
437
- 5 dependencies by 1 author are now used, instead of 38 by 5 authors.
442
+ - v3.0 (Sep 2024): new modules ` bls ` , ` bn ` , ` math `
443
+ change async AES to non-native sync,
444
+ improve typescript compatibility, new dependency [ noble-ciphers] ( https://github.com/paulmillr/noble-ciphers )
445
+ - v2.0 (Apr 2023): switched
446
+ [ noble-secp256k1] ( https://github.com/paulmillr/noble-secp256k1 ) to
447
+ [ noble-curves] ( https://github.com/paulmillr/noble-curves ) ,
448
+ which changes re-exported api of ` secp256k1 ` submodule.
449
+ - v1.0 (Jan 2022): rewritten the library from
450
+ scratch and [ audited] ( #security ) it. It became ** 6x smaller:** ~ 5,000 lines of
451
+ code instead of ~ 24,000 (with all deps); 650KB instead of 10.2MB.
452
+ 5 dependencies by 1 author are now used, instead of 38 by 5 authors.
438
453
439
454
### From v2 to v3
440
455
@@ -444,22 +459,22 @@ code instead of ~24,000 (with all deps); 650KB instead of 10.2MB.
444
459
### From v1 to v2
445
460
446
461
1 . ` secp256k1 ` module was changed massively:
447
- before, it was using [ noble-secp256k1 1.7] ( https://github.com/paulmillr/noble-secp256k1 ) ;
448
- now it uses safer [ noble-curves] ( https://github.com/paulmillr/noble-curves ) . Please refer
449
- to [ upgrading section from curves README] ( https://github.com/paulmillr/noble-curves#upgrading ) .
450
- Main changes to keep in mind: a) ` sign ` now returns ` Signature ` instance
451
- b) ` recoverPublicKey ` got moved onto a ` Signature ` instance
462
+ before, it was using [ noble-secp256k1 1.7] ( https://github.com/paulmillr/noble-secp256k1 ) ;
463
+ now it uses safer [ noble-curves] ( https://github.com/paulmillr/noble-curves ) . Please refer
464
+ to [ upgrading section from curves README] ( https://github.com/paulmillr/noble-curves#upgrading ) .
465
+ Main changes to keep in mind: a) ` sign ` now returns ` Signature ` instance
466
+ b) ` recoverPublicKey ` got moved onto a ` Signature ` instance
452
467
2 . node.js 14 and older support was dropped. Upgrade to node.js 16 or later.
453
468
454
469
### From v0.1 to v1
455
470
456
471
All old APIs remain the same except for the breaking changes:
457
472
458
473
1 . We return ` Uint8Array ` from all methods that worked with ` Buffer ` before.
459
- ` Buffer ` has never been supported in browsers, while ` Uint8Array ` s are supported natively in both
460
- browsers and node.js.
474
+ ` Buffer ` has never been supported in browsers, while ` Uint8Array ` s are supported natively in both
475
+ browsers and node.js.
461
476
2 . We target runtimes with [ bigint] ( https://caniuse.com/bigint ) support,
462
- which is Chrome 67+, Edge 79+, Firefox 68+, Safari 14+, node.js 10+. If you need to support older runtimes, use
` [email protected] `
477
+ which is Chrome 67+, Edge 79+, Firefox 68+, Safari 14+, node.js 10+. If you need to support older runtimes, use
` [email protected] `
463
478
3 . If you've used ` secp256k1 ` , [ rename it to ` secp256k1-compat ` ] ( #legacy-secp256k1-compatibility-layer )
464
479
465
480
``` js
0 commit comments