### Initialize Sharks and Manage Shares Source: https://context7.com/public/blahaj/llms.txt Demonstrates the basic workflow of creating a Sharks instance, splitting a secret into shares, and reconstructing it. ```rust use blahaj::{Sharks, Share}; // Create a Sharks instance with threshold of 3 (need at least 3 shares to recover) let sharks = Sharks(3); // The secret to be split (can be any byte slice of arbitrary length) let secret = b"my secret password"; // Generate shares using the dealer (requires std feature) let dealer = sharks.dealer(secret); // Collect 5 shares (you can generate up to 255 shares) let shares: Vec = dealer.take(5).collect(); // Later: recover the secret using any 3 or more shares let recovered = sharks.recover(&shares[0..3]).unwrap(); assert_eq!(recovered, secret.to_vec()); // Using only 2 shares fails (below threshold) let result = sharks.recover(&shares[0..2]); assert!(result.is_err()); ``` -------------------------------- ### Add Blahaj Dependency for No-std Environment Source: https://github.com/public/blahaj/blob/master/README.md If your environment does not support `std`, use this configuration in your Cargo.toml to disable default features. ```toml [dependencies] blahaj = { version = "0.6", default-features = false } ``` -------------------------------- ### Serialize and Deserialize Shares in Rust Source: https://context7.com/public/blahaj/llms.txt Demonstrates converting a `Share` struct to and from byte arrays. The first byte represents the x-coordinate, and the remaining bytes contain the share data. Ensure shares are at least 2 bytes long to avoid errors. ```rust use blahaj::{Sharks, Share}; use core::convert::TryFrom; let sharks = Sharks(3); let secret = b"serialize me"; // Generate shares let shares: Vec = sharks.dealer(secret).take(3).collect(); // Convert share to bytes for storage/transmission let share_bytes: Vec = Vec::from(&shares[0]); println!("Share as bytes: {:?}", share_bytes); // First byte is x-coordinate, rest is the share data // Convert bytes back to Share let restored_share = Share::try_from(share_bytes.as_slice()).unwrap(); // Store shares as hex strings let hex_shares: Vec = shares.iter() .map(|s| Vec::from(s).iter().map(|b| format!("{:02x}", b)).collect()) .collect(); // Restore from hex strings let restored_shares: Vec = hex_shares.iter() .map(|hex| { let bytes: Vec = (0..hex.len()) .step_by(2) .map(|i| u8::from_str_radix(&hex[i..i+2], 16).unwrap()) .collect(); Share::try_from(bytes.as_slice()).unwrap() }) .collect(); // Recover from restored shares let recovered = sharks.recover(&restored_shares).unwrap(); assert_eq!(recovered, secret.to_vec()); // Error handling: shares must be at least 2 bytes let invalid = Share::try_from(&[0u8][..]); assert!(invalid.is_err()); assert_eq!(invalid.unwrap_err(), "A Share must be at least 2 bytes long"); ``` -------------------------------- ### Create Paper Backup Keys with Blahaj in Rust Source: https://context7.com/public/blahaj/llms.txt A function to generate paper backup keys for a master secret using Shamir's Secret Sharing. Shares are encoded as hex strings for easy representation on paper. Requires a specified threshold and total number of shares. ```rust use blahaj::{Sharks, Share}; use core::convert::TryFrom; fn create_paper_backup_keys(master_secret: &[u8], threshold: u8, total_shares: usize) -> Vec { let sharks = Sharks(threshold); let dealer = sharks.dealer(master_secret); // Generate shares and encode as base64-like strings for paper backup dealer.take(total_shares) .map(|share| { let bytes = Vec::from(&share); // Simple hex encoding for paper backup bytes.iter().map(|b| format!("{:02X}", b)).collect() }) .collect() } fn recover_from_paper_keys(paper_keys: &[String], threshold: u8) -> Result, &'static str> { let sharks = Sharks(threshold); // Parse paper keys back to shares let shares: Result, _> = paper_keys.iter() .map(|hex| { let bytes: Vec = (0..hex.len()) .step_by(2) .map(|i| u8::from_str_radix(&hex[i..i+2], 16).unwrap()) .collect(); Share::try_from(bytes.as_slice()) }) .collect(); sharks.recover(&shares?) } // Usage let master_key = b"super-secret-master-key-12345"; let threshold = 3; let total = 5; // Create 5 paper backup keys (any 3 can recover) let paper_keys = create_paper_backup_keys(master_key, threshold, total); // Distribute keys to 5 trusted parties for (i, key) in paper_keys.iter().enumerate() { println!("Key {} for trusted party: {}", i + 1, key); } // Later: recover using any 3 keys let selected_keys = vec![ paper_keys[0].clone(), paper_keys[2].clone(), paper_keys[4].clone(), ]; let recovered = recover_from_paper_keys(&selected_keys, threshold).unwrap(); assert_eq!(recovered, master_key.to_vec()); ``` -------------------------------- ### Add Blahaj Dependency to Cargo.toml Source: https://github.com/public/blahaj/blob/master/README.md Include the Blahaj crate in your project's dependencies by adding this to your Cargo.toml file. ```toml [dependencies] blahaj = "0.6" ``` -------------------------------- ### Reconstruct Secret from Shares Source: https://context7.com/public/blahaj/llms.txt Demonstrates secret recovery and error handling when providing insufficient or duplicate shares. ```rust use blahaj::{Sharks, Share}; let sharks = Sharks(3); let secret = b"recover me"; // Generate shares let shares: Vec = sharks.dealer(secret).take(5).collect(); // Recover with exactly threshold shares (minimum required) let result = sharks.recover(&shares[0..3]); assert!(result.is_ok()); assert_eq!(result.unwrap(), secret.to_vec()); // Recover with more than threshold shares (also works) let result = sharks.recover(&shares); assert!(result.is_ok()); // Recover with fewer than threshold shares (fails) let result = sharks.recover(&shares[0..2]); assert!(result.is_err()); assert_eq!(result.unwrap_err(), "Not enough shares to recover original secret"); // Duplicate shares don't count toward threshold let mut duplicate_shares = shares[0..2].to_vec(); duplicate_shares.push(shares[0].clone()); // duplicate let result = sharks.recover(&duplicate_shares); assert!(result.is_err()); // Still only 2 unique shares ``` -------------------------------- ### Sharks Struct Initialization Source: https://context7.com/public/blahaj/llms.txt The Sharks struct is the primary entry point for secret sharing operations, initialized with a threshold value. ```APIDOC ## Sharks Struct ### Description Initializes the secret sharing configuration with a specific threshold. ### Parameters - **threshold** (usize) - Required - The minimum number of shares required to reconstruct the secret. ``` -------------------------------- ### Generate Shares with Standard RNG Source: https://context7.com/public/blahaj/llms.txt Uses the thread-local random number generator to create shares. Requires the std feature to be enabled. ```rust use blahaj::{Sharks, Share}; // Initialize with threshold of 5 let sharks = Sharks(5); // Secret can be any length - computations are performed on single bytes let secret = b"This is a longer secret message that can be any length!"; // Get a share dealer/iterator let dealer = sharks.dealer(secret); // Generate exactly 10 shares for distribution let shares: Vec = dealer.take(10).collect(); // Each share can be converted to bytes for storage/transmission for (i, share) in shares.iter().enumerate() { let share_bytes: Vec = Vec::from(share); println!("Share {}: {} bytes", i + 1, share_bytes.len()); // Share bytes length = secret length + 1 (for the x coordinate) } ``` -------------------------------- ### Generate Shares with Custom RNG Source: https://context7.com/public/blahaj/llms.txt Enables share generation with a custom random number generator, suitable for no_std environments or deterministic testing. ```rust use blahaj::{Sharks, Share}; use rand_chacha::rand_core::SeedableRng; use rand_chacha::ChaCha8Rng; // Create Sharks with threshold of 3 let sharks = Sharks(3); // Create a seeded RNG for reproducible results (use random seed in production!) let mut rng = ChaCha8Rng::from_seed([0x42; 32]); // Generate shares with custom RNG let secret = b"no_std compatible secret"; let dealer = sharks.dealer_rng(secret, &mut rng); // Collect shares let shares: Vec = dealer.take(5).collect(); // Recovery works the same way let recovered = sharks.recover(&shares).unwrap(); assert_eq!(recovered, secret.to_vec()); // For no_std environments, add to Cargo.toml: // blahaj = { version = "0.6", default-features = false } ``` -------------------------------- ### recover - Reconstruct Secret Source: https://context7.com/public/blahaj/llms.txt Reconstructs the original secret from a collection of shares. ```APIDOC ## recover(shares: &[Share]) ### Description Attempts to reconstruct the secret from a slice of shares. ### Parameters - **shares** (&[Share]) - Required - A slice of shares to use for reconstruction. ### Response - **Result, Error>** - Returns the original secret on success, or an error if insufficient or invalid shares are provided. ``` -------------------------------- ### dealer - Generate Shares Source: https://context7.com/public/blahaj/llms.txt Generates an iterator of shares for a given secret using the thread-local random number generator. ```APIDOC ## dealer(secret: &[u8]) ### Description Creates a dealer iterator to generate shares for the provided secret. ### Parameters - **secret** (&[u8]) - Required - The byte slice representing the secret to be split. ### Response - **Iterator** - An iterator that yields shares. ``` -------------------------------- ### dealer_rng - Generate Shares with Custom RNG Source: https://context7.com/public/blahaj/llms.txt Generates shares using a custom random number generator, suitable for no_std environments. ```APIDOC ## dealer_rng(secret: &[u8], rng: &mut R) ### Description Creates a dealer iterator using a provided random number generator. ### Parameters - **secret** (&[u8]) - Required - The secret to split. - **rng** (&mut R) - Required - A random number generator implementing the RngCore trait. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.