java.lang.Object
org.bouncycastle.pqc.crypto.mlkem.MLKEMIndCpa
Indistinguishability under Chosen Plaintext Attack (IND-CPA) security for ML-KEM. Provides key generation operations with IND-CPA security guarantees.
Field Summary
Modifier and TypeFieldDescriptionprivate MLKEMEngineThe engine providing the primitives.private intCiphertext size of the IND-CPA scheme.private intPublic key size of the IND-CPA scheme.final intNumber of XOF blocks squeezed per matrix entry, computed once in the constructor.private intThe dimension K of the parameter set.private intSize of a compressed polynomial.private intSize of an uncompressed polynomial vector.private intSize of a compressed polynomial vector.private SymmetricThe symmetric primitives for the hashing.Constructor Summary
ConstructorsConstructorDescriptionMLKEMIndCpa(MLKEMEngine engine) Creates the IND-CPA layer, caching the sizes derived from the engine's parameter set.Method Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] secretKey, byte[] cipherText) Decrypts a ciphertext under the IND-CPA secret key, recovering the plaintext.byte[]encrypt(byte[] publicKeyInput, byte[] msg, byte[] coins) Encrypts a message under the IND-CPA public key, drawing all noise from the supplied coins.(package private) byte[][]generateKeyPair(byte[] d) Generates IndCpa Key PairvoidgenerateMatrix(PolyVec[] aMatrix, byte[] seed, boolean transposed) Expands a 32-byte seed into the KyberK by KyberK public matrix A by rejection sampling.private byte[]packCipherText(PolyVec b, Poly v) Packs the two parts into the ciphertext.byte[]packPublicKey(PolyVec publicKeyPolyVec, byte[] seed) Packs a public key vector and its seed into the encapsulation key wire format.byte[]packSecretKey(PolyVec secretKeyPolyVec) Packs a secret key vector into the decapsulation key wire format.private static intrejectionSampling(Poly outputBuffer, int coeffOff, int len, byte[] inpBuf, int inpBufLen) Fills the polynomial by rejection sampling.private voidunpackCipherText(PolyVec b, Poly v, byte[] cipherText) Unpacks the ciphertext into its two parts.byte[]unpackPublicKey(PolyVec publicKeyPolyVec, byte[] publicKey) Splits a packed encapsulation key into its key vector and its trailing public seed.voidunpackSecretKey(PolyVec secretKeyPolyVec, byte[] secretKey) Deserializes a packed decapsulation key into the destination vector.
Field Details
engine
The engine providing the primitives.kyberK
private int kyberKThe dimension K of the parameter set.indCpaPublicKeyBytes
private int indCpaPublicKeyBytesPublic key size of the IND-CPA scheme.polyVecBytes
private int polyVecBytesSize of an uncompressed polynomial vector.indCpaBytes
private int indCpaBytesCiphertext size of the IND-CPA scheme.polyVecCompressedBytes
private int polyVecCompressedBytesSize of a compressed polynomial vector.polyCompressedBytes
private int polyCompressedBytesSize of a compressed polynomial.symmetric
The symmetric primitives for the hashing.KyberGenerateMatrixNBlocks
public final int KyberGenerateMatrixNBlocksNumber of XOF blocks squeezed per matrix entry, computed once in the constructor.
Constructor Details
MLKEMIndCpa
Creates the IND-CPA layer, caching the sizes derived from the engine's parameter set.- Parameters:
engine- the parameter set supplying the Kyber constants, sizes and symmetric primitives
Method Details
generateKeyPair
byte[][] generateKeyPair(byte[] d) Generates IndCpa Key Pair- Parameters:
d- the 32-byte random seed from which the keypair is derived- Returns:
- KeyPair where each key is represented as bytes
encrypt
public byte[] encrypt(byte[] publicKeyInput, byte[] msg, byte[] coins) Encrypts a message under the IND-CPA public key, drawing all noise from the supplied coins.- Parameters:
publicKeyInput- the packed encapsulation key, the compressed key vector then the 32-byte seedmsg- the plaintext to encrypt, KyberIndCpaMsgBytes longcoins- the 32-byte random coins supplying the encryption noise- Returns:
- the packed ciphertext, KyberIndCpaBytes long
packCipherText
unpackCipherText
packPublicKey
Packs a public key vector and its seed into the encapsulation key wire format.- Parameters:
publicKeyPolyVec- the key vector to serialize, filled in by this callseed- the 32-byte public seed appended after the key vector- Returns:
- the packed encapsulation key, KyberPublicKeyBytes long
unpackPublicKey
Splits a packed encapsulation key into its key vector and its trailing public seed.- Parameters:
publicKeyPolyVec- the destination for the key vector in the leading bytes, before the seedpublicKey- the packed encapsulation key to split- Returns:
- the trailing KyberSymBytes-byte public seed
packSecretKey
Packs a secret key vector into the decapsulation key wire format.- Parameters:
secretKeyPolyVec- the decapsulation key vector to serialize- Returns:
- the packed decapsulation key, KyberIndCpaSecretKeyBytes long
unpackSecretKey
Deserializes a packed decapsulation key into the destination vector.- Parameters:
secretKeyPolyVec- the destination for the deserialized key vectorsecretKey- the packed decapsulation key
generateMatrix
Expands a 32-byte seed into the KyberK by KyberK public matrix A by rejection sampling.- Parameters:
aMatrix- the KyberK by KyberK matrix to fill, one PolyVec per rowseed- the 32-byte seed absorbed by the XOF to expand the matrixtransposed- true to swap the row and column indices, producing the transposed matrix used by encryption
rejectionSampling
private static int rejectionSampling(Poly outputBuffer, int coeffOff, int len, byte[] inpBuf, int inpBufLen) Fills the polynomial by rejection sampling.- Parameters:
outputBuffer- the polynomial to fillcoeffOff- where to start writing coefficientslen- the number of coefficients to readinpBuf- the random bytes to sample frominpBufLen- the length of the random bytes- Returns:
- the number of input bytes consumed
decrypt
public byte[] decrypt(byte[] secretKey, byte[] cipherText) Decrypts a ciphertext under the IND-CPA secret key, recovering the plaintext.- Parameters:
secretKey- the packed decapsulation keycipherText- the packed ciphertext to decrypt- Returns:
- the recovered plaintext, KyberIndCpaMsgBytes long