-
Notifications
You must be signed in to change notification settings - Fork 356
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
Const conversions Uint128 -> Uint256 and Uint256 -> Uint512 #1110
Conversation
Looking commit by commit might make this a bit clearer in this case, I think. ...except for the part where I forgot to update the commit msg. Ahem. |
packages/std/src/math/uint256.rs
Outdated
let words: [u64; 4] = [ | ||
u64::from_le_bytes([ | ||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], | ||
]), | ||
u64::from_le_bytes([ | ||
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], | ||
bytes[15], | ||
]), | ||
0, | ||
0, | ||
]; | ||
Self(U256(words)) |
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.
Wouldn't it be nice to reuse Self::from_be_bytes
here? This reduces the places where we mess with U256 internals.
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.
Yeah, good idea
packages/std/src/math/uint512.rs
Outdated
0, | ||
]; | ||
|
||
Self(U512(words)) |
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.
And with #1111 we can do the same here too, right?
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.
I wonder if that would introduce some overhead though when this function isn't used in a const
context. Does it matter? Probably not.
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.
Good question. Compared to every serialization/deserialization all this conversion is trivial. I think it would be good to avoid exposing implementation details here. Ever Uint* type should have efficient BE/LE exporters and importers. Let's focus on those.
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15], | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
]) | ||
} |
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.
Much nicer!
dce543f
to
fe9d419
Compare
Rebased. |
Do you implement |
That's exactly what I was doing 👍 |
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.
🏅
Closes #1107