Skip to content

Commit

Permalink
Use le for scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-lj committed Aug 6, 2024
1 parent f681a6e commit 6641943
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 3 additions & 6 deletions fastcrypto/src/groups/bn254/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,18 @@ impl groups::Scalar for Scalar {

impl ToFromByteArray<SCALAR_LENGTH> for Scalar {
fn from_byte_array(bytes: &[u8; SCALAR_LENGTH]) -> Result<Self, FastCryptoError> {
// 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
}
}
Expand Down
8 changes: 5 additions & 3 deletions fastcrypto/src/groups/bn254/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn test_serde_and_regression() {
verify_serialization(
&s1,
Some(
hex::decode("0000000000000000000000000000000000000000000000000000000000000001")
hex::decode("0100000000000000000000000000000000000000000000000000000000000000")
.unwrap()
.as_slice(),
),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 6641943

Please sign in to comment.