A compact implementation of Shamir's Secret Sharing for splitting a 32-byte symmetric key that encrypts a file. The code shows the full path from finite-field math to practical key handling and file encryption.
- Shamir secret sharing over a prime field with BigInt arithmetic and Lagrange interpolation
- AEAD file encryption using a randomly generated 32-byte key
- Parameterized n, t, and p with safe defaults for experimentation
- End-to-end flow: encrypt, split, reconstruct, decrypt
- src/main.rs - orchestration and demo entry points
- src/utils/key_generation.rs - RNG key generation
- src/utils/key_ops.rs - Shamir split/reconstruct, modular arithmetic
- src/utils/file_operations.rs - encryption/decryption and shard IO
Let the secret be s in a prime field F_p. Choose random coefficients a_1, ..., a_(t-1) and define:
A shard is
The implementation uses extended Euclid for modular inversion and BigInt for safe arithmetic across the field.
There is no CLI yet. The demo flow is invoked from src/main.rs.
- Choose an input file path and update the
encrypt_filecall - Run:
cargo run- Use the generated shards plus the stored prime to reconstruct the key and decrypt
Example flow (edit main, then run):
encrypt_file("path/to/input.txt".to_string(), None, None, None)?;
decrypt_file("path/to/encrypted_output".to_string(), "path/to/shards_dir")?;- This is a research/demo implementation and is not audited for production use
- Output naming and shard directory layout are fixed in code
- (Being Worked On) Polynomial coefficients are sampled from 128-bit randomness and reduced mod p, which is not uniform when p is large
- Not verifiable secret sharing yet; corrupted shards are not detected
- Test cases
num-bigintfor finite-field arithmeticrandfor randomnessaes-gcmfor AEAD file encryption