Nonce Leakage

Find the secret behind the Sig

Recon

The handout (Chalenge Source.zip) is a small archive with two files, and neither of them does any cryptography for us. chall.py is a display-only printer that dumps the contents of challenge.json to the terminal, and challenge.json holds all the real data: the curve, a public key, eight ECDSA signatures over messages like NVR transaction 001, a leaked value alongside each signature, and an encrypted flag. The printer closes with a three-step mission: analyze the signatures, recover the private key, decrypt the flag.

We list the archive and run the printer to see the data:

$ unzip -l "Chalenge Source.zip"
Archive:  Chalenge Source.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  11-26-2026 09:44   Chalenge Source/
     1418  11-26-2026 09:40   Chalenge Source/chall.py
     4102  11-26-2026 09:30   Chalenge Source/challenge.json
---------                     -------
     5520                     3 files
$ python3 "Chalenge Source/chall.py"
============================================================
              NVR CTF 2026
           THE LEAKED SIGNATURE
============================================================

[+] Curve:
secp256k1

[+] Public Key:
Qx = 41525490912497360717000839975794592821182740766460458437305554001333897175749
Qy = 52115654649795046653842861414979563972866664623174035240671738634689405066611

[+] Nonce Leakage:
Known upper bits: 248
Unknown lower bits: 8

[+] Signatures:

Signature #1
Message: NVR transaction 001
z = 42457143166192837741451184985165498853000295007133820991491487500396413612613
r = 2542277703361253104237432603073246011363470715903971449852008643334540452076
s = 46305430898451398043430759582335044616090760952279088663041836167544408903972
leak = 298605501446304034280199957670051134173169087596478935572660019501604116718

The other seven signatures follow the same shape, and the run ends with the encrypted flag and the mission:

[+] Encrypted Flag:
c07879865c4c5ac5803951574f1290e6a89e022c878365d596dd45b73ad98691d14b48995f4c55

Your mission:
1. Analyze the ECDSA signatures.
2. Recover the private key.
3. Decrypt the encrypted flag.

So per signature we have z, the already-computed hash value that was signed (given to us directly, so we never need to reproduce how NVR transaction 001 was hashed), the signature pair r, s, and leak. The ciphertext is 78 hex chars, which is 39 bytes.

Now the math. On a curve whose group order is n, ECDSA signs by picking a fresh secret nonce k, computing the point k·G, taking r from its x coordinate, and setting s = (z + r·d)·k⁻¹ mod n, where d is the private key. Rearranged, the signing relation is s·k ≡ z + r·d (mod n). If we knew k for any one signature, the private key would fall out immediately:

d = (s·k − z)·r⁻¹ mod n

That equation is why nonce secrecy matters, and this challenge hands us 248 of the 256 nonce bits. The printer labels the leak “Known upper bits: 248” with “Unknown lower bits: 8”, which reads as k = (leak << 8) | k_low with k_low somewhere in [0, 256). A quick size check supports that reading:

$ python3 -c "import json; D=json.load(open('Chalenge Source/challenge.json')); print([s['leak'].bit_length() for s in D['signatures']])"
[248, 247, 245, 248, 246, 247, 248, 248]

Every leak is 245 to 248 bits, exactly the size of a random full-length scalar after its low 8 bits are shifted off.

The textbook attack for partial nonce leakage is a lattice attack on the Hidden Number Problem, which is the right approach when a large chunk of k is unknown. Here the unknown part is 8 bits, so each signature leaves only 256 candidate nonces, and the challenge gives us the public key Q, which works as an exact oracle: for each candidate k, compute d = (s·k − z)·r⁻¹ mod n and check whether d·G equals Q. One signature alone pins the key, and eight signatures let us demand the same d from all of them, which rules out fluke hits. No lattice needed.

Exploitation

Recovering the private key

We need secp256k1 arithmetic, so the solver implements it from scratch: points are affine (x, y) pairs, None plays the point at infinity, add covers both the doubling case (tangent line through the same point) and the generic case (chord line through two points), and mul is double-and-add scalar multiplication. Then it brute-forces the 8 unknown low bits per signature:

#!/usr/bin/env python3
import json
from pathlib import Path

DATA = json.load(open(Path(__file__).with_name("Chalenge Source") / "challenge.json"))

# secp256k1: field prime p, group order n, generator G
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
     0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)

