| Crates.io | shamir_share |
| lib.rs | shamir_share |
| version | 0.2.1 |
| created_at | 2025-06-26 12:49:00.84718+00 |
| updated_at | 2025-06-30 19:26:10.644964+00 |
| description | A secure and efficient Rust library for Shamir's Secret Sharing |
| homepage | |
| repository | https://github.com/ashim-kr-saha/shamir_share |
| max_upload_size | |
| id | 1727248 |
| size | 324,405 |
A secure and efficient Rust library for Shamir's Secret Sharing, built with a security-first, constant-time design to prevent side-channel attacks. Split sensitive data into multiple shares where only a threshold number is needed for reconstruction.
shamir_share provides a robust implementation of Shamir's Secret Sharing scheme as a Rust library. It allows you to split sensitive data (such as database files) into multiple shares, where the original data can only be reconstructed when a predetermined number of shares are combined.
Dealer for memory efficiencyChaCha20RngAdd this to your Cargo.toml:
[dependencies]
shamir_share = "0.1.0"
use shamir_share::{ShamirShare, FileShareStore, ShareStore};
// Create a scheme with 5 shares and threshold 3
let mut scheme = ShamirShare::builder(5, 3).build().unwrap();
// Split a secret
let secret = b"my secret data";
let shares = scheme.split(secret).unwrap();
// Store shares
let temp_dir = tempfile::tempdir().unwrap();
let mut store = FileShareStore::new(temp_dir.path()).unwrap();
for share in &shares {
store.store_share(share).unwrap();
}
// Reconstruct from 3 shares
let loaded_shares = vec![
store.load_share(1).unwrap(),
store.load_share(2).unwrap(),
store.load_share(3).unwrap(),
];
let reconstructed = ShamirShare::reconstruct(&loaded_shares).unwrap();
assert_eq!(reconstructed, secret);
use shamir_share::ShamirShare;
let mut scheme = ShamirShare::builder(10, 5).build().unwrap();
let secret = b"my secret data";
// Generate only the shares you need
let shares: Vec<_> = scheme.dealer(secret).take(5).collect();
// Or use iterator methods for advanced filtering
let even_shares: Vec<_> = scheme.dealer(secret)
.filter(|share| share.index % 2 == 0)
.take(5)
.collect();
let reconstructed = ShamirShare::reconstruct(&shares).unwrap();
assert_eq!(reconstructed, secret);
use shamir_share::{ShamirShare, FileShareStore, ShareStore};
use std::path::Path;
// Create a new ShamirShare instance with 5 shares, threshold of 3
let mut shamir = ShamirShare::builder(5, 3).build()?;
// Split a secret
let secret = b"This is a secret message";
let shares = shamir.split(secret)?;
// Store shares in a directory
let store = FileShareStore::new(Path::new("/path/to/shares"))?;
for share in &shares {
store.store_share(share)?;
}
// Later, reconstruct the secret using at least 3 shares
let mut reconstruction_shares = Vec::new();
for i in 1..=3 {
reconstruction_shares.push(store.load_share(i)?);
}
let reconstructed = ShamirShare::reconstruct(&reconstruction_shares)?;
assert_eq!(&reconstructed, secret);
This library is designed with security as the primary concern, implementing multiple layers of protection against various attack vectors:
ChaCha20Rng seeded from OsRng for polynomial coefficient generationThis library prioritizes security over raw performance. The constant-time implementations of cryptographic operations protect against side-channel attacks but are slower than lookup table-based approaches. For typical use cases (secrets under a few MB), the performance impact is minimal:
| Operation | 1KB Secret | 10KB Secret | 100KB Secret |
|---|---|---|---|
| Split | ~160 us | ~550 us | ~3.4 ms |
| Reconstruct | ~80 us | ~170 us | ~680 us |
| Total | ~240 us | ~720 us | ~4.1 ms |
For detailed API documentation, please visit docs.rs/shamir_share.
ShamirShare: Main implementation of Shamir's Secret Sharing schemeShamirShareBuilder: Builder pattern for configuring ShamirShare instancesDealer: Lazy iterator for generating shares on-demandShare: Represents a single share of the secretFileShareStore: File-based storage for sharesShareStore: Trait for implementing custom storage backendsConfig: Configuration options for the sharing processSplitMode: Enum for specifying how data is split (Sequential or Parallel)# Run all tests
cargo test
# Run tests with coverage
cargo test --all-features
# Run benchmarks
cargo bench
This library includes comprehensive fuzzing targets to ensure robustness against malformed inputs:
# Install nightly Rust and cargo-fuzz
rustup install nightly
cargo +nightly install cargo-fuzz
# Run fuzzing targets
cd fuzz
cargo +nightly fuzz run fuzz_reconstruct # Test reconstruction logic
cargo +nightly fuzz run fuzz_share_storage # Test share parsing logic
# Or use the convenience script
./run_fuzz.sh all 300 # Run all targets for 5 minutes each
The fuzzing targets test:
# Run streaming benchmarks
./run_streaming_benchmarks.sh
# Compare with baseline
cargo run --release --bin performance_compare
Contributions are welcome! Please see CONTRIBUTING.md for guidelines on how to contribute to this project.
This project is licensed under the MIT License - see the LICENSE file for details.