pub struct SecretSharing<const POLY: u16>(pub u8);Expand description
Tuple struct which implements methods to generate shares and recover secrets over a 256 bits Galois Field. Its only parameter is the minimum shares threshold.
Usage example:
// Set a minimum threshold of 10 shares
let sss = SecretSharing::<POLY>(10);
// Obtain an iterator over the shares for secret [1, 2, 3, 4]
let dealer = sss.dealer(&[1, 2, 3, 4]);
// Get 10 shares
let shares = dealer.take(10).collect::<Vec<Share<POLY>>>();
// Recover the original secret!
let secret = sss.recover(&shares).unwrap();
assert_eq!(secret, vec![1, 2, 3, 4]);Tuple Fields§
§0: u8Implementations§
Source§impl<const POLY: u16> SecretSharing<POLY>
impl<const POLY: u16> SecretSharing<POLY>
Sourcepub fn dealer_rng<R: Rng>(
&self,
secret: &[u8],
rng: &mut R,
) -> impl Iterator<Item = Share<POLY>>
pub fn dealer_rng<R: Rng>( &self, secret: &[u8], rng: &mut R, ) -> impl Iterator<Item = Share<POLY>>
This method is useful when std is not available. For typical usage
see the dealer method.
Given a secret byte slice, returns an Iterator along new shares.
The maximum number of shares that can be generated is 256.
A random number generator has to be provided.
Example:
// Obtain an iterator over the shares for secret [1, 2]
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]);
let dealer = sss.dealer_rng::<ChaCha8Rng>(&[1, 2], &mut rng);
// Get 3 shares
let shares = dealer.take(3).collect::<Vec<Share<POLY>>>();Sourcepub fn dealer(&self, secret: &[u8]) -> impl Iterator<Item = Share<POLY>>
pub fn dealer(&self, secret: &[u8]) -> impl Iterator<Item = Share<POLY>>
Given a secret byte slice, returns an Iterator along new shares.
The maximum number of shares that can be generated is 256.
Example:
// Obtain an iterator over the shares for secret [1, 2]
let dealer = sss.dealer(&[1, 2]);
// Get 3 shares
let shares = dealer.take(3).collect::<Vec<Share<POLY>>>();Sourcepub fn recover<'a, T>(&self, shares: T) -> Result<Vec<u8>, &str>
pub fn recover<'a, T>(&self, shares: T) -> Result<Vec<u8>, &str>
Given an iterable collection of shares, recovers the original secret.
If the number of distinct shares is less than the minimum threshold an Err is returned,
otherwise an Ok containing the secret.
Example:
// Recover original secret from shares
let mut secret = sss.recover(&shares);
// Secret correctly recovered
assert!(secret.is_ok());
// Remove shares for demonstration purposes
shares.clear();
secret = sss.recover(&shares);
// Not enough shares to recover secret
assert!(secret.is_err());Given an iterable collection of shares, recovers the original secret.
If the number of distinct shares is less than the minimum threshold an Err is returned,
otherwise an Ok containing the desired number of shares.
Example:
// Recover original shares from original shares up to threshold shares
let recovered_shares = sss.recover_shares(
[Some(&shares[0]), None, Some(&shares[2])],
3,
);
// Shares correctly recovered
assert!(recovered_shares.is_ok());
let recovered_shares = recovered_shares.unwrap();
assert_eq!(recovered_shares.len(), 3);
// Remove shares for demonstration purposes
let recovered_shares = sss.recover_shares([Some(&shares[0]), None, None], 3);
// Not enough shares to recover shares
assert!(recovered_shares.is_err());