diff --git a/fastcrypto/src/groups/bn254/scalar.rs b/fastcrypto/src/groups/bn254/scalar.rs index 3aea9b5a2b..e44f405913 100644 --- a/fastcrypto/src/groups/bn254/scalar.rs +++ b/fastcrypto/src/groups/bn254/scalar.rs @@ -63,21 +63,18 @@ impl groups::Scalar for Scalar { impl ToFromByteArray for Scalar { fn from_byte_array(bytes: &[u8; SCALAR_LENGTH]) -> Result { - // Arkworks uses little-endian byte order for serialization, but we use big-endian. - let mut reversed = *bytes; - reversed.reverse(); - Fr::deserialize_compressed(reversed.as_slice()) + // Note that arkworks uses little-endian byte order for serialization here. + Fr::deserialize_compressed(bytes.as_slice()) .map_err(|_| FastCryptoError::InvalidInput) .map(Scalar) } fn to_byte_array(&self) -> [u8; SCALAR_LENGTH] { + // Note that arkworks uses little-endian byte order for serialization here. let mut bytes = [0u8; SCALAR_LENGTH]; self.0 .serialize_compressed(bytes.as_mut_slice()) .expect("Never fails"); - // Arkworks uses little-endian byte order for serialization, but we use big-endian. - bytes.reverse(); bytes } } diff --git a/fastcrypto/src/groups/bn254/tests.rs b/fastcrypto/src/groups/bn254/tests.rs index 0a0c629d0b..549d54c9e7 100644 --- a/fastcrypto/src/groups/bn254/tests.rs +++ b/fastcrypto/src/groups/bn254/tests.rs @@ -184,7 +184,7 @@ fn test_serde_and_regression() { verify_serialization( &s1, Some( - hex::decode("0000000000000000000000000000000000000000000000000000000000000001") + hex::decode("0100000000000000000000000000000000000000000000000000000000000000") .unwrap() .as_slice(), ), @@ -225,13 +225,15 @@ fn test_serialization_scalar() { assert_eq!(Scalar::from_byte_array(&bytes).unwrap(), Scalar::zero()); // Scalar::from_byte_array should not accept the order or above it. - let order = + let mut order = hex::decode("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001").unwrap(); + order.reverse(); // Little-endian assert!(Scalar::from_byte_array(<&[u8; 32]>::try_from(order.as_slice()).unwrap()).is_err()); // Scalar::from_byte_array should accept the order - 1. - let order_minus_one = + let mut order_minus_one = hex::decode("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000").unwrap(); + order_minus_one.reverse(); // Little-endian assert_eq!( Scalar::from_byte_array(<&[u8; 32]>::try_from(order_minus_one.as_slice()).unwrap()) .unwrap(),