Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose U128, add u128 conversions. #92

Merged
merged 4 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 61 additions & 50 deletions primitive-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

//! Primitive types shared by Substrate and Parity Ethereum.
//!
//! Those are uint types `U256` and `U512`, and fixed hash types `H160`,
//! Those are uint types `U128`, `U256` and `U512`, and fixed hash types `H160`,
//! `H256` and `H512`, with optional serde serialization, parity-codec and
//! rlp encoding.

Expand All @@ -32,16 +32,73 @@ extern crate impl_codec;
#[macro_use]
extern crate impl_rlp;

construct_uint! {
/// 128-bit unsigned integer.
pub struct U128(2);
}
construct_uint! {
/// 256-bit unsigned integer.
pub struct U256(4);
}
construct_uint! {
/// 512-bits unsigned integer.
pub struct U512(8);
}

construct_fixed_hash! {
/// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size.
pub struct H160(20);
}
construct_fixed_hash! {
/// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size.
pub struct H256(32);
}
construct_fixed_hash! {
/// Fixed-size uninterpreted hash type with 64 bytes (512 bits) size.
pub struct H512(64);
}

#[cfg(feature = "impl-serde")]
impl_uint_serde!(U256, 4);
mod serde {
use super::*;

impl_uint_serde!(U128, 2);
impl_uint_serde!(U256, 4);
impl_uint_serde!(U512, 8);

impl_fixed_hash_serde!(H160, 20);
impl_fixed_hash_serde!(H256, 32);
impl_fixed_hash_serde!(H512, 64);
}

#[cfg(feature = "impl-codec")]
impl_uint_codec!(U256, 4);
mod codec {
use super::*;

impl_uint_codec!(U128, 2);
impl_uint_codec!(U256, 4);
impl_uint_codec!(U512, 8);

impl_fixed_hash_codec!(H160, 20);
impl_fixed_hash_codec!(H256, 32);
impl_fixed_hash_codec!(H512, 64);
}

#[cfg(feature = "impl-rlp")]
impl_uint_rlp!(U256, 4);
mod rlp {
use super::*;

impl_uint_rlp!(U128, 2);
impl_uint_rlp!(U256, 4);
impl_uint_rlp!(U512, 8);

impl_fixed_hash_rlp!(H160, 20);
impl_fixed_hash_rlp!(H256, 32);
impl_fixed_hash_rlp!(H512, 64);
}


impl_fixed_hash_conversions!(H256, H160);

impl U256 {
/// Multiplies two 256-bit integers to produce full 512-bit integer
Expand Down Expand Up @@ -105,49 +162,3 @@ impl<'a> From<&'a U512> for U256 {
U256(ret)
}
}

construct_uint! {
/// 512-bits unsigned integer.
pub struct U512(8);
}
#[cfg(feature = "impl-serde")]
impl_uint_serde!(U512, 8);
#[cfg(feature = "impl-codec")]
impl_uint_codec!(U512, 8);
#[cfg(feature = "impl-rlp")]
impl_uint_rlp!(U512, 8);

construct_fixed_hash! {
/// Fixed-size uninterpreted hash type with 20 bytes (160 bits) size.
pub struct H160(20);
}
#[cfg(feature = "impl-serde")]
impl_fixed_hash_serde!(H160, 20);
#[cfg(feature = "impl-codec")]
impl_fixed_hash_codec!(H160, 20);
#[cfg(feature = "impl-rlp")]
impl_fixed_hash_rlp!(H160, 20);

impl_fixed_hash_conversions!(H256, H160);

construct_fixed_hash! {
/// Fixed-size uninterpreted hash type with 32 bytes (256 bits) size.
pub struct H256(32);
}
#[cfg(feature = "impl-serde")]
impl_fixed_hash_serde!(H256, 32);
#[cfg(feature = "impl-codec")]
impl_fixed_hash_codec!(H256, 32);
#[cfg(feature = "impl-rlp")]
impl_fixed_hash_rlp!(H256, 32);

