
Converting a Bitcoin public key to an address can be done using the ecdsa
and library base58
. However, to speed things up, using the Bloom filter algorithm is not practical in this context, as it is used to filter data in the blockchain, not to convert keys. Below is an example script that converts a public key to legacy and SegWit addresses.
Converting a public key to an address
pythonimport ecdsa
import hashlib
import base58
def hash160(public_key):
"""Хэширование открытого ключа с помощью SHA-256 и RIPEMD-160."""
return hashlib.new('ripemd160', hashlib.sha256(public_key).digest()).digest()
def public_key_to_address(public_key, compressed=False, segwit=False):
"""Преобразование открытого ключа в адрес."""
if compressed:
public_key = public_key[1:] # Удаление префикса 04 для сжатого ключа
# Хэширование открытого ключа
h160 = hash160(public_key)
if segwit:
# Для SegWit адресов используется префикс 'bc1'
return 'bc1' + base58.b58encode_check(b'\x00' + h160).decode()
else:
# Для устаревших адресов используется префикс '1'
return '1' + base58.b58encode_check(b'\x00' + h160).decode()
# Пример использования
public_key_uncompressed = b'\x04c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b53f883a9483fb7f2b43f3eacd857c904d1b70ecc168571b64d8f1ab82b57eea88'
public_key_compressed = b'\x02c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b5'
print("Устаревший адрес (несжатый):", public_key_to_address(public_key_uncompressed))
print("Устаревший адрес (сжатый):", public_key_to_address(public_key_compressed, compressed=True))
print("SegWit адрес (несжатый):", public_key_to_address(public_key_uncompressed, segwit=True))
print("SegWit адрес (сжатый):", public_key_to_address(public_key_compressed, compressed=True, segwit=True))
This script converts a public key to legacy and SegWit addresses, both compressed and uncompressed. The prefix is used for SegWit addresses bc1
, and the prefix is used for legacy addresses 1
.
Note : To work with real keys, you must ensure that the public key was derived from a valid private key.
What are the alternatives to the Bloom filter algorithm for converting public keys?
The Bloom Filter algorithm is not used to convert public keys to addresses. It is a probabilistic data structure designed to quickly check whether an element exists in a data set, but not to transform data. Cryptographic algorithms such as ECDSA (Elliptic Curve Digital Signature Algorithm) and hashing with SHA-256 and RIPEMD-160 are used to convert Bitcoin public keys to addresses.
If you’re looking for alternatives to optimize your work with datasets that might be useful in other contexts, there are a few options:
- Cuckoo Filter : This is an advanced data structure compared to Bloom Filter that allows elements to be removed and has a lower probability of false positives 1 .
- Inverse Bloom Filter : Used to store information about the presence of elements in a set, but with reverse logic.
- Hash tables : Offer a deterministic way to store and search for items, but can be less memory and speed efficient than Bloom filters for very large data sets 2 .
There are no alternative algorithms for converting Bitcoin public keys into addresses other than using the cryptographic functions described above.
Example of using a hash table to speed up searches
Although hash tables are not used directly to transform keys, they can be useful for quickly looking up already transformed addresses:
pythonimport hashlib
import base58
# Пример хэш-таблицы для быстрого поиска адресов
class HashTable:
def __init__(self):
self.table = {}
def add(self, public_key, address):
self.table[public_key] = address
def get(self, public_key):
return self.table.get(public_key)
# Пример использования
hash_table = HashTable()
# Преобразование открытого ключа в адрес (аналогично предыдущему примеру)
def public_key_to_address(public_key):
# Реализация преобразования открытого ключа в адрес
h160 = hashlib.new('ripemd160', hashlib.sha256(public_key).digest()).digest()
return '1' + base58.b58encode_check(b'\x00' + h160).decode()
public_key = b'\x04c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b53f883a9483fb7f2b43f3eacd857c904d1b70ecc168571b64d8f1ab82b57eea88'
address = public_key_to_address(public_key)
# Добавление в хэш-таблицу
hash_table.add(public_key, address)
# Быстрый поиск адреса по открытому ключу
print(hash_table.get(public_key))
This example shows how a hash table can be used to quickly look up addresses that have already been converted from public keys.
What parameters affect the efficiency of Bloom filter
The effectiveness of a Bloom filter depends on several key parameters:
- Filter Size (m) : This is the number of bits allocated for the filter. The larger the filter size, the less likely it is to generate false positives, but more memory is required to store it 6 7 .
- Number of hash functions (k) : This is the number of hash functions that are used to set the bits in the filter. Increasing the number of hash functions reduces the probability of false positives, but increases the processing time 3 4 .
- Number of elements (n) : This is the number of unique elements that will be added to the filter. Knowing this number, we can optimize the filter size and the number of hash functions to achieve the desired false positive probability of 1 7 .
- False Positive Probability (p) : This is a measure of the accuracy of the filter. The lower the false positive probability, the more accurate the filter, but it may require more memory and computational resources 1 5 .
- Quality of hash functions : Hash functions should provide a uniform distribution of bits to minimize false positives 4 .
Bloom Filter Optimization Formulas
To optimize the Bloom filter, you can use the following formulas:
- Optimal number of bits (m) : m=−nln(p)(ln(2))2m = -\frac{n \ln(p)}{(\ln(2))^2}m=−(ln(2))2nln(p)
- Optimal number of hash functions (k) : k=mnln(2)k = \frac{m}{n} \ln(2)k=nmln(2)
These formulas allow us to calculate the optimal parameters of the Bloom filter based on the desired false positive rate and the number of elements 3 4 .
What types of hash functions are best to use in a Bloom filter to minimize false positives
To minimize false positives in a Bloom filter, it is important to use hash functions that provide a uniform distribution of bits in the bit mask. While there is no single “best” type of hash function for all cases, there are some guidelines:
- Cryptographic hash functions : Although not always necessary for a Bloom filter, they provide a good distribution and can be used if security is a priority. Examples include SHA-256 or MD5 7 .
- Non-cryptographic hash functions : These are faster and are often used in Bloom filters. Examples include MurmurHash or FNV Hash . These functions are optimized for speed and uniform distribution.
- Hash functions with good variance : These are functions that distribute output values evenly across their range. MurmurHash and CityHash are popular choices.
Basic requirements for hash functions for Bloom filter
- Uniform distribution : Hash functions should generate output values that are uniformly distributed across the entire range to minimize collisions.
- Independence : Hash functions should be independent of each other to reduce the chance of simultaneous collisions.
- Speed : Hash functions must be fast so as not to slow down the Bloom filter.
Example of using MurmurHash in Python
pythonimport mmh3
def murmur_hash(data, seed):
return mmh3.hash(data, seed)
# Пример использования
data = "example_data"
seeds = [1, 2, 3] # Используйте разные семена для разных хэш-функций
hash_values = [murmur_hash(data, seed) for seed in seeds]
print(hash_values)
This example shows how to use MurmurHash with different seeds to generate multiple hash values for the same input, which is useful for Bloom filters.
Citations:
- https://2012.nscf.ru/Tesis/Vasilev.pdf
- https://neerc.ifmo.ru/wiki/index.php?title=%D0%A4%D0%B8%D0%BB%D1%8C%D1%82%D1%80_%D0%91%D0%BB%D1%83%D0%BC%D0%B0
- https://habr.com/ru/companies/otus/articles/541378/
- https://bigdataschool.ru/blog/bloom-filter-for-parquet-files-in-spark-apps.html
- https://ru.hexlet.io/blog/posts/filtr-bluma-zachem-nuzhen-i-kak-rabotaet
- https://otus.ru/nest/post/972/
- https://kesh.kz/blog/%D0%A4%D0%B8%D0%BB%D1%8C%D1%82%D1%80-%D0%91%D0%BB%D1%83%D0%BC%D0%B0-%D0%BD%D0%B0-java/
- https://www.gate.io/ru/learn/articles/what-is—bloom-filter-in-blockchain/809
- https://learn.microsoft.com/ru-ru/azure/databricks/sql/language-manual/delta-create-bloomfilter-index
- https://2012.nscf.ru/Tesis/Vasilev.pdf
- https://habr.com/ru/companies/timeweb/articles/806383/
- https://habr.com/ru/articles/788772/
- https://clickhouse.com/docs/ru/optimize/skipping-indexes
- https://neerc.ifmo.ru/wiki/index.php?title=%D0%A4%D0%B8%D0%BB%D1%8C%D1%82%D1%80_%D0%91%D0%BB%D1%83%D0%BC%D0%B0
- https://www.gate.io/ru/learn/articles/what-is—bloom-filter-in-blockchain/809
- https://ru.hexlet.io/blog/posts/filtr-bluma-zachem-nuzhen-i-kak-rabotaet
- https://habr.com/ru/articles/788772/
- https://habr.com/ru/articles/491132/
- https://www.k0d.cc/storage/books/CRYPTO/%D0%91%D1%80%D1%8E%D1%81%20%D0%A8%D0%BD%D0%B0%D0%B9%D0%B5%D1%80%20-%20%D0%9F%D1%80%D0 %B8%D0%BA%D0%BB%D0%B0%D0%B4%D0%BD%D0%B0%D1%8F%20%D0%BA%D1%80%D0 %B8%D0%BF%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F/7.PDF
- https://joschi.gitlab.io/telegram-clickhouse-archive/clickhouse_ru/2021-04_2.html
- https://bitnovosti.io/2025/01/18/python-bitcoin-crypto/
- https://habr.com/ru/articles/525638/
- https://dzen.ru/a/YsR8D0FFcSzXhY9v
- https://ru.stackoverflow.com/questions/64496/%D0%A8%D0%B8%D1%84%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8 %D0%B5-%D1%81-%D0%BE%D1%82%D0%BA%D1%80%D1%8B%D1%82%D1%8B%D0%BC-%D0%BA%D0%BB%D1%8E%D1%87%D0%BE%D0%BC
- https://habr.com/ru/articles/564256/
- https://bitnovosti.io/2020/07/05/blokchejn-glossarij-terminov/
- https://miningclub.info/threads/keyhunter-py-poisk-privatkey-bitcion-do-2012-na-otformatirovannyx-diskax.31532/
- https://ru.stackoverflow.com/questions/1475317/bitcoin-%D0%B0%D0%B4%D1%80%D0%B5%D1%81%D0%B0-%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%82
Telegram: https://t.me/cryptodeeptech
Video: https://youtu.be/i9KYih_ffr8
Video tutorial: https://dzen.ru/video/watch/6784be61b09e46422395c236
Source: https://cryptodeeptech.ru/discrete-logarithm
Useful information for enthusiasts:
- [1] YouTube Channel CryptoDeepTech
- [2] Telegram Channel CryptoDeepTech
- [3] GitHub Repositories CryptoDeepTools
- [4] Telegram: ExploitDarlenePRO
- [5] YouTube Channel ExploitDarlenePRO
- [6] GitHub Repositories Keyhunters
- [7] Telegram: Bitcoin ChatGPT
- [8] YouTube Channel BitcoinChatGPT
- [9] Bitcoin Core Wallet Vulnerability
- [10] BTC PAYS DOCKEYHUNT
- [11] DOCKEYHUNT
- [12] Telegram: DocKeyHunt
- [13] ExploitDarlenePRO.com
- [14] DUST ATTACK
- [15] Vulnerable Bitcoin Wallets
- [16] ATTACKSAFE SOFTWARE
- [17] LATTICE ATTACK
- [18] RangeNonce
- [19] BitcoinWhosWho
- [20] Bitcoin Wallet by Coinbin
- [21] POLYNONCE ATTACK
- [22] Cold Wallet Vulnerability
- [23] Trezor Hardware Wallet Vulnerability
- [24] Exodus Wallet Vulnerability
- [25] BITCOIN DOCKEYHUNT