Berlekamp–Rabin algorithm in ECDSA cryptanalysis

20.10.2026

Applying a probabilistic root-finding method to a finite field 𝔽 p  to recover the hidden number (private key) in Bitcoin ECDSA transaction signatures.

Bitcoin uses an elliptic curve digital signature scheme (ECDSA) to authenticate the sender of a transaction. ECDSA’s security relies on the computational difficulty of the elliptic curve discrete logarithm problem (ECDLP). However, there are scenarios in which a private key can be recovered without solving the entire ECDLP—for example, if a nonce is reused or if there is a predictable relationship between nonces in multiple signatures.[reference:0]

The Berlekamp–Rabin algorithm is a probabilistic method for finding the roots of polynomials over a finite field 𝔽 p  with polynomial complexity. In the context of ECDSA cryptanalysis, it allows for efficient discovery of a private key if it is possible to construct a polynomial whose root is the desired secret number  d [reference:1].

Key Idea:  The Polynonce attack and its generalizations show that if the nonces in multiple ECDSA signatures are related by a polynomial recurrence relation, then one can construct a polynomial in the private key  d whose roots are easily found using the Berlekamp–Rabin algorithm[reference:2].

2. Mathematical foundations of ECDSA

2.1. Scheme parameters

The ECDSA scheme is defined by the following global parameters:

ParameterDesignationDescription
Elliptic curveECurve over a finite field 𝔽 p
Base pointGGenerator of a subgroup of order  n
Subgroup ordernPrime number (for secp256k1 – 256-bit)
Prime number fieldpFinite field module

For Bitcoin, the secp256k1 curve is used with the following parameters:

p = 2 256  − 2 32  − 2 9  − 2 8  − 2 7  − 2 6  − 2 4  − 1
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
E: y² = x³ + 7 (mod p)

2.2. Signature generation

Let  d  be a private key (an integer, 1 ≤  d  <  n ), and  Q  =  dG  be the corresponding public key. To sign a message with a hash  z  (in Bitcoin  , z  is the double SHA-256 hash of the transaction), the signer performs the following steps:

  1. Generates a random number  k  ∈ [1,  n − 1] —  nonce .
  2. Calculates the point  R  =  kG  and takes  r  =  x R  mod  n .
  3. Calculates  s  =  k −1 ( z  +  r d ) mod  n .
  4. Signature is a pair ( r ,  s ).

The fundamental equation relating the signature components, nonce, and private key is:

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

An equivalent form expressing nonce in terms of known components and  d is:

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

It is this linear relationship between  k  and  d  that is the starting point for constructing the polynomial[reference:3].

3. Berlekamp–Rabin algorithm

3.1. Statement of the problem

The Berlekamp–Rabin algorithm solves the problem of finding all roots of a polynomial f(x) ∈ 𝔽 q [x] that is  split  and  square- free  over a finite field 𝔽 q . This means that  f  has exactly deg( f ) distinct roots in 𝔽 q [reference:4].

3.2 Mathematical basis

The method is based on a fundamental property of finite fields: for any nonzero element α ∈ 𝔽 q ,  α q −1  = 1. Consequently, the set of roots of the polynomial x q−1  − 1 exactly coincides with 𝔽 q × .

For simplicity, let us assume that  q  is odd. Then:

x q−1  − 1 = (x (q−1)/2  − 1) · (x (q−1)/2  + 1)

This factorization allows us to separate the roots of the polynomial  f  into two groups: those for which α (q−1)/2  = 1, and those for which α (q−1)/2  = −1. By calculating gcd(f, x (q−1)/2  − 1), we obtain a polynomial whose roots are the first group. The remaining roots are contained in f / gcd(f, x (q−1)/2  − 1)[reference:5].

A more general version of the algorithm uses a random shift σ ∈ 𝔽 q  and considers polynomials:

g σ  = gcd(f, (x + σ) (q−1)/2  − 1)

and f / g σ . For a randomly chosen σ , each root of  f falls into g σ  with probability 1/2  and into the complementary factor with probability 1/2. This provides efficient probabilistic root separation[reference:6].

3.3. Algorithm

Berlekamp–Rabin algorithm (root finding)

  1. Input:  a normalized polynomial  f  ∈ 𝔽 q [x], separable and square-free.
  2. If deg( f ) = 1, return the single root.
  3. If deg( f ) = 0, return the empty set.
  4. Choose a random σ ∈ 𝔽 q .
  5. Calculate g = gcd(f, (x + σ) (q−1)/2  − 1) using fast exponentiation modulo  f .
  6. If 0 < deg( g ) < deg( f ), then recursively find the roots  of g  and  f/g .
  7. Otherwise, repeat with another σ.
  8. Output:  the set of all roots  of f  in 𝔽 q .

