Skip to main content

KeyShares

Struct KeyShares 

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

The minimum number of shares needed for recovery.

§integrity: String

Integrity check: first 4 characters of base58(sha256(secret)).

Implementations§

Source§

impl KeyShares

Source

pub fn new( points: Vec<PointInFiniteField>, threshold: usize, integrity: String, ) -> Self

Creates a new KeyShares instance.

§Arguments
  • points - The share points
  • threshold - The minimum number of shares needed for recovery
  • integrity - The integrity checksum string
Source

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

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

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

Trait Implementations§

Source§

impl Clone for KeyShares

Source§

fn clone(&self) -> KeyShares

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyShares

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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