Repository Public
1# Mathematical foundations of the QYVORA engine23Status: draft for version 1, 2026-12-04. This document states the definitions, parameters and equations4that the engine implements, with the implementation choices found in the sources. It reports no test5result and no performance figure; those belong to `docs/STATUS.md` and the benchmark reports. At the date6of writing, every primitive described in sections 1 to 12 is implemented under `Sources/` on the main7branch; the Bitcoin transaction layer and the vault constructions of section 13 are in progress. The8engine is unaudited.910Notation: $\Vert$ is concatenation; $\oplus$ is bitwise XOR; $\lll$ and $\ggg$ are rotations; $\gg$ is a11logical right shift; $\lceil x \rfloor$ is rounding to the nearest integer with halves rounded up;12$r' = r \bmod^{\pm} m$ is the representative in $(-m/2,\, m/2]$.1314---1516## 1. Keccak, SHA-3, SHAKE, cSHAKE, KMAC1718Source: `Sources/QyvoraKeccak/Keccak.swift` (FIPS 202), `SP800185.swift` (SP 800-185).1920### 1.1 The permutation Keccak-f[1600]2122The state is a $5\times5$ array of 64-bit lanes $A[x,y]$, $x,y\in\{0,\dots,4\}$, 1600 bits in total. The23code stores lane $(x,y)$ at index $x+5y$, little-endian. One round is24$R = \iota\circ\chi\circ\pi\circ\rho\circ\theta$, and the permutation is 24 rounds.2526$$27\begin{aligned}28\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] \\29\rho:\quad & A[x,y] \leftarrow A[x,y] \lll r[x,y] \\30\pi:\quad & B[y,\; 2x+3y] = A[x,y] \\31\chi:\quad & A[x,y] = B[x,y] \oplus \big(\lnot B[x+1,y] \wedge B[x+2,y]\big) \\32\iota:\quad & A[0,0] \leftarrow A[0,0] \oplus RC[i]33\end{aligned}34$$3536with indices modulo 5. The rotation offsets are $r[0,0]=0$ and, starting from $(x,y)=(1,0)$, for37$t=0,\dots,23$: $r[x,y] = \frac{(t+1)(t+2)}{2} \bmod 64$, then $(x,y)\leftarrow(y,\; 2x+3y \bmod 5)$.38The round constants are defined by the linear feedback shift register39$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 to40$rc(j+7i)$ for $j=0,\dots,6$. The implementation uses the 24 tabulated constants and the 25 rotation41offsets written out, with the state held in 25 local variables; $\chi$ is the only non-linear step.4243### 1.2 The sponge construction4445With rate $r$ and capacity $c = 1600 - r$ bits, the sponge absorbs the padded message in $r$-bit blocks,46XORing each into the first $r$ bits of the state and applying the permutation, then outputs $r$ bits at a47time, permuting between blocks. Padding is `pad10*1`, preceded by the domain-separation suffix. For48byte-aligned messages the suffix and the first padding bit form one byte, XORed at the current position,49and `0x80` is XORed into the last byte of the block.5051| Function | Rate $r$ (bits / bytes) | Capacity $c$ (bits) | Output (bits) | Suffix bits | Suffix byte |52|---|---|---|---|---|---|53| SHA3-224 | 1152 / 144 | 448 | 224 | `01` | `0x06` |54| SHA3-256 | 1088 / 136 | 512 | 256 | `01` | `0x06` |55| SHA3-384 | 832 / 104 | 768 | 384 | `01` | `0x06` |56| SHA3-512 | 576 / 72 | 1024 | 512 | `01` | `0x06` |57| SHAKE128 | 1344 / 168 | 256 | any | `1111` | `0x1f` |58| SHAKE256 | 1088 / 136 | 512 | any | `1111` | `0x1f` |59| cSHAKE128 / cSHAKE256 | 1344 / 1088 | 256 / 512 | any | `00` | `0x04` |60| Keccak-256 (Ethereum) | 1088 / 136 | 512 | 256 | none | `0x01` |6162The generic security of a sponge with capacity $c$ and output length $n$ is63$\min(n/2,\, c/2)$ bits against collisions and $\min(n,\, c/2)$ bits against preimages.64Keccak-256 as used by Ethereum is the pre-standard padding: it is not SHA3-256, and the two give different65digests.6667### 1.3 cSHAKE and KMAC (SP 800-185)6869With `left_encode`, `right_encode`, `encode_string` and `bytepad` as defined in SP 800-185 section 2.3,70and $w$ the rate in bytes (168 or 136):7172$$73\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)74$$7576and cSHAKE with $N=S=\varepsilon$ is defined to be SHAKE.7778$$79\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)80$$8182KMACXOF replaces $\mathrm{right\_encode}(L)$ by $\mathrm{right\_encode}(0)$. Because $L$ is part of the83input of the fixed-length form, tags of different lengths are unrelated.8485---8687## 2. SHA-2, RIPEMD-1608889Source: `Sources/QyvoraSymmetric/SHA2.swift`, `SHA2Variants.swift`, `RIPEMD160.swift` (FIPS 180-4).9091SHA-256 processes 512-bit blocks with eight 32-bit working variables; SHA-512 processes 1024-bit blocks92with eight 64-bit variables. Both are Merkle–Damgård constructions with the Davies–Meyer feed-forward.93The message is padded with a `1` bit, zeros, and its bit length on 64 bits (SHA-256) or 128 bits (SHA-512).9495Message schedule, for $t \ge 16$: $W_t = \sigma_1(W_{t-2}) + W_{t-7} + \sigma_0(W_{t-15}) + W_{t-16}$.9697Round, for $t = 0,\dots,63$ (SHA-256) or $0,\dots,79$ (SHA-512):9899$$100\begin{aligned}101T_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),\\102(a,b,c,d,e,f,g,h) &\leftarrow (T_1+T_2,\; a,\; b,\; c,\; d+T_1,\; e,\; f,\; g)103\end{aligned}104$$105106with $\mathrm{Ch}(x,y,z) = (x\wedge y)\oplus(\lnot x\wedge z)$,107$\mathrm{Maj}(x,y,z) = (x\wedge y)\oplus(x\wedge z)\oplus(y\wedge z)$, additions modulo $2^{32}$ or $2^{64}$,108and after the last round the working variables are added to the chaining value.109110| Function | SHA-256 (32-bit words) | SHA-512 (64-bit words) |111|---|---|---|112| $\Sigma_0(x)$ | $(x\ggg2)\oplus(x\ggg13)\oplus(x\ggg22)$ | $(x\ggg28)\oplus(x\ggg34)\oplus(x\ggg39)$ |113| $\Sigma_1(x)$ | $(x\ggg6)\oplus(x\ggg11)\oplus(x\ggg25)$ | $(x\ggg14)\oplus(x\ggg18)\oplus(x\ggg41)$ |114| $\sigma_0(x)$ | $(x\ggg7)\oplus(x\ggg18)\oplus(x\gg3)$ | $(x\ggg1)\oplus(x\ggg8)\oplus(x\gg7)$ |115| $\sigma_1(x)$ | $(x\ggg17)\oplus(x\ggg19)\oplus(x\gg10)$ | $(x\ggg19)\oplus(x\ggg61)\oplus(x\gg6)$ |116117The constants $K_t$ are the first 32 (resp. 64) bits of the fractional parts of the cube roots of the first11864 (resp. 80) primes; the SHA-256 constants are the upper halves of the first 64 SHA-512 constants, which119is how the code derives them. The initial values of SHA-256 and SHA-512 are the fractional parts of the120square roots of the first eight primes. SHA-224 and SHA-384 are SHA-256 and SHA-512 with other initial121values and truncated outputs.122123Bitcoin uses $\mathrm{SHA256d}(x) = \mathrm{SHA256}(\mathrm{SHA256}(x))$ and124$\mathrm{HASH160}(x) = \mathrm{RIPEMD160}(\mathrm{SHA256}(x))$. RIPEMD-160 is a 160-bit Merkle–Damgård125hash with two parallel lines of 80 steps on 32-bit little-endian words.126127---128129## 3. HMAC, HKDF, PBKDF2130131Source: `Sources/QyvoraSymmetric/SHA2.swift` (HMAC), `KDF.swift`.132133**HMAC** (FIPS 198-1, RFC 2104), with block size $B$ of the hash $H$ and $K'$ the key hashed if longer134than $B$ then zero-padded to $B$ bytes:135136$$\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$$137138**HKDF** (RFC 5869):139140$$141\begin{aligned}142\mathrm{PRK} &= \mathrm{HMAC}(\mathit{salt}, \mathit{IKM}) \quad \text{(an empty salt is } \mathrm{HashLen} \text{ zero bytes)}\\143T(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})\\144\mathrm{OKM} &= \text{first } L \text{ bytes of } T(1)\,\Vert\,T(2)\,\Vert\cdots, \qquad L \le 255\cdot\mathrm{HashLen}145\end{aligned}146$$147148**PBKDF2** (RFC 8018) with $c$ iterations:149150$$151\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})152$$153154The implementation absorbs the two padded key blocks once and copies those hash states for every155iteration, so that an iteration costs two compression calls.156157---158159## 4. secp256k1160161Source: `Sources/QyvoraSecp256k1/U256.swift`, `Secp256k1.swift` (SEC 2, RFC 6979, BIP-340).162163### 4.1 The curve164165$$E:\; y^2 = x^3 + 7 \quad \text{over } \mathbb{F}_p, \qquad p = 2^{256} - 2^{32} - 977$$166167```168p = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F169n = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141170Gx = 79BE667E F9DCBBAC 55A06295 CE870B07 029BFCDB 2DCE28D9 59F2815B 16F81798171Gy = 483ADA77 26A3C465 5DA4FBFC 0E1108A8 FD17B448 A6855419 9C47D08F FB10D4B8172```173174The group $E(\mathbb{F}_p)$ has prime order $n$ and cofactor 1, so every point other than the identity175generates the group and no subgroup check is needed beyond the curve equation. Since $p \equiv 3 \pmod 4$,176a square root of a quadratic residue $a$ is $a^{(p+1)/4} \bmod p$; the code computes this candidate and177checks it by squaring, which is how compressed points are decompressed.178179### 4.2 Montgomery multiplication as implemented (`U256.swift`)180181A 256-bit integer is four 64-bit limbs, least significant first. `Modulus` implements arithmetic modulo182an odd $m$ with $2^{255} < m < 2^{256}$ (true of both $p$ and $n$), with $R = 2^{256}$. An element $a$ is183represented by $\tilde a = aR \bmod m$, and184185$$\mathrm{montMul}(\tilde a, \tilde b) = \tilde a\,\tilde b\,R^{-1} \bmod m = \widetilde{ab}.$$186187Precomputation in `Modulus.init`:188189- $m' = -m^{-1} \bmod 2^{64}$ by Newton iteration $x \leftarrow x\,(2 - m_0 x) \bmod 2^{64}$ from $x = 1$:190 each step doubles the number of correct low bits, so six steps give 64 bits.191- $R \bmod m = 2^{256} - m$, obtained as the two's complement of $m$ because $2^{255} < m$.192- $R^2 \bmod m$ by 256 modular doublings of $R \bmod m$.193194`montMul` is the coarsely integrated operand scanning form. With accumulator $t$ of 4 limbs plus a carry195limb, for each limb $b_i$, $i = 0,\dots,3$:196197$$198\begin{aligned}199t &\leftarrow t + a\cdot b_i, \\200q &\leftarrow t_0\cdot m' \bmod 2^{64}, \\201t &\leftarrow (t + q\cdot m) / 2^{64}.202\end{aligned}203$$204205The division is exact because $t_0 + q\,m_0 \equiv 0 \pmod{2^{64}}$. After four iterations206$t < 2m$, and the result is $t$ or $t - m$. The final subtraction is always computed and the result is207chosen by a mask built from the borrow and the carry limb, not by a branch. Addition, subtraction and208negation modulo $m$ use the same masked selection. Conversion is $\tilde a = \mathrm{montMul}(a, R^2)$209and $a = \mathrm{montMul}(\tilde a, 1)$.210211Inversion uses Fermat's little theorem, $a^{-1} = a^{m-2} \bmod m$, by square-and-multiply over the 256212bits of the exponent. The exponent $m-2$ is a public constant, so the multiplication pattern reveals213nothing secret; the base may be secret. The same exponentiation computes the square root with exponent214$(p+1)/4$.215216### 4.3 Group law217218Points are in homogeneous projective coordinates $(X:Y:Z)$ with affine point $(X/Z, Y/Z)$ and identity219$(0:1:0)$, coordinates in Montgomery form. Addition and doubling use the **complete** formulas of Renes,220Costello and Batina (2016) for short Weierstrass curves with $a = 0$ (their Algorithms 7 and 9), with221$b_3 = 3b = 21$. Complete means that one straight-line sequence of field operations is correct for every222pair of inputs — distinct points, equal points, opposite points, the identity — so that the code has no223exceptional case and no branch. This completeness holds for curves of odd order, which is the case here.224225Scalar multiplication $kP$ is double-and-add-always, most significant bit first, over exactly 256226iterations starting from the identity: each iteration computes $2R$ and $2R+P$ and keeps one of them with227a mask derived from the scalar bit. The sequence of field operations is therefore independent of $k$.228No precomputed table, window or endomorphism is used; the same routine serves signing and verification.229230### 4.4 ECDSA231232For a private key $d \in [1, n-1]$, public key $Q = dG$, and message digest $z$ (the 32-byte hash233interpreted as an integer and reduced modulo $n$):234235$$236R = kG, \qquad r = R_x \bmod n, \qquad s = k^{-1}(z + r\,d) \bmod n,237$$238239with $r \ne 0$ and $s \ne 0$. Verification computes $w = s^{-1}$, $u_1 = zw$, $u_2 = rw$ (mod $n$) and240accepts if the point $u_1G + u_2Q$ is not the identity and its $x$-coordinate is congruent to $r$241modulo $n$.242243**Deterministic nonce (RFC 6979, section 3.2), HMAC-SHA-256.** With $x$ the 32-byte private key and244$h_1 = \mathrm{bits2octets}(\text{digest})$, i.e. the digest reduced modulo $n$:245246$$247\begin{aligned}248&V = \texttt{0x01}^{32}, \quad K = \texttt{0x00}^{32}\\249&K = \mathrm{HMAC}_K(V\,\Vert\,\texttt{0x00}\,\Vert\,x\,\Vert\,h_1), \quad V = \mathrm{HMAC}_K(V)\\250&K = \mathrm{HMAC}_K(V\,\Vert\,\texttt{0x01}\,\Vert\,x\,\Vert\,h_1), \quad V = \mathrm{HMAC}_K(V)\\251&\text{repeat: } V = \mathrm{HMAC}_K(V);\ k = \mathrm{int}(V);\ \text{accept if } 1 \le k < n,\\252&\qquad\text{otherwise } K = \mathrm{HMAC}_K(V\,\Vert\,\texttt{0x00}),\ V = \mathrm{HMAC}_K(V)253\end{aligned}254$$255256If the resulting signature has $r = 0$ or $s = 0$ the next candidate of the same sequence is used.257258**Low-s.** $(r, s)$ and $(r, n-s)$ are both valid. The signer outputs $s \le (n-1)/2$ (BIP-62 / BIP-146259for Bitcoin, EIP-2 for Ethereum), and the verifier rejects high $s$ by default.260261**Public-key recovery.** From $(r, s)$, the digest and a recovery identifier $v \in \{0,1,2,3\}$ (bit 0:262parity of $R_y$; bit 1: whether $R_x \ge n$), the signer's key is263264$$265R = \mathrm{lift}\big(r + n\cdot\lfloor v/2\rfloor,\; v \bmod 2\big), \qquad Q = r^{-1}\,(sR - zG).266$$267268When the signer negates $s$ for the low-s rule, the parity bit is flipped. Ethereum addresses are obtained269this way from transaction signatures. Bit 1 is set with probability about $2^{-128}$; EIP-1559270transactions encode only the parity, and the transaction signer rejects the other case.271272**DER.** Bitcoin signatures before Taproot are encoded as strict DER (BIP-66): `30 len 02 len r 02 len s`273with minimal positive integers.274275### 4.5 BIP-340 Schnorr signatures276277Public keys are 32-byte $x$-coordinates; the point with even $y$ is implied. With the tagged hash278$\mathrm{H}_{\mathit{tag}}(x) = \mathrm{SHA256}(\mathrm{SHA256}(\mathit{tag})\,\Vert\,\mathrm{SHA256}(\mathit{tag})\,\Vert\,x)$:279280$$281\begin{aligned}282&P = d'G, \qquad d = d' \text{ if } P_y \text{ is even, else } n - d'\\283&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\\284&R = k'G, \qquad k = k' \text{ if } R_y \text{ is even, else } n - k'\\285&e = \mathrm{int}\big(\mathrm{H}_{\text{BIP0340/challenge}}(\mathrm{bytes}(R)\,\Vert\,\mathrm{bytes}(P)\,\Vert\,m)\big) \bmod n\\286&\sigma = \mathrm{bytes}(R)\,\Vert\,\mathrm{bytes}\big((k + e\,d) \bmod n\big)287\end{aligned}288$$289290Verification parses $r < p$, $s < n$, lifts $P$ with even $y$, computes $R = sG - eP$ and accepts if $R$291is not the identity, has even $y$ and $R_x = r$. The implementation verifies every signature it produces292before returning it, as the BIP recommends.293294---295296## 5. Hierarchical keys, mnemonics and encodings297298Source: `Sources/QyvoraCore/HD/`, `Sources/QyvoraCore/Encoding/`, `Sources/QyvoraEthereum/`.299300### 5.1 BIP-39301302Entropy of $\mathrm{ENT}$ bits is followed by the first $\mathrm{CS} = \mathrm{ENT}/32$ bits of its303SHA-256 digest; the result is cut into 11-bit groups, each an index into a list of $2^{11} = 2048$ words.304305| ENT (bits) | CS (bits) | ENT + CS | Words |306|---|---|---|---|307| 128 | 4 | 132 | 12 |308| 160 | 5 | 165 | 15 |309| 192 | 6 | 198 | 18 |310| 224 | 7 | 231 | 21 |311| 256 | 8 | 264 | 24 |312313The seed is314$\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)$.315QYVORA generates 24 words by default and refuses to derive a seed from a sentence whose checksum is wrong.316The checksum detects errors; it adds no security.317318### 5.2 BIP-32319320Master key from a seed $S$ of 16 to 64 bytes:321$I = \mathrm{HMAC\text{-}SHA512}(\text{"Bitcoin seed"}, S)$, $k_m = I_L$, $c_m = I_R$, invalid if322$I_L = 0$ or $I_L \ge n$.323324Child derivation with index $i$, where $i \ge 2^{31}$ denotes a hardened child:325326$$327I = \begin{cases}328\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]329\mathrm{HMAC\text{-}SHA512}\big(c_{\mathrm{par}},\; \mathrm{ser}_P(K_{\mathrm{par}})\,\Vert\,\mathrm{ser}_{32}(i)\big) & \text{normal}330\end{cases}331$$332333$$334k_i = (I_L + k_{\mathrm{par}}) \bmod n, \qquad K_i = I_L\cdot G + K_{\mathrm{par}}, \qquad c_i = I_R335$$336337The child is invalid if $I_L \ge n$ or $k_i = 0$ (resp. $K_i$ is the identity). Public derivation338($K_{\mathrm{par}} \to K_i$) exists only for normal children. Consequence used in339`docs/QUANTUM_RESISTANCE.md`: since $k_i - I_L = k_{\mathrm{par}}$ and $I_L$ is computable from the parent340extended public key, one normal child private key together with the parent extended public key gives the341parent private key.342343A serialized extended key is 78 bytes — version (4), depth (1), parent fingerprint (4, the first four344bytes of HASH160 of the parent public key), child number (4), chain code (32), key (33) — in Base58Check.345346Paths used: $m/84'/c'/a'/\{0,1\}/i$ for Bitcoin native SegWit (BIP-84, coin type $c = 0$ on mainnet and 1347on test networks) and $m/44'/60'/0'/0/i$ for Ethereum (BIP-44).348349### 5.3 Base58Check350351$\mathrm{Base58Check}(x) = \mathrm{Base58}\big(x\,\Vert\,\mathrm{SHA256d}(x)[0..3]\big)$, where Base58 is352the big-endian base-58 expansion over an alphabet without `0`, `O`, `I`, `l`, each leading zero byte being353encoded as the character `1`.354355### 5.4 Bech32 and Bech32m356357An address is a human-readable part, the separator `1`, and data characters from a 32-symbol alphabet, the358last six of which are a checksum. The checksum is a BCH code over $\mathbb{F}_{32}$, computed by359360$$361\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_j362$$363364with $g = (\texttt{0x3b6a57b2}, \texttt{0x26508e6d}, \texttt{0x1ea119fa}, \texttt{0x3d4233dd}, \texttt{0x2a1462b3})$.365The input is the expanded human-readable part (high three bits of each character, a zero, low five bits of366each character), the data, and six zeros; the checksum is the result XORed with a constant:367$1$ for Bech32 (BIP-173) and $\texttt{0x2bc830a3}$ for Bech32m (BIP-350). The code guarantees detection of368any error affecting at most four characters in an address of the allowed length, and fails to detect other369errors with probability about $2^{-30}$.370371A SegWit address encodes a witness version $v \in [0,16]$ followed by the witness program regrouped from3728-bit to 5-bit symbols. Version 0 uses Bech32 and requires a 20-byte (P2WPKH) or 32-byte (P2WSH) program;373versions 1 to 16 use Bech32m (version 1 with a 32-byte program is P2TR). The decoder enforces the pairing374of version and checksum constant, the program length, and zero padding bits.375376### 5.5 RLP377378An item is a byte string or a list of items (Ethereum Yellow Paper, appendix B):379380| Item | Encoding |381|---|---|382| A single byte $b < \texttt{0x80}$ | $b$ |383| A string of $0 \le \ell \le 55$ bytes | $(\texttt{0x80}+\ell)\,\Vert\,\text{string}$ |384| A string of $\ell > 55$ bytes | $(\texttt{0xb7}+\Vert\mathrm{BE}(\ell)\Vert)\,\Vert\,\mathrm{BE}(\ell)\,\Vert\,\text{string}$ |385| A list whose concatenated encodings take $0 \le \ell \le 55$ bytes | $(\texttt{0xc0}+\ell)\,\Vert\,\text{items}$ |386| A list with $\ell > 55$ | $(\texttt{0xf7}+\Vert\mathrm{BE}(\ell)\Vert)\,\Vert\,\mathrm{BE}(\ell)\,\Vert\,\text{items}$ |387388where $\mathrm{BE}(\ell)$ is the minimal big-endian encoding of $\ell$ and $\Vert\mathrm{BE}(\ell)\Vert$ its389length in bytes. Integers are minimal big-endian strings (zero is the empty string). The decoder rejects390every non-canonical form: a single byte below `0x80` wrapped in a string header, a long form used for a391length below 56, a length with a leading zero byte, trailing data.392393### 5.6 Ethereum transactions and addresses394395An EIP-1559 (type 2, EIP-2718 envelope) transaction is signed over396397$$398h = \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)399$$400401and the raw transaction is `0x02` followed by the RLP of the same nine fields and402$(y_{\mathrm{parity}}, r, s)$. The chain identifier is inside the signed payload, which gives replay403protection across chains. The most a transaction can cost is404$\mathit{gasLimit}\cdot\mathit{maxFeePerGas} + \mathit{value}$; the fee actually paid per unit of gas is405$\min(\mathit{maxFeePerGas},\; \mathit{baseFee} + \mathit{maxPriorityFeePerGas})$. The base fee changes by406at most a factor $1 \pm 1/8$ from one block to the next.407408An address is the last 20 bytes of $\mathrm{Keccak256}(X\,\Vert\,Y)$ for the uncompressed public key409$(X, Y)$. EIP-55 capitalises the $i$-th hexadecimal character of the address when it is a letter and the410$i$-th nibble of $\mathrm{Keccak256}$ of the lower-case hexadecimal string is at least 8.411412---413414## 6. Module lattices: MLWE and MSIS415416Let $R_q = \mathbb{Z}_q[X]/(X^{256}+1)$. A module lattice problem works with vectors and matrices over417$R_q$.418419- **Module-LWE (decision).** Given a uniform $\mathbf{A} \in R_q^{k\times\ell}$, distinguish420 $(\mathbf{A},\; \mathbf{A}\mathbf{s} + \mathbf{e})$, where $\mathbf{s}$ and $\mathbf{e}$ have small421 coefficients drawn from a narrow distribution, from $(\mathbf{A}, \mathbf{u})$ with uniform $\mathbf{u}$.422- **Module-SIS.** Given a uniform $\mathbf{A} \in R_q^{k\times\ell}$, find a non-zero $\mathbf{z}$ with423 $\Vert\mathbf{z}\Vert_\infty \le \beta$ such that $[\,\mathbf{I} \mid \mathbf{A}\,]\,\mathbf{z} = \mathbf{0}$.424425ML-KEM rests on Module-LWE. ML-DSA rests on Module-LWE for the secrecy of the key and on Module-SIS (in a426"self-target" variant, in the random oracle model) for unforgeability. Security levels are estimated from427the cost of lattice reduction against these instances; no reduction from a worst-case problem covers the428concrete parameters.429430---431432## 7. ML-KEM (FIPS 203)433434Source read: `Sources/QyvoraMLKEM/MLKEM.swift`, `MLKEMArithmetic.swift`.435436### 7.1 Ring, NTT and base multiplication437438$q = 3329 = 2^8\cdot 13 + 1$, $n = 256$. The element $\zeta = 17$ is a primitive 256-th root of unity439modulo $q$; there is no primitive 512-th root, so $X^{256}+1$ does not split into linear factors but into440128 quadratic ones:441442$$443X^{256} + 1 = \prod_{i=0}^{127}\Big(X^2 - \zeta^{\,2\,\mathrm{BitRev}_7(i)+1}\Big)444$$445446The NTT maps $f \in R_q$ to its 128 residues of degree at most 1,447$\hat f = \big(f \bmod (X^2 - \zeta^{2\mathrm{BitRev}_7(i)+1})\big)_{i}$, by seven layers of butterflies.448Multiplication in the NTT domain is pairwise, in each quadratic extension (base multiplication), with449$\gamma = \zeta^{2\mathrm{BitRev}_7(i)+1}$:450451$$452(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)\,X453$$454455*Implementation.* Coefficients are signed 16-bit integers. Products use signed Montgomery reduction with456$R = 2^{16}$ ($q^{-1} \bmod 2^{16} = 62209$, stored as $-3327$): for $|a| < q\,2^{15}$ it returns457$a\,2^{-16} \bmod q$ in $(-q, q)$. Additive results are brought back by a centred Barrett reduction with458the constant $20159 = \lceil 2^{26}/q \rfloor$. The table of $\zeta^{\mathrm{BitRev}_7(i)}$ is computed in459Montgomery form at first use rather than transcribed. The inverse transform ends with a multiplication by460$1441 = 2^{32}\cdot 128^{-1} \bmod q$, which removes the factor $128$ and the factor $2^{-16}$ left by the461Montgomery base multiplication.462463### 7.2 Sampling, compression, encoding464465- $\mathrm{SampleNTT}$: rejection sampling of uniform coefficients in $[0, q)$ from SHAKE128, two 12-bit466 candidates per three bytes. The input is public.467- Centred binomial distribution $\mathrm{CBD}_\eta$: each coefficient is468 $\sum_{i=1}^{\eta} x_i - \sum_{i=1}^{\eta} y_i$ for $2\eta$ uniform bits, so it lies in $[-\eta, \eta]$469 with variance $\eta/2$. The code computes the bit sums with word-parallel masks, without a table.470- Compression to $d$ bits and decompression:471472$$473\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\rfloor474$$475476 with $\big|\mathrm{Decompress}_d(\mathrm{Compress}_d(x)) - x \bmod^{\pm} q\big| \le \lceil q/2^{d+1} \rfloor$.477 *Implementation.* Because $q$ is odd, $\mathrm{Compress}_d(x) = \lfloor (2^d x + 1664)/q \rfloor \bmod 2^d$.478 The quotient is computed as $\lfloor N\cdot M / 2^{36} \rfloor$ with $M = \lceil 2^{36}/q \rfloor$, exact479 for every numerator $N < 2^{23}$. No division instruction is applied to secret data: this is the480 countermeasure to the timing leak known for division-based compression.481- $\mathrm{ByteEncode}_d$ packs 256 $d$-bit integers little-endian into $32d$ bytes.482483### 7.3 K-PKE484485With $G = \text{SHA3-512}$, $H = \text{SHA3-256}$, $J = \text{SHAKE256}$ with 32 bytes of output,486$\mathrm{PRF}_\eta = \text{SHAKE256}$ with $64\eta$ bytes, $\mathrm{XOF} = \text{SHAKE128}$:487488$$489\begin{aligned}490\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)\\491& \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]492\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)\\493& \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)\\494& c = \mathrm{ByteEncode}_{d_u}(\mathrm{Compress}_{d_u}(\mathbf{u}))\,\Vert\,\mathrm{ByteEncode}_{d_v}(\mathrm{Compress}_{d_v}(v))\\[4pt]495\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))496\end{aligned}497$$498499Correctness: $w = \mathbf{e}^{\mathsf T}\mathbf{y} + e_2 - \mathbf{s}^{\mathsf T}\mathbf{e}_1 + \text{compression errors} + \lceil q/2 \rfloor\, m$,500and each bit decodes correctly when the noise term has absolute value below about $q/4$.501502### 7.4 The KEM: Fujisaki–Okamoto transform with implicit rejection503504$$505\begin{aligned}506\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\\507\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)\\508\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)\\509& c' = \text{K-PKE.Encrypt}(\mathit{ek}, m', r'), \qquad K = \begin{cases} K' & c = c' \\ \bar K & c \ne c' \end{cases}510\end{aligned}511$$512513Encryption is made deterministic by deriving its randomness from the message; decapsulation re-encrypts514and compares. On a mismatch it does not fail: it returns a pseudorandom key derived from the secret $z$515and the ciphertext (implicit rejection), so that an attacker learns nothing from the behaviour of the516decapsulator. *Implementation.* The comparison accumulates the XOR of all ciphertext bytes, turns it into517a mask without branching and selects $K'$ or $\bar K$ byte by byte. The checked interface applies the518input checks of FIPS 203 section 7: encapsulation key length and modulus check (every 12-bit coefficient519below $q$), decapsulation key length and hash check, ciphertext length.520521### 7.5 Parameter sets522523| | ML-KEM-512 | ML-KEM-768 | ML-KEM-1024 |524|---|---|---|---|525| NIST security category | 1 | 3 | 5 |526| $k$ | 2 | 3 | 4 |527| $\eta_1$ | 3 | 2 | 2 |528| $\eta_2$ | 2 | 2 | 2 |529| $d_u$ | 10 | 10 | 11 |530| $d_v$ | 4 | 4 | 5 |531| Encapsulation key, $384k+32$ bytes | 800 | 1184 | 1568 |532| Decapsulation key, $768k+96$ bytes | 1632 | 2400 | 3168 |533| Ciphertext, $32(d_uk+d_v)$ bytes | 768 | 1088 | 1568 |534| Shared secret (bytes) | 32 | 32 | 32 |535| Decapsulation failure rate (FIPS 203) | $2^{-138.8}$ | $2^{-164.8}$ | $2^{-174.8}$ |536537$n = 256$ and $q = 3329$ for all sets.538539---540541## 8. ML-DSA (FIPS 204)542543Source read: `Sources/QyvoraMLDSA/MLDSA.swift`, `MLDSAArithmetic.swift`.544545### 8.1 Ring and NTT546547$q = 8380417 = 2^{23} - 2^{13} + 1$, $n = 256$, $d = 13$. Here $q \equiv 1 \pmod{512}$ and $\zeta = 1753$548is a primitive 512-th root of unity, so $X^{256}+1$ splits completely,549550$$551X^{256} + 1 = \prod_{i=0}^{255}\big(X - \zeta^{\,2\,\mathrm{BitRev}_8(i)+1}\big),552$$553554and multiplication in the NTT domain is coefficient-wise. *Implementation.* Coefficients are signed 32-bit555integers; products use signed Montgomery reduction with $R = 2^{32}$556($q^{-1} \bmod 2^{32} = 58728449$); the reduction `reduce32` uses $2^{23} \equiv 2^{13} - 1 \pmod q$;557the inverse transform ends with $41978 = 2^{64}\cdot 256^{-1} \bmod q$.558559### 8.2 Rounding and hints560561$$562\begin{aligned}563\mathrm{Power2Round}(r):\quad & r_0 = r \bmod^{\pm} 2^{d}, \qquad r_1 = (r - r_0)/2^{d}\\564\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)\\565\mathrm{HighBits}(r) &= r_1, \qquad \mathrm{LowBits}(r) = r_0\\566\mathrm{MakeHint}(z, r) &= [\,\mathrm{HighBits}(r) \ne \mathrm{HighBits}(r+z)\,]\\567\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}568\end{aligned}569$$570571The public key holds only the high part $\mathbf{t}_1$ of $\mathbf{t}$; the hint lets the verifier recover572the high bits of $\mathbf{w}$ without knowing $\mathbf{t}_0$. *Implementation.* `decompose` is573division-free and branch-free on its argument (multiplication by a reciprocal constant and shifts, chosen574by the public value of $\gamma_2$).575576### 8.3 Key generation, signing, verification577578With $H = \text{SHAKE256}$ and $\Vert\cdot\Vert_\infty$ the largest centred coefficient in absolute value:579580$$581\begin{aligned}582\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]\\583& \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)\\584& \mathit{sk} = (\rho, K, \mathit{tr}, \mathbf{s}_1, \mathbf{s}_2, \mathbf{t}_0)585\end{aligned}586$$587588$$589\begin{aligned}590\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\\591\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})\\592& \tilde c = H\big(\mu\,\Vert\,\mathrm{w1Encode}(\mathbf{w}_1),\; \lambda/4 \text{ bytes}\big), \qquad c = \mathrm{SampleInBall}(\tilde c)\\593& \mathbf{z} = \mathbf{y} + c\,\mathbf{s}_1, \qquad \mathbf{r}_0 = \mathrm{LowBits}(\mathbf{w} - c\,\mathbf{s}_2)\\594& \text{reject if } \Vert\mathbf{z}\Vert_\infty \ge \gamma_1 - \beta \ \text{ or } \ \Vert\mathbf{r}_0\Vert_\infty \ge \gamma_2 - \beta\\595& \mathbf{h} = \mathrm{MakeHint}(-c\,\mathbf{t}_0,\; \mathbf{w} - c\,\mathbf{s}_2 + c\,\mathbf{t}_0)\\596& \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\\597& \kappa \leftarrow \kappa + \ell \ \text{ on rejection}; \qquad \sigma = (\tilde c, \mathbf{z}, \mathbf{h})598\end{aligned}599$$600601$$602\begin{aligned}603\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}})\\604& \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)605\end{aligned}606$$607608$c$ is a polynomial with exactly $\tau$ coefficients in $\{-1, +1\}$ and the rest zero, so609$\Vert c\,\mathbf{s}_i\Vert_\infty \le \tau\eta = \beta$. The first rejection condition makes the610distribution of $\mathbf{z}$ independent of $\mathbf{s}_1$ (rejection sampling: the scheme is a611"Fiat–Shamir with aborts" signature); the second ensures that the high bits of $\mathbf{w} - c\,\mathbf{s}_2$612equal those of $\mathbf{w}$, which is what the verifier recomputes.613614The message representative of the external interface is615$M' = \mathrm{byte}(0)\,\Vert\,\mathrm{byte}(|\mathit{ctx}|)\,\Vert\,\mathit{ctx}\,\Vert\,M$ for pure ML-DSA and616$M' = \mathrm{byte}(1)\,\Vert\,\mathrm{byte}(|\mathit{ctx}|)\,\Vert\,\mathit{ctx}\,\Vert\,\mathrm{OID}\,\Vert\,\mathrm{PH}(M)$617for HashML-DSA, with a context of at most 255 bytes. $\mathit{rnd}$ is 32 fresh random bytes in the hedged618variant (the default in QYVORA) and 32 zero bytes in the deterministic variant.619620*Implementation.* Norm checks OR an indicator over every coefficient without early exit. Branches of the621rejection loop depend only on candidates that are discarded. The decoder of the hint enforces all the622malformed-input checks of the standard (non-decreasing counters bounded by $\omega$, strictly increasing623positions, zero padding). The private-key decoder checks that the encodings of $\mathbf{s}_1$ and624$\mathbf{s}_2$ lie in $[-\eta, \eta]$. Secret polynomials live in a workspace erased on release.625626### 8.4 Parameter sets627628| | ML-DSA-44 | ML-DSA-65 | ML-DSA-87 |629|---|---|---|---|630| NIST security category | 2 | 3 | 5 |631| $(k, \ell)$ | (4, 4) | (6, 5) | (8, 7) |632| $\eta$ | 2 | 4 | 2 |633| $\tau$ | 39 | 49 | 60 |634| $\beta = \tau\eta$ | 78 | 196 | 120 |635| $\lambda$ (bits; $\tilde c$ is $\lambda/4$ bytes) | 128 | 192 | 256 |636| $\gamma_1$ | $2^{17}$ | $2^{19}$ | $2^{19}$ |637| $\gamma_2$ | $(q-1)/88 = 95232$ | $(q-1)/32 = 261888$ | $(q-1)/32 = 261888$ |638| $\omega$ | 80 | 55 | 75 |639| Expected number of signing iterations (FIPS 204) | 4.25 | 5.1 | 3.85 |640| Public key (bytes) | 1312 | 1952 | 2592 |641| Private key (bytes) | 2560 | 4032 | 4896 |642| Signature (bytes) | 2420 | 3309 | 4627 |643644$q = 8380417$, $d = 13$, $\zeta = 1753$ for all sets. Sizes follow from645$|\mathit{pk}| = 32 + 320k$,646$|\mathit{sk}| = 128 + 32\,b_\eta(k+\ell) + 416k$ with $b_\eta = 3$ for $\eta = 2$ and $4$ for $\eta = 4$, and647$|\sigma| = \lambda/4 + 32\,\ell\,(1 + \log_2\gamma_1) + \omega + k$.648649---650651## 9. SLH-DSA (FIPS 205)652653Source read: `Sources/QyvoraSLHDSA/`.654655SLH-DSA is built from a hash function only. A signature is a chain: a few-time signature (FORS) signs the656message digest; a tree of one-time signatures (the hypertree of XMSS trees over WOTS+) certifies the FORS657public key up to the root contained in the public key. The signer is stateless: which leaf is used is658chosen pseudorandomly from the message.659660### 9.1 WOTS+661662With $w = 16$ ($\lg w = 4$) for every parameter set, an $n$-byte message is cut into663664$$665\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+3666$$667668base-$w$ digits: $\mathit{len}_1$ message digits $m_i$ and $\mathit{len}_2$ digits of the checksum669670$$671C = \sum_{i=0}^{\mathit{len}_1 - 1} (w - 1 - m_i).672$$673674Each digit selects a position in a hash chain of length $w - 1$:675$\mathrm{chain}(X, i, s) = F^{(s)}$ applied to $X$ starting at step $i$, each step with its own address.676The secret values are $\mathit{sk}_j = \mathrm{PRF}(\mathrm{PK.seed}, \mathrm{SK.seed}, \mathrm{ADRS}_j)$,677the signature is $\sigma_j = \mathrm{chain}(\mathit{sk}_j, 0, m_j)$, and the public key is678$T_{\mathit{len}}$ of the chain ends $\mathrm{chain}(\mathit{sk}_j, 0, w-1)$. The verifier completes each679chain by $w - 1 - m_j$ steps. The checksum prevents forgeries: increasing a message digit (which anyone680can do by hashing forward) forces a checksum digit to decrease, which would require inverting $F$.681682### 9.2 XMSS, the hypertree and FORS683684- **XMSS.** A Merkle tree of height $h'$ whose $2^{h'}$ leaves are WOTS+ public keys; internal nodes are685 $H(\text{left}\,\Vert\,\text{right})$ with the node address. An XMSS signature is a WOTS+ signature and686 the $h'$ sibling nodes of the authentication path.687- **Hypertree.** $d$ layers of XMSS trees, total height $h = d\,h'$. The bottom layer signs the FORS public688 key; each higher layer signs the root of the tree below. The public key contains the root of the single689 top tree.690- **FORS.** $k$ Merkle trees of height $a$, each with $2^a$ secret leaves. The first $k\,a$ bits of the691 message digest give $k$ indices of $a$ bits; the signature reveals, for each tree, the selected secret692 value and its authentication path. The FORS public key is $T_k$ of the $k$ roots.693694The message digest of $m$ bytes is split into $\lceil ka/8 \rceil$ bytes for FORS, $\lceil (h-h')/8 \rceil$695bytes for the index of the bottom XMSS tree (reduced modulo $2^{h-h'}$) and $\lceil h'/8 \rceil$ bytes for696the leaf index (reduced modulo $2^{h'}$), so697$m = \lceil ka/8 \rceil + \lceil (h-h')/8 \rceil + \lceil h'/8 \rceil$.698699Signing: $R = \mathrm{PRF}_{\mathrm{msg}}(\mathrm{SK.prf}, \mathit{opt\_rand}, M')$,700$\mathit{digest} = H_{\mathrm{msg}}(R, \mathrm{PK.seed}, \mathrm{PK.root}, M')$,701$\sigma = R\,\Vert\,\sigma_{\mathrm{FORS}}\,\Vert\,\sigma_{\mathrm{HT}}$. In the hedged variant702$\mathit{opt\_rand}$ is $n$ fresh random bytes; in the deterministic variant it is $\mathrm{PK.seed}$.703$M'$ is formed with the same context and pre-hash rules as in ML-DSA.704705*Implementation.* Signing computes each needed tree whole (all leaves, then the levels bottom-up) instead706of the recursive node function of the standard, and may compute leaves on several cores; the values are707the same. Verification compares the recomputed root with $\mathrm{PK.root}$ in constant time.708709### 9.3 Addresses and tweakable hash functions710711Every hash call is bound to its position by a 32-byte address712$\mathrm{ADRS} = \text{layer (4)}\,\Vert\,\text{tree (12)}\,\Vert\,\text{type (4)}\,\Vert\,\text{three words (4 each)}$,713with types `WOTS_HASH` = 0, `WOTS_PK` = 1, `TREE` = 2, `FORS_TREE` = 3, `FORS_ROOTS` = 4, `WOTS_PRF` = 5,714`FORS_PRF` = 6. The address and $\mathrm{PK.seed}$ are the tweak: they make every call a distinct function,715which defeats multi-target attacks.716717SHAKE instantiation (all three security levels):718719$$720\begin{aligned}721F, H, T_\ell(\mathrm{PK.seed}, \mathrm{ADRS}, M) &= \mathrm{SHAKE256}(\mathrm{PK.seed}\,\Vert\,\mathrm{ADRS}\,\Vert\,M,\; 8n)\\722\mathrm{PRF}(\mathrm{PK.seed}, \mathrm{SK.seed}, \mathrm{ADRS}) &= \mathrm{SHAKE256}(\mathrm{PK.seed}\,\Vert\,\mathrm{ADRS}\,\Vert\,\mathrm{SK.seed},\; 8n)\\723\mathrm{PRF}_{\mathrm{msg}}(\mathrm{SK.prf}, \mathit{opt\_rand}, M) &= \mathrm{SHAKE256}(\mathrm{SK.prf}\,\Vert\,\mathit{opt\_rand}\,\Vert\,M,\; 8n)\\724H_{\mathrm{msg}}(R, \mathrm{PK.seed}, \mathrm{PK.root}, M) &= \mathrm{SHAKE256}(R\,\Vert\,\mathrm{PK.seed}\,\Vert\,\mathrm{PK.root}\,\Vert\,M,\; 8m)725\end{aligned}726$$727728SHA-2 instantiation, with the 22-byte compressed address729$\mathrm{ADRS}^c = \mathrm{ADRS}[3]\,\Vert\,\mathrm{ADRS}[8..15]\,\Vert\,\mathrm{ADRS}[19]\,\Vert\,\mathrm{ADRS}[20..31]$:730731| Function | Category 1 ($n = 16$) | Categories 3 and 5 ($n = 24, 32$) |732|---|---|---|733| $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 |734| $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)$ |735| $\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)$ |736| $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 |737738The zero padding fills the first compression block, so the state after absorbing $\mathrm{PK.seed}$ is739computed once and reused for every call (the implementation does this).740741### 9.4 Parameter sets742743Each row applies to the SHA2 and the SHAKE variant of the same name, which gives the twelve sets:744SLH-DSA-SHA2-{128s, 128f, 192s, 192f, 256s, 256f} and SLH-DSA-SHAKE-{128s, 128f, 192s, 192f, 256s, 256f}.745746| Set | $n$ | $h$ | $d$ | $h'$ | $a$ | $k$ | $\lg w$ | $m$ | Category | Public key (bytes) | Signature (bytes) |747|---|---|---|---|---|---|---|---|---|---|---|---|748| 128s | 16 | 63 | 7 | 9 | 12 | 14 | 4 | 30 | 1 | 32 | 7 856 |749| 128f | 16 | 66 | 22 | 3 | 6 | 33 | 4 | 34 | 1 | 32 | 17 088 |750| 192s | 24 | 63 | 7 | 9 | 14 | 17 | 4 | 39 | 3 | 48 | 16 224 |751| 192f | 24 | 66 | 22 | 3 | 8 | 33 | 4 | 42 | 3 | 48 | 35 664 |752| 256s | 32 | 64 | 8 | 8 | 14 | 22 | 4 | 47 | 5 | 64 | 29 792 |753| 256f | 32 | 68 | 17 | 4 | 9 | 35 | 4 | 49 | 5 | 64 | 49 856 |754755The public key is $\mathrm{PK.seed}\,\Vert\,\mathrm{PK.root}$ ($2n$ bytes), the private key756$\mathrm{SK.seed}\,\Vert\,\mathrm{SK.prf}\,\Vert\,\mathrm{PK.seed}\,\Vert\,\mathrm{PK.root}$ ($4n$ bytes), and757758$$759|\sigma| = \big(1 + k(1+a) + h + d\cdot\mathit{len}\big)\cdot n \ \text{bytes}, \qquad \mathit{len} = 2n + 3.760$$761762The "s" sets minimise signature size and the "f" sets minimise signing time. The standard limits each key763to $2^{64}$ signatures.764765---766767## 10. AES and GCM768769Source read: `Sources/QyvoraSymmetric/AES.swift`, `AESGCM.swift` (FIPS 197, SP 800-38D).770771### 10.1 AES772773Bytes are elements of $\mathbb{F}_{2^8} = \mathbb{F}_2[x]/(x^8 + x^4 + x^3 + x + 1)$. The state is a774$4\times4$ array of bytes. With $N_k$ key words: AES-128, -192, -256 have $N_k = 4, 6, 8$ and775$N_r = 10, 12, 14$ rounds. Each round applies776777- **SubBytes**: $S(a) = \mathrm{Aff}(a^{-1})$, with $0^{-1} := 0$ and the affine map over $\mathbb{F}_2$778 $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),779 $c = \texttt{0x63}$;780- **ShiftRows**: row $r$ is rotated left by $r$ positions;781- **MixColumns**: each column, as a polynomial over $\mathbb{F}_{2^8}$, is multiplied by782 $\texttt{03}\,y^3 + \texttt{01}\,y^2 + \texttt{01}\,y + \texttt{02}$ modulo $y^4 + 1$ (omitted in the last round);783- **AddRoundKey**: XOR with the round key.784785The key schedule is $w_i = w_{i-N_k} \oplus g(w_{i-1})$, where $g$ applies RotWord, SubWord and the round786constant $x^{i/N_k - 1}$ when $i \equiv 0 \pmod{N_k}$, SubWord alone when $N_k = 8$ and787$i \equiv 4 \pmod 8$, and is the identity otherwise.788789*Implementation.* There is no lookup table. The state is two 64-bit words; the S-box is computed790algebraically on eight bytes in parallel: the inverse as $a^{254}$ by an addition chain of four791multiplications and seven squarings in $\mathbb{F}_{2^8}$ (squaring is linear), then the affine map as792XORs of byte rotations. No memory index or branch depends on the key or the data. This favours a simple793constant-time structure over speed, and does not use the processor's AES instructions.794795### 10.2 GCM796797Let $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$.798799$$800\begin{aligned}801C_i &= P_i \oplus E_K\big(\mathrm{inc}_{32}^{\,i}(J_0)\big), \quad i = 1, 2, \dots\\802S &= \mathrm{GHASH}_H\big(A\,\Vert\,0^{v}\,\Vert\,C\,\Vert\,0^{u}\,\Vert\,[\mathrm{len}(A)]_{64}\,\Vert\,[\mathrm{len}(C)]_{64}\big)\\803T &= E_K(J_0) \oplus S804\end{aligned}805$$806807$$808\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 H809$$810811in $\mathbb{F}_{2^{128}} = \mathbb{F}_2[x]/(x^{128} + x^7 + x^2 + x + 1)$, with the bit-reflected812convention of the standard (the first bit of a block is the coefficient of $x^0$; multiplication by $x$ is813a right shift with reduction by $R = \texttt{0xe1}\,\Vert\,0^{120}$).814815Constraints: a (key, nonce) pair must never be used twice — a repeat reveals the XOR of the plaintexts and816allows recovery of $H$, hence forgeries; at most $2^{39} - 256$ bits ($2^{36} - 32$ bytes) of plaintext817per nonce; with random 96-bit nonces, at most $2^{32}$ encryptions per key, which keeps the probability of818a repeat below $2^{-32}$.819820*Implementation.* The public interface accepts only 96-bit nonces and 128-bit tags. GHASH precomputes the821128 multiples $H\cdot x^i$ and, for every block, reads the whole table in a fixed order, keeping or822discarding each entry with a mask derived from one bit of the accumulator. Decryption checks the tag in823constant time before any plaintext is produced.824825---826827## 11. ChaCha20 and Poly1305828829Source read: `Sources/QyvoraSymmetric/ChaCha20Poly1305.swift` (RFC 8439).830831**ChaCha20.** The state is sixteen 32-bit words: four constants (`"expand 32-byte k"`), eight key words,832a 32-bit block counter and three nonce words. The quarter round on $(a, b, c, d)$ is833834$$835\begin{aligned}836a &\mathrel{+}= b; & d &\mathrel{\oplus}= a; & d &\leftarrow d \lll 16;\\837c &\mathrel{+}= d; & b &\mathrel{\oplus}= c; & b &\leftarrow b \lll 12;\\838a &\mathrel{+}= b; & d &\mathrel{\oplus}= a; & d &\leftarrow d \lll 8;\\839c &\mathrel{+}= d; & b &\mathrel{\oplus}= c; & b &\leftarrow b \lll 7.840\end{aligned}841$$842843Ten double rounds are applied, each a column round on $(0,4,8,12)$, $(1,5,9,13)$, $(2,6,10,14)$,844$(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)$;845the initial state is then added word-wise and serialised little-endian to give 64 keystream bytes.846847**Poly1305.** With a one-time key $(r, s)$, $r$ clamped by848$r \leftarrow r \wedge \texttt{0x0ffffffc0ffffffc0ffffffc0fffffff}$, and the message cut into 16-byte849blocks $c_i$, each read little-endian with a `1` bit appended above its most significant byte:850851$$852\mathit{acc} \leftarrow \big((\mathit{acc} + c_i)\cdot r\big) \bmod (2^{130} - 5), \qquad \mathrm{tag} = (\mathit{acc} + s) \bmod 2^{128}.853$$854855A one-time key must authenticate one message only. *Implementation.* Five 26-bit limbs with 64-bit856products; the final reduction computes $h + 5 - 2^{130}$ and selects by mask.857858**AEAD.** The one-time key is the first 32 bytes of the ChaCha20 block with counter 0; the plaintext is859encrypted starting at counter 1; the tag is Poly1305 over860$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}}$.861ChaCha20-Poly1305 is part of the engine; the vault of version 1 uses AES-256-GCM.862863---864865## 12. Argon2id866867Source read: `Sources/QyvoraSymmetric/Argon2.swift`, `BLAKE2b.swift` (RFC 9106, RFC 7693).868869Inputs: password $P$, salt $S$, optional secret $K$ and associated data $X$, parallelism $p$, tag length870$T$, memory $m$ in kibibytes, passes $t$, version $v = \texttt{0x13}$, type $y$ (2 for Argon2id). $H^{x}$871denotes BLAKE2b with an $x$-byte digest and all integers are 32-bit little-endian.872873$$874H_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)875$$876877The memory is $m' = 4p\lfloor m/4p \rfloor$ blocks of 1024 bytes, arranged in $p$ lanes of $q = m'/p$878columns, each lane divided into four slices.879880**Variable-length hash $H'$.** For an output of $T$ bytes:881882$$883H'^{\,T}(A) = \begin{cases}884H^{T}(T\,\Vert\,A) & T \le 64\\[2pt]885W_1\,\Vert\cdots\Vert\,W_r\,\Vert\,V_{r+1}, \quad r = \lceil T/32 \rceil - 2 & T > 64886\end{cases}887$$888889with $V_1 = H^{64}(T\,\Vert\,A)$, $V_i = H^{64}(V_{i-1})$, $W_i$ the first 32 bytes of $V_i$, and890$V_{r+1} = H^{T-32r}(V_r)$.891892**Filling the memory.**893894$$895\begin{aligned}896B[i][0] &= H'^{\,1024}(H_0\,\Vert\,0\,\Vert\,i), \qquad B[i][1] = H'^{\,1024}(H_0\,\Vert\,1\,\Vert\,i)\\897B[i][j] &= G\big(B[i][j-1],\; B[l][z]\big) && \text{first pass}\\898B[i][j] &= G\big(B[i][j-1],\; B[l][z]\big) \oplus B[i][j] && \text{later passes (version 0x13)}899\end{aligned}900$$901902with $B[i][-1] := B[i][q-1]$, and finally903$\mathrm{Tag} = H'^{\,T}\big(B[0][q-1] \oplus \cdots \oplus B[p-1][q-1]\big)$.904905**Compression function $G$.** $R = X \oplus Y$ is viewed as an $8\times8$ matrix of 128-bit registers. The906permutation $\mathcal{P}$ (one round of BLAKE2b's structure on sixteen 64-bit words) is applied to each row907and then to each column, giving $Z$; the output is $Z \oplus R$. Inside $\mathcal{P}$ the BLAKE2b addition908is replaced by a multiplicative mix,909910$$911\begin{aligned}912a &\leftarrow a + b + 2\,a_L b_L; & d &\leftarrow (d \oplus a) \ggg 32;\\913c &\leftarrow c + d + 2\,c_L d_L; & b &\leftarrow (b \oplus c) \ggg 24;\\914a &\leftarrow a + b + 2\,a_L b_L; & d &\leftarrow (d \oplus a) \ggg 16;\\915c &\leftarrow c + d + 2\,c_L d_L; & b &\leftarrow (b \oplus c) \ggg 63,916\end{aligned}917$$918919modulo $2^{64}$, where $x_L$ is the low 32 bits of $x$.920921**Indexing rule.** Each new block needs a reference block $B[l][z]$, chosen from two 32-bit values922$(J_1, J_2)$:923924- *Data-independent* (Argon2i; in Argon2id, the first two slices of the first pass):925 $(J_1, J_2)$ are the halves of successive 64-bit words of926 $G(0,\, G(0,\, Z))$ with $Z = (\text{pass}, \text{lane}, \text{slice}, m', t, y, \text{counter}, 0, \dots)$,927 the counter being incremented for every 128 values.928- *Data-dependent* (Argon2d; in Argon2id, everything else): $J_1$ and $J_2$ are the low and high 32 bits929 of the first 64-bit word of the previous block $B[i][j-1]$.930931Then $l = J_2 \bmod p$, except in the first slice of the first pass where $l = i$. Let $\mathcal{W}$ be932the set of blocks that may be referenced: in lane $l$, all blocks of the slices already completed in the933window of the last four slices, plus, when $l = i$, the blocks already built in the current segment,934excluding $B[i][j-1]$; when $l \ne i$ and the block is the first of its segment, the most recent block of935the window is also excluded. With $|\mathcal{W}|$ its size:936937$$938x = \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,939$$940941and $z$ is the $z_{\mathrm{rel}}$-th block of $\mathcal{W}$ counted from its oldest. The quadratic map942biases the choice towards recent blocks.943944The data-independent half protects the early computation against cache-timing observation; the945data-dependent half gives resistance to time–memory trade-offs. *Implementation.* Lanes are filled946sequentially (the result is the same as a multi-threaded computation); the memory matrix is erased before947return. RFC 9106 gives two recommended parameter sets, both with $p = 4$ and a 256-bit tag:948$t = 1$, $m = 2^{21}$ KiB (2 GiB), and, for memory-constrained environments, $t = 3$, $m = 2^{16}$ KiB949(64 MiB). The parameters used by the vault are recorded in the vault file and are not fixed by this950document.951952---953954## 13. Constructions specific to the QYVORA vault955956*Design of version 1; the vault module is being implemented.*957958**Subkeys.** From the random master key $\mathit{MK} \in \{0,1\}^{256}$, each usage has its own key959$K_{\mathit{label}} = \mathrm{HKDF\text{-}SHA256}(\mathit{MK}, \mathit{info} = \mathit{label})$ with distinct960labels, so that a key used for one purpose is never used for another.961962**Record sealing.** $(C, T) = \mathrm{AES\text{-}256\text{-}GCM}_{K}(N, P, A)$ with a fresh random 96-bit963nonce $N$ and the record header (format version, record identifier, type, creation time) as associated964data $A$.965966**Password combiner.**967968$$969\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)970$$971972Let $c_A$ and $c_P$ be the costs of one evaluation of each function. To test one password candidate an973attacker must obtain both inputs of the HKDF, so the cost per guess is $c_A + c_P$ if both974implementations are correct. If the engine's Argon2id were flawed so that its output became cheap to975compute or even constant, the cost per guess would remain at least $c_P$: the vault is never weaker than a976vault protected by the system PBKDF2 alone. The argument requires only that HKDF behaves as a977pseudorandom function of its whole input; it does not cover a flaw that leaks the password itself.978979**Hybrid KEM for device-to-device transfer.**980981$$982K = \mathrm{HKDF}\big(\mathit{ss}_{\mathrm{X25519}} \,\Vert\, \mathit{ss}_{\text{ML-KEM-1024}}\big)983$$984985$K$ is indistinguishable from random as long as at least one of the two shared secrets is, provided the986derivation is bound to the exchanged public values. The exact inputs of the derivation (labels,987public keys, ciphertext) are fixed by the transfer protocol specification, which is not yet written; this988document only states the principle.989990---991992## References993994- NIST FIPS 180-4, *Secure Hash Standard*; FIPS 197, *Advanced Encryption Standard*; FIPS 198-1, *HMAC*.995- NIST FIPS 202, *SHA-3 Standard*; SP 800-185, *SHA-3 Derived Functions*; SP 800-38D, *Galois/Counter Mode*.996- NIST FIPS 203 (ML-KEM), FIPS 204 (ML-DSA), FIPS 205 (SLH-DSA), 2024.997- RFC 2104 (HMAC), RFC 5869 (HKDF), RFC 6979 (deterministic ECDSA), RFC 7693 (BLAKE2), RFC 8017 (MGF1),998 RFC 8018 (PBKDF2), RFC 8439 (ChaCha20 and Poly1305), RFC 9106 (Argon2).999- SEC 1 and SEC 2, Standards for Efficient Cryptography Group.1000- J. Renes, C. Costello, L. Batina, "Complete addition formulas for prime order elliptic curves",1001 *EUROCRYPT 2016*.1002- P. L. Montgomery, "Modular multiplication without trial division", *Mathematics of Computation* 44, 1985.1003- Ç. K. Koç, T. Acar, B. S. Kaliski, "Analyzing and comparing Montgomery multiplication algorithms",1004 *IEEE Micro* 16(3), 1996.1005- BIP-32, BIP-39, BIP-44, BIP-62, BIP-66, BIP-84, BIP-173, BIP-340, BIP-341, BIP-350.1006- EIP-2, EIP-55, EIP-155, EIP-1559, EIP-2718, EIP-2930; Ethereum Yellow Paper, appendix B (RLP).1007