i have a script to convert compressed keys to uncompressed
import binascii
p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
compressed_key_hex = '0250863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352'
x_hex = compressed_key_hex[2:66]
x = int(x_hex, 16)
prefix = compressed_key_hex[0:2]
y_square = (pow(x, 3, p) + 7) % p
y_square_square_root = pow(y_square, (p+1)/4, p)
if (prefix == "02" and y_square_square_root & 1) or (prefix == "03" and not y_square_square_root & 1):
y = (-y_square_square_root) % p
else:
y = y_square_square_root
computed_y_hex = format(y, '064x')
computed_uncompressed_key = "04" + x_hex + computed_y_hex
print computed_uncompressed_key
but i need script where i can convert uncompressed keys to compressed
i guess that will be pretty simple to make as we need to take x value and add 02 or 03 in front of x. right?
but need working code where i can upload file of uncompressed keys and get all compressed keys :p
edit:
i wrote these code to convert uncompressed to compress but output file is blank.
from fastecdsa import curve
from fastecdsa.point import Point
import bit
G = curve.secp256k1.G
N = curve.secp256k1.q
def pub2point(line, file):
x = int(line[2:66], 16)
if len(line) < 70:
y = bit.format.x_to_y(x, int(line[:2], 16) % 2)
else:
y = int(line[66:], 16)
return Point(x, y, curve=curve.secp256k1)
if (y % 2 == 0):
prefix = "02"
else:
prefix = "03"
hx = hex(x)[2:].zfill(64)
hy = hex(y)[2:].zfill(64)
file.write(prefix+hx+"\n") # Writes compressed key to file
with open("1.txt", "r") as f, open("output.txt", "w") as outf:
line = f.readline().strip()
while line != '':
pub2point(line, outf)
line = f.readline().strip()
i guess something is missing or i am doing it wrong