-
Notifications
You must be signed in to change notification settings - Fork 346
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
Uint256 implementation #1059
Merged
Merged
Uint256 implementation #1059
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9d68ac2
Implement Isqrt for Uint128
uint ee4ba68
Implement Isqrt for Uint64
uint b1f0b38
Implement Uint256
uint 0cecd60
Expose Uint128::full_mul with a Uint256 result
uint 946eb9e
Add a big number test for Uint256::isqrt()
uint 2512ac8
Fix Uint256::try_from(&str) to expect dec values
uint 259a8a3
More idiomatic names for Uint256 constructors
uint 122c49b
Simplify Uint256::Shr implementations
uint 9727bf6
Test endianness of Uint256
uint 5509062
Fix uint256 shr tests
uint 98b9fa1
Export ConversionOverflowError
uint 726ae0c
Docs and CHANGELOG
uint 82d6fb9
Implement Uint256::checked_shr
uint de98232
Format .md files
uint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use std::iter::Sum; | |
use std::ops; | ||
|
||
use crate::errors::{DivideByZeroError, OverflowError, OverflowOperation, StdError}; | ||
use crate::Uint256; | ||
|
||
/// A thin wrapper around u128 that is using strings for JSON encoding/decoding, | ||
/// such that the full u128 range can be used for clients that convert JSON numbers to floats, | ||
|
@@ -222,6 +223,38 @@ impl<'a> ops::Sub<&'a Uint128> for Uint128 { | |
} | ||
} | ||
|
||
impl ops::Div<Uint128> for Uint128 { | ||
type Output = Self; | ||
|
||
fn div(self, rhs: Self) -> Self::Output { | ||
Self(self.u128().checked_div(rhs.u128()).unwrap()) | ||
} | ||
} | ||
|
||
impl<'a> ops::Div<&'a Uint128> for Uint128 { | ||
type Output = Self; | ||
|
||
fn div(self, rhs: &'a Uint128) -> Self::Output { | ||
Self(self.u128().checked_div(rhs.u128()).unwrap()) | ||
} | ||
} | ||
|
||
impl ops::Shr<u32> for Uint128 { | ||
type Output = Self; | ||
|
||
fn shr(self, rhs: u32) -> Self::Output { | ||
Self(self.u128().checked_shr(rhs).unwrap()) | ||
} | ||
} | ||
|
||
impl<'a> ops::Shr<&'a u32> for Uint128 { | ||
type Output = Self; | ||
|
||
fn shr(self, rhs: &'a u32) -> Self::Output { | ||
Self(self.u128().checked_shr(*rhs).unwrap()) | ||
} | ||
} | ||
|
||
impl ops::AddAssign<Uint128> for Uint128 { | ||
fn add_assign(&mut self, rhs: Uint128) { | ||
self.0 = self.0.checked_add(rhs.u128()).unwrap(); | ||
|
@@ -246,6 +279,30 @@ impl<'a> ops::SubAssign<&'a Uint128> for Uint128 { | |
} | ||
} | ||
|
||
impl ops::DivAssign<Uint128> for Uint128 { | ||
fn div_assign(&mut self, rhs: Self) { | ||
self.0 = self.0.checked_div(rhs.u128()).unwrap(); | ||
} | ||
} | ||
|
||
impl<'a> ops::DivAssign<&'a Uint128> for Uint128 { | ||
fn div_assign(&mut self, rhs: &'a Uint128) { | ||
self.0 = self.0.checked_div(rhs.u128()).unwrap(); | ||
} | ||
} | ||
|
||
impl ops::ShrAssign<u32> for Uint128 { | ||
fn shr_assign(&mut self, rhs: u32) { | ||
self.0 = self.0.checked_shr(rhs).unwrap(); | ||
} | ||
} | ||
|
||
impl<'a> ops::ShrAssign<&'a u32> for Uint128 { | ||
fn shr_assign(&mut self, rhs: &'a u32) { | ||
self.0 = self.0.checked_shr(*rhs).unwrap(); | ||
} | ||
} | ||
|
||
impl Uint128 { | ||
/// Returns `self * numerator / denominator` | ||
pub fn multiply_ratio<A: Into<u128>, B: Into<u128>>( | ||
|
@@ -258,15 +315,16 @@ impl Uint128 { | |
if denominator == 0 { | ||
panic!("Denominator must not be zero"); | ||
} | ||
let val: u128 = (self.full_mul(numerator) / denominator) | ||
(self.full_mul(numerator) / Uint256::from(denominator)) | ||
.try_into() | ||
.expect("multiplication overflow"); | ||
Uint128::from(val) | ||
.expect("multiplication overflow") | ||
} | ||
|
||
/// Multiplies two u128 values without overflow. | ||
fn full_mul(self, rhs: impl Into<u128>) -> U256 { | ||
U256::from(self.u128()) * U256::from(rhs.into()) | ||
pub fn full_mul(self, rhs: impl Into<u128>) -> Uint256 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new API is nice. Could you add a CHANGELOG entry and an example doc for it? |
||
Uint256::from(self.u128()) | ||
.checked_mul(Uint256::from(rhs.into())) | ||
.unwrap() | ||
} | ||
} | ||
|
||
|
@@ -322,19 +380,6 @@ impl<'a> Sum<&'a Uint128> for Uint128 { | |
} | ||
} | ||
|
||
/// This module is purely a workaround that lets us ignore lints for all the code | ||
/// the `construct_uint!` macro generates. | ||
#[allow(clippy::all)] | ||
mod uints { | ||
uint::construct_uint! { | ||
pub struct U256(4); | ||
} | ||
} | ||
|
||
/// Only used internally - namely to store the intermediate result of | ||
/// multiplying two 128-bit uints. | ||
use uints::U256; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConversionOverflowError
also need to be exported in lib.rs because it is used in StdError now.