-
-
Save RandyMcMillan/b9f13fa3680988dd9c927b5c0e9f3864 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import secrets | |
| from statistics import mean | |
| p = 2**256 - 2**32 - 977 | |
| b = 7 # y^2 = x^3 + 7 | |
| def is_quadratic_residue(n): | |
| return n == 0 or pow(n, (p - 1) // 2, p) == 1 | |
| def sqrt_mod_p(n): | |
| if n == 0: | |
| return 0 | |
| return pow(n, (p + 1) // 4, p) # valid since p % 4 == 3 | |
| def embed_payload_as_pubkey(payload: bytes): | |
| assert len(payload) == 31, "Payload must be exactly 31 bytes" | |
| for nonce in range(256): | |
| x_bytes = payload + bytes([nonce]) | |
| x = int.from_bytes(x_bytes, "big") | |
| if x >= p: | |
| continue | |
| rhs = (pow(x, 3, p) + b) % p | |
| if not is_quadratic_residue(rhs): | |
| continue | |
| y = sqrt_mod_p(rhs) | |
| prefix = 0x02 | (y & 1) # 02 if y even, 03 if y odd | |
| return bytes([prefix]) + x_bytes, nonce | |
| raise RuntimeError("Failed to embed chunk after 256 nonces") | |
| def encode_file(filename: str): | |
| with open(filename, "rb") as f: | |
| data = f.read() | |
| # Save metadata (original byte length) | |
| meta_file = filename + ".meta" | |
| with open(meta_file, "w") as f: | |
| f.write(str(len(data))) | |
| # Split into 31-byte chunks | |
| chunks = [data[i:i+31] for i in range(0, len(data), 31)] | |
| output_file = filename + ".pubkeys.txt" | |
| with open(output_file, "w") as f: | |
| for i, chunk in enumerate(chunks): | |
| if len(chunk) < 31: | |
| chunk = chunk.ljust(31, b"\x00") # pad final chunk | |
| pubkey, nonce = embed_payload_as_pubkey(chunk) | |
| f.write(pubkey.hex() + "\n") | |
| print(f"Chunk {i+1}/{len(chunks)} | nonce={nonce}") | |
| print(f"\nEncoded into {len(chunks)} pubkeys") | |
| print(f"Saved pubkeys to: {output_file}") | |
| print(f"Saved metadata to: {meta_file}") | |
| def restore_file(pubkey_file: str, meta_file: str, output_file: str): | |
| # Read original file size | |
| with open(meta_file, "r") as f: | |
| original_size = int(f.read().strip()) | |
| payload_chunks = [] | |
| with open(pubkey_file, "r") as f: | |
| for line in f: | |
| hex_str = line.strip() | |
| if not hex_str: | |
| continue | |
| pubkey = bytes.fromhex(hex_str) | |
| if len(pubkey) != 33: | |
| raise ValueError(f"Invalid pubkey length: {len(pubkey)}") | |
| payload_chunks.append(pubkey[1:-1]) | |
| data = b"".join(payload_chunks)[:original_size] | |
| with open(output_file, "wb") as f: | |
| f.write(data) | |
| print(f"Reconstructed file saved to: {output_file}") | |
| print(f"Original size: {original_size} | Final size: {len(data)}") | |
| def usage(): | |
| print("Usage:") | |
| print(" pubkey_embed.py --encode <file>") | |
| print(" pubkey_embed.py --restore <base_filename>") | |
| print("\nExample:") | |
| print(" pubkey_embed.py --encode dickbutt.jpg") | |
| print(" pubkey_embed.py --restore dickbutt.jpg") | |
| sys.exit(1) | |
| def main(): | |
| if len(sys.argv) < 3: | |
| usage() | |
| mode = sys.argv[1] | |
| if mode == "--encode": | |
| encode_file(sys.argv[2]) | |
| elif mode == "--restore": | |
| base = sys.argv[2] | |
| pubkey_file = base + ".pubkeys.txt" | |
| meta_file = base + ".meta" | |
| output_file = base.replace(".", "-recon.") | |
| restore_file(pubkey_file, meta_file, output_file) | |
| else: | |
| usage() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment