rustup (1.0 beta)

This commit is contained in:
Sebastian Gesemann
2015-04-06 22:36:54 +02:00
parent d15cb70774
commit 814819af81
4 changed files with 74 additions and 40 deletions

31
Cargo.lock generated
View File

@ -1,47 +1,50 @@
[root] [root]
name = "secretshare" name = "secretshare"
version = "0.1.5" version = "0.1.6"
dependencies = [ dependencies = [
"crc24 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "crc24 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "crc24" name = "crc24"
version = "0.1.5" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "getopts" name = "getopts"
version = "0.2.4" version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"log 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.1.3" version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "log" name = "log"
version = "0.2.5" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.2.1" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"libc 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "rustc-serialize" name = "rustc-serialize"
version = "0.3.6" version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"

View File

@ -1,13 +1,13 @@
[package] [package]
name = "secretshare" name = "secretshare"
version = "0.1.5" version = "0.1.6"
authors = ["Sebastian Gesemann <s.gesemann@gmail.com>"] authors = ["Sebastian Gesemann <s.gesemann@gmail.com>"]
description = "This is an implementation of Shamir's secret sharing scheme." description = "This is an implementation of Shamir's secret sharing scheme."
license = "GPLv3" license = "GPLv3"
readme = "README.md" readme = "README.md"
[dependencies] [dependencies]
getopts = "^0.2.1" getopts = "^0.2.9"
rustc-serialize = "^0.3.0" rustc-serialize = "^0.3.12"
crc24 = "^0.1.5" crc24 = "^0.1.6"
rand = "^0.2.0" rand = "^0.3.7"

View File

@ -1,6 +1,7 @@
//! This module provides the Gf256 type which is used to represent //! This module provides the Gf256 type which is used to represent
//! elements of a finite field wich 256 elements. //! elements of a finite field wich 256 elements.
use std::num::Wrapping;
use std::ops::{ Add, Sub, Mul, Div }; use std::ops::{ Add, Sub, Mul, Div };
use std::sync::{ Once, ONCE_INIT }; use std::sync::{ Once, ONCE_INIT };
@ -8,9 +9,8 @@ const POLY: u8 = 0x1D; // represents x^8 + x^4 + x^3 + x^2 + 1
/// replicates the least significant bit to every other bit /// replicates the least significant bit to every other bit
#[inline] #[inline]
#[allow(unsigned_negation)]
fn mask(bit: u8) -> u8 { fn mask(bit: u8) -> u8 {
-(bit & 1) (Wrapping(0u8) - Wrapping(bit & 1)).0
} }
/// multiplies a polynomial with x and returns the residual /// multiplies a polynomial with x and returns the residual

View File

@ -1,15 +1,12 @@
#![feature(collections)] extern crate rustc_serialize as serialize;
#![feature(core)]
#![feature(exit_status)]
#![feature(hash)]
#![feature(io)]
extern crate "rustc-serialize" as serialize;
extern crate getopts; extern crate getopts;
extern crate crc24; extern crate crc24;
extern crate rand; extern crate rand;
use std::convert;
use std::env; use std::env;
use std::error;
use std::fmt;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::iter::repeat; use std::iter::repeat;
@ -27,25 +24,61 @@ fn new_vec<T: Clone>(n: usize, x: T) -> Vec<T> {
repeat(x).take(n).collect() repeat(x).take(n).collect()
} }
fn other_io_err(descr: &'static str, detail: Option<String>) -> io::Error { #[derive(Debug)]
io::Error::new(io::ErrorKind::Other, descr, detail) pub struct Error {
descr: &'static str,
detail: Option<String>,
}
impl Error {
fn new(descr: &'static str, detail: Option<String>) -> Error {
Error { descr: descr, detail: detail }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.detail {
None => write!(f, "{}", self.descr),
Some(ref detail) => write!(f, "{} ({})", self.descr, detail)
}
}
}
impl error::Error for Error {
fn description(&self) -> &str { self.descr }
fn cause(&self) -> Option<&error::Error> { None }
}
impl convert::From<Error> for io::Error {
fn from(me: Error) -> io::Error {
io::Error::new(io::ErrorKind::Other, me)
}
} }
// a try!-like macro for Option<T> expressions that takes // a try!-like macro for Option<T> expressions that takes
// a &'static str as error message as 2nd parameter // a &'static str as error message as 2nd parameter
// and creates an io::Error out of it if necessary. // and creates an Error out of it if necessary.
macro_rules! otry { macro_rules! otry {
($o:expr, $e:expr) => ( ($o:expr, $e:expr) => (
match $o { match $o {
Some(thing_) => thing_, Some(thing_) => thing_,
None => return Err(other_io_err($e, None)) None => return Err(convert::From::from(Error::new($e, None)))
} }
) )
} }
/// maps a ParseIntError to an io::Error /// maps a ParseIntError to an io::Error
fn pie2io(p: num::ParseIntError) -> io::Error { fn pie2io(p: num::ParseIntError) -> io::Error {
other_io_err("Int parsing error", Some(p.to_string())) convert::From::from(
Error::new("Integer parsing error", Some(p.to_string()))
)
}
fn other_io_err(descr: &'static str, detail: Option<String>) -> io::Error {
convert::From::from(
Error::new(descr, detail)
)
} }
/// evaluates a polynomial at x=1, 2, 3, ... n (inclusive) /// evaluates a polynomial at x=1, 2, 3, ... n (inclusive)
@ -125,11 +158,9 @@ fn parse_k_n(s: &str) -> io::Result<(u8, u8)> {
/// and the raw share data /// and the raw share data
fn crc24_as_bytes(k: u8, n: u8, octets: &[u8]) -> [u8; 3] { fn crc24_as_bytes(k: u8, n: u8, octets: &[u8]) -> [u8; 3] {
use std::hash::Hasher; use std::hash::Hasher;
use std::slice::ref_slice;
let mut h = crc24::Crc24Hasher::new(); let mut h = crc24::Crc24Hasher::new();
h.write(ref_slice(&k)); h.write(&[k, n]);
h.write(ref_slice(&n));
h.write(octets); h.write(octets);
let v = h.finish(); let v = h.finish();
@ -212,7 +243,7 @@ fn read_shares() -> io::Result<(u8, Vec<(u8,Vec<u8>)>)> {
"Share parse error: Base64 decoding of checksum failed", None)) "Share parse error: Base64 decoding of checksum failed", None))
); );
let mychksum = crc24_as_bytes(k, n, &*data); let mychksum = crc24_as_bytes(k, n, &*data);
if mychksum != crc_bytes { if crc_bytes != mychksum {
return Err(other_io_err("Share parse error: Checksum mismatch", None)); return Err(other_io_err("Share parse error: Checksum mismatch", None));
} }
} }
@ -262,11 +293,11 @@ fn main() {
opts.optopt("e", "encode", "for encoding, K is the required number of \ opts.optopt("e", "encode", "for encoding, K is the required number of \
shares for decoding, N is the number of shares \ shares for decoding, N is the number of shares \
to generate. 1 <= K <= N <= 255", "K,N"); to generate. 1 <= K <= N <= 255", "K,N");
let opt_matches = match opts.parse(args.tail()) { let opt_matches = match opts.parse(&args[1..]) {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => {
drop(writeln!(&mut stderr, "Error: {}", f)); drop(writeln!(&mut stderr, "Error: {}", f));
env::set_exit_status(1); // env::set_exit_status(1); // FIXME: unstable feature
return; return;
} }
}; };
@ -311,7 +342,7 @@ fn main() {
if let Err(e) = result { if let Err(e) = result {
drop(writeln!(&mut stderr, "{}", e)); drop(writeln!(&mut stderr, "{}", e));
env::set_exit_status(1); // env::set_exit_status(1); // FIXME: unstable feature
} }
} }