Post
Topic
Board Development & Technical Discussion
Re: the fastest possible way to mass-generate addresses in Python
by
nomachine
on 27/09/2023, 19:28:01 UTC

I tried using the script you posted but I can't generate millions on an android phone, I changed many things but it returned an error everytime, I'm interested in this one because  it uses no external libraries, and since iceland library doesn't support arm/mobile architecture.

What do I need to change to generate my desired range for public keys?


Maybe everything? Grin


Code:
#!/usr/bin/python3
from hashlib import sha256, new
import binascii
import gmpy2

PCURVE = gmpy2.mpz(2 ** 256 - 2 ** 32 - 2 ** 9 - 2 ** 8 - 2 ** 7 - 2 ** 6 - 2 ** 4 - 1)  # The proven prime
N = gmpy2.mpz("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141")  # Number of points in the field
ACURVE = gmpy2.mpz(0)
BCURVE = gmpy2.mpz(7)  # These two define the elliptic curve. y^2 = x^3 + Acurve * x + Bcurve
Gx = gmpy2.mpz(55066263022277343669578718895168534326250603453777594175500187360389116729240)
Gy = gmpy2.mpz(32670510020758816978083085130507043184471273380659243275938904335757337482424)
GPOINT = (Gx, Gy)  # This is our generator point. Trillions of different ones possible

def modinv(a, n=PCURVE):
    a = gmpy2.mpz(a)
    n = gmpy2.mpz(n)
    # MAXIMO COMUN DIVISOR: Extended Euclidean Algorithm/'division' in elliptic curves
    lm, hm = gmpy2.mpz(1), gmpy2.mpz(0)
    resto = a % n
    high = n
    while resto > gmpy2.mpz(1):
        ratio = high // resto
        nm = hm - lm * ratio
        new = high - resto * ratio
        lm, resto, hm, high = nm, new, lm, resto
    return int(lm % n)

def ECadd(a, b):
    a = (gmpy2.mpz(a[0]), gmpy2.mpz(a[1]))
    b = (gmpy2.mpz(b[0]), gmpy2.mpz(b[1]))
    LamAdd = ((b[1] - a[1]) * modinv(b[0] - a[0], PCURVE)) % PCURVE
    x = (LamAdd * LamAdd - a[0] - b[0]) % PCURVE
    y = (LamAdd * (a[0] - x) - a[1]) % PCURVE
    return int(x), int(y)

def ECdouble(a):
    a = (gmpy2.mpz(a[0]), gmpy2.mpz(a[1]))
    Lam = ((3 * a[0] * a[0] + ACURVE) * modinv((2 * a[1]), PCURVE)) % PCURVE
    x = (Lam * Lam - 2 * a[0]) % PCURVE
    y = (Lam * (a[0] - x) - a[1]) % PCURVE
    return int(x), int(y)

def EccMultiply(gen_point, scalar_hex):
    scalar_hex = gmpy2.mpz(scalar_hex)
    if scalar_hex == 0 or scalar_hex >= N:
        raise Exception("Invalid Scalar/Private Key")
    ScalarBin = str(bin(scalar_hex))[2:]  # string binario sin el comienzo 0b
    Q = gen_point
    for i in range(1, len(ScalarBin)):
        Q = ECdouble(Q)
        if ScalarBin[i] == "1":
            Q = ECadd(Q, gen_point)
    return Q

def private_to_hex_publics(hex_private_key):
    public_key = EccMultiply(GPOINT, hex_private_key)
    public_uncompressed = f"04{hex(public_key[0])[2:].upper()}{hex(public_key[1])[2:].upper()}"

    if public_key[1] % 2 == 1:  # If the Y value for the Public Key is odd.
        public_compressed = ("03" + str(hex(public_key[0])[2:]).zfill(64).upper())
    else:  # Or else, if the Y value is even.
        public_compressed = ("02" + str(hex(public_key[0])[2:]).zfill(64).upper())
    return public_uncompressed, public_compressed

def hash_256_from_hex_string_like_bytes(hexstring):
    return sha256(bytes.fromhex(hexstring)).hexdigest()

def ripemd160_from_hex_string_like_bytes(hexstring):
    return new('ripemd160', bytes.fromhex(hexstring)).hexdigest()

