Elliptic curve
@webbuf/p256
Rust/wasm optimized P-256 (NIST) ECDSA and Diffie-Hellman
Install
npm install @webbuf/p256 Usage
import {
p256PublicKeyCreate,
p256PrivateKeyVerify,
p256PublicKeyVerify,
} from "@webbuf/p256";
import { FixedBuf } from "@webbuf/fixedbuf";
// Generate random private key
const privKey = FixedBuf.fromRandom<32>(32);
// Verify private key is valid
p256PrivateKeyVerify(privKey); // true
// Derive public key (compressed, 33 bytes)
const pubKey = p256PublicKeyCreate(privKey);
// Verify public key
p256PublicKeyVerify(pubKey); // true API reference (16 exports)
Functions
p256PrivateKeyAdd
functionp256PrivateKeyAdd(privKey1: FixedBuf<32>, privKey2: FixedBuf<32>): FixedBuf<32> p256PrivateKeyToJwk
functionConvert a raw 32-byte P-256 private key scalar to a JsonWebKey, ready to pass to `crypto.subtle.importKey("jwk", jwk, ...)`. Internally derives the associated public key (Web Crypto requires `x` and `y` alongside `d`).
p256PrivateKeyToJwk(privateKey: FixedBuf<32>): P256PrivateKeyJwk p256PrivateKeyVerify
functionp256PrivateKeyVerify(privateKey: FixedBuf<32>): boolean p256PublicKeyAdd
functionp256PublicKeyAdd(publicKey1: FixedBuf<33>, publicKey2: FixedBuf<33>): FixedBuf<33> p256PublicKeyCompress
functionCompress a 65-byte SEC1 uncompressed P-256 public key into its 33-byte compressed form. Throws if the point is not on the curve.
p256PublicKeyCompress(uncompressed: FixedBuf<65>): FixedBuf<33> p256PublicKeyCreate
functionp256PublicKeyCreate(privateKey: FixedBuf<32>): FixedBuf<33> p256PublicKeyDecompress
functionDecompress a 33-byte SEC1 compressed P-256 public key into its 65-byte uncompressed form (`0x04 || X || Y`). Useful for `crypto.subtle.importKey("raw", ...)`.
p256PublicKeyDecompress(compressed: FixedBuf<33>): FixedBuf<65> p256PublicKeyFromJwk
functionReconstruct a compressed 33-byte P-256 public key from a JsonWebKey's `x` and `y` coordinates. Validates that the point is on the curve.
p256PublicKeyFromJwk(jwk: { x: string; y: string; }): FixedBuf<33> p256PublicKeyToJwk
functionConvert a compressed P-256 public key to a JsonWebKey, ready to pass to `crypto.subtle.importKey("jwk", jwk, ...)`.
p256PublicKeyToJwk(compressed: FixedBuf<33>): P256PublicKeyJwk p256PublicKeyVerify
functionp256PublicKeyVerify(publicKey: FixedBuf<33>): boolean p256Sign
functionp256Sign(digest: FixedBuf<32>, privateKey: FixedBuf<32>, k: FixedBuf<32>): FixedBuf<64> p256Verify
functionp256Verify(signature: FixedBuf<64>, digest: FixedBuf<32>, publicKey: FixedBuf<33>): boolean Interfaces
P256PrivateKeyJwk
interfaceJWK representation of a P-256 private key, suitable for `crypto.subtle.importKey("jwk", jwk, ...)`. Includes the public key coordinates (`x`, `y`) as required by Web Crypto.
d: string
kty: "EC"
crv: "P-256"
x: string
y: string P256PublicKeyJwk
interfaceJWK representation of a P-256 public key, suitable for `crypto.subtle.importKey("jwk", jwk, ...)`.
kty: "EC"
crv: "P-256"
x: string
y: string