TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ecies/js-post-quantum/llms.txt
Use this file to discover all available pages before exploring further.
encrypt function is the primary entry point for post-quantum encryption in @ecies/post-quantum. It accepts a receiver’s ML-KEM public key and plaintext bytes, performs key encapsulation to derive a shared secret, and symmetrically encrypts the data using an AEAD cipher — returning a single self-contained encrypted payload.
Function Signature
Parameters
The receiver’s raw ML-KEM public key. Obtain this from
config.asymmetricModule.keygen().publicKey. The expected byte length depends on the configured variant: 800 bytes for ML-KEM-512, 1184 bytes for ML-KEM-768, and 1568 bytes for ML-KEM-1024.The plaintext data to encrypt. Must be a
Uint8Array. To encrypt a string, convert it first using new TextEncoder().encode(str).Configuration specifying algorithm choices. Defaults to
DEFAULT_CONFIG, which uses ML-KEM-768 for key encapsulation, AES-256-GCM for symmetric encryption, and a 16-byte nonce. Pass a custom Config instance to override any of these settings.Return Value
The encrypted payload as a contiguous byte array with the following layout:The
pke_ciphertext size is determined by config.asymmetricAlgorithm. The nonce size is config.symmetricNonceLength for AES-256-GCM, or always 24 bytes for XChaCha20-Poly1305.How It Works
- Calls
config.asymmetricModule.encapsulate(receiverPK), which returns{ cipherText: pke, sharedSecret }. Thepkeis the encapsulated key that the receiver needs to recover the shared secret. - Uses
sharedSecretas the symmetric key to encryptdatawith the configured AEAD cipher (AES-256-GCM or XChaCha20-Poly1305), producing a payload ofnonce || AEAD tag || ciphertext. - Returns
concat(pke, nonce, tag, ciphertext)as a singleUint8Array.
Examples
Error Cases
- Throws
Error: Unsupported asymmetric algorithm: <value>ifconfig.asymmetricAlgorithmis set to a value outside"ml-kem-512" | "ml-kem-768" | "ml-kem-1024". - Any errors raised internally by the AEAD cipher (e.g., from the underlying
@noble/post-quantumor@noble/ciphersprimitives) propagate up unchanged.