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

request: Fr::from_bytes() and Fr::to_bytes() #31

Open
dan-da opened this issue Sep 8, 2021 · 2 comments
Open

request: Fr::from_bytes() and Fr::to_bytes() #31

dan-da opened this issue Sep 8, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@dan-da
Copy link
Contributor

dan-da commented Sep 8, 2021

Even though Fr is really just an [u64; 4] internally, I don't presently see an easy way to go to/from bytes.

Can we add this please?

@dan-da dan-da added the enhancement New feature or request label Sep 8, 2021
@iancoleman
Copy link
Contributor

There's the convert functions, if that helps?

blsttc/src/convert.rs

Lines 10 to 37 in 94a71e6

/// Convert big endian bytes to bls12_381::Fr
pub fn fr_from_be_bytes(bytes: [u8; SK_SIZE]) -> FromBytesResult<Fr> {
let mut le_bytes = bytes;
le_bytes.reverse();
let mut fr_u64s = [0u64; 4];
for i in 0..4 {
let mut next_u64_bytes = [0u8; 8];
for j in 0..8 {
next_u64_bytes[j] = le_bytes[i * 8 + j];
}
fr_u64s[i] = u64::from_le_bytes(next_u64_bytes);
}
Ok(Fr::from_repr(FrRepr(fr_u64s))?)
}
/// Convert bls12_381::Fr to big endian bytes
pub fn fr_to_be_bytes(fr: Fr) -> [u8; SK_SIZE] {
let mut bytes = [0u8; SK_SIZE];
// iterating 4 u64s which are in order suiting little endian bytes
// and must be reversed to get big endian bytes
fr.into_repr().0.iter().enumerate().for_each(|(le_i, n)| {
let be_i = 4 - le_i - 1;
n.to_be_bytes().iter().enumerate().for_each(|(j, byte)| {
bytes[8 * be_i + j] = *byte;
});
});
bytes
}

@dan-da
Copy link
Contributor Author

dan-da commented Sep 8, 2021

thx, I missed / forgot that. definitely helps me right now.

Still, the reason I didn't see it is because I was looking all over Fr and FrRepr in the docs and didn't see anything about bytes. Since other types have ::from_bytes() and ::to_bytes() methods I thought Fr would/should also.

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