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

Implement a checked conversion from bytes method in Fq #81

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

cronokirby
Copy link
Contributor

Can help implement #80.

/// None, enforcing canonical serialization.
pub fn from_bytes_checked(bytes: &[u8; 32]) -> Option<Self> {
let reduced = Self::from_raw_bytes(bytes);
if reduced.to_bytes_le() == *bytes {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to the contents of this pr, but why is this to_bytes_le rather than just to_bytes? are we ever doing big-endian serialization? if not, could we have it just be to_bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My motivation was that:

  • Arkworks has from_bytes_le and from_bytes_be methods, so this mirrors that.
  • Furthermore, I think it's good to specify what the endianness is, because it prevents any confusion on the user's part.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bikeshed, but my counterpoints would be:

  • it's inconsistent to have from_bytes but to_bytes_le
  • it's better to not convey endianness, because it signals that endianness is not a user-visible concern, the cryptography layer just exposes an "encoding" abstraction which is to be treated as opaque bytes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, although for context, there's a separate type for the Encoding of an Element on the curve, and in practice there's no reason for users to be aware of Fq at all. For Fr, this is a reasonable point though, yeah. One reason having those can be nice is that the direct methods on each field are also what we use to implement the Arkworks traits, and there, because different methods care about endiannness, it's useful to know what endianness we're using when implementing them.

In an ideal world Arkworks would be more opaque; alas.

@cronokirby cronokirby merged commit 6c1aea3 into main Jan 30, 2024
6 checks passed
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