pub struct KeyShares {
pub points: Vec<PointInFiniteField>,
pub threshold: usize,
pub integrity: String,
}Expand description
A collection of key shares that can be used to recover a private key.
Contains the share points, the threshold needed for recovery, and an integrity checksum to verify successful recovery.
Fields§
§points: Vec<PointInFiniteField>The share points (x, y coordinates in the finite field).
threshold: usizeThe minimum number of shares needed for recovery.
integrity: StringIntegrity check: first 4 characters of base58(sha256(secret)).
Implementations§
Sourcepub fn new(
points: Vec<PointInFiniteField>,
threshold: usize,
integrity: String,
) -> Self
pub fn new( points: Vec<PointInFiniteField>, threshold: usize, integrity: String, ) -> Self
Creates a new KeyShares instance.
§Arguments
points- The share pointsthreshold- The minimum number of shares needed for recoveryintegrity- The integrity checksum string
Sourcepub fn from_backup_format(shares: &[String]) -> Result<Self>
pub fn from_backup_format(shares: &[String]) -> Result<Self>
Parses key shares from backup format strings.
Each share string must be in the format: base58(x).base58(y).threshold.integrity
§Arguments
shares- The backup format strings
§Returns
The parsed KeyShares, or an error if parsing fails or shares are inconsistent
§Errors
Returns an error if:
- Any share string has an invalid format
- Shares have different thresholds
- Shares have different integrity values
§Example
use bsv_rs::primitives::bsv::shamir::KeyShares;
let backup = vec![
"2.someY.3.abcd".to_string(),
"3.otherY.3.abcd".to_string(),
"4.anotherY.3.abcd".to_string(),
];
// Note: This example would fail because "someY" etc. aren't valid base58
// In practice, use actual share strings from split_private_key()Sourcepub fn to_backup_format(&self) -> Vec<String>
pub fn to_backup_format(&self) -> Vec<String>
Converts the key shares to backup format strings.
Each share is serialized as: base58(x).base58(y).threshold.integrity
§Returns
A vector of backup format strings, one per share
§Example
use bsv_rs::primitives::bsv::shamir::split_private_key;
use bsv_rs::primitives::ec::PrivateKey;
let key = PrivateKey::random();
let shares = split_private_key(&key, 2, 3).unwrap();
let backup = shares.to_backup_format();
// Each string can be stored separately
for (i, share_str) in backup.iter().enumerate() {
println!("Share {}: {}", i + 1, share_str);
}Sourcepub fn recover_private_key(&self) -> Result<PrivateKey>
pub fn recover_private_key(&self) -> Result<PrivateKey>
Recovers the private key from the shares using Lagrange interpolation.
The secret is the y-intercept (value at x=0) of the polynomial that passes through all the share points.
§Returns
The recovered private key, or an error if:
- There are fewer shares than the threshold
- The integrity check fails
- The recovered value is not a valid private key
§Example
use bsv_rs::primitives::bsv::shamir::split_private_key;
use bsv_rs::primitives::ec::PrivateKey;
let key = PrivateKey::random();
let shares = split_private_key(&key, 3, 5).unwrap();
// Recover from exactly 3 shares
let recovered = shares.recover_private_key().unwrap();
assert_eq!(key.to_bytes(), recovered.to_bytes());