LowerHex and small cleanup.

This commit is contained in:
Tomasz Drwięga 2018-01-28 17:26:50 +01:00
parent 323d51c30d
commit 832362ba35
No known key found for this signature in database
GPG Key ID: B2BA26B1C688F8FC
3 changed files with 53 additions and 70 deletions

View File

@ -139,6 +139,12 @@ macro_rules! construct_hash {
}
}
impl ::core::fmt::LowerHex for $from {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Debug::fmt(self, f)
}
}
impl Copy for $from {}
#[cfg_attr(feature="dev", allow(expl_impl_clone_on_copy))]
impl Clone for $from {
@ -335,13 +341,6 @@ macro_rules! impl_heapsize_for_hash {
#[doc(hidden)]
macro_rules! impl_std_for_hash {
($from: ident, $size: tt) => {
impl $from {
/// Get a hex representation.
pub fn hex(&self) -> String {
format!("{:?}", self)
}
}
impl $crate::rand::Rand for $from {
fn rand<R: $crate::rand::Rng>(r: &mut R) -> Self {
let mut hash = $from::new();

View File

@ -141,7 +141,7 @@ pub fn uint256_arithmetic_test() {
let sub = overflowing!(incr.overflowing_sub(init));
assert_eq!(sub, U256([0x9F30411021524112u64, 0x0001BD5B7DDFBD5A, 0, 0]));
// Multiplication
let mult = sub.mul_u32(300);
let mult = sub * 300u32;
assert_eq!(mult, U256([0x8C8C3EE70C644118u64, 0x0209E7378231E632, 0, 0]));
// Division
assert_eq!(U256::from(105u8) / U256::from(5u8), U256::from(21u8));
@ -196,11 +196,11 @@ pub fn uint256_exp10() {
#[test]
pub fn uint256_mul32() {
assert_eq!(U256::from(0u64).mul_u32(2), U256::from(0u64));
assert_eq!(U256::from(1u64).mul_u32(2), U256::from(2u64));
assert_eq!(U256::from(10u64).mul_u32(2), U256::from(20u64));
assert_eq!(U256::from(10u64).mul_u32(5), U256::from(50u64));
assert_eq!(U256::from(1000u64).mul_u32(50), U256::from(50000u64));
assert_eq!(U256::from(0u64) * 2u32, U256::from(0u64));
assert_eq!(U256::from(1u64) * 2u32, U256::from(2u64));
assert_eq!(U256::from(10u64) * 2u32, U256::from(20u64));
assert_eq!(U256::from(10u64) * 5u32, U256::from(50u64));
assert_eq!(U256::from(1000u64) * 50u32, U256::from(50000u64));
}
#[test]
@ -219,23 +219,27 @@ fn uint256_pow_overflow_panic() {
}
#[test]
fn should_format_hex_correctly() {
assert_eq!(&U256::from(0).to_hex(), &"0");
assert_eq!(&U256::from(0x1).to_hex(), &"1");
assert_eq!(&U256::from(0xf).to_hex(), &"f");
assert_eq!(&U256::from(0x10).to_hex(), &"10");
assert_eq!(&U256::from(0xff).to_hex(), &"ff");
assert_eq!(&U256::from(0x100).to_hex(), &"100");
assert_eq!(&U256::from(0xfff).to_hex(), &"fff");
assert_eq!(&U256::from(0x1000).to_hex(), &"1000");
fn should_format_and_debug_correctly() {
let test = |x: usize, hex: &'static str, dbg: &'static str| {
assert_eq!(format!("{:?}", U256::from(x)), dbg);
assert_eq!(format!("{:x}", U256::from(x)), hex);
};
test(0x1, "0x1", "1");
test(0xf, "0xf", "15");
test(0x10, "0x10", "16");
test(0xff, "0xff", "255");
test(0x100, "0x100", "256");
test(0xfff, "0xfff", "4095");
test(0x1000, "0x1000", "4096");
}
#[test]
fn uint256_overflowing_pow() {
// assert_eq!(
// U256::from(2).overflowing_pow(U256::from(0xff)),
// (U256::from_str("8000000000000000000000000000000000000000000000000000000000000000").unwrap(), false)
// );
assert_eq!(
U256::from(2).overflowing_pow(U256::from(0xff)),
(U256::from_str("8000000000000000000000000000000000000000000000000000000000000000").unwrap(), false)
);
assert_eq!(
U256::from(2).overflowing_pow(U256::from(0x100)),
(U256::zero(), true)
@ -846,7 +850,7 @@ fn u256_multi_muls2() {
#[test]
fn example() {
let mut val: U256 = 1023.into();
for _ in 0..200 { val = val * 2.into() }
for _ in 0..200 { val = val * U256::from(2) }
assert_eq!(&format!("{}", val), "1643897619276947051879427220465009342380213662639797070513307648");
}

View File

@ -717,7 +717,7 @@ macro_rules! construct_uint {
pub fn exp10(n: usize) -> Self {
match n {
0 => Self::from(1u64),
_ => Self::exp10(n - 1).mul_u32(10)
_ => Self::exp10(n - 1) * 10u32
}
}
@ -858,13 +858,6 @@ macro_rules! construct_uint {
(!self, true)
}
/// Multiplication by u32
pub fn mul_u32(self, other: u32) -> Self {
let (ret, overflow) = self.overflowing_mul_u32(other);
panic_on_overflow!(overflow);
ret
}
/// Overflowing multiplication by u32
#[allow(dead_code)] // not used when multiplied with inline assembly
fn overflowing_mul_u32(self, other: u32) -> (Self, bool) {
@ -915,8 +908,6 @@ macro_rules! construct_uint {
$name(ret)
}
impl_std_for_uint_internals!($name, $n_words);
}
impl Default for $name {
@ -982,6 +973,16 @@ macro_rules! construct_uint {
}
}
impl ::core::ops::Mul<u32> for $name {
type Output = $name;
fn mul(self, other: u32) -> $name {
let (ret, overflow) = self.overflowing_mul_u32(other);
panic_on_overflow!(overflow);
ret
}
}
impl ::core::ops::Mul<$name> for $name {
type Output = $name;
@ -1169,28 +1170,6 @@ macro_rules! construct_uint {
);
}
#[cfg(feature="std")]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_std_for_uint_internals {
($name: ident, $n_words: tt) => {
/// Convert to hex string.
#[inline]
pub fn to_hex(&self) -> String {
use core::cmp;
use $crate::rustc_hex::ToHex;;
if self.is_zero() { return "0".to_owned(); } // special case.
let mut bytes = [0u8; 8 * $n_words];
self.to_big_endian(&mut bytes);
let bp7 = self.bits() + 7;
let len = cmp::max(bp7 / 8, 1);
let bytes_hex = bytes[bytes.len() - len..].to_hex();
(&bytes_hex[1 - bp7 % 8 / 4..]).to_owned()
}
}
}
#[cfg(feature="std")]
#[macro_export]
#[doc(hidden)]
@ -1239,15 +1218,23 @@ macro_rules! impl_std_for_uint {
impl ::core::fmt::LowerHex for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let &$name(ref data) = self;
try!(write!(f, "0x"));
let &$name(ref data) = self;
// special case.
if self.is_zero() {
return write!(f, "0x0");
}
write!(f, "0x")?;
let mut latch = false;
for ch in data.iter().rev() {
for x in 0..16 {
let nibble = (ch & (15u64 << ((15 - x) * 4) as u64)) >> (((15 - x) * 4) as u64);
if !latch { latch = nibble != 0 }
if !latch {
latch = nibble != 0;
}
if latch {
try!(write!(f, "{:x}", nibble));
write!(f, "{:x}", nibble)?;
}
}
}
@ -1263,13 +1250,6 @@ macro_rules! impl_std_for_uint {
}
}
#[cfg(not(feature="std"))]
#[macro_export]
#[doc(hidden)]
macro_rules! impl_std_for_uint_internals {
($name: ident, $n_words: tt) => {}
}
#[cfg(not(feature="std"))]
#[macro_export]
#[doc(hidden)]