This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use node crypto for ed25519 signing and verification (#289)
* fix: use node crypto for ed25519 signing and verification In node, use the node crypto module for ed25519 signing/verification now that it's in all LTS releases. Browsers still use the pure-js `@noble/ed25519` implementation. Before: ``` @libp2p/crypto x 484 ops/sec ±0.34% (90 runs sampled) ``` After: ``` @libp2p/crypto x 4,706 ops/sec ±0.81% (84 runs sampled) ``` * chore: pr comments Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com> * chore: avoid array copy * chore: replace all .slice with .subarray Co-authored-by: Marin Petrunić <mpetrunic@users.noreply.github.com>
- Loading branch information
1 parent
d1d0f41
commit 1c623e7
Showing
12 changed files
with
189 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as ed from '@noble/ed25519' | ||
|
||
const PUBLIC_KEY_BYTE_LENGTH = 32 | ||
const PRIVATE_KEY_BYTE_LENGTH = 64 // private key is actually 32 bytes but for historical reasons we concat private and public keys | ||
const KEYS_BYTE_LENGTH = 32 | ||
|
||
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength } | ||
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength } | ||
|
||
export async function generateKey () { | ||
// the actual private key (32 bytes) | ||
const privateKeyRaw = ed.utils.randomPrivateKey() | ||
const publicKey = await ed.getPublicKey(privateKeyRaw) | ||
|
||
// concatenated the public key to the private key | ||
const privateKey = concatKeys(privateKeyRaw, publicKey) | ||
|
||
return { | ||
privateKey, | ||
publicKey | ||
} | ||
} | ||
|
||
/** | ||
* Generate keypair from a 32 byte uint8array | ||
*/ | ||
export async function generateKeyFromSeed (seed: Uint8Array) { | ||
if (seed.length !== KEYS_BYTE_LENGTH) { | ||
throw new TypeError('"seed" must be 32 bytes in length.') | ||
} else if (!(seed instanceof Uint8Array)) { | ||
throw new TypeError('"seed" must be a node.js Buffer, or Uint8Array.') | ||
} | ||
|
||
// based on node forges algorithm, the seed is used directly as private key | ||
const privateKeyRaw = seed | ||
const publicKey = await ed.getPublicKey(privateKeyRaw) | ||
|
||
const privateKey = concatKeys(privateKeyRaw, publicKey) | ||
|
||
return { | ||
privateKey, | ||
publicKey | ||
} | ||
} | ||
|
||
export async function hashAndSign (privateKey: Uint8Array, msg: Uint8Array) { | ||
const privateKeyRaw = privateKey.subarray(0, KEYS_BYTE_LENGTH) | ||
|
||
return await ed.sign(msg, privateKeyRaw) | ||
} | ||
|
||
export async function hashAndVerify (publicKey: Uint8Array, sig: Uint8Array, msg: Uint8Array) { | ||
return await ed.verify(sig, msg, publicKey) | ||
} | ||
|
||
function concatKeys (privateKeyRaw: Uint8Array, publicKey: Uint8Array) { | ||
const privateKey = new Uint8Array(PRIVATE_KEY_BYTE_LENGTH) | ||
for (let i = 0; i < KEYS_BYTE_LENGTH; i++) { | ||
privateKey[i] = privateKeyRaw[i] | ||
privateKey[KEYS_BYTE_LENGTH + i] = publicKey[i] | ||
} | ||
return privateKey | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters