A pure Python implementation of Shamir's Secret Sharing algorithm. This project demonstrates how to securely divide a secret into multiple shares and reconstruct the original secret only when a specified threshold of shares is met.
It's a foundational algorithm for understanding cryptography, data privacy, and building distributed trust systems.
-
Cryptographic Modular Arithmetic: Utilizes finite field arithmetic over a large Mersenne prime (
$2^{107} - 1$ ) to ensure the security and integrity of the operations. -
Performance Optimization: Implements Horner's Method (
_eval_at) for polynomial evaluation, reducing time complexity to$O(n)$ compared to standard exponentiation. -
Lagrange Interpolation: Mathematically guarantees the flawless reconstruction of the original secret from the scattered shares (
reconstruct). -
Zero Dependencies: Built entirely using the Python Standard Library (only requires the
randommodule).
The system consists of two primary functions: make_random_shares for splitting the secret, and reconstruct for merging the shares.
from secret_sharing import make_random_shares, reconstruct
# 1. Define the secret (must be an integer)
secret = 1452
# 2. Split the secret into 6 shares, requiring a minimum of 3 to unlock
distributed_shares = make_random_shares(secret, minimum=3, shares=6)
# 3. Reconstruct the secret using any 3 shares
reconstructed_secret = reconstruct(distributed_shares[:3])
print(f"Recovered Secret: {reconstructed_secret}")