Skip to content

Commit

Permalink
modif in from_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Oct 17, 2023
1 parent 646874e commit 6b6cc01
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions crates/utils/src/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -528,42 +528,43 @@ impl U256Impl of U256Trait {
#[generate_trait]
impl ByteArrayExt of ByteArrayExTrait {
fn from_bytes(mut bytes: Span<u8>) -> ByteArray {
//TODO(eni): optimize deserialization of Span<u8> to ByteArray;
// we can just deserialize bytes by chunks of 31, skipping pending_word
// checks
let mut arr: ByteArray = Default::default();
let (mut q, mut r) = DivRem::div_rem(bytes.len(), 31_u32.try_into().unwrap());
let (mut nb_full_words, mut pending_word_len) = DivRem::div_rem(
bytes.len(), 31_u32.try_into().unwrap()
);
let mut i = 0;
loop {
if q == 0 {
if i == nb_full_words {
break;
};
let mut word: felt252 = 0;
let mut i = 0;
let mut j = 0;
loop {
if i == 31 {
if j == 31 {
break;
};
word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into();
i += 1;
j += 1;
};
arr.data.append(word.try_into().unwrap());
q -= 1;
arr.data.append(word.try_into().unwrap());
i += 1;
};

if r == 0 {
if pending_word_len == 0 {
return arr;
};

arr.pending_word_len = r;
let mut pending_word: felt252 = 0;
let mut i = 0;

loop {
if r == 0 {
if i == pending_word_len {
break;
};
pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into();
r -= 1;
i += 1;
};
arr.pending_word_len = pending_word_len;
arr.pending_word = pending_word;
arr
}
Expand Down

0 comments on commit 6b6cc01

Please sign in to comment.