Repository Public
1{2 "schema": "qyvora-review/1",3 "title": "Manual source and machine-code review of the Ed25519 module (QyvoraEd25519): constant time, validation, memory hygiene",4 "reviewed_revision_base": "13c4391",5 "date": "2026-12-05",6 "method": "The four source files of Sources/QyvoraEd25519 (Field25519, Scalar25519, Edwards25519, Ed25519: 904 lines) read in full by the audit agent. Every construct that touches a secret (seed, expanded key, nonce, secret scalar, the multiples of the base point computed from them) is listed with a verdict. Unlike the first review of the primitives, the generated machine code WAS inspected for this module: every conditional branch of the secret-path functions was read in the disassembly of the release build made by Swift 6.3.3 (cluster node M4M64a) and Swift 6.4 (M5M36); the accepted counts are in security/policies/constant-time-functions.json and are re-checked at every audit run (ct-machine-code). Statistical timing tests: qytiming (public interface on the production build; internals with chosen scalars and field elements on a second build with testability). Internal review by the audit agent: not an independent review.",7 "verdicts": {8 "constant-time by construction": "no secret-dependent branch, memory index or variable-time instruction at source level; branches of the compiled code read and found to test public values only",9 "variable-time, public data only": "data-dependent timing on values that are public (signatures, public keys, messages)",10 "depends on the processor flag": "the instructions used (64x64-bit multiplication) have a duration that depends on their operands on Apple M-series cores unless the data-independent-timing flag is set; the production entry points set it",11 "residual risk (documented)": "not a defect of this code; limit of the language or of the platform, documented",12 "finding (fixed)": "defect, fixed, see finding_id"13 },14 "entries": [15 {16 "construct": "Acc128.addProduct / add / shiftedRight51 (128-bit accumulation of limb products)",17 "location": "Sources/QyvoraEd25519/Field25519.swift:48",18 "verdict": "constant-time by construction",19 "note": "Carries are computed with bit arithmetic ((a & b) | ((a | b) & ~s)) >> 63, never through a Boolean, so the optimiser has nothing to turn into a branch (the lesson of QYV-AUD-002). Compiled code: inlined into Fe.mul, 0 conditional branch."20 },21 {22 "construct": "Fe.mul / square / add / sub / neg / carry (five 51-bit limbs, loose representation below 2^52)",23 "location": "Sources/QyvoraEd25519/Field25519.swift:119",24 "verdict": "constant-time by construction",25 "note": "Fixed sequence of multiplications, additions, shifts and masks. Bounds checked by hand: with limbs below 2^52 each column sum is below 5*19*2^104 < 2^111, the carry out of the last column is below 2^56 and 19 times it below 2^61: no overflow of the 128-bit accumulators or of the 64-bit carries. The limb-range invariant is asserted by the property tests on worst-case limbs (2^52-1 in every limb) and on chains of 40 random operations; results compared with Python integers (differential tests). Compiled code: Fe.mul 0 conditional branch (277 instructions)."26 },27 {28 "construct": "Fe.invert / pow22523 / pow250 (fixed addition chains)",29 "location": "Sources/QyvoraEd25519/Field25519.swift:162",30 "verdict": "constant-time by construction",31 "note": "The exponents p-2 and (p-5)/8 are public constants; the sequence of squarings and multiplications is fixed. Compiled code: 3 conditional branches, the loop counters of the runs of squarings."32 },33 {34 "construct": "Fe.canonicalWords / toBytes / isZeroMask / isNegative / equalMask / select / conditionalSwap",35 "location": "Sources/QyvoraEd25519/Field25519.swift:186",36 "verdict": "constant-time by construction",37 "note": "Canonical reduction by two carry passes and a subtraction of q*p where q is derived by additions and shifts; masks from (v | -v) >> 63. Compiled code: 0 conditional branch in each."38 },39 {40 "construct": "Fe.sqrtRatio and EdPoint.decode (point decompression)",41 "location": "Sources/QyvoraEd25519/Field25519.swift:251",42 "verdict": "variable-time, public data only",43 "note": "sqrtRatio itself is branch-free (masked selections). decode branches on its result and compares encodings with ==; it is called on public keys and on the R of signatures only (and once on the constant encoding of the base point)."44 },45 {46 "construct": "Sc.adc / sbb / addWide / subWide / select / addMod",47 "location": "Sources/QyvoraEd25519/Scalar25519.swift:76",48 "verdict": "constant-time by construction",49 "note": "Carry and borrow by bit arithmetic; the conditional subtraction of the order is a masked selection on the borrow."50 },51 {52 "construct": "Sc.montMul / reduce / reduceWide / mulAdd (Montgomery arithmetic modulo the order)",53 "location": "Sources/QyvoraEd25519/Scalar25519.swift:141",54 "verdict": "constant-time by construction",55 "note": "Four fixed iterations; the next limb of the multiplier is chosen by the loop index (public), which the compiler renders as 2 of the 3 conditional branches of the function (the third is the loop exit). The final subtraction is selected by a mask. Precondition a*b < l*2^256 holds at every call site: reduce (operand R^2 mod l < l), reduceWide, mulAdd (k < l by reduceWide; the clamped secret scalar is below 2^255). Results compared with Python integers on 2 000 to 30 000 cases including the order, its neighbours, 2l, 2^256-1 and 2^512-1."56 },57 {58 "construct": "Sc.isCanonical",59 "location": "Sources/QyvoraEd25519/Scalar25519.swift:115",60 "verdict": "variable-time, public data only",61 "note": "Returns a Bool that the caller branches on; used on the S of a signature being verified (public). The comparison itself is a borrow chain."62 },63 {64 "construct": "EdPoint.add / double (unified, complete formulas of RFC 8032 5.1.4)",65 "location": "Sources/QyvoraEd25519/Edwards25519.swift:67",66 "verdict": "constant-time by construction",67 "note": "The same sequence of field operations for any pair of points (a = -1 is a square and d is not: the formula is complete, verified by the property tests on all 64 pairs of torsion points and on P+P, P+(-P), P+0). Compiled code: double 0 conditional branch; add 1, the one-time initialisation check of the constant 2d (Swift lazy global)."68 },69 {70 "construct": "EdPoint.lookup (table read in the scalar multiplication)",71 "location": "Sources/QyvoraEd25519/Edwards25519.swift:109",72 "verdict": "constant-time by construction",73 "note": "All 16 entries are read and combined under a mask derived from (j ^ index) - 1 >> 63: the memory access pattern does not depend on the window value. Compiled code (inlined into multiply): a vectorised loop over the 16 entries with and/orr on NEON registers; its only branch is the entry counter. This is the requirement 'table lookups must scan under a mask': met."74 },75 {76 "construct": "EdPoint.multiply(scalar:table:) / multiplyBase / multiply(scalar:_:)",77 "location": "Sources/QyvoraEd25519/Edwards25519.swift:128",78 "verdict": "finding (fixed)",79 "note": "64 windows of 4 bits, most significant first; each window does four doublings, one masked lookup and one complete addition, whatever the scalar (including window 0: the neutral element is added). No scalar-dependent branch or index: the window is extracted by shifts from byte i >> 1. Compiled code: 4 conditional branches (two length preconditions, the lookup counter, the window counter i, identified by following the register through the disassembly). The base table is a lazy global built once from public data. FINDING QYV-AUD-006 (fixed): with the table 0·B … 15·B a zero window added the neutral element and the duration of [s]B was measurably different for the scalars 0, 1 and 2^252 (flag set, M4 Max). multiplyBase now runs the same loop on the table 1·B … 16·B and removes the constant [0x11…11]B at the end; the variable-base path (verification, public data) keeps the plain table.",80 "finding_id": "QYV-AUD-006"81 },82 {83 "construct": "EdPoint.encode on secret-derived points ([a]B, [r]B)",84 "location": "Sources/QyvoraEd25519/Edwards25519.swift:153",85 "verdict": "constant-time by construction",86 "note": "One inversion (fixed chain), two multiplications, canonical encoding, sign bit by OR. The results (public key, R) are public; the projective coordinates before encoding are not, and are not branched on. Compiled code: 2 branches (uniqueness and bounds of the output array)."87 },88 {89 "construct": "Ed25519.expand (SHA-512 of the seed, clamping)",90 "location": "Sources/QyvoraEd25519/Ed25519.swift:91",91 "verdict": "constant-time by construction",92 "note": "The digest is split by constant ranges and erased; clamping is three masks. Compiled code: 7 branches, all bounds and uniqueness checks on arrays of public length."93 },94 {95 "construct": "Ed25519.publicKey / Ed25519.sign",96 "location": "Sources/QyvoraEd25519/Ed25519.swift:103",97 "verdict": "constant-time by construction",98 "note": "Both set the data-independent-timing flag for their whole body (DataIndependentTiming.enable / restore by defer). sign recomputes the public key from the seed (never takes it from the caller: a mismatched key would reveal the scalar), derives the nonce r = SHA-512(prefix || M) mod l (RFC 8032, deterministic: no dependence on the system generator), computes S by mulAdd and verifies the signature before returning it (fault countermeasure; the branch on the result is on a public outcome). Compiled code: publicKey 2 branches, sign 4 (seed length, lazy table, scalar length, verification result)."99 },100 {101 "construct": "64x64-bit multiplication instructions under the field and scalar arithmetic",102 "location": "Sources/QyvoraEd25519/Field25519.swift:53",103 "verdict": "depends on the processor flag",104 "note": "Measured by the audit (qytiming, internals build, security/audit/review/ed25519-timing-2026-12-05.json). WITHOUT the data-independent-timing flag: on an M5 Max core (M5M36) field squaring and inversion depend on their operands (inversion of 1 against random values about 0.5 % shorter, max |t| 95 at 40 000 measurements); on an M4 Max core (M4M64a) the field operations alone show nothing but the scalar multiplication does for some scalars (max |t| 33 to 40 at 200 000 measurements). WITH the flag set, the field and scalar operations show no difference on either machine at up to one million measurements. The production entry points set the flag (Ed25519.publicKey, Ed25519.sign, HMAC.authenticate), so the secret path runs with it wherever the processor implements FEAT_DIT. The engine cannot set it on a processor without it (hw.optional.arm.FEAT_DIT = 0): there the property rests on the multiplier of that core, which this audit did not measure. Same residual as QYV-CRY-004 / QYV-AUD-003.",105 "finding_id": "QYV-CRY-004"106 },107 {108 "construct": "Ed25519.verify, isValidPublicKey, isCanonicalPoint, isSmallOrder, isOnCurve",109 "location": "Sources/QyvoraEd25519/Ed25519.swift:157",110 "verdict": "variable-time, public data only",111 "note": "Strict policy checked rule by rule against an integer transcription of RFC 8032, ed25519-dalek verify_strict and libsodium (differential tests, Wycheproof, speccheck). Never traps: lengths are checked first; the slices [0..<32] and [32..<64] are taken after the length guard."112 },113 {114 "construct": "SLIP10.master / Node.deriveHardened / derive",115 "location": "Sources/QyvoraEd25519/Ed25519.swift:231",116 "verdict": "constant-time by construction",117 "note": "HMAC-SHA512 only (hardened children); index range and seed length are public. The HMAC input 0x00 || key || index and the HMAC output are erased; intermediate nodes of a path are wiped as soon as the child exists."118 },119 {120 "construct": "SLIP10.Node: Equatable (synthesised ==)",121 "location": "Sources/QyvoraEd25519/Ed25519.swift:197",122 "verdict": "residual risk (documented)",123 "note": "The synthesised equality compares the private key and the chain code with the array comparison, which stops at the first difference. No code under Sources/ compares two nodes (tests and fuzz harnesses do); a caller that compared a node with attacker-influenced data would need ConstantTime.equal. Hardening item, not changed in this pass."124 }125 ],126 "memory_hygiene": [127 {128 "secret": "SHA-512(seed): clamped scalar and nonce prefix",129 "location": "Sources/QyvoraEd25519/Ed25519.swift:91",130 "wiping": "wiped (digest copy, scalar and prefix arrays by SecureMemory.zero on every exit path through defer); the chaining state of the hash instance was NOT erased before this audit",131 "finding_id": "QYV-CRY-005",132 "residual_risk": "low after the fix"133 },134 {135 "secret": "Signing nonce r (64-byte digest, reduced scalar, its 32-byte encoding)",136 "location": "Sources/QyvoraEd25519/Ed25519.swift:130",137 "wiping": "the 64-byte digest and the 32-byte encoding are erased; the chaining state of the hash instance was not (fixed, QYV-CRY-005); the reduced scalar `r` and the scalar `Sc(littleEndian: scalar)` are structs of four words in registers and stack slots and cannot be erased from Swift",138 "finding_id": "QYV-CRY-003",139 "residual_risk": "medium (same class as the secp256k1 scalars of QYV-CRY-003: a leaked nonce together with its signature gives the secret scalar)"140 },141 {142 "secret": "Projective coordinates of [a]B and [r]B and the table entries selected by the lookup",143 "location": "Sources/QyvoraEd25519/Edwards25519.swift:128",144 "wiping": "not erasable (EdPoint structs on the stack)",145 "finding_id": "QYV-CRY-003",146 "residual_risk": "low (the encoded results are public; the unreduced coordinates are not known to reveal the scalar by themselves)"147 },148 {149 "secret": "SLIP-0010 node (private key = Ed25519 seed, chain code)",150 "location": "Sources/QyvoraEd25519/Ed25519.swift:197",151 "wiping": "Node.wipe() erases both arrays in place; derive() wipes every intermediate node; the HMAC input and output are erased; the outer hash state held the child key (fixed, QYV-CRY-005). The final node is returned to the caller, who owns its lifetime: the Solana signing path of the wallet service is reviewed in security/audit/review/multichain-solana.json",152 "residual_risk": "low"153 }154 ],155 "validation": [156 {157 "item": "Seed and key lengths",158 "verdict": "conforms",159 "note": "publicKey and sign throw invalidSeedLength for anything but 32 bytes; verify returns false for a signature that is not 64 bytes or a key that is not 32 bytes; SLIP-0010 refuses master seeds outside 16..64 bytes and indices at or above 2^31. Fuzz targets ed25519-verify, ed25519-point, ed25519-sign, slip10-ed25519, ed25519-arithmetic."160 },161 {162 "item": "`precondition` in internal functions (Fe.fromBytes, Sc.init, Sc.reduceWide, EdPoint.multiply)",163 "verdict": "conforms",164 "note": "Each is reached only with lengths fixed by the callers (32 or 64 bytes, table of 16): the public entry points check the lengths of attacker-controlled input before calling them. No precondition is reachable from untrusted input."165 },166 {167 "item": "Verification policy (strict, cofactorless)",168 "verdict": "conforms",169 "note": "Documented in the type's header; identical verdicts to ed25519-dalek verify_strict (the rule Solana validators apply) and to libsodium on every generated input; stricter than OpenSSL and CryptoKit in the documented classes only. The one stated difference with dalek (non-canonical encodings of the public key are refused here before the small-order test) is not observable on signatures an attacker can produce: the non-canonical encodings are y = p..p+18, of which the decodable ones are either small-order points (refused by both) or points whose discrete logarithm nobody knows."170 }171 ]172}173