Skip to content

Conversation

@Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Mar 7, 2025

This optimises the performance of getting public keys in a couple of different ways:

  • The Curve.getPublicKey function was changed to always return the public key synchronously. Every curve was already doing public key computations synchronously, so this lets us remove redundant awaits and async functions.
  • In cases where we need to compute the public key (to get the fingerprint for example), that public key is now re-used when possible, to avoid computing it twice.
  • If a public key is not provided to SLIP10Node, it's now lazily computed, and cached. This avoids computing it unnecessarily when it's not used.

Closes #211.

@Mrtenz Mrtenz force-pushed the mrtenz/optimise-public-key branch from d5621bb to 61102e5 Compare March 7, 2025 11:40
@Mrtenz Mrtenz requested a review from Copilot March 7, 2025 11:57
Copy link

Copilot AI left a 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);

@Mrtenz Mrtenz marked this pull request as ready for review March 7, 2025 12:16
@Mrtenz Mrtenz requested a review from a team as a code owner March 7, 2025 12:16
@Mrtenz Mrtenz merged commit b74082a into main Mar 7, 2025
21 checks passed
@Mrtenz Mrtenz deleted the mrtenz/optimise-public-key branch March 7, 2025 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Re-use public key computations more when deriving

3 participants