-
Notifications
You must be signed in to change notification settings - Fork 336
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
Make Uint256::from_{le,be}_bytes and ::new const #1071
Conversation
f32b576
to
5d0745a
Compare
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.
Simple solution. Makes sense to me 👍
pub const fn from_be_bytes(data: [u8; 32]) -> Self { | ||
let words: [u64; 4] = [ | ||
u64::from_le_bytes([ | ||
data[31], data[30], data[29], data[28], data[27], data[26], data[25], data[24], | ||
]), | ||
u64::from_le_bytes([ | ||
data[23], data[22], data[21], data[20], data[19], data[18], data[17], data[16], | ||
]), | ||
u64::from_le_bytes([ | ||
data[15], data[14], data[13], data[12], data[11], data[10], data[9], data[8], | ||
]), | ||
u64::from_le_bytes([ | ||
data[7], data[6], data[5], data[4], data[3], data[2], data[1], data[0], | ||
]), | ||
]; | ||
Uint256(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.
I feel embarrassed I didn't think of the simple solution here. I tried to slice the data
and failed because I couldn't transform/convert it in a const
context.
Nice!
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 42, | ||
]); | ||
assert_eq!(a, Uint256::from(42u128)); |
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.
Douglas Adams is the best.
c7ad0e8
to
e76043e
Compare
This is a PoC. Testing is incomplete and we also want a version for big endian in order to makenew
const.Based on #1073