|
1 | | -use alexandria_encoding::reversible::reversing; |
2 | 1 | use core::byte_array::ByteArrayTrait; |
3 | 2 | use core::integer::u512; |
| 3 | +use core::num::traits::Zero; |
| 4 | +use core::ops::{AddAssign, MulAssign}; |
4 | 5 | use super::bit_array::{one_shift_left_bytes_felt252, one_shift_left_bytes_u128}; |
5 | 6 |
|
6 | 7 | /// Generic support trait for appending signed and unsigned integers onto byte storage. |
@@ -284,3 +285,47 @@ impl ByteAppenderImpl<T, +Drop<T>, +ByteAppenderSupportTrait<T>> of ByteAppender |
284 | 285 |
|
285 | 286 | impl ArrayU8ByteAppenderImpl = ByteAppenderImpl<Array<u8>>; |
286 | 287 | impl ByteArrayByteAppenderImpl = ByteAppenderImpl<ByteArray>; |
| 288 | + |
| 289 | +// Duplicated functions from encoding. To fix dependency publish problem. |
| 290 | + |
| 291 | +#[inline] |
| 292 | +pub fn reversing< |
| 293 | + T, |
| 294 | + +Copy<T>, |
| 295 | + +Zero<T>, |
| 296 | + +TryInto<T, NonZero<T>>, |
| 297 | + +DivRem<T>, |
| 298 | + +Drop<T>, |
| 299 | + +MulAssign<T, T>, |
| 300 | + +Rem<T>, |
| 301 | + +AddAssign<T, T>, |
| 302 | +>( |
| 303 | + word: T, size: usize, step: T, |
| 304 | +) -> (T, T) { |
| 305 | + let result = Zero::zero(); |
| 306 | + reversing_partial_result(word, result, size, step) |
| 307 | +} |
| 308 | + |
| 309 | +#[inline] |
| 310 | +pub fn reversing_partial_result< |
| 311 | + T, |
| 312 | + +Copy<T>, |
| 313 | + +DivRem<T>, |
| 314 | + +TryInto<T, NonZero<T>>, |
| 315 | + +Drop<T>, |
| 316 | + +MulAssign<T, T>, |
| 317 | + +Rem<T>, |
| 318 | + +AddAssign<T, T>, |
| 319 | +>( |
| 320 | + mut word: T, mut onto: T, size: usize, step: T, |
| 321 | +) -> (T, T) { |
| 322 | + let mut i: usize = 0; |
| 323 | + while i != size { |
| 324 | + let (new_word, remainder) = DivRem::div_rem(word, step.try_into().unwrap()); |
| 325 | + word = new_word; |
| 326 | + onto *= step.into(); |
| 327 | + onto += remainder; |
| 328 | + i += 1; |
| 329 | + }; |
| 330 | + (onto, word) |
| 331 | +} |
0 commit comments