On Monday, November 16, about 10.4 million USDC was withdrawn from the derivatives exchange Lighter using one trader's API keys. Lighter's postmortem, as reported by Cointelegraph, says the keys came from the trader's own custom signing software, which reused cryptographic nonces. The signatures were public, so the private keys could be calculated from them. Lighter says its protocol, contracts and official SDKs were not affected, and it recovered about $5 million for the trader.
I won't speculate about that trader's code. The bug class is old and well understood, so let's build it from scratch and then write the check that would have caught it.
The math
An ECDSA signature over a message hash z with private key d is a pair (r, s):
- pick a secret nonce
k r = (k·G).x mod ns = k⁻¹ · (z + r·d) mod n
From one signature you get one equation with two unknowns, k and d. Now sign two different messages with the same k. Both signatures have the same r, and you have two equations:
s1 = k⁻¹ (z1 + r·d)
s2 = k⁻¹ (z2 + r·d)
Subtract them and d cancels:
k = (z1 - z2) / (s1 - s2) mod n
d = (s1·k - z1) / r mod n
Both hashes are known to anyone who sees the messages, and both signatures are public. That's all an attacker needs.
One wrinkle: many signers normalise s to the lower half of the curve order (Bitcoin and Ethereum do, to stop malleability). That can flip either s to n - s, so a recovery routine should try both signs and check the result against the public key.
The script
No dependencies, just BigInt and node:crypto. It implements secp256k1 point math, textbook signing with an explicit nonce (so we can make the mistake on purpose), verification, recovery and an audit for repeated r values.
// nonce-reuse.mjs: why an ECDSA signer must never reuse a nonce.
// Plain Node 18+, no dependencies. secp256k1 math with BigInt.
import { createHash, randomBytes } from 'node:crypto';
const p = 2n ** 256n - 2n ** 32n - 977n;
const n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
const G = [
0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798n,
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8n,
];
const mod = (a, m) => ((a % m) + m) % m;
const inv = (a, m) => {
let [r0, r1, s0, s1] = [mod(a, m), m, 1n, 0n];
while (r1) {
const q = r0 / r1;
[r0, r1] = [r1, r0 - q * r1];
[s0, s1] = [s1, s0 - q * s1];
}
return mod(s0, m);
};
const add = (P, Q) => {
if (!P) return Q;
if (!Q) return P;
if (P[0] === Q[0] && mod(P[1] + Q[1], p) === 0n) return null;
const l = P[0] === Q[0] && P[1] === Q[1]
? mod(3n * P[0] * P[0] * inv(2n * P[1], p), p)
: mod((Q[1] - P[1]) * inv(Q[0] - P[0], p), p);
const x = mod(l * l - P[0] - Q[0], p);
return [x, mod(l * (P[0] - x) - P[1], p)];
};
const mul = (k, P = G) => {
let R = null;
for (; k > 0n; k >>= 1n, P = add(P, P)) if (k & 1n) R = add(R, P);
return R;
};
const rand = () => mod(BigInt('0x' + randomBytes(32).toString('hex')), n - 1n) + 1n;
const z = (msg) => BigInt('0x' + createHash('sha256').update(msg).digest('hex')) % n;
const sign = (d, msg, k) => {
const r = mod(mul(k)[0], n);
const s = mod(inv(k, n) * (z(msg) + r * d), n);
return { r, s, z: z(msg) };
};
const verify = (Q, msg, { r, s }) => {
const w = inv(s, n);
const X = add(mul(mod(z(msg) * w, n)), mul(mod(r * w, n), Q));
return X !== null && mod(X[0], n) === r;
};
const recover = (a, b, Q) => {
for (const s2 of [b.s, n - b.s]) {
const k = mod((a.z - b.z) * inv(a.s - s2, n), n);
const d = mod((a.s * k - a.z) * inv(a.r, n), n);
if (Q && mul(d)[0] === Q[0] && mul(d)[1] === Q[1]) return d;
}
return null;
};
const findRepeatedR = (sigs) => {
const seen = new Map();
const hits = [];
sigs.forEach((sig, i) => {
const key = sig.r.toString(16);
if (seen.has(key)) hits.push([seen.get(key), i]);
else seen.set(key, i);
});
return hits;
};
// demo: four signed API actions, two of them with the same nonce
const d = rand();
const Q = mul(d);
const fixedK = rand();
const msgs = ['order:place BTC-PERP 0.5', 'order:cancel 81723', 'transfer:pool 2500 USDC', 'order:place ETH-PERP 3'];
const log = [
sign(d, msgs[0], rand()),
sign(d, msgs[1], fixedK),
sign(d, msgs[2], rand()),
sign(d, msgs[3], fixedK),
];
console.log('all signatures verify:', log.every((sig, i) => verify(Q, msgs[i], sig)));
const hits = findRepeatedR(log);
console.log('repeated r found at:', hits);
for (const [i, j] of hits) {
const found = recover(log[i], log[j], Q);
console.log('private key recovered:', found !== null);
console.log('matches the real key :', found === d);
}
Output:
all signatures verify: true
repeated r found at: [ [ 1, 3 ] ]
private key recovered: true
matches the real key : true
All four signatures are valid. A verifier has no reason to complain, which is why this bug survives testing: everything works until someone compares two r values. I checked the point multiplication against @noble/curves while writing this; the script itself needs nothing beyond Node.
The audit is the useful part
findRepeatedR is the bit worth keeping. If you run a bot or a custom signer, log the r of every signature it produces (it's public anyway) and alert on any duplicate for the same key. A repeat means the key is already exposed, and the only fix is to rotate it and move funds.
Better still, don't pick nonces yourself. RFC 6979 derives k deterministically from the private key and the message hash, so two different messages can't share a nonce by accident. Mainstream libraries such as @noble/curves, libsecp256k1 and the Go and Rust secp256k1 crates do this by default. The PlayStation 3 signing key leak in 2010 and the 2013 Android SecureRandom bug that exposed Bitcoin wallet keys both came from nonces that weren't unique.
What Lighter changed
According to the postmortem as reported, Lighter now requires wallet signatures for transfers to public pools, has made API-key signatures non-public, and plans IP allowlists and configurable permissions for API keys. Hiding the signatures closes the path this attacker used. A signer that reuses nonces still leaks the key to anyone who sees two of its signatures, so the fix on the client side is the same as always: use a vetted library and let it choose k.
The same morning a Safe multisig lost about $6.45 million through a compromised role key and a permissions policy that allowed the transfer (Cointelegraph). Different mechanism, same place: the code around the contracts.
Top comments (0)