From 5f7b742c02e8b98c5f01102a12a385ee45cc3d32 Mon Sep 17 00:00:00 2001 From: Dragan Pilipovic Date: Fri, 26 Jan 2024 00:51:02 +0100 Subject: [PATCH] set everything to LE --- ffi_interface/src/interop.rs | 9 ++++----- ffi_interface/src/lib.rs | 23 ++++++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ffi_interface/src/interop.rs b/ffi_interface/src/interop.rs index 2e55514..d8b4ad8 100644 --- a/ffi_interface/src/interop.rs +++ b/ffi_interface/src/interop.rs @@ -58,7 +58,7 @@ pub fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_commit ) } - // Each 32-be-bytes are interpreted as field elements. + // Each 32-le-bytes are interpreted as field elements. let mut scalars: Vec = Vec::with_capacity(n_scalars); for b in inp.chunks(32) { scalars.push(Fr::from_le_bytes_mod_order(b)); @@ -74,7 +74,6 @@ pub fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_commit scalar .serialize_compressed(&mut scalar_bytes[..]) .expect("could not serialise Fr into a 32 byte array"); - scalar_bytes.reverse(); scalar_bytes.to_vec() } @@ -123,7 +122,7 @@ pub(crate) fn group_to_field(point: &Element) -> Fr { mod test { use super::Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_pedersenHash; use crate::{ - commit_to_scalars, deprecated_serialize_commitment, fr_to_be_bytes, hash_commitment, + commit_to_scalars, deprecated_serialize_commitment, fr_to_le_bytes, hash_commitment, interop::{ Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_commit, Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_commitRoot, @@ -152,7 +151,7 @@ mod test { let scalars: Vec<_> = (0..256) .flat_map(|i| { let val = Fr::from((i + 1) as u128); - fr_to_be_bytes(-val) + fr_to_le_bytes(-val) }) .collect(); @@ -172,7 +171,7 @@ mod test { let scalars: Vec<_> = (0..256) .flat_map(|i| { let val = Fr::from((i + 1) as u128); - fr_to_be_bytes(-val) + fr_to_le_bytes(-val) }) .collect(); diff --git a/ffi_interface/src/lib.rs b/ffi_interface/src/lib.rs index 9f09157..fc1429e 100644 --- a/ffi_interface/src/lib.rs +++ b/ffi_interface/src/lib.rs @@ -58,11 +58,10 @@ fn _commit_to_scalars(committer: &DefaultCommitter, scalars: &[u8]) -> Result ScalarBytes { // TODO: We could introduce a method named `hash_commit_to_scalars` // TODO: which would save this serialization roundtrip. We should profile/check that // TODO: this is actually a bottleneck for the average workflow before doing this. - fr_to_be_bytes(Element::from_bytes_unchecked_uncompressed(commitment).map_to_scalar_field()) + fr_to_le_bytes(Element::from_bytes_unchecked_uncompressed(commitment).map_to_scalar_field()) } /// Hashes a vector of commitments. /// @@ -165,18 +164,19 @@ pub fn hash_commitments(commitments: &[CommitmentBytes]) -> Vec { Element::batch_map_to_scalar_field(&elements) .into_iter() - .map(fr_to_be_bytes) + .map(fr_to_le_bytes) .collect() } /// This is kept so that commitRoot in the java implementation can be swapped out /// Note: I believe we should not need to expose this method. -pub fn deprecated_serialize_commitment(commitment: CommitmentBytes) -> [u8; 32] { - Element::from_bytes_unchecked_uncompressed(commitment).to_bytes() +pub fn deprecated_serialize_commitment(commitment: CommitmentBytes) -> [u8; 64] { + Element::from_bytes_unchecked_uncompressed(commitment).to_bytes_uncompressed() } // TODO: We use big endian bytes here to be interopable with the java implementation // TODO: we should stick to one endianness everywhere to avoid confusion +#[allow(dead_code)] fn fr_to_be_bytes(fr: banderwagon::Fr) -> [u8; 32] { let mut bytes = [0u8; 32]; fr.serialize_compressed(&mut bytes[..]) @@ -185,6 +185,7 @@ fn fr_to_be_bytes(fr: banderwagon::Fr) -> [u8; 32] { bytes.reverse(); bytes } +#[allow(dead_code)] fn fr_from_be_bytes(bytes: &[u8]) -> Result { let mut bytes = bytes.to_vec(); bytes.reverse(); // deserialize expects the bytes to be in little endian order @@ -349,7 +350,7 @@ mod tests { crs::CRS, }; - use crate::{fr_from_be_bytes, fr_to_be_bytes, fr_to_le_bytes}; + use crate::{fr_from_le_bytes, fr_to_le_bytes}; #[test] fn commitment_update() { let crs = CRS::default(); @@ -429,10 +430,10 @@ mod tests { } #[test] - fn from_be_to_be_bytes() { + fn from_le_to_le_bytes() { let value = banderwagon::Fr::from(123456u128); - let bytes = fr_to_be_bytes(value); - let got_value = fr_from_be_bytes(&bytes).unwrap(); + let bytes = fr_to_le_bytes(value); + let got_value = fr_from_le_bytes(&bytes).unwrap(); assert_eq!(got_value, value) } }