Post
Topic
Board Development & Technical Discussion
Re: I created smaller secp256k1 just for testing
by
Geshma
on 15/03/2025, 13:44:53 UTC
from ecdsa.ellipticcurve import Point
from ecdsa.curves import SECP256k1

# Secp256k1 parameters
curve = SECP256k1.curve
p = curve.p()
n = SECP256k1.order
G = SECP256k1.generator

# Endomorphism constants
beta = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee
lmbda = 0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72
beta2 = (beta * beta) % p
lmbda2 = (lmbda * lmbda) % n

def endomorphism(P, beta_val):
    """Apply x-coordinate endomorphism with given beta"""
    return Point(curve, (beta_val * P.x()) % p, P.y(), order=n)

def negate_point(P):
    """Negate point by flipping y-coordinate"""
    return Point(curve, P.x(), (-P.y()) % p, order=n)

def format_key(P):
    """Format point as uncompressed public key"""
    return f'04{P.x():064x}{P.y():064x}'

# Input private key
k = 5

# Base point
P = k * G

# Generate all 6 keys
keys = [
    # Original y group
    (P, k),
    (endomorphism(P, beta), (k * lmbda) % n),
    (endomorphism(P, beta2), (k * lmbda2) % n),
   
    # Negated y group
    (negate_point(P), (n - k) % n),
    (negate_point(endomorphism(P, beta)), (n - (k * lmbda)) % n),
    (negate_point(endomorphism(P, beta2)), (n - (k * lmbda2)) % n)
]

# Print results
print(f"Uncompressed public key: {format_key(P)}\n")

print("Three x values:")
print(f"x1 = {P.x()}")
print(f"x2 = {endomorphism(P, beta).x()}")
print(f"x3 = {endomorphism(P, beta2).x()}\n")

print("Two y values:")
print(f"y1 = {P.y()}")
print(f"y2 = {negate_point(P).y()}\n")

print("Six public keys with private keys:")
for i, (point, priv) in enumerate(keys, 1):
    print(f"Public key {i}: {format_key(point)} [Private key: {priv}]")
    print(f"  Validated: {point == priv * G}")

sum_y1 = sum(keys[1] for i in range(3))
sum_y2 = sum(keys[1] for i in range(3,6))

print(f"\nSum for y1: {sum_y1} (n = {n})")
print(f"Sum for y2: {sum_y2} (2n = {2*n})")