pub struct Scheme { /* private fields */ }
Expand description
The main struct implementing Shamir’s Secret Sharing scheme
Implementations§
Source§impl Scheme
impl Scheme
Sourcepub fn new(
threshold: usize,
total_shares: usize,
prime_modulus: BigUint,
) -> Result<Self, SssError>
pub fn new( threshold: usize, total_shares: usize, prime_modulus: BigUint, ) -> Result<Self, SssError>
Creates a new Shamir’s Secret Sharing scheme with the specified parameters.
§Arguments
threshold
- Minimum number of shares needed to reconstruct the secret (k)total_shares
- Total number of shares to generate (n)prime_modulus
- Prime number defining the finite field. Must be larger than both the secret and total_shares.
§Returns
Ok(Scheme)
- If parameters are validErr(SssError::InvalidThreshold)
- If threshold is 0 or greater than total_shares
§Example
use num_bigint::BigUint;
use shamir_rs::Scheme;
let prime = BigUint::from(257u32);
let scheme = Scheme::new(3, 5, prime).unwrap();
Sourcepub fn split_secret(&self, secret: &BigUint) -> Vec<Share>
pub fn split_secret(&self, secret: &BigUint) -> Vec<Share>
Splits a secret into n shares where k shares are required to reconstruct.
§Arguments
secret
- The secret to split. Must be less than prime_modulus.
§Returns
A vector of n shares. Each share is a point on a random polynomial of degree k-1 where the constant term is the secret.
§Example
let secret = BigUint::from(123u32);
let shares = scheme.split_secret(&secret);
assert_eq!(shares.len(), 5);
Sourcepub fn reconstruct_secret(&self, shares: &[Share]) -> Result<BigUint, SssError>
pub fn reconstruct_secret(&self, shares: &[Share]) -> Result<BigUint, SssError>
Reconstructs a secret from k or more shares using Lagrange interpolation.
§Arguments
shares
- Slice of shares to use for reconstruction. Must contain at least k shares with unique indices.
§Returns
Ok(BigUint)
- The reconstructed secretErr(SssError::NotEnoughShares)
- If fewer than k shares providedErr(SssError::DuplicateShares)
- If shares contain duplicate indices
§Example
let reconstructed = scheme.reconstruct_secret(&shares[0..3]).unwrap();
assert_eq!(reconstructed, secret);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Scheme
impl RefUnwindSafe for Scheme
impl Send for Scheme
impl Sync for Scheme
impl Unpin for Scheme
impl UnwindSafe for Scheme
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more