# ============================================================
# SECP256K1 REVERSIBLE DECODER
# P = k^{-1} · C → bytes
# ============================================================
# Curve parameters (secp256k1)
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
# Base point G (not needed for decoding, but included for completeness)
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
# ============================================================
# POINT STRUCTURE
# ============================================================
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
# ============================================================
# MODULAR ARITHMETIC
# ============================================================
def mod_inv(a, m):
"""Modular inverse using Fermat's little theorem."""
return pow(a, m - 2, m)
# ============================================================
# POINT ADDITION
# ============================================================
def point_add(P, Q):
if P is None:
return Q
if Q is None:
return P
if P.x == Q.x and P.y != Q.y:
return None
if P.x == Q.x and P.y == Q.y:
# Point doubling
m = (3 * P.x * P.x) * mod_inv(2 * P.y, p) % p
else:
# Point addition
m = (Q.y - P.y) * mod_inv(Q.x - P.x, p) % p
rx = (m * m - P.x - Q.x) % p
ry = (m * (P.x - rx) - P.y) % p
return Point(rx, ry)
# ============================================================
# SCALAR MULTIPLICATION
# ============================================================
def scalar_mult(k, P):
R = None
A = P
while k > 0:
if k & 1:
R = point_add(R, A)
A = point_add(A, A)
k >>= 1
return R
# ============================================================
# INTEGER → BYTES
# ============================================================
def int_to_bytes(x):
"""Convert integer back to bytes (binary block)."""
length = (x.bit_length() + 7) // 8
return x.to_bytes(length, "big")
# ============================================================
# DECODE A SINGLE ECC BLOCK
# ============================================================
def decode_block(Cx, Cy, k):
"""
Input:
Cx, Cy = encoded point coordinates
k = scalar used during encoding
Output:
raw bytes of the original block
"""
C = Point(Cx, Cy)
k_inv = mod_inv(k, n)
P = scalar_mult(k_inv, C)
return int_to_bytes(P.x)
# ============================================================
# DECODE A FULL BINARY FILE (multiple blocks)
# ============================================================
def decode_file(blocks, k):
"""
blocks = list of (Cx, Cy) tuples
k = scalar used during encoding
Returns:
full binary file as bytes
"""
output = b""
for Cx, Cy in blocks:
output += decode_block(Cx, Cy, k)
return output
# ============================================================
# EXAMPLE USAGE
# ============================================================
if __name__ == "__main__":
# Example encoded block (replace with real values)
Cx = 12345678901234567890
Cy = 98765432109876543210
k = 123456789
decoded = decode_block(Cx, Cy, k)
print("Decoded bytes:", decoded)













No comments:
Post a Comment