@@ -18,36 +18,36 @@ primitive. The reason for this is that importing everything from a single file w
1818avoided through tree-shaking, but the possibility of it not working properly
1919on one of [ the supported bundlers] ( #browser-usage ) is too high.
2020
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 )
5151
5252### Dependencies
5353
@@ -59,23 +59,28 @@ re-export of 6 audited [noble & scure libraries](https://paulmillr.com/noble/):
5959
6060ethereum-cryptography pins versions of the libraries to ensure good
6161protection 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.
6363
6464### hashes: sha256, sha512, keccak, ripemd160, blake2b
6565
6666``` js
6767import { sha256 } from " ethereum-cryptography/sha256.js" ;
6868import { 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" ;
7075import { ripemd160 } from " ethereum-cryptography/ripemd160.js" ;
7176import { blake2b } from " ethereum-cryptography/blake2b.js" ;
72- sha256 (Uint8Array .from ([1 , 2 , 3 ])) // A: buffers
77+ sha256 (Uint8Array .from ([1 , 2 , 3 ])); // A: buffers
7378
7479import { utf8ToBytes } from " ethereum-cryptography/utils.js" ;
75- sha256 (utf8ToBytes (" abc" )) // B: strings
80+ sha256 (utf8ToBytes (" abc" )); // B: strings
7681
7782import { bytesToHex as toHex } from " ethereum-cryptography/utils.js" ;
78- toHex (sha256 (utf8ToBytes (" abc" ))) // C: hex
83+ toHex (sha256 (utf8ToBytes (" abc" ))); // C: hex
7984```
8085
8186### kdfs: pbkdf2, scrypt
@@ -86,8 +91,8 @@ import { scrypt, scryptSync } from "ethereum-cryptography/scrypt.js";
8691import { utf8ToBytes } from " ethereum-cryptography/utils.js" ;
8792
8893// 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" );
9196const iters = 131072 ;
9297const outLength = 32 ;
9398console .log (await pbkdf2 (pass, salt, iters, outLength, " sha256" ));
@@ -128,8 +133,10 @@ pseudo-random data in synchronous and asynchronous ways. Backed by [`crypto.getR
128133``` js
129134import { secp256k1 } from " ethereum-cryptography/secp256k1.js" ;
130135// 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" ;
133140const publicKey = secp256k1 .getPublicKey (privateKey);
134141const signature = secp256k1 .sign (messageHash, privateKey);
135142const isSigned = secp256k1 .verify (signature, messageHash, publicKey);
@@ -146,11 +153,7 @@ compromised.
146153``` js
147154import { bn } from " ethereum-cryptography/bls.js" ;
148155
149- console .log (
150- bn254 .G1 ,
151- bn254 .G2 ,
152- bn254 .pairing
153- )
156+ console .log (bn254 .G1 , bn254 .G2 , bn254 .pairing );
154157```
155158
156159For 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
161164import { bls12_381 as bls } from " ethereum-cryptography/bls.js" ;
162165
163166// G1 keys, G2 signatures
164- const privateKey = ' 67d53f170b908cabb9eb326c3c337762d59289a8fec79f7bc9254b584b73265c' ;
165- const message = ' 64726e3da8' ;
167+ const privateKey =
168+ " 67d53f170b908cabb9eb326c3c337762d59289a8fec79f7bc9254b584b73265c" ;
169+ const message = " 64726e3da8" ;
166170const publicKey = bls .getPublicKey (privateKey);
167171const signature = bls .sign (message, privateKey);
168172const isValid = bls .verify (signature, message, publicKey);
@@ -175,12 +179,15 @@ console.log({ publicKey, signature, isValid });
175179// aggregateShortSignatures(signatures)
176180
177181// 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_" };
179183const signatureEth = bls .sign (message, privateKey, htfEthereum);
180184const isValidEth = bls .verify (signature, message, publicKey, htfEthereum);
181185
182186// 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+ ]);
184191// const aggregatedSig = bls.aggregateSignatures(sigs)
185192
186193// Pairings, with and without final exponentiation
@@ -254,7 +261,7 @@ const mn = bip39.generateMnemonic(wordlist);
254261console .log (mn);
255262
256263// 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);
258265
259266// Reversible: Converts raw entropy in form of byte array to mnemonic string.
260267bip39 .entropyToMnemonic (ent, wordlist);
@@ -263,8 +270,8 @@ bip39.entropyToMnemonic(ent, wordlist);
263270bip39 .validateMnemonic (mn, wordlist);
264271
265272// 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" );
268275```
269276
270277The ` bip39 ` submodule provides functions to generate, validate and use seed
@@ -290,7 +297,10 @@ import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils.js";
290297### secp256k1-compat: compatibility layer with other libraries
291298
292299``` js
293- import { createPrivateKeySync , ecdsaSign } from " ethereum-cryptography/secp256k1-compat" ;
300+ import {
301+ createPrivateKeySync ,
302+ ecdsaSign ,
303+ } from " ethereum-cryptography/secp256k1-compat" ;
294304const msgHash = Uint8Array .from (
295305 " 82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28" ,
296306 " hex"
@@ -309,7 +319,12 @@ The API of `secp256k1-compat` is the same as [secp256k1-node](https://github.com
309319``` js
310320import { sha256 } from " ethereum-cryptography/sha256.js" ;
311321import { 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" ;
313328import { ripemd160 } from " ethereum-cryptography/ripemd160.js" ;
314329import { blake2b } from " ethereum-cryptography/blake2b.js" ;
315330
@@ -339,19 +354,19 @@ import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils.js";
339354
340355Using this library with Rollup requires the following plugins:
341356
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 )
344359
345360These can be used by setting your ` plugins ` array like this:
346361
347362``` 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+ ];
355370```
356371
357372### AES
@@ -422,17 +437,17 @@ exception.
422437
423438### Changelog
424439
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.
436451
437452### From v2 to v3
438453
@@ -442,22 +457,22 @@ code instead of ~24,000 (with all deps); 650KB instead of 10.2MB.
442457### From v1 to v2
443458
4444591 . ` 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
4504652 . node.js 14 and older support was dropped. Upgrade to node.js 16 or later.
451466
452467### From v0.1 to v1
453468
454469All old APIs remain the same except for the breaking changes:
455470
4564711 . 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.
4594742 . 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] ` 4614763 . If you've used ` secp256k1 ` , [ rename it to ` secp256k1-compat ` ] ( #legacy-secp256k1-compatibility-layer )
462477
463478``` js
0 commit comments