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.
decrypt function is the counterpart to encrypt in @ecies/post-quantum. It takes an encrypted payload and the receiver’s ML-KEM secret key, recovers the shared secret through KEM decapsulation, and uses the AEAD cipher to authenticate and decrypt the symmetric ciphertext — returning the original plaintext bytes.
Function Signature
Parameters
The receiver’s raw ML-KEM secret key. Obtain this from
config.asymmetricModule.keygen().secretKey. Must correspond to the public key that was used during encrypt().The encrypted payload as returned by The function uses
encrypt(). Expected layout:config.pkeSize to determine where to split the pke_ciphertext prefix from the rest of the payload.Configuration specifying algorithm choices. Defaults to
DEFAULT_CONFIG. This value must exactly match the Config used during encrypt() — including asymmetricAlgorithm, symmetricAlgorithm, and symmetricNonceLength.Return Value
The original plaintext bytes that were passed to
encrypt(). To recover a string, decode with new TextDecoder().decode(plaintext).How It Works
- Splits
dataat byte offsetconfig.pkeSize, extracting thepke_ciphertextprefix and the remaining symmetric payload. - Calls
config.asymmetricModule.decapsulate(pke, receiverSK)to recoversharedSecret. - Uses
sharedSecretto decrypt and authenticate the symmetric payload with the configured AEAD cipher. - Returns the plaintext bytes on success, or throws on authentication failure.
Example
Error Cases
- Throws
"Unsupported state or unable to authenticate data"(Node.js native crypto) or"invalid tag"(noble ciphers) when AEAD authentication fails. This can occur due to:- Wrong secret key (key mismatch between sender and receiver)
- Tampered or corrupted ciphertext
- Mismatched
configbetweenencryptanddecryptcalls
- Throws
Error: Unsupported asymmetric algorithm: <value>ifconfig.asymmetricAlgorithmis set to an unrecognised value.
Authentication failures are detected and surfaced automatically by the AEAD cipher. You do not need to manually verify data integrity — a successful return from
decrypt guarantees the ciphertext was encrypted with the corresponding public key and has not been tampered with.