Recovering the private key of a weak random number generator of the Math.random() function in Bitcoin wallets

02.04.2025

Bitcoin and other cryptocurrencies use cryptographic algorithms to ensure the security of transactions. One of the key components of these systems is a random number generator, which is used to create private keys. However, if the random number generator is weak, it can lead to security vulnerabilities. In this article, we will look at the problem of recovering a private key in Bitcoin wallets when a weak random number generator is used, in particular, the Math.random().

Theoretical basis

ECDSA and Key Generation

The Elliptic Curve Digital Signature Algorithm (ECDSA) is widely used in cryptocurrencies, including Bitcoin. The following process is used to generate ECDSA keys:

  1. Random value x : A random number $$ x $$ is selected from the set of integers modulo $$ p $$, where $$ p $$ is a large prime number.
  2. Public key : The public key is calculated as $$ y = g^x \mod p $$, where $$ g $$ is the group generator.
  3. Private Key : The private key is the value $$ x $$.

Vulnerabilities of a Weak Random Number Generator

If the random number generator is weak, it may lead to predictability or repetition of values ​​$$ x $$. In case of ECDSA, if nonce $$ k $$ is repeated for different messages, an attacker can easily recover the private key using two signatures $$ (r, s_1) $$ and $$ (r, s_2) $$ for messages $$ m_1 $$ and $$ m_2 $$ respectively[2][3].

Problem withMath.random()

A function Math.random()in JavaScript generates pseudo-random numbers, but it is not cryptographically secure. Using such a function to generate private keys can lead to vulnerabilities, since an attacker can try to predict or reconstruct the internal state of the generator if he knows some of the generated values[6].

Mathematical Incorrectness of Private Keys

If private keys are generated using a weak random number generator, they may be vulnerable to recovery. In the case of Bitcoin wallets, if an attacker can predict or recover a private key, they gain complete control over the wallet.

Recovering a private key

To recover a private key in case of a weak random number generator, you can use the following methods:

  1. Repetition Analysis : If a nonce $$ k $$ is repeated, two signatures can be used to recover the private key.
  2. Side-channel attacks : If an attacker can obtain information about the internal state of the generator, he can predict future values.
  3. Lattice attacks : New techniques such as lattice attacks can be used to recover private keys from weak transactions[2].

Conclusion

Using weak random number generators, such as Math.random(), to create private keys in Bitcoin wallets poses a significant security risk. Recovering the private key in such cases can be accomplished using various cryptographic attacks. Therefore, it is extremely important to use cryptographically secure random number generators to ensure the security of cryptocurrency transactions.

Recommendations

  • Using cryptographically secure generators : Always use generators designed for cryptographic purposes.
  • Regular Updates and Audits : Regularly check and update random number generator implementations.
  • User Education : Educate users on the importance of security when creating and storing private keys.

Citations:
[1] https://habr.com/en/articles/671932/comments/
[2] https://habr.com/ru/articles/671932/
[3] https://ru.wikipedia.org/wiki/DSA
[4] https://forum.infostart.ru/forum9/topic125912/
[5] https://2bitcoins.ru/haker-podbiral-privatnye-klyuchi/
[6] https://xakep.ru/2015/07/20/java-random-hack/
[7] https://tangem.com/ru/blog/post/how-to-create-a-private-key-for-a-crypto-wallet-with-and-without-a-seed-phrase/
[8] https://books.ifmo.ru/file/pdf/2474.pdf