Skip to content

Commit

Permalink
MOD: u8 array uses bigendian for deserialize and serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
hongyuanyang-uu committed Dec 27, 2023
1 parent 8163250 commit e626cad
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 46 deletions.
6 changes: 3 additions & 3 deletions core/src/merkle_tree/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::types::merkle_tree::{
RepeatedStorageWrite, TreeKey, TreeOperation, ZkHash,
};
use crate::utils::{deserialize_block_number, serialize_block_number, serialize_tree_leaf};
use byteorder::{ByteOrder, LittleEndian, ReadBytesExt};
use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use itertools::Itertools;
use log::info;
use plonky2::field::goldilocks_field::GoldilocksField;
Expand Down Expand Up @@ -234,12 +234,12 @@ pub(crate) fn serialize_leaf_index_to_key(leaf_index: u64) -> TreeKey {

pub(crate) fn serialize_leaf_index(leaf_index: u64) -> Vec<u8> {
let mut bytes = vec![0; 8];
LittleEndian::write_u64(&mut bytes, leaf_index);
BigEndian::write_u64(&mut bytes, leaf_index);
bytes
}

fn deserialize_leaf_index(mut bytes: &[u8]) -> u64 {
bytes
.read_u64::<LittleEndian>()
.read_u64::<BigEndian>()
.expect("failed to deserialize leaf index")
}
28 changes: 0 additions & 28 deletions core/src/types/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,6 @@ impl AccountTreeId {
pub fn address(&self) -> &Address {
&self.address
}

//todo: the address format need modify!
#[allow(clippy::wrong_self_convention)] // In that case, reference makes more sense.
pub fn to_fixed_bytes(&self) -> [u8; 20] {
let mut result = [0u8; 20];
for (index, item) in self.address.iter().enumerate() {
result[(5 * index)..].copy_from_slice(item.0.to_le_bytes().split_at_mut(5).0);
}
result
}

pub fn from_fixed_bytes(value: [u8; 20]) -> Self {
let mut address = [GoldilocksField::ZERO; TREE_VALUE_LEN];
for index in 0..TREE_VALUE_LEN {
let value = u64::from_le_bytes([
value[0 + index * 5],
value[1 + index * 5],
value[2 + index * 5],
value[3 + index * 5],
value[4 + index * 5],
0,
0,
0,
]);
address[index] = GoldilocksField::from_canonical_u64(value);
}
Self { address }
}
}

impl Default for AccountTreeId {
Expand Down
11 changes: 2 additions & 9 deletions core/src/types/merkle_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub fn u8_arr_to_tree_key(value: &Vec<u8>) -> TreeKey {
.fold(
[GoldilocksField::ZERO; TREE_VALUE_LEN],
|mut tree_key, (index, chunk)| {
tree_key[index] = GoldilocksField::from_canonical_u64(u64::from_le_bytes(
tree_key[index] = GoldilocksField::from_canonical_u64(u64::from_be_bytes(
chunk.map(|e| *e).collect::<Vec<_>>().try_into().unwrap(),
));
tree_key
Expand All @@ -168,7 +168,7 @@ pub fn u8_arr_to_tree_key(value: &Vec<u8>) -> TreeKey {

pub fn tree_key_to_u8_arr(value: &TreeKey) -> Vec<u8> {
value.iter().fold(Vec::new(), |mut key_vec, item| {
key_vec.extend(item.0.to_le_bytes().to_vec());
key_vec.extend(item.0.to_be_bytes().to_vec());
key_vec
})
}
Expand All @@ -185,10 +185,3 @@ pub fn tree_key_to_leaf_index(value: &TreeKey) -> LevelIndex {
let index = tree_key_to_u256(value);
LevelIndex((ROOT_TREE_DEPTH as u16, index))
}

pub fn field_arr_to_u8_arr(value: &Vec<GoldilocksField>) -> Vec<u8> {
value.iter().fold(Vec::new(), |mut key_vec, item| {
key_vec.extend(item.0.to_le_bytes().to_vec());
key_vec
})
}
4 changes: 2 additions & 2 deletions core/src/types/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl StorageKey {

pub fn field_arr_to_u8_arr(value: &Vec<GoldilocksField>) -> Vec<u8> {
value.iter().fold(Vec::new(), |mut key_vec, item| {
key_vec.extend(item.0.to_le_bytes().to_vec());
key_vec.extend(item.0.to_be_bytes().to_vec());
key_vec
})
}
Expand All @@ -69,7 +69,7 @@ pub fn u8_arr_to_field_arr(value: &Vec<u8>) -> Vec<GoldilocksField> {
.into_iter()
.enumerate()
.map(|(_index, chunk)| {
GoldilocksField::from_canonical_u64(u64::from_le_bytes(
GoldilocksField::from_canonical_u64(u64::from_be_bytes(
chunk.map(|e| *e).collect::<Vec<_>>().try_into().unwrap(),
))
})
Expand Down
8 changes: 4 additions & 4 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::types::merkle_tree::TREE_VALUE_LEN;
use byteorder::ReadBytesExt;
use byteorder::{ByteOrder, LittleEndian};
use byteorder::{BigEndian, ByteOrder};
use plonky2::field::goldilocks_field::GoldilocksField;

pub const U8_BITS_MASK: u64 = 0xff;
Expand All @@ -26,20 +26,20 @@ pub fn split_u16_limbs_from_field(value: &GoldilocksField) -> (u64, u64) {

pub fn serialize_block_number(block_number: u32) -> Vec<u8> {
let mut bytes = vec![0; 4];
LittleEndian::write_u32(&mut bytes, block_number);
BigEndian::write_u32(&mut bytes, block_number);
bytes
}

pub fn deserialize_block_number(mut bytes: &[u8]) -> u32 {
bytes
.read_u32::<LittleEndian>()
.read_u32::<BigEndian>()
.expect("failed to deserialize block number")
}

pub fn serialize_tree_leaf(leaf: [GoldilocksField; TREE_VALUE_LEN]) -> Vec<u8> {
let mut bytes = vec![0; 32];
for (index, item) in leaf.iter().enumerate() {
let field_array = item.0.to_le_bytes();
let field_array = item.0.to_be_bytes();
bytes[index * 8..(index * 8 + 8)].copy_from_slice(&field_array);
}
bytes
Expand Down

0 comments on commit e626cad

Please sign in to comment.