java.lang.Object
org.bouncycastle.pqc.crypto.mlkem.Poly

class Polyextends Object
Polynomial representation for ML-KEM operations. Provides polynomial arithmetic operations used in lattice-based cryptography.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private short[]
    The polynomial coefficients, one per ring element.
    private MLKEMEngine
    The engine whose Kyber parameters this polynomial uses.
    private int
    Noise bound eta1, used when generating secret polynomials.
    private int
    Noise bound eta2, used for rounding noise.
    private int
    Bytes one compressed polynomial takes in the encoding.
    private Symmetric
    The engine's hash and PRF primitives.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Poly(MLKEMEngine engine)
    Creates a zeroed polynomial and caches the engine's Kyber parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds another polynomial in place, coefficient by coefficient.
    static void
    Multiplies two NTT-domain polynomials into a third, in Montgomery form.
    byte[]
    Encodes to the compressed representation, reducing coefficients first.
    void
    Subtracts q from any coefficient that is not already reduced.
    void
    Maps every coefficient into the Montgomery domain.
    void
    decompressPoly(byte[] compressedPolyCipherText)
    Decodes a compressed representation into this polynomial's coefficients.
    void
    fromBytes(byte[] inpBytes)
    Loads coefficients from their 12-bit packed wire encoding.
    void
    fromMsg(byte[] msg)
    Expands a one-bit-per-coefficient message into coefficients.
    short
    Reads one coefficient out of the backing array.
    short[]
    Exposes the backing coefficient array itself, not a copy.
    void
    getEta1Noise(byte[] seed, byte nonce)
    getEta1Noise.
    void
    getEta2Noise(byte[] seed, byte nonce)
    getEta2Noise.
    void
    Transforms back out of the NTT domain, leaving it unreduced.
    void
    Transforms into the NTT domain and reduces the result.
    void
    Subtracts another polynomial from this one, in place.
    void
    Barrett-reduces every coefficient modulo q.
    void
    setCoeffIndex(int i, short val)
    Overwrites one coefficient in the backing array.
    void
    setCoeffs(short[] coeffs)
    Adopts a new backing array, storing the caller's reference.
    byte[]
    Packs the coefficients into their 12-bit wire encoding.
    byte[]
    Packs the coefficients into a one-bit-per-coefficient message.
    Renders the coefficients as a bracketed, comma-separated list.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • coeffs

      private short[] coeffs
      The polynomial coefficients, one per ring element.
    • engine

      private MLKEMEngine engine
      The engine whose Kyber parameters this polynomial uses.
    • polyCompressedBytes

      private int polyCompressedBytes
      Bytes one compressed polynomial takes in the encoding.
    • eta1

      private int eta1
      Noise bound eta1, used when generating secret polynomials.
    • eta2

      private int eta2
      Noise bound eta2, used for rounding noise.
    • symmetric

      private Symmetric symmetric
      The engine's hash and PRF primitives.
  • Constructor Details

    • Poly

      public Poly(MLKEMEngine engine)
      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-1
      val - 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

      public static void baseMultMontgomery(Poly r, Poly a, Poly b)
      Multiplies two NTT-domain polynomials into a third, in Montgomery form.
      Parameters:
      r - the polynomial receiving the product, in Montgomery form
      a - the first multiplicand, in the number-theoretic transform domain
      b - the second multiplicand, in the number-theoretic transform domain
    • addCoeffs

      public void addCoeffs(Poly b)
      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 PRF
      nonce - the single-byte domain separator
    • getEta2Noise

      public void getEta2Noise(byte[] seed, byte nonce)
      getEta2Noise.
      Parameters:
      seed - the 32-byte noise seed fed to the PRF
      nonce - the single-byte domain separator
    • polySubtract

      public void polySubtract(Poly b)
      Subtracts another polynomial from this one, in place.
      Parameters:
      b - the polynomial subtracted coefficient-wise from this one
    • toString

      public String toString()
      Renders the coefficients as a bracketed, comma-separated list.
      Overrides:
      toString in class Object