-
Notifications
You must be signed in to change notification settings - Fork 244
Port over relevant changes from bigint #25
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9609d2e
Remove asm and port over performance improvements from `bigint`
dvdplm 16012b3
Add benchmark
dvdplm ae247ae
Port over no_std formatting and use rustc_hex 2.0 to make tests pass …
dvdplm 13d7356
Run uint no_std tests
dvdplm 3356726
Use rustc-hex without default features
dvdplm 4336907
move display/debug funtionality back to `std`-gated macro
dvdplm 2af9e11
don't run tests with --no-default-features
dvdplm 1262de4
Try `travis_wait` to fix timeouts
dvdplm 78cf6e7
Debug travis
dvdplm b49e0ed
debug travis (attempt 3)
dvdplm 85ba498
Try with absurdly long wait time: 400min (attempt 4)
dvdplm 97ecf17
Export U256 and U512
dvdplm d3c335e
Add impl to converto from U256 to [u8; 32]
dvdplm 1906259
Put original travis config back
dvdplm b4b5c6f
Fix repo meta-data key
dvdplm 9261b85
Move conversion inside macro
dvdplm 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -350,6 +350,7 @@ 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 { | ||
|
|
@@ -365,6 +366,7 @@ macro_rules! construct_uint { | |
| } | ||
|
|
||
| impl $name { | ||
| pub const MAX: $name = $name([u64::max_value(); $n_words]); | ||
| /// Convert from a decimal string. | ||
| pub fn from_dec_str(value: &str) -> Result<Self, $crate::FromDecStrErr> { | ||
| if !value.bytes().all(|b| b >= 48 && b <= 57) { | ||
|
|
@@ -1181,35 +1183,9 @@ macro_rules! construct_uint { | |
| } | ||
| } | ||
|
|
||
| impl_std_for_uint!($name, $n_words); | ||
| impl_heapsize_for_uint!($name); | ||
| // `$n_words * 8` because macro expects bytes and | ||
| // uints use 64 bit (8 byte) words | ||
| impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8)); | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(feature="std")] | ||
| #[macro_export] | ||
| #[doc(hidden)] | ||
| macro_rules! impl_std_for_uint_internals { | ||
| ($name: ident, $n_words: tt) => { | ||
| /// Convert to hex string. | ||
| #[deprecated(note = "Use LowerHex instead.")] | ||
| pub fn to_hex(&self) -> String { | ||
| format!("{:x}", self) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature="std")] | ||
| #[macro_export] | ||
| #[doc(hidden)] | ||
| macro_rules! impl_std_for_uint { | ||
| ($name: ident, $n_words: tt) => { | ||
| impl ::core::fmt::Debug for $name { | ||
| fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { | ||
| write!(f, "{:#x}", self) | ||
| ::core::fmt::Display::fmt(self, f) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1219,16 +1195,24 @@ macro_rules! impl_std_for_uint { | |
| return write!(f, "0"); | ||
| } | ||
|
|
||
| let mut s = String::new(); | ||
| let mut buf = [0_u8; $n_words*20]; | ||
| let mut i = buf.len() - 1; | ||
| let mut current = *self; | ||
| let ten = $name::from(10); | ||
|
|
||
| while !current.is_zero() { | ||
| s = format!("{}{}", (current % ten).low_u32(), s); | ||
| loop { | ||
| let digit = (current % ten).low_u64() as u8; | ||
| buf[i] = digit + b'0'; | ||
| current = current / ten; | ||
| if current.is_zero() { | ||
| break; | ||
| } | ||
| i -= 1; | ||
| } | ||
|
|
||
| write!(f, "{}", s) | ||
| // sequence of `'0'..'9'` chars is guaranteed to be a valid UTF8 string | ||
| let s = unsafe {::core::str::from_utf8_unchecked(&buf[i..])}; | ||
| f.write_str(s) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1237,10 +1221,9 @@ macro_rules! impl_std_for_uint { | |
|
|
||
| fn from_str(value: &str) -> Result<$name, Self::Err> { | ||
| use $crate::rustc_hex::FromHex; | ||
|
|
||
| let bytes: Vec<u8> = match value.len() % 2 == 0 { | ||
| true => try!(value.from_hex()), | ||
| false => try!(("0".to_owned() + value).from_hex()) | ||
| true => value.from_hex()?, | ||
| false => ("0".to_owned() + value).from_hex()? | ||
| }; | ||
|
|
||
| let bytes_ref: &[u8] = &bytes; | ||
|
|
@@ -1281,14 +1264,25 @@ macro_rules! impl_std_for_uint { | |
| s.parse().unwrap() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl_heapsize_for_uint!($name); | ||
| // `$n_words * 8` because macro expects bytes and | ||
| // uints use 64 bit (8 byte) words | ||
| impl_quickcheck_arbitrary_for_uint!($name, ($n_words * 8)); | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(not(feature="std"))] | ||
| #[cfg(feature="std")] | ||
| #[macro_export] | ||
| #[doc(hidden)] | ||
| macro_rules! impl_std_for_uint { | ||
| ($name: ident, $n_words: tt) => {} | ||
| macro_rules! impl_std_for_uint_internals { | ||
|
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. Why change the name of this macro? If it's used by another macro then you should use a private pattern of that macro rather than a |
||
| ($name: ident, $n_words: tt) => { | ||
| /// Convert to hex string. | ||
| #[deprecated(note = "Use LowerHex instead.")] | ||
| pub fn to_hex(&self) -> String { | ||
| format!("{:x}", self) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(feature="std"))] | ||
|
|
||
This file contains hidden or 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
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.
Why
$n_words * 20? Surely it'sceil(log10(2^($n_words - 1)))? That's 154 for U512 but this code would create a buffer of size 1280 (way too big).Uh oh!
There was an error while loading. Please reload this page.
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.
This change came from https://github.com/paritytech/bigint/blob/master/src/uint.rs#L927 (part of PR #43)
– not sure what their thinking was there, maybe it was making space for a somewhat long debug string (which doesn't make sense).Should I just go ahead and replace it?
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.
Yes please!
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.
Wait a sec, I think you're confusing
uintwithfixed-hash: here theconstruct_uint!macro is called with the words not the number of bytes, so$n_wordshere is8=>8 * 10 == 160which is fine I think.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.
The
construct_hash!macro takes number of bytes, which is more logical imo. Quite confusing.