feat: Add X25519 key derivation functions - #10503
Merged
Merged
Conversation
Mrtenz
reviewed
Sep 28, 2026
FrederikBolding
force-pushed
the
fb/x25519
branch
from
September 28, 2026 13:49
535b6e2 to
3b51f9c
Compare
FrederikBolding
marked this pull request as ready for review
September 29, 2026 08:57
Mrtenz
reviewed
Sep 29, 2026
| export async function getPublicKey( | ||
| privateKey: BufferSource, | ||
| ): Promise<Uint8Array> { | ||
| return getSharedSecret(privateKey, X25519_BASE_POINT); |
Member
There was a problem hiding this comment.
While the underlying functionality is correct here, using getSharedSecret to get a public key is a bit confusing. Maybe we should extract the functionality into a new function (scalarMultiply or something), and call that from getPublicKey and getSharedSecret.
async function scalarMultiply(privateKey: BufferSource, publicKey: BufferSource): Promise<Uint8Array> {
// Current implementation of `getSharedSecret`.
}
export async function getPublicKey(privateKey: BufferSource): Promise<Uint8Array> {
return await scalarMultiply(privateKey, X25519_BASE_POINT);
}
export async function getSharedSecret(privateKey: BufferSource, publicKey: BufferSource): Promise<Uint8Array> {
return await scalarMultiply(privateKey, publicKey);
}
Member
Author
There was a problem hiding this comment.
Yeah, that's a good idea. It's a bit of a hack, so good to make it clearer.
Mrtenz
approved these changes
Sep 29, 2026
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.
Explanation
Add functions for deriving X25519 public keys and shared secrets.
React Native benchmark:
References
https://consensyssoftware.atlassian.net/browse/WPC-1336
Checklist
Note
Medium Risk
Introduces new cryptographic key-agreement APIs where misuse could affect secret derivation, though behavior is delegated to WebCrypto with strict length checks and RFC test vectors.
Overview
Adds X25519 key agreement to
@metamask/cryptographyas a dedicated subpath export (@metamask/cryptography/x25519), exposinggetPublicKeyandgetSharedSecretfor 32-byte keys.Both operations share a WebCrypto
deriveBitspath: raw private keys are wrapped in a fixed PKCS#8 header before import, public keys use raw import, and scalar multiplication covers public-key derivation from the RFC 7748 base point and ECDH-style shared secrets. A smalltoUint8Arrayhelper normalizesBufferSourceinputs (includingArrayBufferandDataView).The changelog documents the new API, and tests cover custom vectors plus RFC 7748 Section 6.1 Alice/Bob cases, input-type variants, and explicit errors for wrong key lengths.
Reviewed by Cursor Bugbot for commit 935bff1. Bugbot is set up for automated code reviews on this repo. Configure here.