Skip to content
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

Merged
merged 5 commits into from
Sep 6, 2021

Conversation

webmaster128
Copy link
Member

@webmaster128 webmaster128 commented Sep 2, 2021

This is a PoC. Testing is incomplete and we also want a version for big endian in order to make new const.

Based on #1073

@webmaster128 webmaster128 changed the title Make from_le_bytes const Make Uint256::from_le_bytes const Sep 2, 2021
@webmaster128 webmaster128 mentioned this pull request Sep 2, 2021
@webmaster128 webmaster128 marked this pull request as ready for review September 3, 2021 15:31
@webmaster128 webmaster128 changed the title Make Uint256::from_le_bytes const Make Uint256::from_{le,be}_bytes and ::new const Sep 3, 2021
@webmaster128 webmaster128 added this to the 0.16.2 milestone Sep 6, 2021
Copy link
Contributor

@uint uint left a 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 👍

Comment on lines +66 to +82
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))
}
Copy link
Contributor

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));
Copy link
Contributor

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.

@webmaster128 webmaster128 merged commit 604e6af into main Sep 6, 2021
@webmaster128 webmaster128 deleted the const-constructors branch September 6, 2021 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants