Open weikengchen opened 8 months ago
Related: https://github.com/l2iterative/ark-bn254-r0/blob/main/src/fields/fr.rs
Currently, without this flexibility in FpConfig
, one would likely need to implement Fp
anew.
Sounds reasonable. Plonky2/3 does this for small prime fields, and holds a potentially non-canonical representation.
This is reasonable
Some software optimization for special primes uses unnormalized representations. For example, when two numbers are added together, the number does not always be smaller than the modulus. The same can hold for multiplication. In such situations, 1 and p+1 may both be valid results.
FpConfig
allows certain level of flexibility.We can make sure serialization always handles the normalized representation, by implementing the
into_bigint
in some special way.But the problem rests on
PartialEq
andEq
.Currently,
PartialEq
andEq
are auto-derived inFp
, which is auto-derived inBigInt
as well. It examines the u64 limbs and require each limb to be the same. If we have 1 and p+1,PartialEq
andEq
will return negative for them.To solve this problem, it is advisable to add a method in
FpConfig
, likely callednormalize
, which supposedly modifyp+1
into1
. Instead of auto-deriving thePartialEq
andEq
, we implement them manually withnormalize
.