java.lang.Object
org.bouncycastle.pqc.crypto.mlkem.Poly
Polynomial representation for ML-KEM operations. Provides polynomial arithmetic operations used in lattice-based cryptography.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate short[]The polynomial coefficients, one per ring element.private MLKEMEngineThe engine whose Kyber parameters this polynomial uses.private intNoise bound eta1, used when generating secret polynomials.private intNoise bound eta2, used for rounding noise.private intBytes one compressed polynomial takes in the encoding.private SymmetricThe engine's hash and PRF primitives.Constructor Summary
ConstructorsConstructorDescriptionPoly(MLKEMEngine engine) Creates a zeroed polynomial and caches the engine's Kyber parameters.Method Summary
Modifier and TypeMethodDescriptionvoidAdds another polynomial in place, coefficient by coefficient.static voidbaseMultMontgomery(Poly r, Poly a, Poly b) Multiplies two NTT-domain polynomials into a third, in Montgomery form.byte[]Encodes to the compressed representation, reducing coefficients first.voidSubtracts q from any coefficient that is not already reduced.voidMaps every coefficient into the Montgomery domain.voiddecompressPoly(byte[] compressedPolyCipherText) Decodes a compressed representation into this polynomial's coefficients.voidfromBytes(byte[] inpBytes) Loads coefficients from their 12-bit packed wire encoding.voidfromMsg(byte[] msg) Expands a one-bit-per-coefficient message into coefficients.shortgetCoeffIndex(int i) Reads one coefficient out of the backing array.short[]Exposes the backing coefficient array itself, not a copy.voidgetEta1Noise(byte[] seed, byte nonce) getEta1Noise.voidgetEta2Noise(byte[] seed, byte nonce) getEta2Noise.voidTransforms back out of the NTT domain, leaving it unreduced.voidpolyNtt()Transforms into the NTT domain and reduces the result.voidpolySubtract(Poly b) Subtracts another polynomial from this one, in place.voidreduce()Barrett-reduces every coefficient modulo q.voidsetCoeffIndex(int i, short val) Overwrites one coefficient in the backing array.voidsetCoeffs(short[] coeffs) Adopts a new backing array, storing the caller's reference.byte[]toBytes()Packs the coefficients into their 12-bit wire encoding.byte[]toMsg()Packs the coefficients into a one-bit-per-coefficient message.toString()Renders the coefficients as a bracketed, comma-separated list.
Field Details
coeffs
private short[] coeffsThe polynomial coefficients, one per ring element.engine
The engine whose Kyber parameters this polynomial uses.polyCompressedBytes
private int polyCompressedBytesBytes one compressed polynomial takes in the encoding.eta1
private int eta1Noise bound eta1, used when generating secret polynomials.eta2
private int eta2Noise bound eta2, used for rounding noise.symmetric
The engine's hash and PRF primitives.
Constructor Details
Poly
Creates a zeroed polynomial and caches the engine's Kyber parameters.- Parameters:
engine- the engine supplying the Kyber parameters, RNG and symmetric primitives
Method Details
getCoeffIndex
public short getCoeffIndex(int i) Reads one coefficient out of the backing array.- Parameters:
i- the coefficient index, 0 to KyberN-1- Returns:
- the coefficient at that index
getCoeffs
public short[] getCoeffs()Exposes the backing coefficient array itself, not a copy.- Returns:
- the backing coefficient array, shared rather than copied
setCoeffIndex
public void setCoeffIndex(int i, short val) Overwrites one coefficient in the backing array.- Parameters:
i- the coefficient index, 0 to KyberN-1val- the replacement coefficient
setCoeffs
public void setCoeffs(short[] coeffs) Adopts a new backing array, storing the caller's reference.- Parameters:
coeffs- the new backing array, adopted by reference and not copied
polyNtt
public void polyNtt()Transforms into the NTT domain and reduces the result.polyInverseNttToMont
public void polyInverseNttToMont()Transforms back out of the NTT domain, leaving it unreduced.reduce
public void reduce()Barrett-reduces every coefficient modulo q.baseMultMontgomery
Multiplies two NTT-domain polynomials into a third, in Montgomery form.- Parameters:
r- the polynomial receiving the product, in Montgomery forma- the first multiplicand, in the number-theoretic transform domainb- the second multiplicand, in the number-theoretic transform domain
addCoeffs
Adds another polynomial in place, coefficient by coefficient.- Parameters:
b- the polynomial added coefficient-wise to this one
convertToMont
public void convertToMont()Maps every coefficient into the Montgomery domain.compressPoly
public byte[] compressPoly()Encodes to the compressed representation, reducing coefficients first.- Returns:
- the lossy compressed encoding, polyCompressedBytes long
decompressPoly
public void decompressPoly(byte[] compressedPolyCipherText) Decodes a compressed representation into this polynomial's coefficients.- Parameters:
compressedPolyCipherText- the compressed encoding to decode into this polynomial
toBytes
public byte[] toBytes()Packs the coefficients into their 12-bit wire encoding.- Returns:
- the coefficients packed little-endian into KyberPolyBytes bytes
fromBytes
public void fromBytes(byte[] inpBytes) Loads coefficients from their 12-bit packed wire encoding.- Parameters:
inpBytes- the packed coefficients to load, low 12 bits of each 2-byte group
toMsg
public byte[] toMsg()Packs the coefficients into a one-bit-per-coefficient message.- Returns:
- the coefficients packed as one bit each, KyberN/8 bytes long
fromMsg
public void fromMsg(byte[] msg) Expands a one-bit-per-coefficient message into coefficients.- Parameters:
msg- the one-bit-per-coefficient message, exactly KyberN/8 bytes
conditionalSubQ
public void conditionalSubQ()Subtracts q from any coefficient that is not already reduced.getEta1Noise
public void getEta1Noise(byte[] seed, byte nonce) getEta1Noise.- Parameters:
seed- the 32-byte noise seed fed to the PRFnonce- the single-byte domain separator
getEta2Noise
public void getEta2Noise(byte[] seed, byte nonce) getEta2Noise.- Parameters:
seed- the 32-byte noise seed fed to the PRFnonce- the single-byte domain separator
polySubtract
Subtracts another polynomial from this one, in place.- Parameters:
b- the polynomial subtracted coefficient-wise from this one
toString