def b58encode(hex_string, expected_length=None):
    v = binascii.unhexlify(hex_string)
    alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
    lev, number = gmpy2.mpz(1), gmpy2.mpz(0)
    for char in reversed(v):
        number += lev * char
        lev = lev << 8  # 2^8
    string = ""
    while number:
        number, modulo = divmod(number, 58)
        string = alphabet[modulo] + string
    if not expected_length:
        return string
    elif len(string) != expected_length:
        raise Exception(f"b58encode: Expected length={expected_length} obtained length={len(string)}")
    else:
        return string

def sha256_get_checksum(hex_string_to_checksum):
    hasha1 = hash_256_from_hex_string_like_bytes(hex_string_to_checksum)
    hasha2 = hash_256_from_hex_string_like_bytes(hasha1)
    return hasha2[:8].upper()

def sha_ripe_digest(hex_string_to_checksum):
    hashc1 = hash_256_from_hex_string_like_bytes(hex_string_to_checksum)
    hashc2 = ripemd160_from_hex_string_like_bytes(hashc1)
    return hashc2.upper()

def wif_from_private(privkey):
    # put 80 for bitcoin and concatenate with privkey
    prepend = "80"
    private_key_str = hex(privkey)[2:].zfill(64)
    prepended = (prepend + private_key_str).upper()
    compressed = (prepend + private_key_str + "01").upper()

    if len(prepended) != 66 or len(compressed) != 68:
        raise Exception("WIF conversion: Wrong prepended or compressed private key, length not 66")

    uncompressed_checksum = sha256_get_checksum(prepended)
    compressed_checksum = sha256_get_checksum(compressed)
    private_key_uncompressed_checksum = prepended + uncompressed_checksum
    private_key_compressed_checksum = compressed + compressed_checksum
    private_key_WIF_uncompressed_Base58 = b58encode(private_key_uncompressed_checksum, 51)
    private_key_WIF_compressed_Base58 = b58encode(private_key_compressed_checksum, 52)

    print("PREPENDED:\t\t\t\t", prepended)
    print("PRIV_UNCOMP+CHECKSUM:\t\t\t", private_key_uncompressed_checksum)
    print("Private_key_WIF_uncompressed_Base58:\t", private_key_WIF_uncompressed_Base58)
    print("PRIV_COMP+CHECKSUM:\t\t\t", private_key_compressed_checksum)
    print("Private_key_WIF_compressed_Base58:\t", private_key_WIF_compressed_Base58)
    return private_key_WIF_uncompressed_Base58, private_key_WIF_compressed_Base58

def hex_public_to_public_addresses(hex_publics):
    uncompressed = hex_publics[0]
    public_key_hashC_uncompressed = "00" + sha_ripe_digest(uncompressed)
    checksum = sha256_get_checksum(public_key_hashC_uncompressed)
    PublicKeyChecksumC = public_key_hashC_uncompressed + checksum
    public_address_uncompressed = "1" + b58encode(PublicKeyChecksumC, 33)
    print("Public address uncompressed:\t", public_address_uncompressed)

    compressed = hex_publics[1]
    PublicKeyVersionHashD = "00" + sha_ripe_digest(compressed)
    compressed_checksum = sha256_get_checksum(PublicKeyVersionHashD)
    PublicKeyChecksumC = PublicKeyVersionHashD + compressed_checksum
    public_address_compressed = "1" + b58encode(PublicKeyChecksumC, 33)
    print("Public address compressed:\t", public_address_compressed)
    return public_address_uncompressed, public_address_compressed

def generate_public_keys_in_range(start_range, end_range):
    current_scalar = start_range

    while current_scalar <= end_range:
        hex_private_key = hex(current_scalar)
        hex_publics = private_to_hex_publics(hex_private_key)
        print(f"Private Key: {hex_private_key}")
        print(f"Uncompressed Public Key: {hex_publics[0]}")
        print(f"Compressed Public Key: {hex_publics[1]}\n")

        current_scalar += 1

if __name__ == "__main__":
    start_range = int("20000000000000000", 16)
    end_range = int("3FFFFFFFFFFFFFFFF", 16)

    generate_public_keys_in_range(start_range, end_range)