Using Kullback–Leibler Divergence (KL Divergence) in Cryptanalysis of Bitcoin ECDSA Transaction Signatures for Private Key Recovery

21.10.2026

The Kullback–Leibler divergence is a fundamental tool for cryptanalysis of ECDSA in the context of the Bitcoin blockchain. Its application allows:

  • Detect  statistical biases in nonce generation that are not visible with simple entropy analysis.
  • Quantify  the information leakage in bits per signature, which provides an accurate estimate of the attack difficulty.
  • Optimize  lattice attacks by selecting the most informative bins and estimating the required number of signatures.
  • Relate  statistical properties of the implementation to the information-theoretic limits of cryptanalysis.

In the public environment of the Bitcoin blockchain, where all signatures are available for analysis, even a slight shift in the nonce (the order of fractions of a bit) can lead to

Content

1. Introduction

The Kullback–Leibler divergence (KL divergence), also known as the relative entropy, information divergence, or discriminative information, is a nonnegative-valued functional measuring the asymmetric “distance” between two probability distributions  P  and  Q defined on a common sample space.[reference:0] Unlike a metric, the KL divergence is not symmetric and does not satisfy the triangle inequality, but it has fundamental properties that make it a powerful tool in information theory, statistics, and cryptography.

The Elliptic Curve Digital Signature Algorithm (ECDSA) is the cornerstone of Bitcoin transaction security. Each ECDSA signature requires the generation of a unique ephemeral secret value  k  , called  a nonce . If the nonce is not generated uniformly randomly, but with some bias, an attacker can exploit this bias to recover the long-term private key.[reference:1] In this paper, we demonstrate how KL divergence allows one to quantify this bias, extract private key information from public Bitcoin blockchain data, and construct an effective attack based on lattice methods.

2. Mathematical foundations of KL-divergence

For two discrete probability distributions  P  and  Q defined on the same set of outcomes, the KL divergence is defined as follows:

D KL (P ‖ Q) = Σ x  P(x) log 2  [ P(x) / Q(x) ]

For continuous distributions with densities  p(x)  and  q(x) :

D KL (P ‖ Q) = ∫ p(x) log 2  [ p(x) / q(x) ] dx

Key properties of KL divergence:

  • Non-negativity:  D KL (P ‖ Q) ≥ 0, and equality is achieved if and only if P = Q almost everywhere.
  • Asymmetry:  D KL (P ‖ Q) ≠ D KL (Q ‖ P) in general.
  • Connection with entropy:  D KL (P ‖ Q) = H(P, Q) − H(P), where H(P, Q) is the cross entropy, H(P) is the Shannon entropy of the distribution P.

In the context of cryptanalysis, the KL divergence has a fundamental interpretation: it measures  the information gain an attacker receives when observing data distributed over  P while expecting a distribution  Q . This interpretation underlies its application to nonce analysis in ECDSA [reference:2].

3. ECDSA: Signature Structure and Nonce Vulnerability

The ECDSA algorithm over an elliptic curve  E  with a base point  G  of order  n  works as follows:

  1. Key generation:  private key  d  ∈ [1, n−1], public key  Q = dG .
  2. Message signature  with hash  h :
    • A random nonce  k  ∈ [1, n−1] is generated.
    • The point  R = kG is calculated ,  r = x(R)  mod n is taken.
    • Calculate  s = k −1 (h + r d)  mod n.
    • Signature is a pair  (r, s) .

If two different messages are signed with the same nonce  k , the private key can be calculated directly:

k = (h 1  − h 2 ) · (s 1  − s 2 ) −1  mod n

However, even  a partial  bias in the nonce (for example, a shift in the high-order bits) creates statistical leakage sufficient to recover the key using lattice methods[reference:3]. This is where KL divergence plays a central role: it allows us  to detect  the bias,  quantify  the leakage, and  estimate  the number of signatures required for a successful attack.

4. Application of KL-divergence in ECDSA cryptanalysis

4.1. Statistical model of nonce distribution

Consider a set of signatures  (r i , s i , h i ) extracted from the public Bitcoin blockchain. For each signature, it is possible to recover  r i  = x(k i G) , but not  k i itself . However, from the signature equation:

