### Shamir's Secret Sharing Usage Examples Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Demonstrates basic secret sharing and recovery in both standard and no_std environments. ```rust use blahaj::{ Sharks, Share }; // Set a minimum threshold of 10 shares let sharks = Sharks(10); // Obtain an iterator over the shares for secret [1, 2, 3, 4] # #[cfg(feature = "std")] # { let dealer = sharks.dealer(&[1, 2, 3, 4]); // Get 10 shares let shares: Vec = dealer.take(10).collect(); // Recover the original secret! let secret = sharks.recover(shares.as_slice()).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); # } ``` ```rust use blahaj::{ Sharks, Share }; use rand_chacha::rand_core::SeedableRng; // Set a minimum threshold of 10 shares let sharks = Sharks(10); // Obtain an iterator over the shares for secret [1, 2, 3, 4] let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let dealer = sharks.dealer_rng(&[1, 2, 3, 4], &mut rng); // Get 10 shares let shares: Vec = dealer.take(10).collect(); // Recover the original secret! let secret = sharks.recover(shares.as_slice()).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); ``` -------------------------------- ### Generate and Recover Secret (std) Source: https://docs.rs/blahaj/0.6.0/blahaj Use this example when the standard library is available. It shows how to set a minimum threshold, generate shares for a secret, and then recover the original secret from those shares. ```rust use blahaj::{ Sharks, Share }; // Set a minimum threshold of 10 shares let sharks = Sharks(10); // Obtain an iterator over the shares for secret [1, 2, 3, 4] let dealer = sharks.dealer(&[1, 2, 3, 4]); // Get 10 shares let shares: Vec = dealer.take(10).collect(); // Recover the original secret! let secret = sharks.recover(shares.as_slice()).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); ``` -------------------------------- ### Usage example for Sharks struct Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Sharks.html Demonstrates how to use the Sharks struct to set a threshold, generate shares for a secret, and recover the original secret from those shares. ```rust // Set a minimum threshold of 10 shares let sharks = Sharks(10); // Obtain an iterator over the shares for secret [1, 2, 3, 4] let dealer = sharks.dealer(&[1, 2, 3, 4]); // Get 10 shares let shares: Vec = dealer.take(10).collect(); // Recover the original secret! let secret = sharks.recover(shares.as_slice()).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); ``` -------------------------------- ### Generate and Recover Secret (no std) Source: https://docs.rs/blahaj/0.6.0/blahaj This example is for environments where the standard library is not available. It requires a random number generator, like `rand_chacha`, to be seeded and provided for share generation. ```rust use blahaj::{ Sharks, Share }; use rand_chacha::rand_core::SeedableRng; // Set a minimum threshold of 10 shares let sharks = Sharks(10); // Obtain an iterator over the shares for secret [1, 2, 3, 4] let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let dealer = sharks.dealer_rng(&[1, 2, 3, 4], &mut rng); // Get 10 shares let shares: Vec = dealer.take(10).collect(); // Recover the original secret! let secret = sharks.recover(shares.as_slice()).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); ``` -------------------------------- ### Get TypeId of self Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Sharks.html Retrieves the `TypeId` of the current instance. This is a standard method for trait objects. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Math Module Unit Tests Source: https://docs.rs/blahaj/0.6.0/src/blahaj/math.rs.html Verification tests for polynomial generation, evaluation, and secret interpolation logic. ```rust #[cfg(test)] mod tests { use super::{get_evaluator, interpolate, random_polynomial, Share, GF256}; use alloc::{vec, vec::Vec}; use rand_chacha::rand_core::SeedableRng; #[test] fn random_polynomial_works() { let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let poly = random_polynomial(GF256(1), 3, &mut rng); assert_eq!(poly.len(), 3); assert_eq!(poly[2], GF256(1)); } #[test] fn evaluator_works() { let iter = get_evaluator(vec![vec![GF256(3), GF256(2), GF256(5)]]); let values: Vec<_> = iter.take(2).map(|s| (s.x.clone(), s.y.clone())).collect(); assert_eq!( values, vec![(GF256(1), vec![GF256(4)]), (GF256(2), vec![GF256(13)])] ); } #[test] fn interpolate_works() { let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let poly = random_polynomial(GF256(185), 10, &mut rng); let iter = get_evaluator(vec![poly]); let shares: Vec = iter.take(10).collect(); let root = interpolate(&shares); assert_eq!(root, vec![185]); } } ``` -------------------------------- ### Usage of Share for secret distribution and recovery Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Share.html Demonstrates generating shares from a dealer and recovering a secret from serialized share bytes. ```rust use blahaj::{Sharks, Share}; use core::convert::TryFrom; // Transmit the share bytes to a printer let sharks = Sharks(3); let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let dealer = sharks.dealer_rng(&[1, 2, 3], &mut rng); // Get 5 shares and print paper keys for s in dealer.take(5) { send_to_printer(Vec::from(&s)); }; // Get share bytes from an external source and recover secret let shares_bytes: Vec> = ask_shares(); let shares: Vec = shares_bytes.iter().map(|s| Share::try_from(s.as_slice()).unwrap()).collect(); let secret = sharks.recover(&shares).unwrap(); ``` -------------------------------- ### Test Vec to Share Conversion Source: https://docs.rs/blahaj/0.6.0/src/blahaj/share.rs.html Unit test for the `TryFrom<&[u8]> for Share` implementation, verifying that a byte slice is correctly parsed into a Share instance. ```rust #[test] fn share_from_u8_slice_works() { let bytes = [1, 2, 3]; let share = Share::try_from(&bytes[..]).unwrap(); assert_eq!(share.x, GF256(1)); assert_eq!(share.y, vec![GF256(2), GF256(3)]); } ``` -------------------------------- ### Share Serialization and Conversion Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Share.html Methods for converting Share instances to byte vectors and reconstructing them from byte slices. ```APIDOC ## Serialization and Conversion ### Description Methods to convert a Share instance into a byte vector or reconstruct a Share from a byte slice. ### Request Body - **Share** (struct) - The share object containing x and y coordinates. ### Response #### Success Response (200) - **Vec** (byte vector) - Serialized representation of the share. - **Share** (struct) - Reconstructed share object from bytes. ### Request Example // Converting Share to bytes let share_bytes: Vec = Vec::from(&share); // Reconstructing Share from bytes let share = Share::try_from(bytes.as_slice()).unwrap(); ``` -------------------------------- ### Test Share to Vec Conversion Source: https://docs.rs/blahaj/0.6.0/src/blahaj/share.rs.html Unit test for the `From<&Share> for Vec` implementation, ensuring that a Share is correctly converted into its byte representation. ```rust #[test] fn vec_from_share_works() { let share = Share { x: GF256(1), y: vec![GF256(2), GF256(3)], }; let bytes = Vec::from(&share); assert_eq!(bytes, vec![1, 2, 3]); } ``` -------------------------------- ### Shamir's Secret Sharing Math Functions Source: https://docs.rs/blahaj/0.6.0/src/blahaj/math.rs.html Core functions for interpolating secrets, generating random polynomials, and evaluating polynomials for share creation. ```rust pub fn interpolate(shares: &[Share]) -> Vec { (0..shares[0].y.len()) .map(|s| { shares .iter() .map(|s_i| { shares .iter() .filter(|s_j| s_j.x != s_i.x) .map(|s_j| s_j.x.clone() / (s_j.x.clone() - s_i.x.clone())) .product::() * s_i.y[s].clone() }) .sum::() .0 }) .collect() } pub fn random_polynomial(s: GF256, k: u8, rng: &mut R) -> Vec { let k = k as usize; let mut poly = Vec::with_capacity(k); let between = Uniform::new_inclusive(0, 255); for _ in 1..k { poly.push(GF256(between.sample(rng))); } poly.push(s); poly } pub fn get_evaluator(polys: Vec>) -> impl Iterator { (1..=u8::MAX).map(GF256).map(move |x| Share { x: x.clone(), y: polys .iter() .map(|p| { p.iter() .fold(GF256(0), |acc, c| acc * x.clone() + c.clone()) }) .collect(), }) } ``` -------------------------------- ### Generating Shares with dealer Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Generates shares using the thread-local random number generator. Requires the std feature. ```rust # use blahaj::{ Sharks, Share }; # let sharks = Sharks(3); // Obtain an iterator over the shares for secret [1, 2] let dealer = sharks.dealer(&[1, 2]); // Get 3 shares let shares: Vec = dealer.take(3).collect(); ``` -------------------------------- ### Test secret recovery scenarios Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Unit tests verifying error conditions for insufficient or duplicate shares, and successful integration recovery. ```rust #[test] fn test_insufficient_shares_err() { let sharks = Sharks(255); let shares: Vec = sharks.make_shares(&[1]).take(254).collect(); let secret = sharks.recover(&shares); assert!(secret.is_err()); } #[test] fn test_duplicate_shares_err() { let sharks = Sharks(255); let mut shares: Vec = sharks.make_shares(&[1]).take(255).collect(); shares[1] = Share { x: shares[0].x.clone(), y: shares[0].y.clone(), }; let secret = sharks.recover(&shares); assert!(secret.is_err()); } #[test] fn test_integration_works() { let sharks = Sharks(255); let shares: Vec = sharks.make_shares(&[1, 2, 3, 4]).take(255).collect(); let secret = sharks.recover(&shares).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); } ``` -------------------------------- ### Test GF256 Product Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Confirms the product calculation for a list of GF256 values. ```rust let values = vec![GF256(1), GF256(1), GF256(4)]; assert_eq!(values.into_iter().product::().0, 4); ``` -------------------------------- ### Galois Field 256 Pre-calculated Tables Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Logarithmic and exponential lookup tables used for field arithmetic. These constants are essential for performing multiplication and division within the field. ```rust const LOG_TABLE: [u8; 256] = [ 0x00, 0x00, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71, 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6, 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88, 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 0xf1, 0xd2, 0x13, 0x5c,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, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, 0x82, ]; ``` -------------------------------- ### Convert Share to Vec Source: https://docs.rs/blahaj/0.6.0/src/blahaj/share.rs.html Implements the From<&Share> trait to convert a Share instance into a byte vector. This is useful for serializing shares for storage or transmission. ```rust impl From<&Share> for Vec { fn from(s: &Share) -> Vec { let mut bytes = Vec::with_capacity(s.y.len() + 1); bytes.push(s.x.0); bytes.extend(s.y.iter().map(|p| p.0)); bytes } } ``` -------------------------------- ### GF256 Product Implementation Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Implements the `Product` trait for GF256, allowing calculation of the product of an iterator of GF256 elements using the defined multiplication operation. ```rust impl Product for GF256 { fn product>(iter: I) -> Self { iter.fold(Self(1), |acc, x| acc * x) } } ``` -------------------------------- ### GF256 Addition Test Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Tests the addition implementation for GF256 by comparing results against a precomputed answer table. Ensures correctness of the XOR-based addition. ```rust #[test] fn add_works() { let answers: [u8; 256] = [ 1, 2, 5, 17, 18, 18, 90, 70, 30, 229, 71, 6, 214, 239, 212, 109, 72, 252, 205, 84, 128, 248, 5, 72, 147, 194, 111, 244, 208, 56, 44, 177, 152, 173, 43, 179, 196, 110, 155, 20, 95, 71, 59, 173, 30, 211, 29, 102, 91, 57, 199, 119, 126, 15, 169, 25, 148, 32, 96, 170, 244, 139, 172, 7, 89, 1, 234, 160, 255, 242, 110, 65, 135, 82, 172, 188, 14, 173, 90, 120, 203, 55, 71, 117, 228, 64, 106, 194, 15, 51, 204, 255, 216, 142, 55, 162, 199, 237, 245, 37, 210, 106, 58, 230, 102, 32, 28, 60, 42, 56, 221, 243, 75, 65, 165, 227, 242, 248, 190, 184, 117, 162, 9, 105, 228, 192, 193, 155, 130, 103, 238, 171, 52, 237, 185, 164, 40, 212, 255, 175, 181, 208, 212, 76, 75, 232, 3, 94, 116, 28, 225, 214, 88, 214, 171, 171, 199, 245, 62, 93, 209, 238, 110, 56, 83, 45, 240, 179, 108, 98, 64, 1, 167, 10, 79, 158, 17, 141, 120, 224, 130, 27, 63, 90, 17, 11, 87, 143, 226, 58, 239, 227, 157, 52, 113, 188, 127, 246, 163, 120, 216, 47, 57, 12, 162, 171, 60, 80, 61, 3, 98, 224, 80, 111, 172, 69, 56, 251, 173, 231, 23, 137, 180, 83, 217, 125, 23, 32, 161, 211, 84, 164, 252, 6, 237, 0, 177, 254, 39, 193, 99, 246, 101, 148, 28, 14, 98, 107, 111, 224, 152, 50, 5, 23, 214, 174, ]; for (i, a) in answers.iter().enumerate() { assert_eq!((GF256(LOG_TABLE[i]) + GF256(EXP_TABLE[i])).0, *a); } } ``` -------------------------------- ### Return the argument unchanged Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Sharks.html A simple conversion function that returns the input value as is. This is part of the `From` trait implementation. ```rust fn from(t: T) -> T ``` -------------------------------- ### dealer Method Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Sharks.html Generates shares for a given secret. The maximum number of shares that can be generated is 256. ```APIDOC ## pub fn dealer(&self, secret: &[u8]) -> impl Iterator ### Description Given a `secret` byte slice, returns an `Iterator` along new shares. The maximum number of shares that can be generated is 256. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust // Obtain an iterator over the shares for secret [1, 2] let dealer = sharks.dealer(&[1, 2]); // Get 3 shares let shares: Vec = dealer.take(3).collect(); ``` ### Response #### Success Response (200) An iterator that yields `Share` items. #### Response Example (Iterator output, not a direct JSON response) ``` -------------------------------- ### Convert Vec to Share Source: https://docs.rs/blahaj/0.6.0/src/blahaj/share.rs.html Implements the TryFrom<&[u8]> trait to convert a byte slice into a Share instance. This is used for deserializing shares received from external sources. Returns an error if the byte slice is too short. ```rust impl core::convert::TryFrom<&[u8]> for Share { type Error = &'static str; fn try_from(s: &[u8]) -> Result { if s.len() < 2 { Err("A Share must be at least 2 bytes long") } else { let x = GF256(s[0]); let y = s[1..].iter().map(|p| GF256(*p)).collect(); Ok(Share { x, y }) } } } ``` -------------------------------- ### Generating Shares with dealer_rng Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Generates shares using a provided random number generator, suitable for no_std environments. ```rust # use blahaj::{ Sharks, Share }; # use rand_chacha::rand_core::SeedableRng; # let sharks = Sharks(3); // Obtain an iterator over the shares for secret [1, 2] let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); let dealer = sharks.dealer_rng(&[1, 2], &mut rng); // Get 3 shares let shares: Vec = dealer.take(3).collect(); ``` -------------------------------- ### GF256 Multiplication Implementation Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Implements the `Mul` trait for GF256. Multiplication uses precomputed logarithm and exponentiation tables for efficiency. Handles multiplication by zero. ```rust impl Mul for GF256 { type Output = Self; fn mul(self, other: Self) -> Self::Output { let log_x = LOG_TABLE[self.0 as usize] as usize; let log_y = LOG_TABLE[other.0 as usize] as usize; if self.0 == 0 || other.0 == 0 { Self(0) } else { Self(EXP_TABLE[log_x + log_y]) } } } ``` -------------------------------- ### GF256 Addition Implementation Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Implements the `Add` trait for GF256. Addition in GF(256) is equivalent to the bitwise XOR operation. ```rust impl Add for GF256 { type Output = GF256; fn add(self, other: Self) -> Self::Output { Self(self.0 ^ other.0) } } ``` -------------------------------- ### Generate shares for a secret Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Sharks.html Generates shares for a given secret. The maximum number of shares that can be generated is 256. This is a typical usage scenario for share generation. ```rust // Obtain an iterator over the shares for secret [1, 2] let dealer = sharks.dealer(&[1, 2]); // Get 3 shares let shares: Vec = dealer.take(3).collect(); ``` -------------------------------- ### Sharks Struct Usage Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Basic usage of the Sharks struct to generate and recover shares. ```rust # use blahaj::{ Sharks, Share }; // Set a minimum threshold of 10 shares let sharks = Sharks(10); // Obtain an iterator over the shares for secret [1, 2, 3, 4] # #[cfg(feature = "std")] # { let dealer = sharks.dealer(&[1, 2, 3, 4]); // Get 10 shares let shares: Vec = dealer.take(10).collect(); // Recover the original secret! let secret = sharks.recover(shares.as_slice()).unwrap(); assert_eq!(secret, vec![1, 2, 3, 4]); # } ``` -------------------------------- ### Recovering Secrets from Shares Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Recovers the original secret from a collection of shares. ```rust # use blahaj::{ Sharks, Share }; # use rand_chacha::rand_core::SeedableRng; # let sharks = Sharks(3); # let mut rng = rand_chacha::ChaCha8Rng::from_seed([0x90; 32]); # let mut shares: Vec = sharks.dealer_rng(&[1], &mut rng).take(3).collect(); // Recover original secret from shares let mut secret = sharks.recover(&shares); // Secret correctly recovered ``` -------------------------------- ### GF256 Division Implementation Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Implements the `Div` trait for GF256. Division uses precomputed logarithm and exponentiation tables. Handles division by zero by returning zero. ```rust impl Div for GF256 { type Output = Self; fn div(self, other: Self) -> Self::Output { let log_x = LOG_TABLE[self.0 as usize] as usize; let log_y = LOG_TABLE[other.0 as usize] as usize; if self.0 == 0 { Self(0) } else { Self(EXP_TABLE[log_x + 255 - log_y]) } } } ``` -------------------------------- ### recover(shares) Source: https://docs.rs/blahaj/0.6.0/src/blahaj/lib.rs.html Attempts to reconstruct the original secret from a provided collection of shares. It validates that all shares have consistent lengths and that a sufficient number of unique shares are provided. ```APIDOC ## recover(shares) ### Description Reconstructs the original secret from a set of shares. The method verifies that the number of unique shares meets the threshold defined by the Sharks instance. ### Parameters #### Request Body - **shares** (IntoIterator) - Required - An iterator of shares to be used for recovery. ### Response #### Success Response (200) - **Result, &str>** - Returns the recovered secret as a byte vector on success. #### Error Response - **Err(&str)** - Returns an error message if shares have inconsistent lengths, or if there are insufficient unique shares to recover the secret. ``` -------------------------------- ### GF256 Multiplication Test Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Tests the multiplication implementation for GF256 by comparing results against a precomputed answer table. Verifies the correctness of the logarithm-based multiplication. ```rust #[test] fn mul_works() { let answers: [u8; 256] = [ 0, 0, 4, 200, 32, 14, 206, 179, 39, 134, 169, 160, 32, 59, 184, 50, 45, 121, 69, 43, 102, 43, 139, 169, 18, 94, 107, 84, 18, 157, 159, 51, 211, 1, 52, 13, 51, 128, 31, 219, 240, 230, 212, 219, 197, 19, 11, 135, 93, 163, 237, 53, 91, 177, 135, 124, 240, 224, 6, 158, 167, 155, 155, 38, 223, 144, 70, 54, 50, 45, 134, 170, 126, 223, 103, 207, 253, 176, 75, 98, 137, 87, 59, 50, 208, 116, 29, 200, 128, 82, 13, 138, 107, 53, 42, 34, 123, 203, 65, 174, 111, 101, 19, 78, 165, 62, 115, 108, 175, 139, 126, 107, 55, 196, 30, 209, 126, 8, 15, 211, 57, 191, 37, 254, 24, 136, 30, 111, 188, 30, 209, 208, 49, 132, 181, 22, 207, 241, 28, 2, 97, 58, 244, 179, 190, 120, 249, 174, 99, 6, 215, 232, ]; for (i, a) in answers.iter().enumerate() { assert_eq!((GF256(LOG_TABLE[i]) * GF256(EXP_TABLE[i])).0, *a); } } ``` -------------------------------- ### Test GF256 Summation Source: https://docs.rs/blahaj/0.6.0/src/blahaj/field.rs.html Verifies the summation of GF256 values, ensuring the result is correct. ```rust let values = vec![GF256(0x53), GF256(0xCA), GF256(0)]; assert_eq!(values.into_iter().sum::().0, 0x99); ``` -------------------------------- ### recover Method Source: https://docs.rs/blahaj/0.6.0/blahaj/struct.Sharks.html Recovers the original secret from a collection of shares. Returns an error if the number of distinct shares is less than the minimum threshold. ```APIDOC ## pub fn recover<'a, T>(&self, shares: T) -> Result, &str> where T: IntoIterator, T::IntoIter: Iterator, ### Description 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. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **shares** (T): An iterable collection of `Share` references. ### Request Example ```rust // Recover original secret from shares let mut secret = sharks.recover(&shares); // Secret correctly recovered assert!(secret.is_ok()); // Remove shares for demonstration purposes shares.clear(); secret = sharks.recover(&shares); // Not enough shares to recover secret assert!(secret.is_err()); ``` ### Response #### Success Response (200) - **Vec** - A vector of bytes representing the recovered secret. #### Error Response (400) - **&str** - An error message indicating insufficient shares. #### Response Example ```json { "secret": [1, 2, 3, 4] } ``` ```json { "error": "Not enough shares to recover secret" } ``` ```