iyi

Standard library, 0.22.0

std/mlkem

ML-KEM-768 (FIPS 203), the module-lattice key-encapsulation mechanism, written in iyi.

import std/mlkem to use it. Source: src/std/mlkem.iyi at v0.22.0.

  import std/mlkem::{MlKem768, MlKem768PrivateKey, MlKemError}
  key = MlKem768PrivateKey.from_seed(seed)       # 64 bytes: d, then z
  key.encapsulation_key                          # 1,184 bytes, sent
  ciphertext, secret = MlKem768.encapsulate(their_key, m).or_panic  # m: 32 random bytes
  secret = key.decapsulate(ciphertext).or_panic  # 32 bytes, the same
  key.decapsulation_key                          # 2,400 bytes
  MlKem768PrivateKey.from_bytes(dk).or_panic     # read back, checked

Keys, ciphertexts and randomness are a String, whose bytes are taken, or an Array(UInt8); what comes back is an Array(UInt8). The caller brings every random byte, as std/curve25519's caller does: the 64-byte seed of a key pair (FIPS 203's d and z, the "internal" key generation of Algorithm 16) and the 32-byte m of an encapsulation (Algorithm 17). Random::Secure.random_bytes from std/random is where they come from. This module imports std/digest's SHA3-256, SHA3-512, SHAKE128, SHAKE256 and ValueBarrier, and std/lazy, which keeps its table, and nothing else, so a package that uses it is not counted as reaching the operating system (iyi mod reach).

The arithmetic is FIPS 203's: polynomials of 256 coefficients modulo q = 3329, multiplied in the NTT domain (Algorithms 9 to 12), the zetas 17^BitRev7(i) and the base-case gammas 17^(2 BitRev7(i) + 1) tabled as FIPS 203's appendix A lists them. A coefficient is a UInt32 below q. Reduction is Barrett's, as Go's crypto/internal/mlkem768 does it: a product is multiplied by 5039 = floor(2^24 / q) and shifted, which leaves a remainder below 2q for anything under 2q^2, and the last subtraction of q is a mask made from the borrow's sign bit, never a branch. Compress (FIPS 203 section 4.2.1) is the same Barrett division with the rounding taken from two borrows, so no division instruction, whose time can depend on its operands, is ever issued on a secret.

Constant time on secrets. Nothing branches on a secret and no memory index is one. The NTT, the products, the CBD sampling (sums of bits, eta = 2) and the decoding of the secret vector run the same instructions whatever the values; every loop bound is a size. Decapsulation decrypts, encrypts again and compares the ciphertexts by OR-ing the XOR of every byte, and the comparison's result picks between the real key and the implicit-rejection key J(z || c) by a mask over all 32 bytes of both: FIPS 203 Algorithm 18 with no early exit. The mask goes through std/digest's ValueBarrier before it is used, so the compiler cannot know it is 0 or all ones and has to read both keys' bytes and combine them; without that, LLVM picked one key's address by a conditional move and read that key alone (see decapsulate). How that was checked: by reading, every if, while, ? :, unless and [] on the paths from decapsulate and the key generation was listed, and each depends on a size, a loop counter, the ciphertext (which is public) or the encapsulation key (public too). The branches that do depend on data are all on public data: SampleNTT's rejection of 12-bit values of q and up reads the public seed rho, and the modulus check reads the public key. The gate also times decapsulation of good ciphertexts against tampered ones (the implicit-rejection path), and records it, measured, not checked.

Input checking (FIPS 203 section 7). encapsulate refuses, as an MlKemError, an encapsulation key that is not 1,184 bytes and one that fails section 7.2's modulus check (a 12-bit coefficient of q or more, which ByteDecode would reduce, so ByteEncode of it would differ). from_bytes refuses a decapsulation key that is not 2,400 bytes and one whose stored H(ek) is not the hash of the encapsulation key inside it (section 7.3's hash check). decapsulate refuses a ciphertext that is not 1,088 bytes; any ciphertext of the right size is answered with a key, the real one or the implicit rejection's, as FIPS 203 requires.

Measured on one x86_64 Linux machine, --release, the best of five runs of 200 (bench/std_mlkem_exercise.sh): a key pair took 41 microseconds against OpenSSL 3.5.5's 25 (its own speed), an encapsulation 48 against 16, a decapsulation 36 against 24. Decapsulation is the cheaper of the two here because a key keeps its matrix A and vector t, so its encryption again samples nothing from rho; an encapsulation expands A from the key it is given. Decapsulating a good ciphertext and a tampered one, 2,000 of each interleaved, took a median of 34,590 and 34,591 ns.