k i  = s i −1 (h i  + r i d) mod n

it follows that  k i  depends linearly on  d . If we assume that  k i  were generated from a distribution  P  with some bias, and the “ideal” distribution is uniform  Q = U[1, n−1] , then the KL divergence  D KL (P ‖ Q)  gives a measure of the deviation of the real generator from the ideal one.

In practice, bias often manifests itself as unevenness in the high-order bits of the nonce. For example, when using the modular reduction  k = r mod n  with  r having more bits than  n , the high-order bits  of k  may systematically deviate from a uniform distribution [reference:4]. KL divergence allows one to quantify this deviation.

4.2 Detecting Bias via KL Divergence

Let  k 1 , k 2 , …, k m  be a sample of nonces (or more precisely, their estimates obtained from signatures for some hypothetical  d ). We divide the range of possible nonce values ​​into  B  bins (e.g., by the most significant  b  bits). The empirical distribution  P̂  is constructed as:

P̂(j) = (number of k i  in bin j) / m

Then the sample estimate of KL-divergence is:

D̂ KL (P̂ ‖ Q) = Σ j=1 B  P̂(j) log 2  [ P̂(j) / Q(j) ]

where  Q(j) = 1/B  for a uniform distribution. A significant deviation  of D̂ KL  from zero (taking into account statistical error) indicates the presence of bias. The critical value can be determined using the asymptotic distribution of the likelihood ratio statistic:  2m · D̂ KL  ~ χ²(B−1) .

Important:  To correctly calculate  P̂(j),  we need to know  d , which is the target of the attack. In practice, an iterative approach is used: first, a hypothesis about the nonce distribution is constructed (e.g., based on an implementation analysis), then  D̂ KL  is calculated  for each candidate d , and the one that minimizes the discrepancy with the expected biased distribution is selected.

4.3. Information leakage assessment

KL divergence is directly related to the amount of information an adversary can extract about a nonce from observations. If the true nonce distribution  P  differs from the uniform  Q  by  D KL (P ‖ Q)  bits per signature, then recovering  d  (which has  L  bits of entropy) would require approximately:

m ≥ L / D KL (P ‖ Q)

signatures. This provides a lower bound on the complexity of the attack and allows us to assess its practical feasibility[reference:5].

Moreover, the KL-divergence allows us to calculate  the mutual information  between the observed signatures and the private key:

I(d; signatures) = H(d) − H(d | signatures) ≈ m · D KL (P ‖ Q)

Thus, KL divergence acts as a bridge between the statistical properties of the nonce generator and the information-theoretic limits of cryptanalysis.

4.4. Lattice construction and key recovery

Once the bias is detected and quantified, a lattice attack is applied to the Hidden Number Problem (HNP). For each signature, it is known that:

k i  = s i −1  · h i  + s i −1  · r i  · d mod n

If it is known that  k i  lies in some narrow interval (for example, the most significant bits are fixed due to bias), then this can be reformulated as an inequality:

| k i  − t i  · d − u i  | n  ≤ B

where  t i  = s i −1  r i ,  u i  = s i −1  h i , and  B  is the boundary determined by the offset. A system of such inequalities leads to the problem of finding a short vector in a lattice, which is solved by the LLL or BKZ algorithm[reference:6].

KL divergence plays a dual role here:

  1. It determines how much  B  can be reduced (the stronger the offset, the smaller  the B  and the easier the attack).
  2. It allows one to choose the optimal binning to maximize the information gain when constructing the lattice.

5. Practical implementation and examples

Let’s look at a simplified Python example demonstrating the calculation of KL divergence for a nonce sample with an artificially introduced bias.

import numpy as np
from scipy.stats import entropy
from scipy.special import rel_entr

# Parameters
n = 2**256 # curve order secp256k1
m = 10000 # number of signatures

# Ideal uniform distribution (for the upper 8 bits)
Q = np.ones(256) / 256

# Biased distribution: the high-order bits of the nonce are biased towards 0
# (simulated modular reduction with redundant bits)
def biased_nonce_sample(m):
    samples = []
    for _ in range(m):
        r = np.random.randint(0, 2**264) # 264-bit random
        k = r % n
        samples.append(k)
    return np.array(samples)

