Repository Public
1{2 "id": "QYV-AUD-002",3 "title": "secp256k1 field and scalar arithmetic compiled with conditional branches on carries (secret-dependent timing of k·G, ECDSA and BIP-340 signing)",4 "severity": "high",5 "cvss_vector": "CVSS:3.1/AV:L/AC:H/PR:L/UI:N/S:U/C:H/I:N/A:N",6 "cvss_score": 4.7,7 "cwe": "CWE-208",8 "masvs": "MASVS-CRYPTO-1",9 "area": "crypto",10 "location": "Sources/QyvoraSecp256k1/U256.swift:99",11 "tool": "dudect-timing",12 "rule": "secp256k1-mul-fixed",13 "status": "fixed",14 "fix_commit": "5d28ee3",15 "regression_test": "Tests/QyvoraSecp256k1Tests/CarryChainTests.swift (functional equivalence); security/audit/runners/timing.py machine-code check (U256.add, U256.sub = 0 conditional branch, Modulus.montMul <= 4) and dudect targets secp256k1-mul-sparse, secp256k1-mul-dense, secp256k1-mul-fixed, ecdsa-sign-key, ecdsa-sign-fixed-message, schnorr-sign-key; step ct-ios (tool ct-machine-code-ios) repeats the machine-code check on the iOS application binary in every full run",16 "description": "Root cause: the 256-bit addition, subtraction and Montgomery multiplication computed carries with `addingReportingOverflow` followed by `flag ? 1 : 0`. The source has no branch, but the Swift optimiser (6.3.3 and 6.4.2, -O) compiled these selections into conditional branches on the carry flags: 8 conditional branches in U256.add, 8 in U256.sub and 20 in Modulus.montMul (disassembly of the release build). A carry depends on the operands, which are secret during k·G with a secret scalar (public-key derivation, ECDSA nonce, BIP-340 nonce) and during the scalar arithmetic of signing (k^-1, r·d). How it was found: the dudect-style timing test, not the source review (which had rated the code constant-time by construction). Measurements before the fix (laptop, Swift 6.4.2, 2000 measurements per class): k·G with scalar 1 took 131 µs against 285 µs for random scalars (max |t| = 295); scalar n−1 against random scalars max |t| = 97; BIP-340 signing with one repeated key against random keys 850 µs against 1013 µs (max |t| = 164). Impact: the duration of signing and key derivation depended on secret values, and the secret-dependent branches are observable by an attacker able to measure the device finely (co-located code, branch-predictor side channels). The severity is rated high by the wallet-impact rule of the severity policy (a leak on the signing key or on an ECDSA nonce leads to loss of funds) although the CVSS base score is medium (local vector, high complexity). No practical key recovery was attempted or demonstrated.",17 "recommendation": "Fixed: carries and borrows are computed with bit arithmetic only (majority functions on the operand and result words), so that no Boolean exists for the optimiser to branch on. After the fix the release build has 0 conditional branch in U256.add and U256.sub, and 4 in Modulus.montMul (selection of the multiplier limb by the loop index, a public value). Timing after the fix on a quiet node (M4M64a, Swift 6.3.3, 12 000 measurements per class for k·G): max |t| = 2.60 (scalar 1), 1.87 (scalar n−1), 2.96 (one repeated scalar), ECDSA 0.65 and 1.67, BIP-340 1.33 — all below the 4.5 threshold. The machine-code check and the timing targets run at every audit and fail the gate if a branch reappears (for example after a compiler update). The same check runs on the iOS application binary (symbols from the archive's dSYM) in every full audit run. The signing throughput decreased after the change: re-run docs/BENCHMARKS.md.",18 "evidence": "Before: Modulus.montMul 20 conditional branches (tbz/tbnz/cbz on carry flags), U256.add 8, U256.sub 8. After: 4, 0, 0. qytiming before the fix: secp256k1-mul-sparse mean A 131142 ns / B 285301 ns, t(raw) −51.2, max |t| 295.2. iOS application binary (unsigned Release archive built at commit f00aa0a, before the fix; symbols from its dSYM): U256.add 8 conditional branches, U256.sub 8, Modulus.montMul 20 — the defect was present in the application binary, not only in the macOS package build.",19 "fingerprint": ""20}21