-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support streaming hashes for key sign/verify (#2255)
Accept `Uint8ArrayList`s as arguments to sign or verify and use streaming hashes internally when they are passed. This way we can skip having to copy the `Uint8ArrayList` contents into a `Uint8Array` first.
- Loading branch information
1 parent
f3ec538
commit ac7bc38
Showing
14 changed files
with
232 additions
and
59 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
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,71 @@ | ||
import { CodeError } from '@libp2p/interface/errors' | ||
import { secp256k1 as secp } from '@noble/curves/secp256k1' | ||
import { sha256 } from 'multiformats/hashes/sha2' | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
|
||
const PRIVATE_KEY_BYTE_LENGTH = 32 | ||
|
||
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength } | ||
|
||
export function generateKey (): Uint8Array { | ||
return secp.utils.randomPrivateKey() | ||
} | ||
|
||
/** | ||
* Hash and sign message with private key | ||
*/ | ||
export async function hashAndSign (key: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<Uint8Array> { | ||
const { digest } = await sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray()) | ||
try { | ||
const signature = secp.sign(digest, key) | ||
return signature.toDERRawBytes() | ||
} catch (err) { | ||
throw new CodeError(String(err), 'ERR_INVALID_INPUT') | ||
} | ||
} | ||
|
||
/** | ||
* Hash message and verify signature with public key | ||
*/ | ||
export async function hashAndVerify (key: Uint8Array, sig: Uint8Array, msg: Uint8Array | Uint8ArrayList): Promise<boolean> { | ||
try { | ||
const { digest } = await sha256.digest(msg instanceof Uint8Array ? msg : msg.subarray()) | ||
return secp.verify(sig, digest, key) | ||
} catch (err) { | ||
throw new CodeError(String(err), 'ERR_INVALID_INPUT') | ||
} | ||
} | ||
|
||
export function compressPublicKey (key: Uint8Array): Uint8Array { | ||
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(true) | ||
return point | ||
} | ||
|
||
export function decompressPublicKey (key: Uint8Array): Uint8Array { | ||
const point = secp.ProjectivePoint.fromHex(key).toRawBytes(false) | ||
return point | ||
} | ||
|
||
export function validatePrivateKey (key: Uint8Array): void { | ||
try { | ||
secp.getPublicKey(key, true) | ||
} catch (err) { | ||
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY') | ||
} | ||
} | ||
|
||
export function validatePublicKey (key: Uint8Array): void { | ||
try { | ||
secp.ProjectivePoint.fromHex(key) | ||
} catch (err) { | ||
throw new CodeError(String(err), 'ERR_INVALID_PUBLIC_KEY') | ||
} | ||
} | ||
|
||
export function computePublicKey (privateKey: Uint8Array): Uint8Array { | ||
try { | ||
return secp.getPublicKey(privateKey, true) | ||
} catch (err) { | ||
throw new CodeError(String(err), 'ERR_INVALID_PRIVATE_KEY') | ||
} | ||
} |
Oops, something went wrong.