A methodological guide on using the mathematical apparatus of a classical course in higher mathematics to recover a hidden number (private key) in ECDSA signatures on the secp256k1 curve.
The analysis shows that the classical course of higher mathematics, presented in the textbook by Danko, Popov, and Kozhevnikova, contains a complete set of tools necessary for understanding and implementing cryptanalytic attacks on ECDSA:
| Danko course section | Application in ECDSA cryptanalysis |
|---|---|
| Linear algebra (matrices, Gaussian method) | Construction of the HNP lattice matrix, reduction of systems of linear comparisons to canonical form |
| Vector algebra (Gram–Schmidt orthogonalization) | LLL-reduction of lattice basis, calculation of Gram–Schmidt coefficients |
| Number theory (modular arithmetic) | Calculating inverse elements modulo n, solving congruences |
| Probability theory | Nonce bias analysis, estimation of required number of signatures |
| Numerical methods | Implementation of LLL/BKZ algorithms, solution of large-scale systems |
Thus, despite being written in 1986—long before the advent of Bitcoin and the widespread adoption of ECDSA—Danko et al.’s textbook remains a relevant source of mathematical knowledge underlying modern cryptanalysis. The methods of linear algebra, probability theory, and numerical analysis presented therein provide a solid foundation for understanding advanced techniques such as lattice attacks on the hidden number problem.
Practical significance Understanding the relationship between classical higher mathematics and modern cryptography allows us to:
- Develop more robust ECDSA implementations (deterministic nonces, RFC 6979).
- Audit existing cryptographic libraries for nonce-related vulnerabilities.
- Assess the resistance of blockchain transactions to key recovery attacks.
Source: Danko P. E., Popov A. G., Kozhevnikova T. Ya. – “Higher Mathematics in Exercises and Problems”, Moscow: Higher School, 1986, in 2 parts.
Content
- Introduction: Statement of the Problem
- Mathematical foundations of ECDSA
- Hidden Number Problem (HNP)
- Linear algebra apparatus from Danko’s course
- Lattice construction and LLL reduction
- Probability Theory and Nonce Bias Analysis
- Practical example: key recovery
- Conclusion
- Directions for further research
01 Introduction: Statement of the Problem
Cryptanalysis of the Elliptic Curve Digital Signature Algorithm (ECDSA)—one of the most common digital signature schemes used in the Bitcoin protocol—traditionally relies on methods from algebraic number theory, lattice theory, and statistical analysis. However, the fundamental mathematical tools needed to understand and implement attacks are largely contained in classical higher mathematics courses for technical universities.
This paper is devoted to the systematic application of the apparatus presented in the textbook by P. E. Danko, A. G. Popov, and T. Ya. Kozhevnikova, “Higher Mathematics in Exercises and Problems” (Moscow: Vysshaya shkola, 1986, 4th ed., in 2 parts), to the problem of recovering the private key of Bitcoin ECDSA transactions. The textbook covers analytic geometry, linear algebra, differential and integral calculus, probability theory, numerical methods, and elements of the calculus of variations—and it is this set of sections that forms the complete mathematical foundation for understanding modern lattice attacks on ECDSA.
Key Idea: The problem of recovering an ECDSA private key given a known leak of information about the nonce (a one-time random number used for signatures) reduces to the Hidden Number Problem (HNP), which is solved using integer linear algebra methods—specifically, lattice construction and its reduction using the LLL algorithm. The matrix apparatus, Gaussian method, and linear space theory, outlined in Part I of Danko et al.’s textbook, form the basis for constructing such lattices.
In the following sections, we will successively show how each section of the classic course finds direct application in the cryptanalysis of ECDSA, and provide a practical implementation of a nonce-reuse attack and a biased nonce attack.
02 Mathematical Foundations of ECDSA
2.1. Secp256k1 Curve Parameters
Bitcoin uses a standardized elliptic curve secp256k1defined over a finite field , where . The equation of the curve in Weierstrass form is:Fpp = 2256 − 232 − 977
The equation of the curve secp256k1 is y ² = x ³ + 7 (mod p)
The order of a group of points of a curve (the number of points) is denoted by n and for secp256k1 is a prime number:
Group order n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
The generator point G has order n, that is, n·G = O (the point at infinity). The private key d is an integer in the range [1, n−1], and the public key Q = d·G is a point on the curve.
2.2. ECDSA signature generation algorithm
To sign a message with a hash z = H(m) (modulo n), the following algorithm is used:
- A random number is selected
k ∈ [1, n−1]– nonce (one-time key). - A point
R = k·Gon the curve is calculated. - We take , where is the x-coordinate of the point .
r = xR mod nxRR - Calculated .
s = k−1(z + r·d) mod n - Signature – pair
(r, s).
The basic equation of the ECDSA signature is s = k ⁻¹ (z + r d ) (mod n)
Here d is the private key that needs to be recovered. If an attacker obtains information about k (even partially), they can construct a system of equations for d and k.
2.3 Nonce Reuse Vulnerability
If two different messages are signed with the same k, then from two equations one can directly express d:
Key recovery for repeated nonce s₁ = k ⁻¹ (z₁ + r d) s₂ = k ⁻¹ (z₂ + r d) ───────────────────────────────── s₁ − s₂ = k ⁻¹ (z₁ − z₂) k = (z₁ − z₂) · (s₁ − s₂) ⁻¹ (mod n) d = (s₁ k − z₁) · r ⁻¹ (mod n)
This is the simplest case, and it has been observed repeatedly on the Bitcoin blockchain. However, a more realistic scenario is when the nonce is not exactly repeated, but has a bias : for example, the high- or low-order bits k are known or predictable. This is where linear algebra and lattice theory come in.
03 Hidden Number Problem (HNP)
3.1. Statement of the problem
Let n be a prime number (the order of the group), and let be a secret number (the private key ). Given pairs , where and is an approximation to with known error . Find .α ∈ Zndm(ti, ai)ti ∈ Znaiα·ti mod n|α·ti − ai|n ≤ Bα
In the context of ECDSA, α the private key plays a role d, and are related to the signature parameters. If the high-order bits of the nonce are known to be equal to some value (that is , , where is the small error), then the signature equation is transformed into a relation of the form .tikiki = ai + eieid·ti ≈ bi
3.2. Reduction of ECDSA to HNP
From the equation we express :si = ki−1(zi + ri·d)ki
Expressing a nonce through signature parameters k i = s i ⁻¹ (z i + r i ·d) (mod n)
If it is known that it has an offset (for example, the most significant bits are zero or fixed), then we can write:kiℓ
The offset of nonce k i = k i ′ + e i , where 0 ≤ e i < 2 ^(n_bits − ℓ)
Substituting this into the signature equation and multiplying by , we get:si
Approximate equation of HNP s i ·k i ′ ≈ z i + r i ·d (mod n) → r i ·d − s i ·k i ′ ≈ −z i (mod n)
This is a system of linear congruences with small unknowns d and . Such a system is solved using integer linear algebra methods—by constructing a lattice and searching for a short vector in it.ki′
04 Linear algebra apparatus from Danko’s course
4.1 Matrices and systems of linear equations
Part I of Danko, Popov, and Kozhevnikova’s textbook contains a chapter entitled “Fundamentals of Linear Algebra,” which covers matrices, determinants, the Gauss method, matrix rank, linear spaces, and quadratic forms. These tools form the basis for constructing lattices for HNP.
Let’s consider a system of m HNP equations. Each equation has the form:
HNP equation in matrix form r i ·d − s i ·k i ′ + n·q i = −z i
where is an integer multiplier (the quotient of division by modulo). This is a linear system of unknowns . In matrix form:qid, k1′, …, km′, q1, …, qm
Matrix notation A X = Z where: A = [ r₁ −s₁ 0 … 0 n 0 … 0 ] [ r₂ 0 −s₂ … 0 0 n … 0 ] [ … … … … … … … … … ] [ r_m 0 0 … −s_m 0 0 … n ] X = [ d, k₁′, k₂′, …, k_m′, q₁, q₂, …, q_m ] ᵀ Z = [ −z₁, −z₂, …, −z_m ] ᵀ
The matrix A has size m × (2m+1). The rank of the matrix and the methods for reducing it to step-like form (the Gaussian method), described in Danko’s course, allow us to analyze the consistency and structure of solutions to this system. However, a direct solution is impossible due to the presence of small but unknown variables .ki′
4.2 Vector spaces and bases
Danko’s course covers n -dimensional vector spaces, linear dependence of vectors, basis, and dimension in detail. These concepts are directly applicable to lattice theory: a lattice L is a discrete subgroup generated by linearly independent vectors (the basis of the lattice).Rnb1, …, bn
Definition of a lattice A lattice L in is the set of all integer linear combinations of basis vectors:Rn
L = { Σ i=1 ⁿ a i ·b i : a i ∈ Z }
The matrix is called the basis matrix of the lattice. The determinant of the lattice is independent of the choice of basis and is a fundamental characteristic—analogous to the “volume” of the fundamental domain. In Danko’s course, determinants and their properties are studied in the chapter on linear algebra; this knowledge is directly applied in analyzing the quality of lattice bases.B = [b1, …, bn]det(L) = |det(B)|
4.3 Gaussian Method and Matrix Reduction
The Gaussian method, discussed in detail in the manual, is used in ECDSA cryptanalysis at several levels:
- Reducing the HNP system to canonical form. Before constructing the lattice, the system of linear congruences is reduced to a form convenient for calculations.
- Calculating the rank. The rank of a matrix
Adetermines how many independent equations are attackable. If the rank is less thanm, the system may be intractable or have infinitely many solutions. - Matrix inversion modulo. The calculation requires the extended Euclidean algorithm, which is also discussed in the number theory section.
k = (z₁ − z₂)·(s₁ − s₂)−1 mod n
05 Lattice construction and LLL-reduction
5.1 Canonical Lattice for HNP
To solve the HNP system, a lattice of dimension m+1 (or higher, depending on the number of unknowns) is constructed. The classical construction, dating back to Boneh–Venkatesan (1996), is as follows:
The basis matrix of the lattice LB = [ n 0 0 … 0 0 ] [ 0 n 0 … 0 0 ] [ 0 0 n … 0 0 ] [ … … … … … … ] [ 0 0 0 … n 0 ] [ t₁ t₂ t₃ … t_m 1/B ]
where , and is a parameter related to the error bound (the smaller , the more accurate the attack). The vector is a short vector in this lattice (up to errors) and can be found using basis reduction methods.ti = ri · si−1 mod nBB(d·t₁ mod n, …, d·tm mod n, d/B)
5.2. LLL algorithm (Lenstra–Lenstra–Lovász)
The LLL algorithm, developed in 1982, is a polynomial-time lattice basis reduction algorithm. It finds a basis consisting of “nearly orthogonal” vectors and allows for an efficient solution to the Shortest Vector Problem (SVP) in moderate dimensions.
Connection with Danko’s course
The LLL algorithm makes significant use of the Gram–Schmidt orthogonalization process, which is discussed in Danko’s textbook in the section on vector algebra and linear spaces. The Gram–Schmidt coefficients play a key role in the reduction condition:μi,j
|μ i,j | ≤ 1/2 for all j < i
Furthermore, the Lovász condition requires that , where . This condition ensures that the basis vectors do not become too short and that the first basis vector is a good approximation to the shortest lattice vector.||bi*||² ≥ (δ − μi,i−1²)·||bi−1*||²δ = 3/4
5.3. Attack stages
- Signature collection.
mSignatures created with a single private key and nonce with a known offset are collected .(ri, si, zi)d - Calculation of t i . For each signature, is calculated .
ti = ri·si−1 mod n - Construction of matrix B. The basis matrix of the lattice of dimension is formed
(m+1) × (m+1)according to the canonical construction. - LLL reduction. The LLL algorithm is applied to the matrix (if necessary, followed by BKZ processing to improve the quality of the reduction).
- Candidate extraction. A vector is searched in the reduced basis whose last coordinate is a multiple of
1/B, and whose firstmcoordinates are close to integers. This vector contains information aboutd. - Verification. The candidate
dis verified against the public keyQ = d·G.
Limitations of the method: The LLL attack is effective when the nonce offset is large enough (usually ≥ 4 bits out of 256) and the number of signatures is sufficient to create a lattice of dimension in which LLL finds a short vector. For smaller offsets, more signatures are required, which increases the lattice dimension and computational complexity.
06 Probability Theory and Nonce Bias Analysis
6.1. Statistical nature of nonce
Part II of Danko, Popov, and Kozhevnikova’s textbook contains a chapter on “Probability Theory,” which covers random variables, their distributions, mathematical expectation, variance, and limit theorems. These concepts are critical for analyzing nonce quality in ECDSA.
In an ideal implementation, ECDSA k should be uniformly distributed over {1, …, n−1}. However, in practice, due to bugs in pseudorandom number generators (PRNGs) or implementation details (e.g., modular pruning, truncation, side-channel leaks), the distribution k may be biased .
6.2. Modular bias
One common source of bias is the generation k of a random n_bits-bit number followed by modulo conversion n (group order). If n is not a power of two, the distribution k = r mod n becomes uneven:
The probability of a value k under modular reduction is P(k = x) = ⌊(2 ^n_bits − x) / n⌋ / 2 ^n_bits for x ∈ [0, n−1]
This means that values kclose to zero have a slightly higher probability than values close to n−1. The magnitude of the bias can be estimated using probability theory formulas and distribution analysis.
6.3. MSB Bias
A more serious case is when the high-order ℓ bits k are known or predictable. For example, if an implementation generates k a n_bits-bit number, but the high-order bits are always zero due to range limitations, then . In this case, the error is bounded by , and signatures are sufficient for the attack.k < 2n_bits − ℓe = k − k′2n_bits − ℓm ≈ n_bits / ℓ
Relationship with Probability Theory : Estimating the number of signatures required m for a successful attack is based on probabilistic considerations: the probability that a random vector in the lattice will be short enough for LLL to find it depends on the ratio of the vector to the length of the target vector. Probability theory allows us to estimate this probability and choose it so that it is sufficiently high (e.g., > 0.5).det(L)1/(m+1)m
6.4. Statistical Analysis of Bitcoin Transactions
A real attack on the Bitcoin blockchain requires collecting signatures created by the same address (or a cluster of addresses controlled by a single key). Probability theory and mathematical statistics allow us to:
- Assess the correlation between signatures and determine whether they use the same key.
- Test the hypothesis about the presence of bias in
kbased on the observed valuessof andr. - Estimate the number of transactions required to achieve statistical significance of the attack.
07 Practical example: key recovery
7.1 Nonce Reuse Case
Let’s consider the simplest case: two messages are signed with the same k. Let two signatures be given:
Input data Signature 1: r₁, s₁, z₁ Signature 2: r₂ = r₁, s₂, z₂ (the same r means the same k, since r = x(k G) mod n)
Then, as shown in section 2.3, the private key is calculated using the formulas:
Key recovery k = (z₁ − z₂) · (s₁ − s₂) ⁻¹ (mod n) d = (s₁ k − z₁) · r₁ ⁻¹ (mod n)
Python implementation (using a library ecdsa or pure Python for modular arithmetic):
# Восстановление приватного ключа ECDSA при повторном использовании nonce
from ecdsa.curves import SECP256k1
n = SECP256k1.order # порядок группы secp256k1
def inv_mod(a, m):
"""Обратный элемент по модулю m (расширенный алгоритм Евклида)."""
return pow(a, -1, m)
def recover_key_nonce_reuse(r, s1, s2, z1, z2, n):
"""
Восстанавливает приватный ключ d и nonce k
при повторном использовании k в двух подписях.
"""
# k = (z1 - z2) / (s1 - s2) mod n
numerator_k = (z1 - z2) % n
denominator_k = (s1 - s2) % n
k = (numerator_k * inv_mod(denominator_k, n)) % n
# d = (s1 * k - z1) / r mod n
numerator_d = (s1 * k - z1) % n
d = (numerator_d * inv_mod(r, n)) % n
return d, k
# Пример (гипотетические значения)
r = 0x...
7.2. Biased Nonce Attack (LLL)
A more complex, but also more realistic, scenario: nonces have a bias in the high-order bits. For demonstration purposes ecdsa-break, we’ll use a library that implements an LLL attack on HNP.
# Установка: pip install ecdsa-break
# Генерация слабых подписей (для теста)
# gen-weak-sigs > nonces.csv
from ecdsa_break import (
load_csv, make_matrix, reduce_matrix,
privkeys_from_reduced_matrix, display_keys,
)
# Загрузка подписей из CSV
msgs, sigs, pubs = load_csv("nonces.csv", limit=6)
# Построение матрицы решётки (B = 176 — параметр, связанный с размером смещения)
matrix = make_matrix(msgs, sigs, pubs, B=176)
# LLL-редукция (do_bkz=False для скорости; True для лучшего качества)
reduced = reduce_matrix(matrix, do_bkz=False)
# Извлечение кандидатов приватного ключа
keys = privkeys_from_reduced_matrix(msgs, sigs, pubs, reduced)
display_keys(keys)
This parameter B is related to the error size: the smaller the offset, the larger it should be B (and the more signatures are required). In the example above, B = 176 this corresponds to an offset of approximately 256 − 176 = 80 bits, which is quite a large offset.
Important: This code is intended solely for educational purposes and for analyzing your own transactions. Using it to attack someone else’s keys without the owner’s express permission is illegal.
7.3 Numerical example with specific values
To illustrate, we give a simplified example with small numbers (not on the real secp256k1 curve, but on an abstract group of order n = 101).
Example parameters: n = 101 (group order) d = 42 (private key, unknown to the attacker) k = 17 (nonce, used twice) Message 1: z₁ = 55 Message 2: z₂ = 73 r = x(k G) mod n = 29 (same for both signatures) s₁ = k⁻¹(z₁ + r d) = 17⁻¹(55 + 29 42) = 17⁻¹(55 + 1218) = 17⁻¹ 1273 = 17⁻¹ 1273 mod 101 17⁻¹ mod 101 = 6 (since 17 6 = 102 ≡ 1) s₁ = 6 1273 mod 101 = 6 (1273 − 12 101) = 6 61 = 366 ≡ 63 s₂ = k⁻¹(z₂ + r d) = 6 (73 + 1218) = 6 1291 1291 mod 101 = 1291 − 12 101 = 79 s₂ = 6 79 = 474 ≡ 70
The attacker knows: r = 29, s₁ = 63, s₂ = 70, z₁ = 55, z₂ = 73, n = 101. We calculate:
Reconstruction k = (z₁ − z₂) · (s₁ − s₂)⁻¹ mod n = (55 − 73) · (63 − 70)⁻¹ mod 101 = (−18) · (−7)⁻¹ mod 101 = 83 · 7⁻¹ mod 101 7⁻¹ mod 101 = 29 (since 7 · 29 = 203 = 2 · 101 + 1) k = 83 · 29 mod 101 = 2407 mod 101 = 2407 − 23 · 101 = 2407 − 2323 = 84 Check: 84 mod 101 = 84. But the true k = 17. Error? Let’s recalculate. In fact: s₁ − s₂ = 63 − 70 = −7 ≡ 94 (mod 101) (s₁ − s₂)⁻¹ = 94⁻¹ mod 101 94 ≡ −7, (−7)⁻¹ = −(7⁻¹) = −29 ≡ 72 k = (55 − 73) · 72 mod 101 = (−18) · 72 = −1296 mod 101 −1296 mod 101: 1296 = 12 · 101 + 84, so −1296 ≡ −84 ≡ 17 (mod 101) k = 17 ✓ d = (s₁k − z₁) r⁻¹ mod n = (63 17 − 55) 29⁻¹ mod 101 = (1071 − 55) 29⁻¹ = 1016 29⁻¹ mod 101 1016 mod 101 = 1016 − 10·101 = 6 29⁻¹ mod 101: 29·7 = 203 = 2·101 + 1, so 29⁻¹ = 7 d = 6 · 7 = 42 ✓
The private key d = 42 has been successfully recovered. In real-world scenarios, secp256k1 numbers are 256 bits, and a nonce replay attack requires only two signatures.
08 Conclusion
The analysis shows that the classical course of higher mathematics, presented in the textbook by Danko, Popov, and Kozhevnikova, contains a complete set of tools necessary for understanding and implementing cryptanalytic attacks on ECDSA:
| Danko course section | Application in ECDSA cryptanalysis |
|---|---|
| Linear algebra (matrices, Gaussian method) | Construction of the HNP lattice matrix, reduction of systems of linear comparisons to canonical form |
| Vector algebra (Gram–Schmidt orthogonalization) | LLL-reduction of lattice basis, calculation of Gram–Schmidt coefficients |
| Number theory (modular arithmetic) | Calculating inverse elements modulo n, solving congruences |
| Probability theory | Nonce bias analysis, estimation of required number of signatures |
| Numerical methods | Implementation of LLL/BKZ algorithms, solution of large-scale systems |
Thus, despite being written in 1986—long before the advent of Bitcoin and the widespread adoption of ECDSA—Danko et al.’s textbook remains a relevant source of mathematical knowledge underlying modern cryptanalysis. The methods of linear algebra, probability theory, and numerical analysis presented therein provide a solid foundation for understanding advanced techniques such as lattice attacks on the hidden number problem.
Practical significance Understanding the relationship between classical higher mathematics and modern cryptography allows us to:
- Develop more robust ECDSA implementations (deterministic nonces, RFC 6979).
- Audit existing cryptographic libraries for nonce-related vulnerabilities.
- Assess the resistance of blockchain transactions to key recovery attacks.
09 Directions for further research for cryptanalysis based on this area
This work opens several promising directions for further research, combining classical mathematical apparatus with modern cryptanalysis methods:
- An extension of HNP to the case of partial leakage of the least significant bits (LSBs). The classical HNP construction focuses on leakage of the most significant bits of the nonce. Investigating symmetric constructions for the least significant bits requires a modification of the lattice model and an analysis of the properties of modular inversion studied in number theory.
- Lattice dimensionality optimization using quadratic forms. The section of Danko’s course devoted to quadratic forms and their reduction to canonical form can be applied to construct lattices with improved spectral properties, which will reduce the number of labels required.
- Applying Linear Programming Methods to HNP Boundary Analysis. The linear programming elements presented in Part I of the tutorial can be used to accurately estimate the error bounds within which the LLL attack remains effective and to formulate the problem of finding an optimal signature collection strategy.
- Statistical analysis of the Bitcoin blockchain using random process theory. Probability theory and random processes (Markov chains, Poisson processes) can be applied to model nonce behavior in real wallets and identify transaction clusters vulnerable to attack.
- Developing secure nonce generation schemes based on numerical methods. Numerical methods, including Monte Carlo and iterative algorithms, can be used to create nonce generators with provably uniform distributions and resistance to statistical attacks.
- Analysis of ECDSA on curves with other parameters (NIST P-256, Ed25519). Although this work focuses on secp256k1, the mathematical framework is applicable to any elliptic curve. Examining the specific properties of specific curves (e.g., the group order structure) may reveal additional vulnerabilities.
- Quantum-Resistant Cryptanalysis: From Classical Lattices to Post-Quantum Schemes. Studying lattice problems in the context of post-quantum cryptography (LWE, NTRU) will help us understand which classical methods remain effective under quantum computing conditions and what new mathematical tools are required.
Thus, the classical course in higher mathematics is not only not outdated but continues to serve as a starting point for the most modern research in cryptanalysis. The synthesis of fundamental mathematical knowledge with practical problems of information security remains one of the most productive areas of scientific research.
Source: Danko P. E., Popov A. G., Kozhevnikova T. Ya. “Higher Mathematics in Exercises and Problems”: Textbook for students of higher educational institutions. In 2 parts. Parts I-II. – 4th ed., corrected and supplemented. – Moscow: Higher. school, 1986.
Additional materials: Boneh D., Venkatesan R. “Hardness of Computing the Most Significant Bits of Secret Keys in Diffie–Hellman and Related Schemes” (CRYPTO 1996); Nguyen PQ, Shparlinski IE “The Insecurity of the Digital Signature Algorithm with Partially Known Nonces” (J. Cryptology, 2003); Breitner J., Heninger N. “Biased Nonce Sense: Lattice Attacks against Weak ECDSA Signatures in Cryptocurrencies” (FC 2019).
