SecretSharing

Struct SecretSharing 

Source
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: u8

Implementations§

Source§

impl<const POLY: u16> SecretSharing<POLY>

Source

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>>>();
Source

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>>>();
Source

pub fn recover<'a, T>(&self, shares: T) -> Result<Vec<u8>, &str>
where T: IntoIterator<Item = &'a Share<POLY>>, T::IntoIter: Iterator<Item = &'a Share<POLY>>,

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());
Source

pub fn recover_shares<'a, T>( &self, shares: T, n: usize, ) -> Result<Vec<Share<POLY>>, &str>
where T: IntoIterator<Item = Option<&'a Share<POLY>>>, T::IntoIter: Iterator<Item = Option<&'a Share<POLY>>>,

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());

Auto Trait Implementations§

§

impl<const POLY: u16> Freeze for SecretSharing<POLY>

§

impl<const POLY: u16> RefUnwindSafe for SecretSharing<POLY>

§

impl<const POLY: u16> Send for SecretSharing<POLY>

§

impl<const POLY: u16> Sync for SecretSharing<POLY>

§

impl<const POLY: u16> Unpin for SecretSharing<POLY>

§

impl<const POLY: u16> UnwindSafe for SecretSharing<POLY>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V