def add(P, Q):
    if P is None: return Q
    if Q is None: return P
    if P[0] == Q[0] and (P[1] + Q[1]) % p == 0: return None
    if P == Q:
        l = (3 * P[0] * P[0]) * pow(2 * P[1], -1, p) % p
    else:
        l = (Q[1] - P[1]) * pow(Q[0] - P[0], -1, p) % p
    x = (l * l - P[0] - Q[0]) % p
    return (x, (l * (P[0] - x) - P[1]) % p)

def mul(k, P=G):
    R = None
    while k:
        if k & 1: R = add(R, P)
        P = add(P, P); k >>= 1
    return R

Q = (DATA["public_key"]["x"], DATA["public_key"]["y"])
print("[*] Q on curve:", (Q[1] ** 2 - Q[0] ** 3 - 7) % p == 0)

privs = []
for i, sig in enumerate(DATA["signatures"], 1):
    z, r, s, leak = sig["z"], sig["r"], sig["s"], sig["leak"]
    rinv = pow(r, -1, n)
    found = []
    for low in range(256):
        k = (leak << 8) | low
        d = (s * k - z) * rinv % n
        if mul(d) == Q:                  # public key as oracle
            found.append((low, d))
    print(f"[*] sig#{i}: candidates {found}")
    privs.append(found)

# keep the d consistent across all 8 signatures
common = set(c[1] for c in privs[0])
for c in privs[1:]:
    common &= set(x[1] for x in c)
d = common.pop()
print("[+] PRIVATE KEY d =", d)

The fact that the brute force finds hits confirms the shifted-nonce reading:

$ python3 solve.py
[*] Q on curve: True
[*] sig#1: candidates [(108, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#2: candidates [(4, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#3: candidates [(12, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#4: candidates [(250, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#5: candidates [(87, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#6: candidates [(152, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#7: candidates [(103, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[*] sig#8: candidates [(1, 84331172793571343118467952114742513671658753086447497951200079284062563161947)]
[+] PRIVATE KEY d = 84331172793571343118467952114742513671658753086447497951200079284062563161947

Every signature returns exactly one candidate and it is the same d all eight times, so the key is confirmed eight independent ways. The recovered low bytes (108, 4, 12, 250, 87, 152, 103, 1) look like ordinary random nonce tails, which is consistent with a per-signature random k whose top bits leaked.

Deriving the flag key from the private key

The ciphertext is 39 bytes, not a multiple of 16, which rules out AES in a block mode like ECB or CBC and points at a stream or XOR construction. The challenge calls the private key “the secret behind the Sig”, so d itself has to seed the keystream, and the most standard way to stretch a secret number into one is a hash over its raw bytes. We tried that directly, SHA-256 over the big-endian bytes of d used as a repeating XOR pad over the 39 ciphertext bytes:

import hashlib, itertools

ct = bytes.fromhex("c07879865c4c5ac5803951574f1290e6a89e022c878365d596dd45b73ad98691d14b48995f4c55")
d = 84331172793571343118467952114742513671658753086447497951200079284062563161947
key = hashlib.sha256(d.to_bytes((d.bit_length() + 7) // 8, "big")).digest()
pt = bytes(c ^ k for c, k in zip(ct, itertools.cycle(key)))
print("ct bytes:", len(ct), "| all printable:", all(32 <= b < 127 for b in pt))
print("prefix:", pt[:16])
ct bytes: 39 | all printable: True
prefix: b'NVR{partial_nonc'

The first candidate produced 39 fully printable bytes opening with the flag format, so no other derivation was needed. The pad cycles for the last 7 ciphertext bytes because d is exactly 256 bits long, so its minimal big-endian encoding already is a full 32-byte key. The standalone check, saved as verify.py, is the same computation with the plaintext printed in full:

import hashlib, itertools

ct = bytes.fromhex("c07879865c4c5ac5803951574f1290e6a89e022c878365d596dd45b73ad98691d14b48995f4c55")
d = 84331172793571343118467952114742513671658753086447497951200079284062563161947
key = hashlib.sha256(d.to_bytes((d.bit_length() + 7) // 8, "big")).digest()
pt = bytes(c ^ k for c, k in zip(ct, itertools.cycle(key)))
print("ct bytes:", len(ct), "| plaintext bytes:", len(pt))
print("all printable:", all(32 <= b < 127 for b in pt))
print("plaintext:", pt.decode())

Flag capture

$ python3 verify.py
ct bytes: 39 | plaintext bytes: 39
all printable: True
plaintext: NVR{partial_nonce_leakage_breaks_ecdsa}