From da01e4e450ae8ae6eb04e2d0f41faa48f0c95519 Mon Sep 17 00:00:00 2001 From: Frederic Jacobs Date: Sat, 2 Apr 2016 16:29:30 +0200 Subject: [PATCH] Rename methods ahead of FFI support. --- Cargo.lock | 3 +-- Cargo.toml | 6 ++--- src/lib/mod.rs | 68 +++++++++++++++++++++++++------------------------- src/main.rs | 4 +-- 4 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5f4e7b3..b51397d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,9 +1,8 @@ [root] -name = "rusty-secrets" +name = "rusty_secrets" version = "0.0.1" dependencies = [ "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/Cargo.toml b/Cargo.toml index 071a296..e7a6ba0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rusty-secrets" +name = "rusty_secrets" version = "0.0.1" description = "Implementation of threshold Shamir secret sharing in the Rust programming language." license = "GPLv3" @@ -9,11 +9,11 @@ readme = "README.md" getopts = "^0.2.14" rustc-serialize = "^0.3.18" rand = "^0.3.14" -libc = "^0.2.0" [lib] -name = "rusty_secrets_lib" +name = "rusty_secrets" path = "src/lib/mod.rs" +crate_type = ["rlib"] [[bin]] name = "rusty_secrets_bin" diff --git a/src/lib/mod.rs b/src/lib/mod.rs index 46dbfca..5be2e3e 100644 --- a/src/lib/mod.rs +++ b/src/lib/mod.rs @@ -14,6 +14,40 @@ use std::iter::repeat; pub mod custom_error; use self::custom_error::*; +pub fn generate_shares(k: u8, n: u8, secret: Vec) -> io::Result>> { + let shares = try!(secret_share(&*secret, k, n)); + let config = base64::Config { + pad: false, + ..base64::STANDARD + }; + + let mut result = Vec::with_capacity(n as usize); + + for (index, share) in shares.iter().enumerate() { + let salad = share.to_base64(config); + let string = format!("{}-{}-{}", k, index+1, salad).into_bytes(); + result.push(string); + } + + Ok(result) +} + +pub fn recover_secret(k: u8, shares: Vec<(u8,Vec)>) -> io::Result> { + assert!(!shares.is_empty()); + let slen = shares[0].1.len(); + let mut col_in = Vec::with_capacity(k as usize); + let mut secret = Vec::with_capacity(slen); + for byteindex in 0 .. slen { + col_in.clear(); + for s in shares.iter().take(k as usize) { + col_in.push((s.0, s.1[byteindex])); + } + secret.push(lagrange_interpolate(&*col_in, 0u8)); + } + + return Ok(secret) as io::Result>; +} + fn new_vec(n: usize, x: T) -> Vec { repeat(x).take(n).collect() } @@ -75,37 +109,3 @@ fn secret_share(src: &[u8], k: u8, n: u8) -> io::Result>> { } Ok(result) } - -pub fn perform_encode(k: u8, n: u8, secret: Vec) -> io::Result>> { - let shares = try!(secret_share(&*secret, k, n)); - let config = base64::Config { - pad: false, - ..base64::STANDARD - }; - - let mut result = Vec::with_capacity(n as usize); - - for (index, share) in shares.iter().enumerate() { - let salad = share.to_base64(config); - let string = format!("{}-{}-{}", k, index+1, salad).into_bytes(); - result.push(string); - } - - Ok(result) -} - -pub fn perform_decode(k: u8, shares: Vec<(u8,Vec)>) -> io::Result> { - assert!(!shares.is_empty()); - let slen = shares[0].1.len(); - let mut col_in = Vec::with_capacity(k as usize); - let mut secret = Vec::with_capacity(slen); - for byteindex in 0 .. slen { - col_in.clear(); - for s in shares.iter().take(k as usize) { - col_in.push((s.0, s.1[byteindex])); - } - secret.push(lagrange_interpolate(&*col_in, 0u8)); - } - - return Ok(secret) as io::Result>; -} diff --git a/src/main.rs b/src/main.rs index 5146bf3..1bc1fc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -107,7 +107,7 @@ fn perform_encode_from_io(k: u8, n: u8) -> io::Result<()> { } tmp }; - match lib::perform_encode(k, n, secret) { + match lib::generate_shares(k, n, secret) { Ok(shares) => { for share in shares {println!("{:?}", str::from_utf8(&share).unwrap())}; } @@ -168,7 +168,7 @@ fn read_shares() -> io::Result<(u8, Vec<(u8,Vec)>)> { fn perform_decode_from_io() -> io::Result<()> { let (k, shares) = try!(read_shares()); - return match lib::perform_decode(k, shares) { + return match lib::recover_secret(k, shares) { Ok(secret) => { let mut out = io::stdout(); try!(out.write_all(&*secret));