Optimise performance of getting public keys#213
Merged
Conversation
d5621bb to
61102e5
Compare
There was a problem hiding this comment.
PR Overview
This PR optimises the performance of public key derivation in SLIP10 nodes by moving to synchronous public key computation, reusing computed values, and introducing lazy caching of the public key bytes. Key changes include:
- Refactoring SLIP10Node to use a lazy getter for publicKeyBytes and introducing a guard symbol for validating trusted public keys.
- Adjusting tests and deriver modules (bip39, bip32, shared) to align with the synchronous public key API.
- Updating curve implementations (e.g. ed25519Bip32) to remove asynchronous behavior in getPublicKey.
Reviewed Changes
| File | Description |
|---|---|
| src/SLIP10Node.test.ts | Updated tests to include additional readonly properties and lazy caching. |
| src/SLIP10Node.ts | Refactored constructor and publicKeyBytes handling to use lazy evaluation. |
| src/constants.ts | Added a guard symbol (PUBLIC_KEY_GUARD) for trusted public key validation. |
| src/derivers/bip39.ts | Modified derivation to compute and pass publicKey with proper guard usage. |
| src/derivers/shared.ts | Updated child key derivation to pass publicKey instead of re-computing it. |
| src/curves/curve.ts | Changed getPublicKey API to be synchronous across curve implementations. |
| src/curves/ed25519Bip32.ts | Adjusted getPublicKey to synchronous behavior. |
| src/derivers/bip32.ts | Adapted error handling to work with synchronous public key derivation. |
| src/derivers/cip3.ts | Updated usage of getPublicKey to align with synchronous API changes. |
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Comments suppressed due to low confidence (3)
src/SLIP10Node.ts:343
- Using 'trustedPublicKey' here may lead to publicKeyBytes being undefined when the guard condition is not met, relying solely on lazy computation later. Please verify that this change is intentional and that consumers of SLIP10Node are aware that the public key might be computed lazily.
publicKey: trustedPublicKey,
src/derivers/shared.ts:252
- Ensure that the 'publicKey' passed into derivePublicExtension is already in the expected (compressed) format, as the removal of the asynchronous getPublicKey call assumes a valid public key is provided.
return derivePublicExtension({ parentPublicKey: publicKey, childIndex });
src/derivers/bip39.ts:247
- Explicitly passing 'false' for the compression parameter returns an uncompressed public key; please confirm that downstream consumers (such as fingerprint generation) expect an uncompressed key before further compressing it, ensuring consistent key formats.
const publicKey = curve.getPublicKey(privateKey, false);
FrederikBolding
approved these changes
Mar 7, 2025
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This optimises the performance of getting public keys in a couple of different ways:
Curve.getPublicKeyfunction was changed to always return the public key synchronously. Every curve was already doing public key computations synchronously, so this lets us remove redundantawaits and async functions.SLIP10Node, it's now lazily computed, and cached. This avoids computing it unnecessarily when it's not used.Closes #211.