Skip to content

Commit

Permalink
Move reconstruct_secret to impl block
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed May 12, 2019
1 parent cb4909d commit 243bcb2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/ss_example.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate secretsharing;

use secretsharing::ss::{reconstruct_secret, Charset, SecretSharing};
use secretsharing::ss::{Charset, SecretSharing};

fn main() {
let threshold = 3;
Expand All @@ -12,6 +12,6 @@ fn main() {
Charset::Alphanumeric, // Charset of secret.
);
let shares = ss.generate_shares(secret).unwrap();
let reconstructed_secret = reconstruct_secret(&shares[0..3].to_vec(), ss).unwrap();
let reconstructed_secret = ss.reconstruct_secret(&shares[0..3].to_vec()).unwrap();
assert_eq!(reconstructed_secret, secret);
}
30 changes: 15 additions & 15 deletions src/ss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,23 @@ impl SecretSharing {
.collect();
shares
}
}

/// Reconstruct secret from shares.
pub fn reconstruct_secret(shares: &[String], ss: SecretSharing) -> Result<String, SSError> {
// Not enough shares to reconstruct secret.
if (shares.len() as u32) < ss.threshold() {
return Err(SSError::InsufficientShares);
/// Reconstruct secret from shares.
pub fn reconstruct_secret(&self, shares: &[String]) -> Result<String, SSError> {
// Not enough shares to reconstruct secret.
if (shares.len() as u32) < self.threshold() {
return Err(SSError::InsufficientShares);
}
// Convert shares to their point representations.
let point_shares: Result<Vec<_>, SSError> = shares
.iter()
.map(|share| utils::share_str_to_point(share.as_str(), self.charset()))
.collect();
// Recover secret_int.
let secret_int = utils::points_to_secret_int(point_shares?, self.prime()?)?;
// Convert secret_int to secret based on charset.
utils::int_to_charset_repr(secret_int, self.charset())
}
// Convert shares to their point representations.
let point_shares: Result<Vec<_>, SSError> = shares
.iter()
.map(|share| utils::share_str_to_point(share.as_str(), ss.charset()))
.collect();
// Recover secret_int.
let secret_int = utils::points_to_secret_int(point_shares?, ss.prime()?)?;
// Convert secret_int to secret based on charset.
utils::int_to_charset_repr(secret_int, ss.charset())
}

/// Possible charsets for secret.
Expand Down

0 comments on commit 243bcb2

Please sign in to comment.