The complexity of the algorithm is O(M(d) log d log q) operations in the field, where  M(d)  is the complexity of multiplication of polynomials of degree  d . In the bit model, this is Õ(d log²q)[reference:7].

4. Construction of a polynomial for key recovery

4.1. Polynonce Attack

The Polynonce attack (Kudelski Security, 2023) exploits the case where nonces in multiple ECDSA signatures are related by a polynomial recurrence relation. The simplest case is linear recurrence:

k i  = a + b k i −1  (mod n)

where  a  and  b  are unknown constants[reference:8].

From the signature equation, we express each nonce through the private key  d :

k i (d) = c i,0  + c i,1  · d

Where:

c i,0  = z i  · s i −1  (mod n)
c i,1  = r i  · s i −1  (mod n)

4.2. Exclusion condition

The key observation is that for any four consecutive signatures whose nonces satisfy a linear recurrence, the identity holds:

(k 1  − k 0 )(k 3  − k 2 ) = (k 2  − k 1 )²

Substituting k i (d) = c i,0  + c i,1 d into this identity and expanding the brackets, we obtain a quadratic equation with respect to  d :

A d² + B d + C = 0 (mod n)

The coefficients are calculated using the formulas:

CoefficientFormula
Ab 0 b 2  − b 1 ²
Ba 0 b 2  + a 2 b 0  − 2a 1 b 1
Ca 0 a 2  − a 1 ²

where (a i , b i ) are the coefficients of the differences of successive nonces: diff i  = k i+1  − k i  = a i  + b i d[reference:9].

Critical:  The private key  d  is one of the roots of this quadratic equation. To find  d  , simply apply the Berlekamp–Rabin algorithm to find the roots of the polynomial f(x) = Ax² + Bx + C over the field 𝔽 n , where  n  is the order of the base point of the secp256k1 curve.

4.3 Generalization to higher-degree polynomials

If nonces are related by a recurrence of degree  D :

k i+1  = a 0  + a 1 k i  + a 2 k i ² + … + a D−1 k i D−1

then to restore the key  N  ≥  D  + 3 signatures are required, and the degree of the polynomial in  d  grows as:

deg = 1 + Σ i=1 N−3  i

For example, for quadratic recurrence ( N  = 5) the degree of the polynomial is 4, for cubic recurrence ( N  = 6) it is 7, etc.[reference:10].

5. Practical implementation

5.1. Complete attack algorithm

  1. Data collection:  Obtain a set of signatures ( r i ,  s i ) and corresponding message hashes  z i  from the Bitcoin blockchain generated by the same private key.
  2. Check condition:  Make sure that the number of signatures  N  ≥ 4 (for linear recurrence) and they are ordered by time.
  3. Calculating the coefficients:  For each signature, calculate c i,0  = z i s i −1  and c i,1  = r i s i −1  modulo  n .
  4. Polynomial construction:  Form a polynomial f(x) = Ax² + Bx + C (or higher degree) with coefficients calculated using the formulas in Section 4.2.
  5. Root finding:  Apply the Berlekamp–Rabin algorithm to find all roots  of f  in the field 𝔽 n .
  6. Verification:  For each found root  d cand  , verify that the public key  Q  =  d cand G  matches the known public key. The found root is the desired private key.

5.2. Python code example

# Recovering an ECDSA private key by searching for the roots of a polynomial
# using the Berlekamp-Rabin algorithm

import hashlib
from sympy import symbols, Poly, GF, gcd

# secp256k1 curve parameters
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