Held by bench/std_mlkem_exercise.sh to NIST's ACVP vectors for ML-KEM-768 (key generation, encapsulation, decapsulation with implicit rejection, and both key checks), to OpenSSL 3.5's ML-KEM-768 and to Python's cryptography, each side encapsulating to the other's keys and both answering a tampered ciphertext with the same key, and by broken copies of this module, each caught at the check that names it. FIPS 203's own intermediate values are not among the oracles: NIST's example-values page listed none for ML-KEM when this was written (2026-10-19).

Refused with a sentence, as the caller's own misuse: a seed that is not 64 bytes and an m that is not 32.

bench/ct_timing.sh, decapsulate: one key's valid ciphertext against ciphertexts with one bit flipped, which take the implicit rejection, |t| = 2.1 to 2.4 over 50,000; one key and its ciphertext against 64 other keys and theirs, |t| = 5.2 to 6.3 over 50,000 (under 10 is no leak seen, on a machine at load 8 to 11). The --release assembly of decapsulate (x86_64 and aarch64) reads every ciphertext byte in the re-encryption compare with no early exit, and for each of the 32 bytes loads K's and K-bar's, xors them, ands the mask and xors K-bar's again; its conditional jumps compare loop counters, constants and pointer ranges. Timing cannot tell a key picked by a conditional move of its address from one picked by masks, and did not: the first is what the assembly showed before the mask went through the barrier, at the |t| above. bench/std_mlkem_exercise.sh reads the assembly for it.

MlKem768struct

ML-KEM-768's encapsulation, and its sizes (FIPS 203 section 8, table 3).

std/mlkemiyi doc src/std/mlkem.iyi
pub struct MlKem768
  # The ciphertext for *encapsulation_key* and the 32-byte shared secret it
  # carries, FIPS 203 Algorithm 17 from the 32 random bytes *randomness*
  # (m), or an `MlKemError`: a key that is not 1,184 bytes or fails the
  # modulus check of section 7.2.
  def self.encapsulate(encapsulation_key : String | Array(UInt8), randomness : String | Array(UInt8)) : ::Tuple(Array(UInt8), Array(UInt8)) | MlKemError
  def initialize
end

MlKem768PrivateKeystruct

An ML-KEM-768 key pair: the decapsulation key and the encapsulation key that goes with it. Holds the secret vector in the NTT domain, and the public matrix and vector that decapsulation encrypts with again.

std/mlkemiyi doc src/std/mlkem.iyi
pub struct MlKem768PrivateKey
  # The 32-byte shared secret *ciphertext* carries, FIPS 203 Algorithm 18,
  # or an `MlKemError` for a ciphertext that is not 1,088 bytes. A
  # ciphertext that does not re-encrypt to itself is answered with the
  # implicit-rejection key J(z || c), chosen by a mask: the caller cannot
  # tell the two apart, and neither can the time taken or the addresses
  # read. Every byte of the answer is K-bar's xored with the mask's share
  # of K's difference from it, both loaded. The mask is 0xff or 0, and a
  # compiler that knows so may read only the key it keeps: a --release
  # build did, `cmovle` on the two arrays and one load through the winner,
  # which is the validity of the ciphertext in the address of a load. So
  # the mask is passed through `ValueBarrier` first.
  def decapsulate(ciphertext : String | Array(UInt8)) : Array(UInt8) | MlKemError
  # The 2,400-byte decapsulation key, as `from_bytes` reads it.
  def decapsulation_key : Array(UInt8)
  # The 1,184 bytes a peer encapsulates to: ByteEncode12 of t, then rho.
  def encapsulation_key : Array(UInt8)
  # The key whose 2,400-byte decapsulation key is *bytes* (ByteEncode12 of
  # s, the encapsulation key, H of it, z), or an `MlKemError`: the wrong
  # size, or a stored H(ek) that is not the encapsulation key's hash (FIPS
  # 203 section 7.3).
  def self.from_bytes(bytes : String | Array(UInt8)) : MlKem768PrivateKey | MlKemError
  # The key pair of *seed*, d then z, 32 bytes each: FIPS 203 Algorithm 16
  # (ML-KEM.KeyGen_internal) over Algorithm 13 (K-PKE.KeyGen).
  def self.from_seed(seed : String | Array(UInt8)) : MlKem768PrivateKey
  def initialize(encapsulation_key : Array(UInt8), s : Pointer(UInt32), t : Pointer(UInt32), a : Pointer(UInt32), h : Array(UInt8), z : Array(UInt8))
end

MlKemErrorstruct

What is wrong with a key or a ciphertext from outside: message is the sentence.

std/mlkemiyi doc src/std/mlkem.iyi
pub struct MlKemError
  def initialize(message : String)
  def to_s : String
end

Error for MlKemErrorimpl

std/mlkemiyi doc src/std/mlkem.iyi
impl Error for MlKemError
  def message : String
end