Skip to content

Commit 710aa7b

Browse files
committed
Use le for scalars
1 parent f82b1de commit 710aa7b

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

fastcrypto/src/groups/bn254/scalar.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,18 @@ impl groups::Scalar for Scalar {
6363

6464
impl ToFromByteArray<SCALAR_LENGTH> for Scalar {
6565
fn from_byte_array(bytes: &[u8; SCALAR_LENGTH]) -> Result<Self, FastCryptoError> {
66-
// Arkworks uses little-endian byte order for serialization, but we use big-endian.
67-
let mut reversed = *bytes;
68-
reversed.reverse();
69-
Fr::deserialize_compressed(reversed.as_slice())
66+
// Note that arkworks uses little-endian byte order for serialization here.
67+
Fr::deserialize_compressed(bytes.as_slice())
7068
.map_err(|_| FastCryptoError::InvalidInput)
7169
.map(Scalar)
7270
}
7371

7472
fn to_byte_array(&self) -> [u8; SCALAR_LENGTH] {
73+
// Note that arkworks uses little-endian byte order for serialization here.
7574
let mut bytes = [0u8; SCALAR_LENGTH];
7675
self.0
7776
.serialize_compressed(bytes.as_mut_slice())
7877
.expect("Never fails");
79-
// Arkworks uses little-endian byte order for serialization, but we use big-endian.
80-
bytes.reverse();
8178
bytes
8279
}
8380
}

fastcrypto/src/groups/bn254/tests.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn test_serde_and_regression() {
184184
verify_serialization(
185185
&s1,
186186
Some(
187-
hex::decode("0000000000000000000000000000000000000000000000000000000000000001")
187+
hex::decode("0100000000000000000000000000000000000000000000000000000000000000")
188188
.unwrap()
189189
.as_slice(),
190190
),
@@ -225,13 +225,15 @@ fn test_serialization_scalar() {
225225
assert_eq!(Scalar::from_byte_array(&bytes).unwrap(), Scalar::zero());
226226

227227
// Scalar::from_byte_array should not accept the order or above it.
228-
let order =
228+
let mut order =
229229
hex::decode("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001").unwrap();
230+
order.reverse(); // Little-endian
230231
assert!(Scalar::from_byte_array(<&[u8; 32]>::try_from(order.as_slice()).unwrap()).is_err());
231232

232233
// Scalar::from_byte_array should accept the order - 1.
233-
let order_minus_one =
234+
let mut order_minus_one =
234235
hex::decode("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000000").unwrap();
236+
order_minus_one.reverse(); // Little-endian
235237
assert_eq!(
236238
Scalar::from_byte_array(<&[u8; 32]>::try_from(order_minus_one.as_slice()).unwrap())
237239
.unwrap(),

0 commit comments

Comments
 (0)