WebBuf WebBuf
Docs

Elliptic curve

@webbuf/x25519

Rust/wasm X25519 ECDH (RFC 7748)

Install

npm install @webbuf/x25519

Usage

import { x25519PublicKeyCreate, x25519SharedSecretRaw } from "@webbuf/x25519";
import { FixedBuf } from "@webbuf/fixedbuf";

// Each party generates a 32-byte private key and computes its public key.
const alicePriv = FixedBuf.fromRandom<32>(32);
const alicePub = x25519PublicKeyCreate(alicePriv);

const bobPriv = FixedBuf.fromRandom<32>(32);
const bobPub = x25519PublicKeyCreate(bobPriv);

// Each side computes the same shared secret independently.
const aliceSS = x25519SharedSecretRaw(alicePriv, bobPub);
const bobSS = x25519SharedSecretRaw(bobPriv, alicePub);
// aliceSS.toHex() === bobSS.toHex()

API reference (2 exports)

Functions

x25519PublicKeyCreate

function

Compute the X25519 public key (RFC 7748 §5) for a 32-byte private key. Accepts any 32 raw bytes; clamping per RFC 7748 §5 ("decodeScalar25519") is applied internally — callers do not need to pre-clamp.

x25519PublicKeyCreate(privKey: FixedBuf<32>): FixedBuf<32>

x25519SharedSecretRaw

function

Compute the raw 32-byte X25519 ECDH shared secret (RFC 7748 §6.1). Throws if the resulting shared secret is non-contributory — i.e. if the peer's public key is small-order. This protects hybrid encryption schemes from being collapsed to PQ-only by a malicious peer's small-order public key.

x25519SharedSecretRaw(privKey: FixedBuf<32>, pubKey: FixedBuf<32>): FixedBuf<32>