# Sample generation
k_samples = biased_nonce_sample(m)

# Extract the most significant 8 bits
top_bits = (k_samples >> 248) & 0xFF

# Empirical distribution
P_hat = np.bincount(top_bits, minlength=256) / m

# KL-divergence
D_KL = np.sum(rel_entr(P_hat, Q))
print(f"KL divergence D_KL(P || Q) = {D_KL:.6f} bits")
print(f"Expected signature leak: {D_KL:.6f} bits")
print(f"The required number of signatures for a 256-bit key: {int(256 / max(D_KL, 1e-6))}")

# Testing significance using chi-square
chi2_stat = 2 * m * D_KL
from scipy.stats import chi2
p_value = 1 - chi2.cdf(chi2_stat, df=255)
print(f"p-value = {p_value:.2e} (significant at p < 0.01)" if p_value < 0.01 else "The bias is not significant")

In real-world attacks on the Bitcoin blockchain , researchers analyzed millions of signatures and found biases corresponding to KL divergence on the order of 10−3 –10−2 bits  per signature, requiring tens of thousands of signatures to recover the key [reference:7]. The famous paper “Biased Nonce Sense” demonstrated the recovery of hundreds of Bitcoin and Ethereum private keys in precisely this manner [reference:8].

6. Conclusion

The Kullback–Leibler divergence is a fundamental tool for cryptanalysis of ECDSA in the context of the Bitcoin blockchain. Its application allows:

  • Detect  statistical biases in nonce generation that are not visible with simple entropy analysis.
  • Quantify  the information leakage in bits per signature, which provides an accurate estimate of the attack difficulty.
  • Optimize  lattice attacks by selecting the most informative bins and estimating the required number of signatures.
  • Relate  statistical properties of the implementation to the information-theoretic limits of cryptanalysis.

Given the public nature of the Bitcoin blockchain, where all signatures are available for analysis, even a slight shift in the nonce (on the order of fractions of a bit) can lead to the complete recovery of the private key given enough transactions. K-L divergence is the mathematical framework that makes this threat measurable and practically achievable.

7. Directions for further research for cryptanalysis based on this area

Based on the presented approach, the following promising research areas can be identified:

  1. Adaptive KL-divergence estimates for nonstationary nonce distributions.  Modern nonce generators can change their behavior over time (for example, after a firmware update). Developing online KL-divergence estimation methods with a sliding window will allow tracking the evolution of the bias and adapting the attack in real time.
  2. Multivariate KL divergences for joint analysis of nonces and other leaks.  In addition to the high-order bits of the nonce, correlations with signature time, fee size, or other transaction metadata can be observed. Constructing multivariate distributions and calculating the KL divergence between them opens the way to more accurate leak modeling.
  3. Using KL divergence in attacks on deterministic ECDSA (RFC 6979).  Although deterministic ECDSA eliminates nonce randomness, it can be vulnerable to side-channel attacks. KL divergence can be used to estimate information leakage in such implementations.
  4. Optimizing lattice attacks using KL divergence for basis selection.  Instead of a standard lattice basis, one can construct a basis that maximizes the information gain per unit of reduction complexity. This requires solving a KL divergence optimization problem with respect to the lattice parameters.
  5. Cross-blockchain analysis.  Similar methods can be applied to other cryptocurrencies (Ethereum, Litecoin, Monero), taking into account their signature generation and data availability.
  6. Developing security measures based on KL divergence thresholds.  Cryptographic libraries and hardware wallets can calculate KL divergence estimates for generated nonces in real time and block the signature if the bias exceeds an acceptable threshold. This requires studying the statistical properties of KL divergence estimates for small samples.
  7. The Relationship of KL Divergence to Differential Privacy.  The concept of information gain, which underlies KL divergence, is closely related to the concept of differential privacy. Studying this relationship may lead to new methods for leakage analysis in cryptographic protocols.
  8. Application of KL divergence to the analysis of other ECDSA components.  In addition to nonces, KL divergence can be used to analyze the distribution of  r  and  s  in signatures, as well as to detect anomalies in verification processes.

These areas are of both theoretical and practical interest and can significantly expand the arsenal of methods for cryptanalysis and protection of cryptographic systems.