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

feat: u64 Byte reverse #3589

Open
feltroidprime opened this issue Jul 4, 2023 · 1 comment
Open

feat: u64 Byte reverse #3589

feltroidprime opened this issue Jul 4, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@feltroidprime
Copy link
Contributor

feltroidprime commented Jul 4, 2023

Hello!

reverse endianness of u64 is useful in many cases when dealing with keccak

I wanted to implement it based off this https://github.com/starkware-libs/cairo-lang/blob/3a32740d0a743af6f4bfe227d1b5ecab0fc2884b/src/starkware/cairo/common/uint256.cairo#L464-L510

with 3 steps instead of 4

fn u64_byte_reverse(word: u64) -> u64 {
    let word: u128 = word.into();
    let word = (word & 0x00ff00ff00ff00ff00ff00ff00ff00ff) * (65535) + word;
    let word = (word & 0x00ffff0000ffff0000ffff0000ffff00) * (4294967295) + word;
    let word = (word & 0x0fffffffffffffff0000000000000000) * (18446744073709551615) + word;

    return (word / (72057594037927936)).try_into().unwrap();
}

the problem is that & is not implemented on felt252 and u128 isn't large enough and gives mul overflow

is it possible to add a u64_byte_reverse in the integer core lib ?

thanks!!

@feltroidprime feltroidprime added the enhancement New feature or request label Jul 4, 2023
@orizi
Copy link
Collaborator

orizi commented Jul 10, 2023

we should add these for additional types - but currently you can rather efficiently implement by:

fn u64_byte_reverse(word: u64) -> u64 {
    (u128_byte_reverse(word.into()) / 0x10000000000000000).try_into().unwrap()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants