From 28b13f448b1329c45b1129705662503daa059ada Mon Sep 17 00:00:00 2001 From: zer0x64 Date: Wed, 22 Jan 2020 08:44:05 -0500 Subject: [PATCH] duplicate share check using hashset --- src/lib.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cf48e9f..fd659bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,7 @@ mod share; use field::GF256; pub use share::Share; +use std::collections::HashSet; /// Tuple struct which implements methods to generate shares and recover secrets over a 256 bits Galois Field. /// Its only parameter is the minimum shares threshold. @@ -81,9 +82,9 @@ impl Sharks { /// // Not enough shares to recover secret /// assert!(secret.is_err()); pub fn recover(&self, shares: &[Share]) -> Result, &str> { - // TODO: Discuss use of slice instead of hashmap here + let shares_x: HashSet = shares.iter().map(|s| s.x.0).collect(); - if shares.len() < self.0 as usize { + if shares_x.len() < self.0 as usize { Err("Not enough shares to recover original secret") } else { Ok(math::interpolate(shares)) @@ -104,6 +105,19 @@ mod tests { assert!(secret.is_err()); } + #[test] + fn test_duplicate_shares_err() { + let sharks = Sharks(255); + let dealer = sharks.dealer(&[1]); + let mut shares: Vec = dealer.take(255).collect(); + shares[1] = Share { + x: shares[0].x, + y: shares[0].y.clone(), + }; + let secret = sharks.recover(&shares); + assert!(secret.is_err()); + } + #[test] fn test_integration_works() { let sharks = Sharks(255);