Post
Topic
Board Development & Technical Discussion
Re: Determining the positivity or negativity of a Bitcoin public key
by
bjpark
on 14/11/2024, 09:11:12 UTC
With this program, if only the public key is provided, I can determine with 100% accuracy whether it is positive or negative.




import random
import ecdsa
import binascii

# Public key generation function
def generate_public_key(private_key_int):
    # Convert the private key to a 32-byte format
    private_key_bytes = abs(private_key_int).to_bytes(32, byteorder="big", signed=True)
   
    # Generate the elliptic curve public key (secp256k1)
    sk = ecdsa.SigningKey.from_string(private_key_bytes, curve=ecdsa.SECP256k1)
    vk = sk.verifying_key
   
    # Determine the first byte of the compressed public key based on the private key's sign
    if private_key_int < 0:
        public_key_bytes_compressed = (
            b"\x03" + vk.to_string()[:32] if vk.to_string()[-1] % 2 == 0 else b"\x02" + vk.to_string()[:32]
        )
    else:
        public_key_bytes_compressed = (
            b"\x02" + vk.to_string()[:32] if vk.to_string()[-1] % 2 == 0 else b"\x03" + vk.to_string()[:32]
        )
   
    # Convert the public key to a hexadecimal string
    public_key_compressed = binascii.hexlify(public_key_bytes_compressed).decode()
   
    return public_key_compressed

# List to store (pma, pmb) pairs
pma_pmb_pairs = []

# Loop 10 times to generate and print public keys for pma and pmb
for i in range(20):
    # Set range and generate pma, pmb
    a = random.randint(2**134, 2**135)
    am = 10**37
    b = a - am
    sign = random.choice([1, -1])  # Set both as either positive or negative
    pma = sign * a
    pmb = sign * b

    # Generate public keys using pma and pmb as private keys
    public_key_a_compressed = generate_public_key(pma)
    public_key_b_compressed = generate_public_key(pmb)
   
    # Store only the signs of pma and pmb
    pma_sign = '+' if pma > 0 else '-'
    pmb_sign = '+' if pmb > 0 else '-'
    pma_pmb_pairs.append((pma_sign, pmb_sign))

    # Print results
    print(f"Problem {i + 1}:")
    print("public_key_a (compressed public key a):", public_key_a_compressed)
    print("public_key_b (compressed public key b):", public_key_b_compressed)
    print()

# List of (pma, pmb) sign pairs (answer key for private reference)
print("List of (pma, pmb) sign pairs:")
for pair in pma_pmb_pairs:
    print(f"({pair[0]}, {pair[1]})")