Mathematical foundations of the QYVORA engine
Status: draft for version 1, 2026-12-04. This document states the definitions, parameters and equations
that the engine implements, with the implementation choices found in the sources. It reports no test
result and no performance figure; those belong to docs/STATUS.md and the benchmark reports. At the date
of writing, every primitive described in sections 1 to 12 is implemented under Sources/ on the main
branch; the Bitcoin transaction layer and the vault constructions of section 13 are in progress. The
engine is unaudited.
Notation: $\Vert$ is concatenation; $\oplus$ is bitwise XOR; $\lll$ and $\ggg$ are rotations; $\gg$ is a logical right shift; $\lceil x \rfloor$ is rounding to the nearest integer with halves rounded up; $r' = r \bmod^{\pm} m$ is the representative in $(-m/2,, m/2]$.
1. Keccak, SHA-3, SHAKE, cSHAKE, KMAC
Source: Sources/QyvoraKeccak/Keccak.swift (FIPS 202), SP800185.swift (SP 800-185).
1.1 The permutation Keccak-f[1600]
The state is a $5\times5$ array of 64-bit lanes $A[x,y]$, $x,y\in{0,\dots,4}$, 1600 bits in total. The code stores lane $(x,y)$ at index $x+5y$, little-endian. One round is $R = \iota\circ\chi\circ\pi\circ\rho\circ\theta$, and the permutation is 24 rounds.
$$ \begin{aligned} \theta:\quad & C[x] = \bigoplus_{y=0}^{4} A[x,y], \qquad D[x] = C[x-1] \oplus (C[x+1] \lll 1), \qquad A[x,y] \leftarrow A[x,y]\oplus D[x] \ \rho:\quad & A[x,y] \leftarrow A[x,y] \lll r[x,y] \ \pi:\quad & B[y,; 2x+3y] = A[x,y] \ \chi:\quad & A[x,y] = B[x,y] \oplus \big(\lnot B[x+1,y] \wedge B[x+2,y]\big) \ \iota:\quad & A[0,0] \leftarrow A[0,0] \oplus RC[i] \end{aligned} $$
with indices modulo 5. The rotation offsets are $r[0,0]=0$ and, starting from $(x,y)=(1,0)$, for $t=0,\dots,23$: $r[x,y] = \frac{(t+1)(t+2)}{2} \bmod 64$, then $(x,y)\leftarrow(y,; 2x+3y \bmod 5)$. The round constants are defined by the linear feedback shift register $rc(t) = \big(x^{t} \bmod x^8+x^6+x^5+x^4+1\big) \bmod x$, with bit $2^j-1$ of $RC[i]$ equal to $rc(j+7i)$ for $j=0,\dots,6$. The implementation uses the 24 tabulated constants and the 25 rotation offsets written out, with the state held in 25 local variables; $\chi$ is the only non-linear step.
1.2 The sponge construction
With rate $r$ and capacity $c = 1600 - r$ bits, the sponge absorbs the padded message in $r$-bit blocks,
XORing each into the first $r$ bits of the state and applying the permutation, then outputs $r$ bits at a
time, permuting between blocks. Padding is pad10*1, preceded by the domain-separation suffix. For
byte-aligned messages the suffix and the first padding bit form one byte, XORed at the current position,
and 0x80 is XORed into the last byte of the block.
| Function | Rate $r$ (bits / bytes) | Capacity $c$ (bits) | Output (bits) | Suffix bits | Suffix byte |
|---|---|---|---|---|---|
| SHA3-224 | 1152 / 144 | 448 | 224 | 01 |
0x06 |
| SHA3-256 | 1088 / 136 | 512 | 256 | 01 |
0x06 |
| SHA3-384 | 832 / 104 | 768 | 384 | 01 |
0x06 |
| SHA3-512 | 576 / 72 | 1024 | 512 | 01 |
0x06 |
| SHAKE128 | 1344 / 168 | 256 | any | 1111 |
0x1f |
| SHAKE256 | 1088 / 136 | 512 | any | 1111 |
0x1f |
| cSHAKE128 / cSHAKE256 | 1344 / 1088 | 256 / 512 | any | 00 |
0x04 |
| Keccak-256 (Ethereum) | 1088 / 136 | 512 | 256 | none | 0x01 |
The generic security of a sponge with capacity $c$ and output length $n$ is $\min(n/2,, c/2)$ bits against collisions and $\min(n,, c/2)$ bits against preimages. Keccak-256 as used by Ethereum is the pre-standard padding: it is not SHA3-256, and the two give different digests.
1.3 cSHAKE and KMAC (SP 800-185)
With left_encode, right_encode, encode_string and bytepad as defined in SP 800-185 section 2.3,
and $w$ the rate in bytes (168 or 136):
$$ \mathrm{cSHAKE}(X, L, N, S) = \mathrm{Keccak}[c]\big(\mathrm{bytepad}(\mathrm{encode_string}(N),\Vert,\mathrm{encode_string}(S),, w),\Vert, X ,\Vert, 00,; L\big) $$
and cSHAKE with $N=S=\varepsilon$ is defined to be SHAKE.
$$ \mathrm{KMAC}(K, X, L, S) = \mathrm{cSHAKE}\big(\mathrm{bytepad}(\mathrm{encode_string}(K),, w),\Vert, X ,\Vert, \mathrm{right_encode}(L),; L,; \text{"KMAC"},; S\big) $$
KMACXOF replaces $\mathrm{right_encode}(L)$ by $\mathrm{right_encode}(0)$. Because $L$ is part of the input of the fixed-length form, tags of different lengths are unrelated.
2. SHA-2, RIPEMD-160
Source: Sources/QyvoraSymmetric/SHA2.swift, SHA2Variants.swift, RIPEMD160.swift (FIPS 180-4).
SHA-256 processes 512-bit blocks with eight 32-bit working variables; SHA-512 processes 1024-bit blocks
with eight 64-bit variables. Both are Merkle–Damgård constructions with the Davies–Meyer feed-forward.
The message is padded with a 1 bit, zeros, and its bit length on 64 bits (SHA-256) or 128 bits (SHA-512).
Message schedule, for $t \ge 16$: $W_t = \sigma_1(W_{t-2}) + W_{t-7} + \sigma_0(W_{t-15}) + W_{t-16}$.
Round, for $t = 0,\dots,63$ (SHA-256) or $0,\dots,79$ (SHA-512):
$$ \begin{aligned} T_1 &= h + \Sigma_1(e) + \mathrm{Ch}(e,f,g) + K_t + W_t, \qquad T_2 = \Sigma_0(a) + \mathrm{Maj}(a,b,c),\ (a,b,c,d,e,f,g,h) &\leftarrow (T_1+T_2,; a,; b,; c,; d+T_1,; e,; f,; g) \end{aligned} $$
with $\mathrm{Ch}(x,y,z) = (x\wedge y)\oplus(\lnot x\wedge z)$, $\mathrm{Maj}(x,y,z) = (x\wedge y)\oplus(x\wedge z)\oplus(y\wedge z)$, additions modulo $2^{32}$ or $2^{64}$, and after the last round the working variables are added to the chaining value.
| Function | SHA-256 (32-bit words) | SHA-512 (64-bit words) |
|---|---|---|
| $\Sigma_0(x)$ | $(x\ggg2)\oplus(x\ggg13)\oplus(x\ggg22)$ | $(x\ggg28)\oplus(x\ggg34)\oplus(x\ggg39)$ |
| $\Sigma_1(x)$ | $(x\ggg6)\oplus(x\ggg11)\oplus(x\ggg25)$ | $(x\ggg14)\oplus(x\ggg18)\oplus(x\ggg41)$ |
| $\sigma_0(x)$ | $(x\ggg7)\oplus(x\ggg18)\oplus(x\gg3)$ | $(x\ggg1)\oplus(x\ggg8)\oplus(x\gg7)$ |
| $\sigma_1(x)$ | $(x\ggg17)\oplus(x\ggg19)\oplus(x\gg10)$ | $(x\ggg19)\oplus(x\ggg61)\oplus(x\gg6)$ |
The constants $K_t$ are the first 32 (resp. 64) bits of the fractional parts of the cube roots of the first 64 (resp. 80) primes; the SHA-256 constants are the upper halves of the first 64 SHA-512 constants, which is how the code derives them. The initial values of SHA-256 and SHA-512 are the fractional parts of the square roots of the first eight primes. SHA-224 and SHA-384 are SHA-256 and SHA-512 with other initial values and truncated outputs.
Bitcoin uses $\mathrm{SHA256d}(x) = \mathrm{SHA256}(\mathrm{SHA256}(x))$ and $\mathrm{HASH160}(x) = \mathrm{RIPEMD160}(\mathrm{SHA256}(x))$. RIPEMD-160 is a 160-bit Merkle–Damgård hash with two parallel lines of 80 steps on 32-bit little-endian words.
3. HMAC, HKDF, PBKDF2
Source: Sources/QyvoraSymmetric/SHA2.swift (HMAC), KDF.swift.
HMAC (FIPS 198-1, RFC 2104), with block size $B$ of the hash $H$ and $K'$ the key hashed if longer than $B$ then zero-padded to $B$ bytes:
$$\mathrm{HMAC}(K, m) = H\big((K' \oplus \mathrm{opad}) ,\Vert, H((K' \oplus \mathrm{ipad}) ,\Vert, m)\big), \qquad \mathrm{ipad} = \texttt{0x36}^B,\ \mathrm{opad} = \texttt{0x5c}^B$$
HKDF (RFC 5869):
$$ \begin{aligned} \mathrm{PRK} &= \mathrm{HMAC}(\mathit{salt}, \mathit{IKM}) \quad \text{(an empty salt is } \mathrm{HashLen} \text{ zero bytes)}\ T(0) &= \varepsilon, \qquad T(i) = \mathrm{HMAC}\big(\mathrm{PRK},; T(i-1),\Vert,\mathit{info},\Vert, i\big), \quad i = 1, 2, \dots \ (\text{one byte})\ \mathrm{OKM} &= \text{first } L \text{ bytes of } T(1),\Vert,T(2),\Vert\cdots, \qquad L \le 255\cdot\mathrm{HashLen} \end{aligned} $$
PBKDF2 (RFC 8018) with $c$ iterations:
$$ \mathrm{DK} = F(1),\Vert,F(2),\Vert\cdots, \qquad F(i) = U_1\oplus U_2\oplus\cdots\oplus U_c, \qquad U_1 = \mathrm{HMAC}(P,; S,\Vert,\mathrm{INT}{32}(i)), \quad U_j = \mathrm{HMAC}(P, U{j-1}) $$
The implementation absorbs the two padded key blocks once and copies those hash states for every iteration, so that an iteration costs two compression calls.
4. secp256k1
Source: Sources/QyvoraSecp256k1/U256.swift, Secp256k1.swift (SEC 2, RFC 6979, BIP-340).
4.1 The curve
$$E:; y^2 = x^3 + 7 \quad \text{over } \mathbb{F}_p, \qquad p = 2^{256} - 2^{32} - 977$$
p = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F
n = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141
Gx = 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798
Gy = 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8The group $E(\mathbb{F}_p)$ has prime order $n$ and cofactor 1, so every point other than the identity generates the group and no subgroup check is needed beyond the curve equation. Since $p \equiv 3 \pmod 4$, a square root of a quadratic residue $a$ is $a^{(p+1)/4} \bmod p$; the code computes this candidate and checks it by squaring, which is how compressed points are decompressed.
4.2 Montgomery multiplication as implemented (U256.swift)
A 256-bit integer is four 64-bit limbs, least significant first. Modulus implements arithmetic modulo
an odd $m$ with $2^{255} < m < 2^{256}$ (true of both $p$ and $n$), with $R = 2^{256}$. An element $a$ is
represented by $\tilde a = aR \bmod m$, and
$$\mathrm{montMul}(\tilde a, \tilde b) = \tilde a,\tilde b,R^{-1} \bmod m = \widetilde{ab}.$$
Precomputation in Modulus.init:
- $m' = -m^{-1} \bmod 2^{64}$ by Newton iteration $x \leftarrow x,(2 - m_0 x) \bmod 2^{64}$ from $x = 1$: each step doubles the number of correct low bits, so six steps give 64 bits.
- $R \bmod m = 2^{256} - m$, obtained as the two's complement of $m$ because $2^{255} < m$.
- $R^2 \bmod m$ by 256 modular doublings of $R \bmod m$.
montMul is the coarsely integrated operand scanning form. With accumulator $t$ of 4 limbs plus a carry
limb, for each limb $b_i$, $i = 0,\dots,3$:
$$ \begin{aligned} t &\leftarrow t + a\cdot b_i, \ q &\leftarrow t_0\cdot m' \bmod 2^{64}, \ t &\leftarrow (t + q\cdot m) / 2^{64}. \end{aligned} $$
The division is exact because $t_0 + q,m_0 \equiv 0 \pmod{2^{64}}$. After four iterations $t < 2m$, and the result is $t$ or $t - m$. The final subtraction is always computed and the result is chosen by a mask built from the borrow and the carry limb, not by a branch. Addition, subtraction and negation modulo $m$ use the same masked selection. Conversion is $\tilde a = \mathrm{montMul}(a, R^2)$ and $a = \mathrm{montMul}(\tilde a, 1)$.
Inversion uses Fermat's little theorem, $a^{-1} = a^{m-2} \bmod m$, by square-and-multiply over the 256 bits of the exponent. The exponent $m-2$ is a public constant, so the multiplication pattern reveals nothing secret; the base may be secret. The same exponentiation computes the square root with exponent $(p+1)/4$.
4.3 Group law
Points are in homogeneous projective coordinates $(X:Y:Z)$ with affine point $(X/Z, Y/Z)$ and identity $(0:1:0)$, coordinates in Montgomery form. Addition and doubling use the complete formulas of Renes, Costello and Batina (2016) for short Weierstrass curves with $a = 0$ (their Algorithms 7 and 9), with $b_3 = 3b = 21$. Complete means that one straight-line sequence of field operations is correct for every pair of inputs — distinct points, equal points, opposite points, the identity — so that the code has no exceptional case and no branch. This completeness holds for curves of odd order, which is the case here.
Scalar multiplication $kP$ is double-and-add-always, most significant bit first, over exactly 256 iterations starting from the identity: each iteration computes $2R$ and $2R+P$ and keeps one of them with a mask derived from the scalar bit. The sequence of field operations is therefore independent of $k$. No precomputed table, window or endomorphism is used; the same routine serves signing and verification.
4.4 ECDSA
For a private key $d \in [1, n-1]$, public key $Q = dG$, and message digest $z$ (the 32-byte hash interpreted as an integer and reduced modulo $n$):
$$ R = kG, \qquad r = R_x \bmod n, \qquad s = k^{-1}(z + r,d) \bmod n, $$
with $r \ne 0$ and $s \ne 0$. Verification computes $w = s^{-1}$, $u_1 = zw$, $u_2 = rw$ (mod $n$) and accepts if the point $u_1G + u_2Q$ is not the identity and its $x$-coordinate is congruent to $r$ modulo $n$.
Deterministic nonce (RFC 6979, section 3.2), HMAC-SHA-256. With $x$ the 32-byte private key and $h_1 = \mathrm{bits2octets}(\text{digest})$, i.e. the digest reduced modulo $n$:
$$ \begin{aligned} &V = \texttt{0x01}^{32}, \quad K = \texttt{0x00}^{32}\ &K = \mathrm{HMAC}_K(V,\Vert,\texttt{0x00},\Vert,x,\Vert,h_1), \quad V = \mathrm{HMAC}_K(V)\ &K = \mathrm{HMAC}_K(V,\Vert,\texttt{0x01},\Vert,x,\Vert,h_1), \quad V = \mathrm{HMAC}_K(V)\ &\text{repeat: } V = \mathrm{HMAC}_K(V);\ k = \mathrm{int}(V);\ \text{accept if } 1 \le k < n,\ &\qquad\text{otherwise } K = \mathrm{HMAC}_K(V,\Vert,\texttt{0x00}),\ V = \mathrm{HMAC}_K(V) \end{aligned} $$
If the resulting signature has $r = 0$ or $s = 0$ the next candidate of the same sequence is used.
Low-s. $(r, s)$ and $(r, n-s)$ are both valid. The signer outputs $s \le (n-1)/2$ (BIP-62 / BIP-146 for Bitcoin, EIP-2 for Ethereum), and the verifier rejects high $s$ by default.
Public-key recovery. From $(r, s)$, the digest and a recovery identifier $v \in {0,1,2,3}$ (bit 0: parity of $R_y$; bit 1: whether $R_x \ge n$), the signer's key is
$$ R = \mathrm{lift}\big(r + n\cdot\lfloor v/2\rfloor,; v \bmod 2\big), \qquad Q = r^{-1},(sR - zG). $$
When the signer negates $s$ for the low-s rule, the parity bit is flipped. Ethereum addresses are obtained this way from transaction signatures. Bit 1 is set with probability about $2^{-128}$; EIP-1559 transactions encode only the parity, and the transaction signer rejects the other case.
DER. Bitcoin signatures before Taproot are encoded as strict DER (BIP-66): 30 len 02 len r 02 len s
with minimal positive integers.
4.5 BIP-340 Schnorr signatures
Public keys are 32-byte $x$-coordinates; the point with even $y$ is implied. With the tagged hash $\mathrm{H}_{\mathit{tag}}(x) = \mathrm{SHA256}(\mathrm{SHA256}(\mathit{tag}),\Vert,\mathrm{SHA256}(\mathit{tag}),\Vert,x)$:
$$ \begin{aligned} &P = d'G, \qquad d = d' \text{ if } P_y \text{ is even, else } n - d'\ &t = \mathrm{bytes}(d) \oplus \mathrm{H}{\text{BIP0340/aux}}(a), \qquad k' = \mathrm{int}\big(\mathrm{H}{\text{BIP0340/nonce}}(t,\Vert,\mathrm{bytes}(P),\Vert,m)\big) \bmod n \ne 0\ &R = k'G, \qquad k = k' \text{ if } R_y \text{ is even, else } n - k'\ &e = \mathrm{int}\big(\mathrm{H}_{\text{BIP0340/challenge}}(\mathrm{bytes}(R),\Vert,\mathrm{bytes}(P),\Vert,m)\big) \bmod n\ &\sigma = \mathrm{bytes}(R),\Vert,\mathrm{bytes}\big((k + e,d) \bmod n\big) \end{aligned} $$
Verification parses $r < p$, $s < n$, lifts $P$ with even $y$, computes $R = sG - eP$ and accepts if $R$ is not the identity, has even $y$ and $R_x = r$. The implementation verifies every signature it produces before returning it, as the BIP recommends.
5. Hierarchical keys, mnemonics and encodings
Source: Sources/QyvoraCore/HD/, Sources/QyvoraCore/Encoding/, Sources/QyvoraEthereum/.
5.1 BIP-39
Entropy of $\mathrm{ENT}$ bits is followed by the first $\mathrm{CS} = \mathrm{ENT}/32$ bits of its SHA-256 digest; the result is cut into 11-bit groups, each an index into a list of $2^{11} = 2048$ words.
| ENT (bits) | CS (bits) | ENT + CS | Words |
|---|---|---|---|
| 128 | 4 | 132 | 12 |
| 160 | 5 | 165 | 15 |
| 192 | 6 | 198 | 18 |
| 224 | 7 | 231 | 21 |
| 256 | 8 | 264 | 24 |
The seed is $\mathrm{PBKDF2\text{-}HMAC\text{-}SHA512}\big(P = \mathrm{NFKD}(\text{sentence}),; S = \text{"mnemonic"},\Vert,\mathrm{NFKD}(\text{passphrase}),; c = 2048,; 64 \text{ bytes}\big)$. QYVORA generates 24 words by default and refuses to derive a seed from a sentence whose checksum is wrong. The checksum detects errors; it adds no security.
5.2 BIP-32
Master key from a seed $S$ of 16 to 64 bytes: $I = \mathrm{HMAC\text{-}SHA512}(\text{"Bitcoin seed"}, S)$, $k_m = I_L$, $c_m = I_R$, invalid if $I_L = 0$ or $I_L \ge n$.
Child derivation with index $i$, where $i \ge 2^{31}$ denotes a hardened child:
$$ I = \begin{cases} \mathrm{HMAC\text{-}SHA512}\big(c_{\mathrm{par}},; \texttt{0x00},\Vert,\mathrm{ser}{256}(k{\mathrm{par}}),\Vert,\mathrm{ser}{32}(i)\big) & \text{hardened}\[2pt] \mathrm{HMAC\text{-}SHA512}\big(c{\mathrm{par}},; \mathrm{ser}P(K{\mathrm{par}}),\Vert,\mathrm{ser}_{32}(i)\big) & \text{normal} \end{cases} $$
$$ k_i = (I_L + k_{\mathrm{par}}) \bmod n, \qquad K_i = I_L\cdot G + K_{\mathrm{par}}, \qquad c_i = I_R $$
The child is invalid if $I_L \ge n$ or $k_i = 0$ (resp. $K_i$ is the identity). Public derivation
($K_{\mathrm{par}} \to K_i$) exists only for normal children. Consequence used in
docs/QUANTUM_RESISTANCE.md: since $k_i - I_L = k_{\mathrm{par}}$ and $I_L$ is computable from the parent
extended public key, one normal child private key together with the parent extended public key gives the
parent private key.
A serialized extended key is 78 bytes — version (4), depth (1), parent fingerprint (4, the first four bytes of HASH160 of the parent public key), child number (4), chain code (32), key (33) — in Base58Check.
Paths used: $m/84'/c'/a'/{0,1}/i$ for Bitcoin native SegWit (BIP-84, coin type $c = 0$ on mainnet and 1 on test networks) and $m/44'/60'/0'/0/i$ for Ethereum (BIP-44).
5.3 Base58Check
$\mathrm{Base58Check}(x) = \mathrm{Base58}\big(x,\Vert,\mathrm{SHA256d}(x)[0..3]\big)$, where Base58 is
the big-endian base-58 expansion over an alphabet without 0, O, I, l, each leading zero byte being
encoded as the character 1.
5.4 Bech32 and Bech32m
An address is a human-readable part, the separator 1, and data characters from a 32-symbol alphabet, the
last six of which are a checksum. The checksum is a BCH code over $\mathbb{F}_{32}$, computed by
$$ \mathrm{polymod}(v_0,\dots,v_{k}):\quad c \leftarrow 1;\ \ \text{for each } v:\ \ b = c \gg 25,\quad c = \big((c \wedge \texttt{0x1ffffff}) \ll 5\big) \oplus v \oplus \bigoplus_{j:, b_j = 1} g_j $$
with $g = (\texttt{0x3b6a57b2}, \texttt{0x26508e6d}, \texttt{0x1ea119fa}, \texttt{0x3d4233dd}, \texttt{0x2a1462b3})$. The input is the expanded human-readable part (high three bits of each character, a zero, low five bits of each character), the data, and six zeros; the checksum is the result XORed with a constant: $1$ for Bech32 (BIP-173) and $\texttt{0x2bc830a3}$ for Bech32m (BIP-350). The code guarantees detection of any error affecting at most four characters in an address of the allowed length, and fails to detect other errors with probability about $2^{-30}$.
A SegWit address encodes a witness version $v \in [0,16]$ followed by the witness program regrouped from 8-bit to 5-bit symbols. Version 0 uses Bech32 and requires a 20-byte (P2WPKH) or 32-byte (P2WSH) program; versions 1 to 16 use Bech32m (version 1 with a 32-byte program is P2TR). The decoder enforces the pairing of version and checksum constant, the program length, and zero padding bits.
5.5 RLP
An item is a byte string or a list of items (Ethereum Yellow Paper, appendix B):
| Item | Encoding |
|---|---|
| A single byte $b < \texttt{0x80}$ | $b$ |
| A string of $0 \le \ell \le 55$ bytes | $(\texttt{0x80}+\ell),\Vert,\text{string}$ |
| A string of $\ell > 55$ bytes | $(\texttt{0xb7}+\Vert\mathrm{BE}(\ell)\Vert),\Vert,\mathrm{BE}(\ell),\Vert,\text{string}$ |
| A list whose concatenated encodings take $0 \le \ell \le 55$ bytes | $(\texttt{0xc0}+\ell),\Vert,\text{items}$ |
| A list with $\ell > 55$ | $(\texttt{0xf7}+\Vert\mathrm{BE}(\ell)\Vert),\Vert,\mathrm{BE}(\ell),\Vert,\text{items}$ |
where $\mathrm{BE}(\ell)$ is the minimal big-endian encoding of $\ell$ and $\Vert\mathrm{BE}(\ell)\Vert$ its
length in bytes. Integers are minimal big-endian strings (zero is the empty string). The decoder rejects
every non-canonical form: a single byte below 0x80 wrapped in a string header, a long form used for a
length below 56, a length with a leading zero byte, trailing data.
5.6 Ethereum transactions and addresses
An EIP-1559 (type 2, EIP-2718 envelope) transaction is signed over
$$ h = \mathrm{Keccak256}\big(\texttt{0x02},\Vert,\mathrm{RLP}([\mathit{chainId}, \mathit{nonce}, \mathit{maxPriorityFeePerGas}, \mathit{maxFeePerGas}, \mathit{gasLimit}, \mathit{to}, \mathit{value}, \mathit{data}, \mathit{accessList}])\big) $$
and the raw transaction is 0x02 followed by the RLP of the same nine fields and
$(y_{\mathrm{parity}}, r, s)$. The chain identifier is inside the signed payload, which gives replay
protection across chains. The most a transaction can cost is
$\mathit{gasLimit}\cdot\mathit{maxFeePerGas} + \mathit{value}$; the fee actually paid per unit of gas is
$\min(\mathit{maxFeePerGas},; \mathit{baseFee} + \mathit{maxPriorityFeePerGas})$. The base fee changes by
at most a factor $1 \pm 1/8$ from one block to the next.
An address is the last 20 bytes of $\mathrm{Keccak256}(X,\Vert,Y)$ for the uncompressed public key $(X, Y)$. EIP-55 capitalises the $i$-th hexadecimal character of the address when it is a letter and the $i$-th nibble of $\mathrm{Keccak256}$ of the lower-case hexadecimal string is at least 8.
6. Module lattices: MLWE and MSIS
Let $R_q = \mathbb{Z}_q[X]/(X^{256}+1)$. A module lattice problem works with vectors and matrices over $R_q$.
- Module-LWE (decision). Given a uniform $\mathbf{A} \in R_q^{k\times\ell}$, distinguish $(\mathbf{A},; \mathbf{A}\mathbf{s} + \mathbf{e})$, where $\mathbf{s}$ and $\mathbf{e}$ have small coefficients drawn from a narrow distribution, from $(\mathbf{A}, \mathbf{u})$ with uniform $\mathbf{u}$.
- Module-SIS. Given a uniform $\mathbf{A} \in R_q^{k\times\ell}$, find a non-zero $\mathbf{z}$ with $\Vert\mathbf{z}\Vert_\infty \le \beta$ such that $[,\mathbf{I} \mid \mathbf{A},],\mathbf{z} = \mathbf{0}$.
ML-KEM rests on Module-LWE. ML-DSA rests on Module-LWE for the secrecy of the key and on Module-SIS (in a "self-target" variant, in the random oracle model) for unforgeability. Security levels are estimated from the cost of lattice reduction against these instances; no reduction from a worst-case problem covers the concrete parameters.
7. ML-KEM (FIPS 203)
Source read: Sources/QyvoraMLKEM/MLKEM.swift, MLKEMArithmetic.swift.
7.1 Ring, NTT and base multiplication
$q = 3329 = 2^8\cdot 13 + 1$, $n = 256$. The element $\zeta = 17$ is a primitive 256-th root of unity modulo $q$; there is no primitive 512-th root, so $X^{256}+1$ does not split into linear factors but into 128 quadratic ones:
$$ X^{256} + 1 = \prod_{i=0}^{127}\Big(X^2 - \zeta^{,2,\mathrm{BitRev}_7(i)+1}\Big) $$
The NTT maps $f \in R_q$ to its 128 residues of degree at most 1, $\hat f = \big(f \bmod (X^2 - \zeta^{2\mathrm{BitRev}7(i)+1})\big){i}$, by seven layers of butterflies. Multiplication in the NTT domain is pairwise, in each quadratic extension (base multiplication), with $\gamma = \zeta^{2\mathrm{BitRev}_7(i)+1}$:
$$ (a_0 + a_1X)(b_0 + b_1X) \bmod (X^2 - \gamma) = (a_0b_0 + a_1b_1\gamma) + (a_0b_1 + a_1b_0),X $$
Implementation. Coefficients are signed 16-bit integers. Products use signed Montgomery reduction with $R = 2^{16}$ ($q^{-1} \bmod 2^{16} = 62209$, stored as $-3327$): for $|a| < q,2^{15}$ it returns $a,2^{-16} \bmod q$ in $(-q, q)$. Additive results are brought back by a centred Barrett reduction with the constant $20159 = \lceil 2^{26}/q \rfloor$. The table of $\zeta^{\mathrm{BitRev}_7(i)}$ is computed in Montgomery form at first use rather than transcribed. The inverse transform ends with a multiplication by $1441 = 2^{32}\cdot 128^{-1} \bmod q$, which removes the factor $128$ and the factor $2^{-16}$ left by the Montgomery base multiplication.
7.2 Sampling, compression, encoding
- $\mathrm{SampleNTT}$: rejection sampling of uniform coefficients in $[0, q)$ from SHAKE128, two 12-bit candidates per three bytes. The input is public.
- Centred binomial distribution $\mathrm{CBD}\eta$: each coefficient is $\sum{i=1}^{\eta} x_i - \sum_{i=1}^{\eta} y_i$ for $2\eta$ uniform bits, so it lies in $[-\eta, \eta]$ with variance $\eta/2$. The code computes the bit sums with word-parallel masks, without a table.
- Compression to $d$ bits and decompression:
$$ \mathrm{Compress}_d(x) = \Big\lceil \frac{2^d}{q},x \Big\rfloor \bmod 2^d, \qquad \mathrm{Decompress}_d(y) = \Big\lceil \frac{q}{2^d},y \Big\rfloor $$
with $\big|\mathrm{Decompress}_d(\mathrm{Compress}_d(x)) - x \bmod^{\pm} q\big| \le \lceil q/2^{d+1} \rfloor$. Implementation. Because $q$ is odd, $\mathrm{Compress}_d(x) = \lfloor (2^d x + 1664)/q \rfloor \bmod 2^d$. The quotient is computed as $\lfloor N\cdot M / 2^{36} \rfloor$ with $M = \lceil 2^{36}/q \rfloor$, exact for every numerator $N < 2^{23}$. No division instruction is applied to secret data: this is the countermeasure to the timing leak known for division-based compression.
- $\mathrm{ByteEncode}_d$ packs 256 $d$-bit integers little-endian into $32d$ bytes.
7.3 K-PKE
With $G = \text{SHA3-512}$, $H = \text{SHA3-256}$, $J = \text{SHAKE256}$ with 32 bytes of output, $\mathrm{PRF}_\eta = \text{SHAKE256}$ with $64\eta$ bytes, $\mathrm{XOF} = \text{SHAKE128}$:
$$ \begin{aligned} \textbf{KeyGen}(d):\quad & (\rho, \sigma) = G(d,\Vert,k), \quad \hat{\mathbf{A}}[i,j] = \mathrm{SampleNTT}(\rho,\Vert,j,\Vert,i), \quad \mathbf{s}, \mathbf{e} \leftarrow \mathrm{CBD}{\eta_1}(\sigma)\ & \hat{\mathbf{t}} = \hat{\mathbf{A}}\circ\hat{\mathbf{s}} + \hat{\mathbf{e}}, \qquad \mathit{ek}{\mathrm{PKE}} = \mathrm{ByteEncode}{12}(\hat{\mathbf{t}}),\Vert,\rho, \qquad \mathit{dk}{\mathrm{PKE}} = \mathrm{ByteEncode}{12}(\hat{\mathbf{s}})\[4pt] \textbf{Encrypt}(\mathit{ek}, m, r):\quad & \mathbf{y} \leftarrow \mathrm{CBD}{\eta_1}(r), \quad \mathbf{e}1 \leftarrow \mathrm{CBD}{\eta_2}(r), \quad e_2 \leftarrow \mathrm{CBD}{\eta_2}(r)\ & \mathbf{u} = \mathrm{NTT}^{-1}(\hat{\mathbf{A}}^{\mathsf T}\circ\hat{\mathbf{y}}) + \mathbf{e}1, \qquad v = \mathrm{NTT}^{-1}(\hat{\mathbf{t}}^{\mathsf T}\circ\hat{\mathbf{y}}) + e_2 + \mathrm{Decompress}1(m)\ & c = \mathrm{ByteEncode}{d_u}(\mathrm{Compress}{d_u}(\mathbf{u})),\Vert,\mathrm{ByteEncode}{d_v}(\mathrm{Compress}_{d_v}(v))\[4pt] \textbf{Decrypt}(\mathit{dk}, c):\quad & w = v' - \mathrm{NTT}^{-1}(\hat{\mathbf{s}}^{\mathsf T}\circ\mathrm{NTT}(\mathbf{u}')), \qquad m = \mathrm{ByteEncode}_1(\mathrm{Compress}_1(w)) \end{aligned} $$
Correctness: $w = \mathbf{e}^{\mathsf T}\mathbf{y} + e_2 - \mathbf{s}^{\mathsf T}\mathbf{e}_1 + \text{compression errors} + \lceil q/2 \rfloor, m$, and each bit decodes correctly when the noise term has absolute value below about $q/4$.
7.4 The KEM: Fujisaki–Okamoto transform with implicit rejection
$$ \begin{aligned} \textbf{KeyGen}(d, z):\quad & (\mathit{ek}, \mathit{dk}{\mathrm{PKE}}) = \text{K-PKE.KeyGen}(d), \qquad \mathit{dk} = \mathit{dk}{\mathrm{PKE}},\Vert,\mathit{ek},\Vert,H(\mathit{ek}),\Vert,z\ \textbf{Encaps}(\mathit{ek}, m):\quad & (K, r) = G\big(m,\Vert,H(\mathit{ek})\big), \qquad c = \text{K-PKE.Encrypt}(\mathit{ek}, m, r)\ \textbf{Decaps}(\mathit{dk}, c):\quad & m' = \text{K-PKE.Decrypt}(\mathit{dk}_{\mathrm{PKE}}, c), \quad (K', r') = G(m',\Vert,h), \quad \bar K = J(z,\Vert,c)\ & c' = \text{K-PKE.Encrypt}(\mathit{ek}, m', r'), \qquad K = \begin{cases} K' & c = c' \ \bar K & c \ne c' \end{cases} \end{aligned} $$
Encryption is made deterministic by deriving its randomness from the message; decapsulation re-encrypts and compares. On a mismatch it does not fail: it returns a pseudorandom key derived from the secret $z$ and the ciphertext (implicit rejection), so that an attacker learns nothing from the behaviour of the decapsulator. Implementation. The comparison accumulates the XOR of all ciphertext bytes, turns it into a mask without branching and selects $K'$ or $\bar K$ byte by byte. The checked interface applies the input checks of FIPS 203 section 7: encapsulation key length and modulus check (every 12-bit coefficient below $q$), decapsulation key length and hash check, ciphertext length.
7.5 Parameter sets
| ML-KEM-512 | ML-KEM-768 | ML-KEM-1024 | |
|---|---|---|---|
| NIST security category | 1 | 3 | 5 |
| $k$ | 2 | 3 | 4 |
| $\eta_1$ | 3 | 2 | 2 |
| $\eta_2$ | 2 | 2 | 2 |
| $d_u$ | 10 | 10 | 11 |
| $d_v$ | 4 | 4 | 5 |
| Encapsulation key, $384k+32$ bytes | 800 | 1184 | 1568 |
| Decapsulation key, $768k+96$ bytes | 1632 | 2400 | 3168 |
| Ciphertext, $32(d_uk+d_v)$ bytes | 768 | 1088 | 1568 |
| Shared secret (bytes) | 32 | 32 | 32 |
| Decapsulation failure rate (FIPS 203) | $2^{-138.8}$ | $2^{-164.8}$ | $2^{-174.8}$ |
$n = 256$ and $q = 3329$ for all sets.
8. ML-DSA (FIPS 204)
Source read: Sources/QyvoraMLDSA/MLDSA.swift, MLDSAArithmetic.swift.
8.1 Ring and NTT
$q = 8380417 = 2^{23} - 2^{13} + 1$, $n = 256$, $d = 13$. Here $q \equiv 1 \pmod{512}$ and $\zeta = 1753$ is a primitive 512-th root of unity, so $X^{256}+1$ splits completely,
$$ X^{256} + 1 = \prod_{i=0}^{255}\big(X - \zeta^{,2,\mathrm{BitRev}_8(i)+1}\big), $$
and multiplication in the NTT domain is coefficient-wise. Implementation. Coefficients are signed 32-bit
integers; products use signed Montgomery reduction with $R = 2^{32}$
($q^{-1} \bmod 2^{32} = 58728449$); the reduction reduce32 uses $2^{23} \equiv 2^{13} - 1 \pmod q$;
the inverse transform ends with $41978 = 2^{64}\cdot 256^{-1} \bmod q$.
8.2 Rounding and hints
$$ \begin{aligned} \mathrm{Power2Round}(r):\quad & r_0 = r \bmod^{\pm} 2^{d}, \qquad r_1 = (r - r_0)/2^{d}\ \mathrm{Decompose}(r):\quad & r_0 = r \bmod^{\pm} 2\gamma_2; \quad \text{if } r - r_0 = q - 1:\ r_1 = 0,\ r_0 \leftarrow r_0 - 1; \quad \text{else } r_1 = (r - r_0)/(2\gamma_2)\ \mathrm{HighBits}(r) &= r_1, \qquad \mathrm{LowBits}(r) = r_0\ \mathrm{MakeHint}(z, r) &= [,\mathrm{HighBits}(r) \ne \mathrm{HighBits}(r+z),]\ \mathrm{UseHint}(h, r) &= \begin{cases} r_1 & h = 0\ (r_1 + 1) \bmod m & h = 1,\ r_0 > 0\ (r_1 - 1) \bmod m & h = 1,\ r_0 \le 0 \end{cases} \qquad m = \frac{q-1}{2\gamma_2} \end{aligned} $$
The public key holds only the high part $\mathbf{t}_1$ of $\mathbf{t}$; the hint lets the verifier recover
the high bits of $\mathbf{w}$ without knowing $\mathbf{t}_0$. Implementation. decompose is
division-free and branch-free on its argument (multiplication by a reciprocal constant and shifts, chosen
by the public value of $\gamma_2$).
8.3 Key generation, signing, verification
With $H = \text{SHAKE256}$ and $\Vert\cdot\Vert_\infty$ the largest centred coefficient in absolute value:
$$ \begin{aligned} \textbf{KeyGen}(\xi):\quad & (\rho, \rho', K) = H(\xi,\Vert,k,\Vert,\ell,; 128 \text{ bytes}), \quad \hat{\mathbf{A}} = \mathrm{ExpandA}(\rho), \quad (\mathbf{s}_1, \mathbf{s}_2) = \mathrm{ExpandS}(\rho') \in [-\eta, \eta]\ & \mathbf{t} = \mathbf{A}\mathbf{s}_1 + \mathbf{s}_2, \quad (\mathbf{t}_1, \mathbf{t}_0) = \mathrm{Power2Round}(\mathbf{t}), \quad \mathit{pk} = (\rho, \mathbf{t}_1), \quad \mathit{tr} = H(\mathit{pk}, 64)\ & \mathit{sk} = (\rho, K, \mathit{tr}, \mathbf{s}_1, \mathbf{s}_2, \mathbf{t}_0) \end{aligned} $$
$$ \begin{aligned} \textbf{Sign}(\mathit{sk}, M', \mathit{rnd}):\quad & \mu = H(\mathit{tr},\Vert,M', 64), \qquad \rho'' = H(K,\Vert,\mathit{rnd},\Vert,\mu, 64), \qquad \kappa = 0\ \text{repeat:}\quad & \mathbf{y} = \mathrm{ExpandMask}(\rho'', \kappa) \in (-\gamma_1, \gamma_1]^{\ell}, \qquad \mathbf{w} = \mathbf{A}\mathbf{y}, \qquad \mathbf{w}_1 = \mathrm{HighBits}(\mathbf{w})\ & \tilde c = H\big(\mu,\Vert,\mathrm{w1Encode}(\mathbf{w}_1),; \lambda/4 \text{ bytes}\big), \qquad c = \mathrm{SampleInBall}(\tilde c)\ & \mathbf{z} = \mathbf{y} + c,\mathbf{s}_1, \qquad \mathbf{r}_0 = \mathrm{LowBits}(\mathbf{w} - c,\mathbf{s}2)\ & \text{reject if } \Vert\mathbf{z}\Vert\infty \ge \gamma_1 - \beta \ \text{ or } \ \Vert\mathbf{r}0\Vert\infty \ge \gamma_2 - \beta\ & \mathbf{h} = \mathrm{MakeHint}(-c,\mathbf{t}_0,; \mathbf{w} - c,\mathbf{s}_2 + c,\mathbf{t}_0)\ & \text{reject if } \Vert c,\mathbf{t}0\Vert\infty \ge \gamma_2 \ \text{ or the number of ones in } \mathbf{h} \text{ exceeds } \omega\ & \kappa \leftarrow \kappa + \ell \ \text{ on rejection}; \qquad \sigma = (\tilde c, \mathbf{z}, \mathbf{h}) \end{aligned} $$
$$ \begin{aligned} \textbf{Verify}(\mathit{pk}, M', \sigma):\quad & \mathbf{w}'_{\mathrm{approx}} = \mathbf{A}\mathbf{z} - c,\mathbf{t}1 2^{d}, \qquad \mathbf{w}'1 = \mathrm{UseHint}(\mathbf{h}, \mathbf{w}'{\mathrm{approx}})\ & \text{accept iff } \Vert\mathbf{z}\Vert\infty < \gamma_1 - \beta, \ \text{ the hint is well formed with at most } \omega \text{ ones, and } \tilde c = H\big(\mu,\Vert,\mathrm{w1Encode}(\mathbf{w}'_1), \lambda/4\big) \end{aligned} $$
$c$ is a polynomial with exactly $\tau$ coefficients in ${-1, +1}$ and the rest zero, so $\Vert c,\mathbf{s}i\Vert\infty \le \tau\eta = \beta$. The first rejection condition makes the distribution of $\mathbf{z}$ independent of $\mathbf{s}_1$ (rejection sampling: the scheme is a "Fiat–Shamir with aborts" signature); the second ensures that the high bits of $\mathbf{w} - c,\mathbf{s}_2$ equal those of $\mathbf{w}$, which is what the verifier recomputes.
The message representative of the external interface is $M' = \mathrm{byte}(0),\Vert,\mathrm{byte}(|\mathit{ctx}|),\Vert,\mathit{ctx},\Vert,M$ for pure ML-DSA and $M' = \mathrm{byte}(1),\Vert,\mathrm{byte}(|\mathit{ctx}|),\Vert,\mathit{ctx},\Vert,\mathrm{OID},\Vert,\mathrm{PH}(M)$ for HashML-DSA, with a context of at most 255 bytes. $\mathit{rnd}$ is 32 fresh random bytes in the hedged variant (the default in QYVORA) and 32 zero bytes in the deterministic variant.
Implementation. Norm checks OR an indicator over every coefficient without early exit. Branches of the rejection loop depend only on candidates that are discarded. The decoder of the hint enforces all the malformed-input checks of the standard (non-decreasing counters bounded by $\omega$, strictly increasing positions, zero padding). The private-key decoder checks that the encodings of $\mathbf{s}_1$ and $\mathbf{s}_2$ lie in $[-\eta, \eta]$. Secret polynomials live in a workspace erased on release.
8.4 Parameter sets
| ML-DSA-44 | ML-DSA-65 | ML-DSA-87 | |
|---|---|---|---|
| NIST security category | 2 | 3 | 5 |
| $(k, \ell)$ | (4, 4) | (6, 5) | (8, 7) |
| $\eta$ | 2 | 4 | 2 |
| $\tau$ | 39 | 49 | 60 |
| $\beta = \tau\eta$ | 78 | 196 | 120 |
| $\lambda$ (bits; $\tilde c$ is $\lambda/4$ bytes) | 128 | 192 | 256 |
| $\gamma_1$ | $2^{17}$ | $2^{19}$ | $2^{19}$ |
| $\gamma_2$ | $(q-1)/88 = 95232$ | $(q-1)/32 = 261888$ | $(q-1)/32 = 261888$ |
| $\omega$ | 80 | 55 | 75 |
| Expected number of signing iterations (FIPS 204) | 4.25 | 5.1 | 3.85 |
| Public key (bytes) | 1312 | 1952 | 2592 |
| Private key (bytes) | 2560 | 4032 | 4896 |
| Signature (bytes) | 2420 | 3309 | 4627 |
$q = 8380417$, $d = 13$, $\zeta = 1753$ for all sets. Sizes follow from $|\mathit{pk}| = 32 + 320k$, $|\mathit{sk}| = 128 + 32,b_\eta(k+\ell) + 416k$ with $b_\eta = 3$ for $\eta = 2$ and $4$ for $\eta = 4$, and $|\sigma| = \lambda/4 + 32,\ell,(1 + \log_2\gamma_1) + \omega + k$.
9. SLH-DSA (FIPS 205)
Source read: Sources/QyvoraSLHDSA/.
SLH-DSA is built from a hash function only. A signature is a chain: a few-time signature (FORS) signs the message digest; a tree of one-time signatures (the hypertree of XMSS trees over WOTS+) certifies the FORS public key up to the root contained in the public key. The signer is stateless: which leaf is used is chosen pseudorandomly from the message.
9.1 WOTS+
With $w = 16$ ($\lg w = 4$) for every parameter set, an $n$-byte message is cut into
$$ \mathit{len}_1 = \Big\lceil \frac{8n}{\lg w} \Big\rceil = 2n, \qquad \mathit{len}_2 = \Big\lfloor \frac{\log_2(\mathit{len}_1(w-1))}{\lg w} \Big\rfloor + 1 = 3, \qquad \mathit{len} = \mathit{len}_1 + \mathit{len}_2 = 2n+3 $$
base-$w$ digits: $\mathit{len}_1$ message digits $m_i$ and $\mathit{len}_2$ digits of the checksum
$$ C = \sum_{i=0}^{\mathit{len}_1 - 1} (w - 1 - m_i). $$
Each digit selects a position in a hash chain of length $w - 1$: $\mathrm{chain}(X, i, s) = F^{(s)}$ applied to $X$ starting at step $i$, each step with its own address. The secret values are $\mathit{sk}_j = \mathrm{PRF}(\mathrm{PK.seed}, \mathrm{SK.seed}, \mathrm{ADRS}_j)$, the signature is $\sigma_j = \mathrm{chain}(\mathit{sk}j, 0, m_j)$, and the public key is $T{\mathit{len}}$ of the chain ends $\mathrm{chain}(\mathit{sk}_j, 0, w-1)$. The verifier completes each chain by $w - 1 - m_j$ steps. The checksum prevents forgeries: increasing a message digit (which anyone can do by hashing forward) forces a checksum digit to decrease, which would require inverting $F$.
9.2 XMSS, the hypertree and FORS
- XMSS. A Merkle tree of height $h'$ whose $2^{h'}$ leaves are WOTS+ public keys; internal nodes are $H(\text{left},\Vert,\text{right})$ with the node address. An XMSS signature is a WOTS+ signature and the $h'$ sibling nodes of the authentication path.
- Hypertree. $d$ layers of XMSS trees, total height $h = d,h'$. The bottom layer signs the FORS public key; each higher layer signs the root of the tree below. The public key contains the root of the single top tree.
- FORS. $k$ Merkle trees of height $a$, each with $2^a$ secret leaves. The first $k,a$ bits of the message digest give $k$ indices of $a$ bits; the signature reveals, for each tree, the selected secret value and its authentication path. The FORS public key is $T_k$ of the $k$ roots.
The message digest of $m$ bytes is split into $\lceil ka/8 \rceil$ bytes for FORS, $\lceil (h-h')/8 \rceil$ bytes for the index of the bottom XMSS tree (reduced modulo $2^{h-h'}$) and $\lceil h'/8 \rceil$ bytes for the leaf index (reduced modulo $2^{h'}$), so $m = \lceil ka/8 \rceil + \lceil (h-h')/8 \rceil + \lceil h'/8 \rceil$.
Signing: $R = \mathrm{PRF}{\mathrm{msg}}(\mathrm{SK.prf}, \mathit{opt_rand}, M')$, $\mathit{digest} = H{\mathrm{msg}}(R, \mathrm{PK.seed}, \mathrm{PK.root}, M')$, $\sigma = R,\Vert,\sigma_{\mathrm{FORS}},\Vert,\sigma_{\mathrm{HT}}$. In the hedged variant $\mathit{opt_rand}$ is $n$ fresh random bytes; in the deterministic variant it is $\mathrm{PK.seed}$. $M'$ is formed with the same context and pre-hash rules as in ML-DSA.
Implementation. Signing computes each needed tree whole (all leaves, then the levels bottom-up) instead of the recursive node function of the standard, and may compute leaves on several cores; the values are the same. Verification compares the recomputed root with $\mathrm{PK.root}$ in constant time.
9.3 Addresses and tweakable hash functions
Every hash call is bound to its position by a 32-byte address
$\mathrm{ADRS} = \text{layer (4)},\Vert,\text{tree (12)},\Vert,\text{type (4)},\Vert,\text{three words (4 each)}$,
with types WOTS_HASH = 0, WOTS_PK = 1, TREE = 2, FORS_TREE = 3, FORS_ROOTS = 4, WOTS_PRF = 5,
FORS_PRF = 6. The address and $\mathrm{PK.seed}$ are the tweak: they make every call a distinct function,
which defeats multi-target attacks.
SHAKE instantiation (all three security levels):
$$ \begin{aligned} F, H, T_\ell(\mathrm{PK.seed}, \mathrm{ADRS}, M) &= \mathrm{SHAKE256}(\mathrm{PK.seed},\Vert,\mathrm{ADRS},\Vert,M,; 8n)\ \mathrm{PRF}(\mathrm{PK.seed}, \mathrm{SK.seed}, \mathrm{ADRS}) &= \mathrm{SHAKE256}(\mathrm{PK.seed},\Vert,\mathrm{ADRS},\Vert,\mathrm{SK.seed},; 8n)\ \mathrm{PRF}{\mathrm{msg}}(\mathrm{SK.prf}, \mathit{opt_rand}, M) &= \mathrm{SHAKE256}(\mathrm{SK.prf},\Vert,\mathit{opt_rand},\Vert,M,; 8n)\ H{\mathrm{msg}}(R, \mathrm{PK.seed}, \mathrm{PK.root}, M) &= \mathrm{SHAKE256}(R,\Vert,\mathrm{PK.seed},\Vert,\mathrm{PK.root},\Vert,M,; 8m) \end{aligned} $$
SHA-2 instantiation, with the 22-byte compressed address $\mathrm{ADRS}^c = \mathrm{ADRS}[3],\Vert,\mathrm{ADRS}[8..15],\Vert,\mathrm{ADRS}[19],\Vert,\mathrm{ADRS}[20..31]$:
| Function | Category 1 ($n = 16$) | Categories 3 and 5 ($n = 24, 32$) |
|---|---|---|
| $F$, $\mathrm{PRF}$ | $\mathrm{Trunc}_n,\mathrm{SHA256}(\mathrm{PK.seed},\Vert,0^{64-n},\Vert,\mathrm{ADRS}^c,\Vert,M)$ | the same, with SHA-256 |
| $H$, $T_\ell$ | $\mathrm{Trunc}_n,\mathrm{SHA256}(\mathrm{PK.seed},\Vert,0^{64-n},\Vert,\mathrm{ADRS}^c,\Vert,M)$ | $\mathrm{Trunc}_n,\mathrm{SHA512}(\mathrm{PK.seed},\Vert,0^{128-n},\Vert,\mathrm{ADRS}^c,\Vert,M)$ |
| $\mathrm{PRF}_{\mathrm{msg}}$ | $\mathrm{Trunc}_n,\mathrm{HMAC\text{-}SHA256}(\mathrm{SK.prf},, \mathit{opt_rand},\Vert,M)$ | $\mathrm{Trunc}_n,\mathrm{HMAC\text{-}SHA512}(\mathrm{SK.prf},, \mathit{opt_rand},\Vert,M)$ |
| $H_{\mathrm{msg}}$ | $\mathrm{MGF1\text{-}SHA256}(R,\Vert,\mathrm{PK.seed},\Vert,\mathrm{SHA256}(R,\Vert,\mathrm{PK.seed},\Vert,\mathrm{PK.root},\Vert,M),, m)$ | the same with SHA-512 |
The zero padding fills the first compression block, so the state after absorbing $\mathrm{PK.seed}$ is computed once and reused for every call (the implementation does this).
9.4 Parameter sets
Each row applies to the SHA2 and the SHAKE variant of the same name, which gives the twelve sets: SLH-DSA-SHA2-{128s, 128f, 192s, 192f, 256s, 256f} and SLH-DSA-SHAKE-{128s, 128f, 192s, 192f, 256s, 256f}.
| Set | $n$ | $h$ | $d$ | $h'$ | $a$ | $k$ | $\lg w$ | $m$ | Category | Public key (bytes) | Signature (bytes) |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 128s | 16 | 63 | 7 | 9 | 12 | 14 | 4 | 30 | 1 | 32 | 7 856 |
| 128f | 16 | 66 | 22 | 3 | 6 | 33 | 4 | 34 | 1 | 32 | 17 088 |
| 192s | 24 | 63 | 7 | 9 | 14 | 17 | 4 | 39 | 3 | 48 | 16 224 |
| 192f | 24 | 66 | 22 | 3 | 8 | 33 | 4 | 42 | 3 | 48 | 35 664 |
| 256s | 32 | 64 | 8 | 8 | 14 | 22 | 4 | 47 | 5 | 64 | 29 792 |
| 256f | 32 | 68 | 17 | 4 | 9 | 35 | 4 | 49 | 5 | 64 | 49 856 |
The public key is $\mathrm{PK.seed},\Vert,\mathrm{PK.root}$ ($2n$ bytes), the private key $\mathrm{SK.seed},\Vert,\mathrm{SK.prf},\Vert,\mathrm{PK.seed},\Vert,\mathrm{PK.root}$ ($4n$ bytes), and
$$ |\sigma| = \big(1 + k(1+a) + h + d\cdot\mathit{len}\big)\cdot n \ \text{bytes}, \qquad \mathit{len} = 2n + 3. $$
The "s" sets minimise signature size and the "f" sets minimise signing time. The standard limits each key to $2^{64}$ signatures.
10. AES and GCM
Source read: Sources/QyvoraSymmetric/AES.swift, AESGCM.swift (FIPS 197, SP 800-38D).
10.1 AES
Bytes are elements of $\mathbb{F}_{2^8} = \mathbb{F}_2[x]/(x^8 + x^4 + x^3 + x + 1)$. The state is a $4\times4$ array of bytes. With $N_k$ key words: AES-128, -192, -256 have $N_k = 4, 6, 8$ and $N_r = 10, 12, 14$ rounds. Each round applies
- SubBytes: $S(a) = \mathrm{Aff}(a^{-1})$, with $0^{-1} := 0$ and the affine map over $\mathbb{F}2$ $b_i = a_i \oplus a{i+4} \oplus a_{i+5} \oplus a_{i+6} \oplus a_{i+7} \oplus c_i$ (indices modulo 8), $c = \texttt{0x63}$;
- ShiftRows: row $r$ is rotated left by $r$ positions;
- MixColumns: each column, as a polynomial over $\mathbb{F}_{2^8}$, is multiplied by $\texttt{03},y^3 + \texttt{01},y^2 + \texttt{01},y + \texttt{02}$ modulo $y^4 + 1$ (omitted in the last round);
- AddRoundKey: XOR with the round key.
The key schedule is $w_i = w_{i-N_k} \oplus g(w_{i-1})$, where $g$ applies RotWord, SubWord and the round constant $x^{i/N_k - 1}$ when $i \equiv 0 \pmod{N_k}$, SubWord alone when $N_k = 8$ and $i \equiv 4 \pmod 8$, and is the identity otherwise.
Implementation. There is no lookup table. The state is two 64-bit words; the S-box is computed algebraically on eight bytes in parallel: the inverse as $a^{254}$ by an addition chain of four multiplications and seven squarings in $\mathbb{F}_{2^8}$ (squaring is linear), then the affine map as XORs of byte rotations. No memory index or branch depends on the key or the data. This favours a simple constant-time structure over speed, and does not use the processor's AES instructions.
10.2 GCM
Let $E_K$ be AES encryption, $H = E_K(0^{128})$, and for a 96-bit nonce $J_0 = \mathit{IV},\Vert,0^{31},\Vert,1$.
$$ \begin{aligned} C_i &= P_i \oplus E_K\big(\mathrm{inc}{32}^{,i}(J_0)\big), \quad i = 1, 2, \dots\ S &= \mathrm{GHASH}H\big(A,\Vert,0^{v},\Vert,C,\Vert,0^{u},\Vert,[\mathrm{len}(A)]{64},\Vert,[\mathrm{len}(C)]{64}\big)\ T &= E_K(J_0) \oplus S \end{aligned} $$
$$ \mathrm{GHASH}H(X_1,\dots,X_m) = \bigoplus{i=1}^{m} X_i\cdot H^{,m-i+1} \quad\Longleftrightarrow\quad Y_0 = 0,\ \ Y_i = (Y_{i-1}\oplus X_i)\cdot H $$
in $\mathbb{F}_{2^{128}} = \mathbb{F}_2[x]/(x^{128} + x^7 + x^2 + x + 1)$, with the bit-reflected convention of the standard (the first bit of a block is the coefficient of $x^0$; multiplication by $x$ is a right shift with reduction by $R = \texttt{0xe1},\Vert,0^{120}$).
Constraints: a (key, nonce) pair must never be used twice — a repeat reveals the XOR of the plaintexts and allows recovery of $H$, hence forgeries; at most $2^{39} - 256$ bits ($2^{36} - 32$ bytes) of plaintext per nonce; with random 96-bit nonces, at most $2^{32}$ encryptions per key, which keeps the probability of a repeat below $2^{-32}$.
Implementation. The public interface accepts only 96-bit nonces and 128-bit tags. GHASH precomputes the 128 multiples $H\cdot x^i$ and, for every block, reads the whole table in a fixed order, keeping or discarding each entry with a mask derived from one bit of the accumulator. Decryption checks the tag in constant time before any plaintext is produced.
11. ChaCha20 and Poly1305
Source read: Sources/QyvoraSymmetric/ChaCha20Poly1305.swift (RFC 8439).
ChaCha20. The state is sixteen 32-bit words: four constants ("expand 32-byte k"), eight key words,
a 32-bit block counter and three nonce words. The quarter round on $(a, b, c, d)$ is
$$ \begin{aligned} a &\mathrel{+}= b; & d &\mathrel{\oplus}= a; & d &\leftarrow d \lll 16;\ c &\mathrel{+}= d; & b &\mathrel{\oplus}= c; & b &\leftarrow b \lll 12;\ a &\mathrel{+}= b; & d &\mathrel{\oplus}= a; & d &\leftarrow d \lll 8;\ c &\mathrel{+}= d; & b &\mathrel{\oplus}= c; & b &\leftarrow b \lll 7. \end{aligned} $$
Ten double rounds are applied, each a column round on $(0,4,8,12)$, $(1,5,9,13)$, $(2,6,10,14)$, $(3,7,11,15)$ followed by a diagonal round on $(0,5,10,15)$, $(1,6,11,12)$, $(2,7,8,13)$, $(3,4,9,14)$; the initial state is then added word-wise and serialised little-endian to give 64 keystream bytes.
Poly1305. With a one-time key $(r, s)$, $r$ clamped by
$r \leftarrow r \wedge \texttt{0x0ffffffc0ffffffc0ffffffc0fffffff}$, and the message cut into 16-byte
blocks $c_i$, each read little-endian with a 1 bit appended above its most significant byte:
$$ \mathit{acc} \leftarrow \big((\mathit{acc} + c_i)\cdot r\big) \bmod (2^{130} - 5), \qquad \mathrm{tag} = (\mathit{acc} + s) \bmod 2^{128}. $$
A one-time key must authenticate one message only. Implementation. Five 26-bit limbs with 64-bit products; the final reduction computes $h + 5 - 2^{130}$ and selects by mask.
AEAD. The one-time key is the first 32 bytes of the ChaCha20 block with counter 0; the plaintext is encrypted starting at counter 1; the tag is Poly1305 over $A,\Vert,\mathrm{pad}{16},\Vert,C,\Vert,\mathrm{pad}{16},\Vert,[\mathrm{len}(A)]{64}^{\mathrm{LE}},\Vert,[\mathrm{len}(C)]{64}^{\mathrm{LE}}$. ChaCha20-Poly1305 is part of the engine; the vault of version 1 uses AES-256-GCM.
12. Argon2id
Source read: Sources/QyvoraSymmetric/Argon2.swift, BLAKE2b.swift (RFC 9106, RFC 7693).
Inputs: password $P$, salt $S$, optional secret $K$ and associated data $X$, parallelism $p$, tag length $T$, memory $m$ in kibibytes, passes $t$, version $v = \texttt{0x13}$, type $y$ (2 for Argon2id). $H^{x}$ denotes BLAKE2b with an $x$-byte digest and all integers are 32-bit little-endian.
$$ H_0 = H^{64}\big(p,\Vert,T,\Vert,m,\Vert,t,\Vert,v,\Vert,y,\Vert,|P|,\Vert,P,\Vert,|S|,\Vert,S,\Vert,|K|,\Vert,K,\Vert,|X|,\Vert,X\big) $$
The memory is $m' = 4p\lfloor m/4p \rfloor$ blocks of 1024 bytes, arranged in $p$ lanes of $q = m'/p$ columns, each lane divided into four slices.
Variable-length hash $H'$. For an output of $T$ bytes:
$$ H'^{,T}(A) = \begin{cases} H^{T}(T,\Vert,A) & T \le 64\[2pt] W_1,\Vert\cdots\Vert,W_r,\Vert,V_{r+1}, \quad r = \lceil T/32 \rceil - 2 & T > 64 \end{cases} $$
with $V_1 = H^{64}(T,\Vert,A)$, $V_i = H^{64}(V_{i-1})$, $W_i$ the first 32 bytes of $V_i$, and $V_{r+1} = H^{T-32r}(V_r)$.
Filling the memory.
$$ \begin{aligned} B[i][0] &= H'^{,1024}(H_0,\Vert,0,\Vert,i), \qquad B[i][1] = H'^{,1024}(H_0,\Vert,1,\Vert,i)\ B[i][j] &= G\big(B[i][j-1],; B[l][z]\big) && \text{first pass}\ B[i][j] &= G\big(B[i][j-1],; B[l][z]\big) \oplus B[i][j] && \text{later passes (version 0x13)} \end{aligned} $$
with $B[i][-1] := B[i][q-1]$, and finally $\mathrm{Tag} = H'^{,T}\big(B[0][q-1] \oplus \cdots \oplus B[p-1][q-1]\big)$.
Compression function $G$. $R = X \oplus Y$ is viewed as an $8\times8$ matrix of 128-bit registers. The permutation $\mathcal{P}$ (one round of BLAKE2b's structure on sixteen 64-bit words) is applied to each row and then to each column, giving $Z$; the output is $Z \oplus R$. Inside $\mathcal{P}$ the BLAKE2b addition is replaced by a multiplicative mix,
$$ \begin{aligned} a &\leftarrow a + b + 2,a_L b_L; & d &\leftarrow (d \oplus a) \ggg 32;\ c &\leftarrow c + d + 2,c_L d_L; & b &\leftarrow (b \oplus c) \ggg 24;\ a &\leftarrow a + b + 2,a_L b_L; & d &\leftarrow (d \oplus a) \ggg 16;\ c &\leftarrow c + d + 2,c_L d_L; & b &\leftarrow (b \oplus c) \ggg 63, \end{aligned} $$
modulo $2^{64}$, where $x_L$ is the low 32 bits of $x$.
Indexing rule. Each new block needs a reference block $B[l][z]$, chosen from two 32-bit values $(J_1, J_2)$:
- Data-independent (Argon2i; in Argon2id, the first two slices of the first pass): $(J_1, J_2)$ are the halves of successive 64-bit words of $G(0,, G(0,, Z))$ with $Z = (\text{pass}, \text{lane}, \text{slice}, m', t, y, \text{counter}, 0, \dots)$, the counter being incremented for every 128 values.
- Data-dependent (Argon2d; in Argon2id, everything else): $J_1$ and $J_2$ are the low and high 32 bits of the first 64-bit word of the previous block $B[i][j-1]$.
Then $l = J_2 \bmod p$, except in the first slice of the first pass where $l = i$. Let $\mathcal{W}$ be the set of blocks that may be referenced: in lane $l$, all blocks of the slices already completed in the window of the last four slices, plus, when $l = i$, the blocks already built in the current segment, excluding $B[i][j-1]$; when $l \ne i$ and the block is the first of its segment, the most recent block of the window is also excluded. With $|\mathcal{W}|$ its size:
$$ x = \lfloor J_1^2 / 2^{32} \rfloor, \qquad y = \lfloor |\mathcal{W}|\cdot x / 2^{32} \rfloor, \qquad z_{\mathrm{rel}} = |\mathcal{W}| - 1 - y, $$
and $z$ is the $z_{\mathrm{rel}}$-th block of $\mathcal{W}$ counted from its oldest. The quadratic map biases the choice towards recent blocks.
The data-independent half protects the early computation against cache-timing observation; the data-dependent half gives resistance to time–memory trade-offs. Implementation. Lanes are filled sequentially (the result is the same as a multi-threaded computation); the memory matrix is erased before return. RFC 9106 gives two recommended parameter sets, both with $p = 4$ and a 256-bit tag: $t = 1$, $m = 2^{21}$ KiB (2 GiB), and, for memory-constrained environments, $t = 3$, $m = 2^{16}$ KiB (64 MiB). The parameters used by the vault are recorded in the vault file and are not fixed by this document.
13. Constructions specific to the QYVORA vault
Design of version 1; the vault module is being implemented.
Subkeys. From the random master key $\mathit{MK} \in {0,1}^{256}$, each usage has its own key $K_{\mathit{label}} = \mathrm{HKDF\text{-}SHA256}(\mathit{MK}, \mathit{info} = \mathit{label})$ with distinct labels, so that a key used for one purpose is never used for another.
Record sealing. $(C, T) = \mathrm{AES\text{-}256\text{-}GCM}_{K}(N, P, A)$ with a fresh random 96-bit nonce $N$ and the record header (format version, record identifier, type, creation time) as associated data $A$.
Password combiner.
$$ \mathrm{KEK} = \mathrm{HKDF}\Big(\mathrm{Argon2id}{\text{engine}}(\mathit{pw}, \mathit{salt}) ,\Vert, \mathrm{PBKDF2\text{-}HMAC\text{-}SHA512}{\text{system}}(\mathit{pw}, \mathit{salt})\Big) $$
Let $c_A$ and $c_P$ be the costs of one evaluation of each function. To test one password candidate an attacker must obtain both inputs of the HKDF, so the cost per guess is $c_A + c_P$ if both implementations are correct. If the engine's Argon2id were flawed so that its output became cheap to compute or even constant, the cost per guess would remain at least $c_P$: the vault is never weaker than a vault protected by the system PBKDF2 alone. The argument requires only that HKDF behaves as a pseudorandom function of its whole input; it does not cover a flaw that leaks the password itself.
Hybrid KEM for device-to-device transfer.
$$ K = \mathrm{HKDF}\big(\mathit{ss}{\mathrm{X25519}} ,\Vert, \mathit{ss}{\text{ML-KEM-1024}}\big) $$
$K$ is indistinguishable from random as long as at least one of the two shared secrets is, provided the derivation is bound to the exchanged public values. The exact inputs of the derivation (labels, public keys, ciphertext) are fixed by the transfer protocol specification, which is not yet written; this document only states the principle.
References
- NIST FIPS 180-4, Secure Hash Standard; FIPS 197, Advanced Encryption Standard; FIPS 198-1, HMAC.
- NIST FIPS 202, SHA-3 Standard; SP 800-185, SHA-3 Derived Functions; SP 800-38D, Galois/Counter Mode.
- NIST FIPS 203 (ML-KEM), FIPS 204 (ML-DSA), FIPS 205 (SLH-DSA), 2024.
- RFC 2104 (HMAC), RFC 5869 (HKDF), RFC 6979 (deterministic ECDSA), RFC 7693 (BLAKE2), RFC 8017 (MGF1), RFC 8018 (PBKDF2), RFC 8439 (ChaCha20 and Poly1305), RFC 9106 (Argon2).
- SEC 1 and SEC 2, Standards for Efficient Cryptography Group.
- J. Renes, C. Costello, L. Batina, "Complete addition formulas for prime order elliptic curves", EUROCRYPT 2016.
- P. L. Montgomery, "Modular multiplication without trial division", Mathematics of Computation 44, 1985.
- Ç. K. Koç, T. Acar, B. S. Kaliski, "Analyzing and comparing Montgomery multiplication algorithms", IEEE Micro 16(3), 1996.
- BIP-32, BIP-39, BIP-44, BIP-62, BIP-66, BIP-84, BIP-173, BIP-340, BIP-341, BIP-350.
- EIP-2, EIP-55, EIP-155, EIP-1559, EIP-2718, EIP-2930; Ethereum Yellow Paper, appendix B (RLP).