diff --git a/examples/ss_example.rs b/examples/ss_example.rs index 6e3f5e8..af07063 100644 --- a/examples/ss_example.rs +++ b/examples/ss_example.rs @@ -1,6 +1,6 @@ extern crate secretsharing; -use secretsharing::ss::{reconstruct_secret, Charset, SecretSharing}; +use secretsharing::ss::{Charset, SecretSharing}; fn main() { let threshold = 3; @@ -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); } diff --git a/src/ss.rs b/src/ss.rs index f1e3e78..262881e 100644 --- a/src/ss.rs +++ b/src/ss.rs @@ -78,23 +78,23 @@ impl SecretSharing { .collect(); shares } -} -/// Reconstruct secret from shares. -pub fn reconstruct_secret(shares: &[String], ss: SecretSharing) -> Result { - // 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 { + // 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, 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, 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.