From 391edbd7dab41d408b1ed8294a6b03086f3ad42d Mon Sep 17 00:00:00 2001 From: zer0x64 <17575242+zer0x64@users.noreply.github.com> Date: Thu, 27 Feb 2020 04:52:04 -0500 Subject: [PATCH] Interpolate Now Takes Any IntoIterator (#9) * interpolate now takes an IntoIterator * simplify generic types, update docs, revert interpolate parameter types * update version strings Co-authored-by: Aitor Ruano <45633475+c0dearm@users.noreply.github.com> --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- src/field.rs | 2 +- src/lib.rs | 25 ++++++++++++++++++++----- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dca2df..c08be68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.3.1] - 2020-01-23 +### Changed +- Sharks recover method now accepts any iterable collection + ## [0.3.0] - 2020-01-22 ### Added - Share struct which allows to convert from/to byte vectors diff --git a/Cargo.toml b/Cargo.toml index abe5a4b..11eea96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sharks" -version = "0.3.0" +version = "0.3.1" authors = ["Aitor Ruano "] description = "Fast, small and secure Shamir's Secret Sharing library crate" homepage = "https://github.com/c0dearm/sharks" diff --git a/src/field.rs b/src/field.rs index 95f87b3..aa2d7eb 100644 --- a/src/field.rs +++ b/src/field.rs @@ -58,7 +58,7 @@ const EXP_TABLE: [u8; 512] = [ 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01, 0x02, ]; -#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Debug, PartialEq, Copy, Clone)] pub struct GF256(pub u8); #[allow(clippy::suspicious_arithmetic_impl)] diff --git a/src/lib.rs b/src/lib.rs index 0580124..a9d673e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ impl Sharks { math::get_evaluator(polys) } - /// Given a slice of shares, recovers the original secret. + /// Given an iterable collection of shares, recovers the original secret. /// If the number of distinct shares is less than the minimum threshold an `Err` is returned, /// otherwise an `Ok` containing the secret. /// @@ -82,13 +82,28 @@ impl Sharks { /// secret = sharks.recover(&shares); /// // Not enough shares to recover secret /// assert!(secret.is_err()); - pub fn recover(&self, shares: &[Share]) -> Result, &str> { - let shares_x: HashSet = shares.iter().map(|s| s.x.0).collect(); + pub fn recover<'a, T>(&self, shares: T) -> Result, &str> + where + T: IntoIterator, + T::IntoIter: Iterator, + { + let (keys, shares) = shares + .into_iter() + .map(|s| { + ( + s.x.0, + Share { + x: s.x, + y: s.y.clone(), + }, + ) + }) + .unzip::, Vec>(); - if shares_x.len() < self.0 as usize { + if keys.len() < self.0 as usize { Err("Not enough shares to recover original secret") } else { - Ok(math::interpolate(shares)) + Ok(math::interpolate(shares.as_slice())) } } }