Repository navigation
Conversation
48fb332 to
8f44c47
Compare
| Downstream consumers of `PrimeField::Repr`, including the `group` crate, need to instead handle it | ||
| in an endian-aware manner. | ||
|
|
||
| # Guide-level explanation |
There was a problem hiding this comment.
I may have misunderstood the purpose of this section versus "Reference-level explanation" and could potentially swap them?
| pub enum ReprEndianness { | ||
| Big, | ||
| #[default] | ||
| Little, | ||
| } |
There was a problem hiding this comment.
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
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`.
| pub trait PrimeField { | ||
| type Repr: ...; | ||
|
|
||
| const REPR_ENDIANNESS: ReprEndianness = ReprEndianness::Little; |
There was a problem hiding this comment.
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.
| # Reference-level explanation | ||
| [reference-level-explanation]: #reference-level-explanation |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| 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. |
There was a problem hiding this comment.
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`, |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 aims to provide a solution to zkcrypto/group#78.
Rendered