From 47b27711dfdc09182670060df468bf453f8bd506 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 22 Aug 2018 14:35:12 +0300 Subject: [PATCH 1/4] Remove outdated comments and switches --- uint/src/uint.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index dc2db06..b9b5105 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -753,7 +753,6 @@ macro_rules! construct_uint { } /// Overflowing multiplication by u32 - #[allow(dead_code)] // not used when multiplied with inline assembly fn overflowing_mul_u32(self, other: u32) -> (Self, bool) { let $name(ref arr) = self; let mut ret = [0u64; $n_words]; @@ -772,8 +771,6 @@ macro_rules! construct_uint { impl_std_for_uint_internals!($name, $n_words); /// Converts from big endian representation bytes in memory - /// Can also be used as (&slice).into(), as it is default `From` - /// slice implementation for U256 pub fn from_big_endian(slice: &[u8]) -> Self { assert!($n_words * 8 >= slice.len()); @@ -1376,4 +1373,4 @@ macro_rules! impl_quickcheck_arbitrary_for_uint { } construct_uint!(U256, 4); -construct_uint!(U512, 8); \ No newline at end of file +construct_uint!(U512, 8); From 79985a0903a4698de544984b5ea900cb9a972b74 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Wed, 22 Aug 2018 16:57:32 +0300 Subject: [PATCH 2/4] bring back final line --- uint/src/uint.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 67933ec..0e2d6f9 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1396,4 +1396,4 @@ impl U256 { pub fn full_mul(self, other: U256) -> U512 { U512(uint_full_mul_reg!(U256, 4, self, other)) } -} \ No newline at end of file +} From 78426954ad487f5aaf6527d11cdac8abe14e12ca Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 22 Aug 2018 17:06:22 +0300 Subject: [PATCH 3/4] more fixes --- uint/src/uint.rs | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 0e2d6f9..3bb0a01 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -350,7 +350,6 @@ macro_rules! construct_uint { /// Little-endian large integer type #[repr(C)] #[derive(Copy, Clone, Eq, PartialEq, Hash)] - // TODO: serialize stuff? #[cfg_attr(feature="serialize", derive(Serialize, Deserialize))] pub struct $name(pub [u64; $n_words]); impl AsRef<$name> for $name { @@ -366,7 +365,10 @@ macro_rules! construct_uint { } impl $name { + + /// Maximum value. pub const MAX: $name = $name([u64::max_value(); $n_words]); + /// Convert from a decimal string. pub fn from_dec_str(value: &str) -> Result { if !value.bytes().all(|b| b >= 48 && b <= 57) { @@ -395,7 +397,7 @@ macro_rules! construct_uint { arr[0] as u32 } - /// Conversion to u64 + /// Low word (u64) #[inline] pub fn low_u64(&self) -> u64 { let &$name(ref arr) = self; @@ -420,7 +422,7 @@ macro_rules! construct_uint { /// /// # Panics /// - /// Panics if the number is larger than 2^64. + /// Panics if the number is larger than u64::max_value(). #[inline] pub fn as_u64(&self) -> u64 { let &$name(ref arr) = self; @@ -608,8 +610,7 @@ macro_rules! construct_uint { x * y } - /// Fast exponentation by squaring - /// https://en.wikipedia.org/wiki/Exponentiation_by_squaring + /// Fast exponentation by squaring. Returns result and overflow flag. pub fn overflowing_pow(self, expon: Self) -> (Self, bool) { if expon.is_zero() { return (Self::one(), false) } @@ -635,13 +636,13 @@ macro_rules! construct_uint { (res, overflow) } - /// Optimized instructions + /// Add with overflow. #[inline(always)] pub fn overflowing_add(self, other: $name) -> ($name, bool) { uint_overflowing_add!($name, $n_words, self, other) } - /// Addition which saturates at the maximum value. + /// Addition which saturates at the maximum value (Self::max_value()). pub fn saturating_add(self, other: $name) -> $name { match self.overflowing_add(other) { (_, true) => $name::max_value(), @@ -701,11 +702,6 @@ macro_rules! construct_uint { } } - /// Division with overflow - pub fn overflowing_div(self, other: $name) -> ($name, bool) { - (self / other, false) - } - /// Checked division. Returns `None` if `other == 0`. pub fn checked_div(self, other: $name) -> Option<$name> { if other.is_zero() { @@ -715,11 +711,6 @@ macro_rules! construct_uint { } } - /// Modulus with overflow. - pub fn overflowing_rem(self, other: $name) -> ($name, bool) { - (self % other, false) - } - /// Checked modulus. Returns `None` if `other == 0`. pub fn checked_rem(self, other: $name) -> Option<$name> { if other.is_zero() { @@ -746,13 +737,13 @@ macro_rules! construct_uint { } } - /// Multiplication by u32 + /// Multiplication by u32. #[deprecated(note = "Use Mul instead.")] pub fn mul_u32(self, other: u32) -> Self { self * other } - /// Overflowing multiplication by u32 + /// Overflowing multiplication by u32. fn overflowing_mul_u32(self, other: u32) -> (Self, bool) { let $name(ref arr) = self; let mut ret = [0u64; $n_words]; @@ -770,7 +761,7 @@ macro_rules! construct_uint { impl_std_for_uint_internals!($name, $n_words); - /// Converts from big endian representation bytes in memory + /// Converts from big endian representation bytes in memory. pub fn from_big_endian(slice: &[u8]) -> Self { assert!($n_words * 8 >= slice.len()); @@ -789,7 +780,7 @@ macro_rules! construct_uint { $name(ret) } - /// Converts from little endian representation bytes in memory + /// Converts from little endian representation bytes in memory. pub fn from_little_endian(slice: &[u8]) -> Self { assert!($n_words * 8 >= slice.len()); From 13195b7bc9701ca3477868acc44ee1ab7949e8b2 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 22 Aug 2018 17:43:34 +0300 Subject: [PATCH 4/4] fix div assign --- uint/src/uint.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/uint/src/uint.rs b/uint/src/uint.rs index 3bb0a01..938172c 100644 --- a/uint/src/uint.rs +++ b/uint/src/uint.rs @@ -1006,9 +1006,7 @@ macro_rules! construct_uint { impl ::core::ops::DivAssign for $name where T: Into<$name> { fn div_assign(&mut self, other: T) { - let (result, overflow) = self.overflowing_div(other.into()); - panic_on_overflow!(overflow); - *self = result + *self = *self / other.into(); } }