ethereum-types refactor

This commit is contained in:
debris 2017-10-24 16:09:44 +08:00
commit c110f3eac9
5 changed files with 1427 additions and 0 deletions

28
Cargo.toml Normal file
View File

@ -0,0 +1,28 @@
[package]
description = "Large fixed-size integers arithmetics"
homepage = "http://parity.io"
repository = "https://github.com/ethcore/bigint"
license = "MIT/Apache-2.0"
name = "uint"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
build = "build.rs"
[build-dependencies]
rustc_version = "0.2"
[dependencies]
rustc-hex = { version = "1.0", optional = true }
heapsize = { version = "0.4", optional = true }
byteorder = { version = "1", default-features = false }
[dev-dependencies]
quickcheck = "0.4"
[features]
heapsizeof = ["heapsize", "std"]
std = ["rustc-hex"]
[[example]]
name = "modular"
required-features = ["std"]

17
build.rs Normal file
View File

@ -0,0 +1,17 @@
// Copyright 2015-2017 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
extern crate rustc_version;
use rustc_version::{version_meta, Channel};
fn main() {
if let Channel::Nightly = version_meta().unwrap().channel {
println!("cargo:rustc-cfg=asm_available");
}
}

55
examples/modular.rs Normal file
View File

@ -0,0 +1,55 @@
// Copyright 2015-2017 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
extern crate uint;
use uint::U256;
fn main() {
// Example modular arithmetic using bigint U256 primitives
// imagine the field 0..p
// where the p is defined below
// (it's a prime!)
let p = U256::from_dec_str(
"38873241744847760218045702002058062581688990428170398542849190507947196700873"
).expect("p to be a good number in the example");
// then, on this field,
// (p-1) + (p+1) = 0
// (p - 1) mod p
let p_minus_1 = (p - 1u64.into()) % p;
// (p + 1) mod p
let p_plus_1 = (p + 1u64.into()) % p;
// ((p - 1) mod p + (p + 1) mod p) mod p
let sum = (p_minus_1 + p_plus_1) % p;
assert_eq!(sum, 0.into());
// on this field,
// (p-1) + (p-1) = p-2
let p_minus_1 = (p - 1u64.into()) % p;
let sum = (p_minus_1 + p_minus_1) % p;
assert_eq!(sum, p - 2.into());
// on this field,
// (p-1) * 3 = p-3
let p_minus_1 = (p - 1u64.into()) % p;
// multiplication is a series of additions
let multiplicator = 3;
let mul = {
let mut result = p_minus_1;
for _ in 0..multiplicator-1 {
result = (p_minus_1 + result) % p;
}
result
};
assert_eq!(mul, p - 3.into());
}

29
src/lib.rs Normal file
View File

@ -0,0 +1,29 @@
// Copyright 2015-2017 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Efficient large, fixed-size big integers and hashes.
#![cfg_attr(asm_available, feature(asm))]
#[doc(hidden)]
pub extern crate byteorder;
#[cfg(feature="heapsizeof")]
#[doc(hidden)]
pub extern crate heapsize;
#[cfg(feature="std")]
#[doc(hidden)]
pub extern crate core;
#[cfg(feature = "std")]
#[doc(hidden)]
pub extern crate rustc_hex;
mod uint;
pub use uint::*;

1298
src/uint.rs Normal file

File diff suppressed because it is too large Load Diff