Skip to content

Instantly share code, notes, and snippets.

@Goatghosts
Last active October 22, 2024 18:45
Show Gist options
  • Save Goatghosts/0c4aa8dd6f0ce1a61f484b655b6664ac to your computer and use it in GitHub Desktop.
Save Goatghosts/0c4aa8dd6f0ce1a61f484b655b6664ac to your computer and use it in GitHub Desktop.
Я попросил chatGPT написать код для получения публичного ключа из приватного ключа. Она написала. Вот и все, в принципе.
# Параметры кривой secp256k1
p = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
a = 0x0000000000000000000000000000000000000000000000000000000000000000
b = 0x0000000000000000000000000000000000000000000000000000000000000007
Gx = int(0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798)
Gy = int(0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)
n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141
def is_on_secp256k1(x, y):
return (y**2 - x**3 - a*x - b) % p == 0
def find_y(x):
y_squared = (x ** 3 + a * x + b) % p
y1 = pow(y_squared, (p + 1) // 4, p)
if (y1 ** 2) % p != y_squared:
raise ValueError("No y value for this x on this curve")
y2 = p - y1
return y1, y2
def int_to_hex(a):
return int.to_bytes(private_key, 32, 'big').hex()
# Определение функции умножения скаляра на точку
def scalar_mult(scalar, x, y):
# Алгоритм двоичного разложения
bits = bin(scalar)[2:]
for bit in bits[1:]:
x, y = double_point(x, y)
if bit == '1':
x, y = add_points(x, y, Gx, Gy)
return (x, y)
# Определение функции удвоения точки
def double_point(x, y):
s = ((3 * x * x + a) * pow(2 * y, p-2, p)) % p
x3 = (s * s - 2 * x) % p
y3 = (s * (x - x3) - y) % p
return (x3, y3)
def point_subtract(x1, y1, x2, y2):
# вычисление разности двух точек на кривой
return add_points(x1, y1, x2, -y2 % p)
# Определение функции сложения точек
def add_points(x1, y1, x2, y2):
s = ((y2 - y1) * pow(x2 - x1, p-2, p)) % p
x3 = (s * s - x1 - x2) % p
y3 = (s * (x1 - x3) - y1) % p
return (x3, y3)
if __name__ == '__main__':
private_key = 2131231523432432
public_key = scalar_mult(private_key, Gx, Gy)
uncompressed_key = (b'\x04' + public_key[0].to_bytes(32, 'big') + public_key[1].to_bytes(32, 'big')).hex()
print("Private key (DEC):", private_key)
print("Private key (HEX):", int_to_hex(private_key).upper())
print("Uncompressed public key:", uncompressed_key.upper())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment