This program generates public keys from randomly created private keys using elliptic curve cryptography, specifically the secp256k1 curve used in Bitcoin. The private keys (pma and pmb) are set with the same sign, either both positive or both negative. Based on the sign of each private key, the program outputs corresponding compressed and uncompressed public keys. The compressed public keys’ prefixes (0x02 or 0x03) are adjusted according to whether the private key is positive or negative.
There is a program that determines whether the private key is positive or negative based solely on the generated public key output.
program
import random
import ecdsa
import binascii
# Define the range
a = random.randint(2**134, 2**135)
am = 10**37
b = a - am
# Set pma and pmb: both values should have the same sign
sign = random.choice([1, -1]) # Set both as either positive or negative
pma = sign * a
pmb = sign * b
# Function to generate a public key from a private key
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
# Create uncompressed public key format
public_key_bytes_uncompressed = b"\x04" + vk.to_string()
# Determine the compressed public key's prefix 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 keys to hexadecimal strings
public_key_uncompressed = binascii.hexlify(public_key_bytes_uncompressed).decode()
public_key_compressed = binascii.hexlify(public_key_bytes_compressed).decode()
return public_key_uncompressed, public_key_compressed
# Generate public keys using pma and pmb as private keys
public_key_a_uncompressed, public_key_a_compressed = generate_public_key(pma)
public_key_b_uncompressed, public_key_b_compressed = generate_public_key(pmb)
# Output the results
print("a:", a)
print("b:", b)
print("am:", am)
print("pma (Private Key a):", pma)
print("pmb (Private Key b):", pmb)
print("public_key_a (Uncompressed Public Key a):", public_key_a_uncompressed)
print("public_key_a (Compressed Public Key a):", public_key_a_compressed)
print("public_key_b (Uncompressed Public Key b):", public_key_b_uncompressed)
print("public_key_b (Compressed Public Key b):", public_key_b_compressed)
When |pma| > |pmb|, the positivity or negativity can be determined with 100% accuracy, but when |pma| < |pmb|, errors occur in determining positivity or negativity. I am looking for someone to collaborate on this issue.