def berlekamp_rabin_roots(poly_coeffs, p):
    """
    Finds the roots of a polynomial over a finite field GF(p)
    poly_coeffs: list of coefficients [c0, c1, c2, ...] for c0 + c1*x + c2*x^2 + ...
    """
    x = symbols('x')
    f = sum(coeff * x**i for i, coeff in enumerate(poly_coeffs))
    f = Poly(f, x, domain=GF(p))
    
    # Reduce to a square-free form
    f_sqfree = f.sqfreepart()
    
    roots = []
    stack = [f_sqfree]
    
    while stack:
        g = stack.pop()
        if g.degree() == 1:
            root = -g.coeffs()[1] * pow(g.coeffs()[0], -1, p) % p
            roots.append(root)
            continue
        
        # Random shift sigma
        sigma = 1
        while True:
            h = Poly((x + sigma)**((p - 1) // 2) - 1, x, domain=GF(p))
            d = gcd(g, h)
            if d.degree() > 0 and d.degree() < g.degree():
                break
            sigma += 1
        
        stack.append(d)
        stack.append(g // d)
    
    return roots

def recover_private_key(signatures, public_key_point):
    """
    Recovering a private key from a set of signatures,
    nonce which satisfy linear recurrence.
    signatures: list of tuples (r, s, z)
    """
    if len(signatures) < 4:
        raise ValueError("At least 4 signatures required")
    
    # Calculate c_{i,0} and c_{i,1}
    c = []
    for r, s, z in signatures:
        s_inv = pow(s, -1, n)
        c.append((z * s_inv % n, r * s_inv % n))
    
    # Calculate the differences
    a = []
    b = []
    for i in range(3):
        a.append((c[i+1][0] - c[i][0]) % n)
        b.append((c[i+1][1] - c[i][1]) % n)
    
    # Polynomial coefficients A*d^2 + B*d + C
    A = (b[0] * b[2] - b[1]**2) % n
    B = (a[0] * b[2] + a[2] * b[0] - 2 * a[1] * b[1]) % n
    C = (a[0] * a[2] - a[1]**2) % n
    
    # Finding the roots of a polynomial
    candidates = berlekamp_rabin_roots([C, B, A], n)
    
    # Verification: check that d*G == public_key
    for d in candidates:
        if d * G == public_key_point:
            return d
    
    return None
    

5.3 Why it works in practice

In 2023, researchers at Kudelski Security discovered that thousands of signatures exist on the real Bitcoin and Ethereum blockchains where nonces follow polynomial recurrence relations. This is due to bugs in random number generator implementations, the use of linear congruential generators (LCGs) or counters as nonce sources [reference:11].

The Polynonce attack requires only 4 signatures for the linear case and runs in a fraction of a second on a typical computer, since finding the roots of a quadratic equation over a finite field is a trivial task[reference:12].

6. Advanced Scenarios

6.1 Biased Nonce Attack

Another class of attacks, described in the paper “Biased Nonce Sense,” uses lattice-based methods to solve the hidden number problem (HNP) when nonces have missing entropy—for example, the high-order bits of the nonce are known or equal to zero [reference:13]. The Berlekamp-Rabin algorithm can be used in combination with these methods to finally extract the key from the constructed polynomial.

6.2. Attack with a known relationship between nonces

If the affine relation k₂ = a k₁ + b between two nonces is known, then the system of ECDSA equations becomes solvable. In the general case, when the nonces are related by a polynomial relation of degree  D , constructing a polynomial in  d  and finding its roots using the Berlekamp–Rabin method yields a complete solution to the key recovery problem [reference:14].

7. Countermeasures

Critical for developers:  ECDSA signatures are extremely sensitive to the quality of nonce generation. Any predictability or correlation between nonces in different signatures can lead to complete compromise of the private key.

7.1. Recommendations

  • Use deterministic nonce generation  according to RFC 6979. This standard defines nonce as  k  = HMAC_DRBG(private key, message hash), which eliminates any correlation between signatures.
  • Never use LCGs or counters  as a nonce source. Linear congruential generators create precisely the polynomial relationships exploited by the Polynonce attack.
  • Check ECDSA implementations  for nonce reuse or statistical dependencies between successive signatures.
  • Use hardware random number generators (TRNGs)  with cryptographically strong post-processors to generate nonces.

7.2. Implications for blockchain

If a private key is compromised through a nonce attack, all funds associated with the corresponding address can be immediately stolen. Since blockchain transactions are irreversible, recovering stolen funds is virtually impossible without the attacker’s consent.

8. Conclusion

The Berlekamp–Rabin algorithm is a powerful cryptanalytic tool that allows for efficient root finding of polynomials over finite fields. In the context of Bitcoin’s ECDSA, this algorithm finds direct application in attacks exploiting polynomial relationships between nonces in multiple signatures.

Key findings:

1.  If nonces in  N  ≥ 4 ECDSA signatures are related by a polynomial recurrence of degree  D  ≤  N  − 3, then one can construct a polynomial in the private key  d whose degree increases with  D .

2.  The roots of this polynomial are found by the Berlekamp-Rabin algorithm in polynomial time.

3.  The desired private key is guaranteed to be present among the roots, which is then verified using the public key.

4.  The attack requires minimal computational resources (fractions of a second for the linear case) and poses a real threat to cryptocurrency wallets with poor nonce generation implementation.

This material demonstrates the deep connection between classical algebraic theory (finite fields, factorization of polynomials) and practical cryptography, emphasizing the importance of correct implementation of cryptographic primitives in real systems.