construct_fixed_hash! {
/// Fixed-size uninterpreted hash type with 64 bytes (512 bits) size.
pub struct H512(64);
}
#[cfg(feature = "impl-serde")]
impl_fixed_hash_serde!(H512, 64);
#[cfg(feature = "impl-codec")]
impl_fixed_hash_codec!(H512, 64);
#[cfg(feature = "impl-rlp")]
impl_fixed_hash_rlp!(H512, 64);
63 changes: 56 additions & 7 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,58 @@ pub fn split_u128(a: u128) -> (u64, u64) {

#[macro_export]
macro_rules! construct_uint {
( $(#[$attr:meta])* $visibility:vis struct $name:ident (1); ) => {
construct_uint!{ @construct $($attr)* $visibility struct $name (1); }
};

( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => {
construct_uint! { @construct $($attr)* $visibility struct $name ($n_words); }

impl $crate::core_::convert::From<u128> for $name {
fn from(value: u128) -> $name {
let mut ret = [0; $n_words];
ret[0] = value as u64;
ret[1] = (value >> 64) as u64;
$name(ret)
}
}

impl $crate::core_::convert::From<i128> for $name {
fn from(value: i128) -> $name {
match value >= 0 {
Copy link
Member

@niklasad1 niklasad1 Jan 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be match (value as u128) >> 127 == 0 { ... } but the assembly is identical according to https://rust.godbolt.org/z/5kXf8K, so better keep it way more readable

true => From::from(value as u128),
false => { panic!("Unsigned integer can't be created from negative value"); }
}
}
}

impl $name {
/// Low 2 words (u128)
#[inline]
pub fn low_u128(&self) -> u128 {
let &$name(ref arr) = self;
((arr[1] as u128) << 64) + arr[0] as u128
}

/// Conversion to u128 with overflow checking
///
/// # Panics
///
/// Panics if the number is larger than 2^128.
#[inline]
pub fn as_u128(&self) -> u128 {
let &$name(ref arr) = self;
for i in (2..$n_words) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needless paratheses

Suggested change
for i in (2..$n_words) {
for i in 2..$n_words {

if arr[i] != 0 {
panic!("Integer overflow when casting to u128")
}

}
self.low_u128()
}
}
};
( @construct $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_words:tt ); ) => {
/// Little-endian large integer type
#[repr(C)]
$(#[$attr])*
Expand All @@ -366,7 +417,6 @@ macro_rules! construct_uint {
}

impl $name {

/// Maximum value.
pub const MAX: $name = $name([u64::max_value(); $n_words]);

Expand Down Expand Up @@ -414,7 +464,7 @@ macro_rules! construct_uint {
pub fn as_u32(&self) -> u32 {
let &$name(ref arr) = self;
if (arr[0] & (0xffffffffu64 << 32)) != 0 {
panic!("Integer overflow when casting U256")
panic!("Integer overflow when casting to u64")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
panic!("Integer overflow when casting to u64")
panic!("Integer overflow when casting to u32")

}
self.as_u64() as u32
}
Expand All @@ -429,7 +479,7 @@ macro_rules! construct_uint {
let &$name(ref arr) = self;
for i in 1..$n_words {
if arr[i] != 0 {
panic!("Integer overflow when casting U256")
panic!("Integer overflow when casting to u64")
}
}
arr[0]
Expand All @@ -445,11 +495,11 @@ macro_rules! construct_uint {
let &$name(ref arr) = self;
for i in 1..$n_words {
if arr[i] != 0 {
panic!("Integer overflow when casting U256")
panic!("Integer overflow when casting to usize")
}
}
if arr[0] > usize::max_value() as u64 {
panic!("Integer overflow when casting U256")
panic!("Integer overflow when casting to usize")
}
arr[0] as usize
}
Expand Down Expand Up @@ -821,7 +871,6 @@ macro_rules! construct_uint {
}
}


impl_map_from!($name, u8, u64);
impl_map_from!($name, u16, u64);
impl_map_from!($name, u32, u64);
Expand All @@ -841,7 +890,7 @@ macro_rules! construct_uint {
impl_map_from!($name, i32, i64);
impl_map_from!($name, isize, i64);

// Converts from big endian representation of U256
// Converts from big endian representation of $name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a comment. Does macro name resolve for comments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, seems it doesn't rust-lang/rust#37903

impl<'a> $crate::core_::convert::From<&'a [u8]> for $name {
fn from(bytes: &[u8]) -> $name {
Self::from_big_endian(bytes)
Expand Down
10 changes: 10 additions & 0 deletions uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ construct_uint! {
pub struct U512(8);
}

#[test]
fn u128_conversions() {
let mut a = U256::from(u128::max_value());
assert_eq!(a.low_u128(), u128::max_value());
a += 2u128.into();
assert_eq!(a.low_u128(), 1u128);
a -= 3u128.into();
assert_eq!(a.low_u128(), u128::max_value() - 1);
}

#[test]
fn uint256_checked_ops() {
let z = U256::from(0);
Expand Down