From 646874eca848c9b8336a6d1a4b32bd4388f3a795 Mon Sep 17 00:00:00 2001 From: chachaleo Date: Tue, 17 Oct 2023 17:28:38 +0700 Subject: [PATCH 1/3] modification function from_byte --- crates/utils/src/helpers.cairo | 37 ++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 5153a7e16..826ae240f 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -7,6 +7,8 @@ use utils::constants::{ POW_256_16, }; use keccak::u128_split; +use traits::DivRem; +use integer::U32TryIntoNonZero; /// Ceils a number of bits to the next word (32 bytes) @@ -530,12 +532,39 @@ impl ByteArrayExt of ByteArrayExTrait { // 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()); loop { - match bytes.pop_front() { - Option::Some(byte) => { arr.append_byte(*byte); }, - Option::None => { break; } - } + if q == 0 { + break; + }; + let mut word: felt252 = 0; + let mut i = 0; + loop { + if i == 31 { + break; + }; + word = word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + i += 1; + }; + arr.data.append(word.try_into().unwrap()); + q -= 1; + }; + + if r == 0 { + return arr; + }; + + arr.pending_word_len = r; + let mut pending_word: felt252 = 0; + + loop { + if r == 0 { + break; + }; + pending_word = pending_word * POW_256_1.into() + (*bytes.pop_front().unwrap()).into(); + r -= 1; }; + arr.pending_word = pending_word; arr } } From 6b6cc01c8bafc67abaf954d4124680ed374fee60 Mon Sep 17 00:00:00 2001 From: chachaleo Date: Tue, 17 Oct 2023 19:03:01 +0700 Subject: [PATCH 2/3] modif in from_bytes --- crates/utils/src/helpers.cairo | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 826ae240f..2eb94f6b2 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -528,42 +528,43 @@ impl U256Impl of U256Trait { #[generate_trait] impl ByteArrayExt of ByteArrayExTrait { fn from_bytes(mut bytes: Span) -> ByteArray { - //TODO(eni): optimize deserialization of Span 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 } From 9605b49b546020bcf11e02f2968f51e4d67b09e4 Mon Sep 17 00:00:00 2001 From: Charlotte <49371958+chachaleo@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:27:20 +0700 Subject: [PATCH 3/3] Update crates/utils/src/helpers.cairo Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com> --- crates/utils/src/helpers.cairo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/utils/src/helpers.cairo b/crates/utils/src/helpers.cairo index 2eb94f6b2..b77d329e9 100644 --- a/crates/utils/src/helpers.cairo +++ b/crates/utils/src/helpers.cairo @@ -529,7 +529,7 @@ impl U256Impl of U256Trait { impl ByteArrayExt of ByteArrayExTrait { fn from_bytes(mut bytes: Span) -> ByteArray { let mut arr: ByteArray = Default::default(); - let (mut nb_full_words, mut pending_word_len) = DivRem::div_rem( + let (nb_full_words, pending_word_len) = DivRem::div_rem( bytes.len(), 31_u32.try_into().unwrap() ); let mut i = 0;