@@ -20,36 +20,36 @@ primitive. The reason for this is that importing everything from a single file w
2020avoided through tree-shaking, but the possibility of it not working properly
2121on one of [ the supported bundlers] ( #browser-usage ) is too high.
2222
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 )
5353
5454### Dependencies
5555
@@ -61,23 +61,28 @@ re-export of 6 audited [noble & scure libraries](https://paulmillr.com/noble/):
6161
6262ethereum-cryptography pins versions of the libraries to ensure good
6363protection 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.
6565
6666### hashes: sha256, sha512, keccak, ripemd160, blake2b
6767
6868``` js
6969import { sha256 } from " ethereum-cryptography/sha256.js" ;
7070import { 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" ;
7277import { ripemd160 } from " ethereum-cryptography/ripemd160.js" ;
7378import { blake2b } from " ethereum-cryptography/blake2b.js" ;
74- sha256 (Uint8Array .from ([1 , 2 , 3 ])) // A: buffers
79+ sha256 (Uint8Array .from ([1 , 2 , 3 ])); // A: buffers
7580
7681import { utf8ToBytes } from " ethereum-cryptography/utils.js" ;
77- sha256 (utf8ToBytes (" abc" )) // B: strings
82+ sha256 (utf8ToBytes (" abc" )); // B: strings
7883
7984import { bytesToHex as toHex } from " ethereum-cryptography/utils.js" ;
80- toHex (sha256 (utf8ToBytes (" abc" ))) // C: hex
85+ toHex (sha256 (utf8ToBytes (" abc" ))); // C: hex
8186```
8287
8388### kdfs: pbkdf2, scrypt
@@ -88,8 +93,8 @@ import { scrypt, scryptSync } from "ethereum-cryptography/scrypt.js";
8893import { utf8ToBytes } from " ethereum-cryptography/utils.js" ;
8994
9095// 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" );
9398const iters = 131072 ;
9499const outLength = 32 ;
95100console .log (await pbkdf2 (pass, salt, iters, outLength, " sha256" ));
@@ -130,8 +135,10 @@ pseudo-random data in synchronous and asynchronous ways. Backed by [`crypto.getR
130135``` js
131136import { secp256k1 } from " ethereum-cryptography/secp256k1.js" ;
132137// 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" ;
135142const publicKey = secp256k1 .getPublicKey (privateKey);
136143const signature = secp256k1 .sign (messageHash, privateKey);
137144const isSigned = secp256k1 .verify (signature, messageHash, publicKey);
@@ -148,11 +155,7 @@ compromised.
148155``` js
149156import { bn } from " ethereum-cryptography/bls.js" ;
150157
151- console .log (
152- bn254 .G1 ,
153- bn254 .G2 ,
154- bn254 .pairing
155- )
158+ console .log (bn254 .G1 , bn254 .G2 , bn254 .pairing );
156159```
157160
158161For 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
163166import { bls12_381 as bls } from " ethereum-cryptography/bls.js" ;
164167
165168// G1 keys, G2 signatures
166- const privateKey = ' 67d53f170b908cabb9eb326c3c337762d59289a8fec79f7bc9254b584b73265c' ;
167- const message = ' 64726e3da8' ;
169+ const privateKey =
170+ " 67d53f170b908cabb9eb326c3c337762d59289a8fec79f7bc9254b584b73265c" ;
171+ const message = " 64726e3da8" ;
168172const publicKey = bls .getPublicKey (privateKey);
169173const signature = bls .sign (message, privateKey);
170174const isValid = bls .verify (signature, message, publicKey);
@@ -177,12 +181,15 @@ console.log({ publicKey, signature, isValid });
177181// aggregateShortSignatures(signatures)
178182
179183// 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_" };
181185const signatureEth = bls .sign (message, privateKey, htfEthereum);
182186const isValidEth = bls .verify (signature, message, publicKey, htfEthereum);
183187
184188// 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+ ]);
186193// const aggregatedSig = bls.aggregateSignatures(sigs)
187194
188195// Pairings, with and without final exponentiation
@@ -256,7 +263,7 @@ const mn = bip39.generateMnemonic(wordlist);
256263console .log (mn);
257264
258265// 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);
260267
261268// Reversible: Converts raw entropy in form of byte array to mnemonic string.
262269bip39 .entropyToMnemonic (ent, wordlist);
@@ -265,8 +272,8 @@ bip39.entropyToMnemonic(ent, wordlist);
265272bip39 .validateMnemonic (mn, wordlist);
266273
267274// 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" );
270277```
271278
272279The ` bip39 ` submodule provides functions to generate, validate and use seed
@@ -292,7 +299,10 @@ import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils.js";
292299### secp256k1-compat: compatibility layer with other libraries
293300
294301``` js
295- import { createPrivateKeySync , ecdsaSign } from " ethereum-cryptography/secp256k1-compat" ;
302+ import {
303+ createPrivateKeySync ,
304+ ecdsaSign ,
305+ } from " ethereum-cryptography/secp256k1-compat" ;
296306const msgHash = Uint8Array .from (
297307 " 82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" ,
298308 " hex"
@@ -311,7 +321,12 @@ The API of `secp256k1-compat` is the same as [secp256k1-node](https://github.com
311321``` js
312322import { sha256 } from " ethereum-cryptography/sha256.js" ;
313323import { 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" ;
315330import { ripemd160 } from " ethereum-cryptography/ripemd160.js" ;
316331import { blake2b } from " ethereum-cryptography/blake2b.js" ;
317332
@@ -341,19 +356,19 @@ import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils.js";
341356
342357Using this library with Rollup requires the following plugins:
343358
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 )
346361
347362These can be used by setting your ` plugins ` array like this:
348363
349364``` 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+ ];
357372```
358373
359374### AES
@@ -424,17 +439,17 @@ exception.
424439
425440### Changelog
426441
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.
438453
439454### From v2 to v3
440455
@@ -444,22 +459,22 @@ code instead of ~24,000 (with all deps); 650KB instead of 10.2MB.
444459### From v1 to v2
445460
4464611 . ` 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
4524672 . node.js 14 and older support was dropped. Upgrade to node.js 16 or later.
453468
454469### From v0.1 to v1
455470
456471All old APIs remain the same except for the breaking changes:
457472
4584731 . 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.
4614762 . 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] ` 4634783 . If you've used ` secp256k1 ` , [ rename it to ` secp256k1-compat ` ] ( #legacy-secp256k1-compatibility-layer )
464479
465480``` js
0 commit comments