From 814819af81530d578b46974e18f2e9947e157b3c Mon Sep 17 00:00:00 2001 From: Sebastian Gesemann Date: Mon, 6 Apr 2015 22:36:54 +0200 Subject: [PATCH] rustup (1.0 beta) --- Cargo.lock | 31 ++++++++++++----------- Cargo.toml | 10 ++++---- src/gf256.rs | 4 +-- src/main.rs | 69 +++++++++++++++++++++++++++++++++++++--------------- 4 files changed, 74 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4fdc2a8..2355526 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,47 +1,50 @@ [root] name = "secretshare" -version = "0.1.5" +version = "0.1.6" dependencies = [ - "crc24 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "getopts 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.6 (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.9 (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.12 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "crc24" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "getopts" -version = "0.2.4" +version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "libc" -version = "0.1.3" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "log" -version = "0.2.5" +version = "0.3.1" 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]] name = "rand" -version = "0.2.1" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rustc-serialize" -version = "0.3.6" +version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/Cargo.toml b/Cargo.toml index a929942..421f17d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "secretshare" -version = "0.1.5" +version = "0.1.6" authors = ["Sebastian Gesemann "] description = "This is an implementation of Shamir's secret sharing scheme." license = "GPLv3" readme = "README.md" [dependencies] -getopts = "^0.2.1" -rustc-serialize = "^0.3.0" -crc24 = "^0.1.5" -rand = "^0.2.0" +getopts = "^0.2.9" +rustc-serialize = "^0.3.12" +crc24 = "^0.1.6" +rand = "^0.3.7" diff --git a/src/gf256.rs b/src/gf256.rs index fd6b060..f84bcf5 100644 --- a/src/gf256.rs +++ b/src/gf256.rs @@ -1,6 +1,7 @@ //! This module provides the Gf256 type which is used to represent //! elements of a finite field wich 256 elements. +use std::num::Wrapping; use std::ops::{ Add, Sub, Mul, Div }; 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 #[inline] -#[allow(unsigned_negation)] fn mask(bit: u8) -> u8 { - -(bit & 1) + (Wrapping(0u8) - Wrapping(bit & 1)).0 } /// multiplies a polynomial with x and returns the residual diff --git a/src/main.rs b/src/main.rs index 58c8e75..2add32f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,12 @@ -#![feature(collections)] -#![feature(core)] -#![feature(exit_status)] -#![feature(hash)] -#![feature(io)] - -extern crate "rustc-serialize" as serialize; +extern crate rustc_serialize as serialize; extern crate getopts; extern crate crc24; extern crate rand; +use std::convert; use std::env; +use std::error; +use std::fmt; use std::io; use std::io::prelude::*; use std::iter::repeat; @@ -27,25 +24,61 @@ fn new_vec(n: usize, x: T) -> Vec { repeat(x).take(n).collect() } -fn other_io_err(descr: &'static str, detail: Option) -> io::Error { - io::Error::new(io::ErrorKind::Other, descr, detail) +#[derive(Debug)] +pub struct Error { + descr: &'static str, + detail: Option, +} + +impl Error { + fn new(descr: &'static str, detail: Option) -> 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 for io::Error { + fn from(me: Error) -> io::Error { + io::Error::new(io::ErrorKind::Other, me) + } } // a try!-like macro for Option expressions that takes // 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 { ($o:expr, $e:expr) => ( match $o { 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 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) -> io::Error { + convert::From::from( + Error::new(descr, detail) + ) } /// 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 fn crc24_as_bytes(k: u8, n: u8, octets: &[u8]) -> [u8; 3] { use std::hash::Hasher; - use std::slice::ref_slice; let mut h = crc24::Crc24Hasher::new(); - h.write(ref_slice(&k)); - h.write(ref_slice(&n)); + h.write(&[k, n]); h.write(octets); let v = h.finish(); @@ -212,7 +243,7 @@ fn read_shares() -> io::Result<(u8, Vec<(u8,Vec)>)> { "Share parse error: Base64 decoding of checksum failed", None)) ); 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)); } } @@ -262,11 +293,11 @@ fn main() { opts.optopt("e", "encode", "for encoding, K is the required number of \ shares for decoding, N is the number of shares \ 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, Err(f) => { drop(writeln!(&mut stderr, "Error: {}", f)); - env::set_exit_status(1); + // env::set_exit_status(1); // FIXME: unstable feature return; } }; @@ -311,7 +342,7 @@ fn main() { if let Err(e) = result { drop(writeln!(&mut stderr, "{}", e)); - env::set_exit_status(1); + // env::set_exit_status(1); // FIXME: unstable feature } }