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