Skip to content

RFC: Add enum ReprEndianness constant to PrimeField trait - #4

Open
tarcieri wants to merge 1 commit into
zkcrypto:mainfrom
tarcieri:ff-prime-field-repr-endianness
Open

tarcieri wants to merge 1 commit into
zkcrypto:mainfrom
tarcieri:ff-prime-field-repr-endianness

Conversation

@tarcieri

@tarcieri tarcieri commented Jun 4, 2026 •

Copy link
Copy Markdown

This aims to provide a solution to zkcrypto/group#78.

Rendered

@tarcieri
tarcieri force-pushed the ff-prime-field-repr-endianness branch from 48fb332 to 8f44c47 Compare June 4, 2026 22:07
Downstream consumers of `PrimeField::Repr`, including the `group` crate, need to instead handle it
in an endian-aware manner.

# Guide-level explanation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I may have misunderstood the purpose of this section versus "Reference-level explanation" and could potentially swap them?

Comment on lines +36 to +40
pub enum ReprEndianness {
Big,
#[default]
Little,
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I chose this name based on ff_derive, but I think a pub enum ByteOrder ala the primefield crate might be a little more reusable

tarcieri added a commit to RustCrypto/elliptic-curves that referenced this pull request Jun 16, 2026
Adds a trait for specifying the endianness of the serialized
representation of a field element as a stopgap until zkcrypto/rfcs#4 is
accepted.

This uses the `ByteOrder` enum and replaces the previous
`FIELD_REPR_IS_BE` flag on `PrimeCurveParams`. It also means
`Radix16Decomposition` now "Just Works" and doesn't need an endianness
parameter passed to `new`.

The `primefield::monty_field_element!` macro now writes impls for this
trait, sourcing the endianness information from the `MontyFieldParams`.
@str4d str4d added T-ff Relevant to the `ff` crate maintainers, who will review and decide on the RFC. T-group Relevant to the `group` crate maintainers, who will review and decide on the RFC. labels Sep 23, 2026
pub trait PrimeField {
type Repr: ...;

const REPR_ENDIANNESS: ReprEndianness = ReprEndianness::Little;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Previously we had attempted to avoid exposing any specific endianness in the ff and group APIs, in particular to avoid people accidentally deploying mixed-endianness systems (due to invalid combinations of Field impls with F: Field generic code, e.g. different generic code calling at-the-time-proposed-but-rejected field constructors with different endiannesses). Obviously this failed in the group crate's w-NAF implementation, so we do need a solution here.

Associated constants didn't exist when we started implementing ff (they were added in Rust 1.20, released August 2017), so we didn't consider them initially for this. But I think this would preserve the property we wanted to achieve, which is that a specific ff::PrimeField implementation has one specific representation. So I'm generally in support of this approach.

Comment on lines +58 to +59
# Reference-level explanation
[reference-level-explanation]: #reference-level-explanation

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This section is missing some of the precision we need here. See https://github.com/zkcrypto/rfcs/blob/main/text/0002-group-curveaffine-trait.md for an example: this section needs to specify the actual proposed change, while the guide-level explanation should say what that change looks like to trait users.

}
```

An associated constant added to `PrimeField`, with a default of `ReprEndianness::Little` in order

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm -1 on making this a non-breaking change, because that would immediately make the field completely unreliable in downstream generic code (and potentially lead to mixed-endianness issues in big-endian codebases). As annoying as it is, I think this needs to go into the next set of breaking changes.

Comment on lines +73 to +75
The main drawback of this proposed approach is it leaves the door open to potential misuse where
the associated `const` for `ReprEndianness` isn't checked/honored and code continues to assume that
`PrimeField::Repr` is always little endian.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we make this a breaking change, then code using the ff traits prior to the breaking change would continue to work fine as-is, while code using the ff traits after the breaking change should be fixed as part of that update. We can ensure the changelog for the update makes it clear (e.g. to AI agents that might be doing the update) that existing generic code should be audited for any endianness assumptions.

iterate over the bits of a field element.

The main alternative would be to define an API that allows for iterating over the bits of a
serialized field element in least-to-most significant order, such as `PrimeField::to_le_bits`,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We already have this API: PrimeFieldBits::to_le_bits behind the bits feature flag. We use it in bellman for certain parts of the proof stack.

I'm surprised we don't use it in group::wnaf already. If we did that, does that fix the issue entirely and remove the need for this change? (Or at least for this change to be deployed as non-breaking?)

@tarcieri tarcieri Sep 23, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That would require a dependency on bitvec, which is unsound. We have completely removed this feature from all @RustCrypto crates due to the unsoundness and lack of maintenance.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, it is still a breaking change in group to make this fix, as it requires adding the ff::PrimeFieldBits bound to the group::wnaf APIs. But it doesn't require a breaking change to ff.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T-ff Relevant to the `ff` crate maintainers, who will review and decide on the RFC. T-group Relevant to the `group` crate maintainers, who will review and decide on the RFC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants