-
-
Notifications
You must be signed in to change notification settings - Fork 17
Optimise performance of getting public keys #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
d5621bb to
61102e5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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);
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.