ff_derive: Generate modulus representation with correct endianness

Now that PrimeField::ReprEndianness exists, users can obtain a
known-endianness representation from the output of PrimeField::char
(which is a PrimeField::Repr, and should return a representation with
the same endianness as PrimeField::into_repr).
This commit is contained in:
Jack Grigg
2020-05-02 18:25:26 +12:00
parent 15e229509a
commit 9114c367f4
3 changed files with 42 additions and 29 deletions

View File

@@ -1644,7 +1644,8 @@ fn test_fq_pow() {
use byteorder::ByteOrder;
let mut char_limbs = [0; 6];
byteorder::LittleEndian::read_u64_into(Fq::char().as_ref(), &mut char_limbs);
byteorder::BigEndian::read_u64_into(Fq::char().as_ref(), &mut char_limbs);
char_limbs.reverse();
for _ in 0..1000 {
// Exponentiating by the modulus should have no effect in a prime field.

View File

@@ -147,13 +147,11 @@ fn test_g1_uncompressed_invalid_vectors() {
}
}
// PrimeField::char() returns the modulus in its little-endian byte representation,
// but Fq field elements use big-endian encoding, so flip the endianness.
let m: Vec<_> = Fq::char().as_ref().iter().cloned().rev().collect();
let m = Fq::char();
{
let mut o = o;
o.as_mut()[..48].copy_from_slice(&m[..]);
o.as_mut()[..48].copy_from_slice(m.as_ref());
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
assert_eq!(coordinate, "x coordinate");
@@ -164,7 +162,7 @@ fn test_g1_uncompressed_invalid_vectors() {
{
let mut o = o;
o.as_mut()[48..].copy_from_slice(&m[..]);
o.as_mut()[48..].copy_from_slice(m.as_ref());
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
assert_eq!(coordinate, "y coordinate");
@@ -265,13 +263,11 @@ fn test_g2_uncompressed_invalid_vectors() {
}
}
// PrimeField::char() returns the modulus in its little-endian byte representation,
// but Fq field elements use big-endian encoding, so flip the endianness.
let m: Vec<_> = Fq::char().as_ref().iter().cloned().rev().collect();
let m = Fq::char();
{
let mut o = o;
o.as_mut()[..48].copy_from_slice(&m[..]);
o.as_mut()[..48].copy_from_slice(m.as_ref());
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
assert_eq!(coordinate, "x coordinate (c1)");
@@ -282,7 +278,7 @@ fn test_g2_uncompressed_invalid_vectors() {
{
let mut o = o;
o.as_mut()[48..96].copy_from_slice(&m[..]);
o.as_mut()[48..96].copy_from_slice(m.as_ref());
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
assert_eq!(coordinate, "x coordinate (c0)");
@@ -293,7 +289,7 @@ fn test_g2_uncompressed_invalid_vectors() {
{
let mut o = o;
o.as_mut()[96..144].copy_from_slice(&m[..]);
o.as_mut()[96..144].copy_from_slice(m.as_ref());
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
assert_eq!(coordinate, "y coordinate (c1)");
@@ -304,7 +300,7 @@ fn test_g2_uncompressed_invalid_vectors() {
{
let mut o = o;
o.as_mut()[144..].copy_from_slice(&m[..]);
o.as_mut()[144..].copy_from_slice(m.as_ref());
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
assert_eq!(coordinate, "y coordinate (c0)");
@@ -411,13 +407,11 @@ fn test_g1_compressed_invalid_vectors() {
}
}
// PrimeField::char() returns the modulus in its little-endian byte representation,
// but Fq field elements use big-endian encoding, so flip the endianness.
let m: Vec<_> = Fq::char().as_ref().iter().cloned().rev().collect();
let m = Fq::char();
{
let mut o = o;
o.as_mut()[..48].copy_from_slice(&m[..]);
o.as_mut()[..48].copy_from_slice(m.as_ref());
o.as_mut()[0] |= 0b1000_0000;
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
@@ -527,13 +521,11 @@ fn test_g2_compressed_invalid_vectors() {
}
}
// PrimeField::char() returns the modulus in its little-endian byte representation,
// but Fq field elements use big-endian encoding, so flip the endianness.
let m: Vec<_> = Fq::char().as_ref().iter().cloned().rev().collect();
let m = Fq::char();
{
let mut o = o;
o.as_mut()[..48].copy_from_slice(&m[..]);
o.as_mut()[..48].copy_from_slice(m.as_ref());
o.as_mut()[0] |= 0b1000_0000;
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {
@@ -545,7 +537,7 @@ fn test_g2_compressed_invalid_vectors() {
{
let mut o = o;
o.as_mut()[48..96].copy_from_slice(&m[..]);
o.as_mut()[48..96].copy_from_slice(m.as_ref());
o.as_mut()[0] |= 0b1000_0000;
if let Err(GroupDecodingError::CoordinateDecodingError(coordinate)) = o.into_affine() {