Skip to content

Commit

Permalink
Upgrading to 2.5.4 (#281)
Browse files Browse the repository at this point in the history
## Pull Request type


Please check the type of change your PR introduces:

- [ ] Bugfix
- [ ] Feature
- [x] Code style update (formatting, renaming)
- [x] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):


## Other information

Upgraded to 2.5.4 + adapted some of the code to use new `while () {}`
loop syntax
  • Loading branch information
gaetbout authored Feb 26, 2024
1 parent c6e777e commit e7b6957
Show file tree
Hide file tree
Showing 27 changed files with 112 additions and 263 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.5.3
scarb 2.5.4
3 changes: 2 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ name = "alexandria"
version = "0.1.0"
description = "Community maintained Cairo and Starknet libraries"
homepage = "https://github.com/keep-starknet-strange/alexandria/"
cairo-version = "2.5.4"

[workspace.dependencies]
starknet = ">=2.5.3"
starknet = "=2.5.4"

[workspace.tool.fmt]
sort-module-level-items = true
Expand Down
2 changes: 1 addition & 1 deletion src/bytes/src/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl BytesImpl of BytesTrait {
fn append_u128_packed(ref self: Bytes, value: u128, size: usize) {
assert(size <= 16, 'size must be less than 16');

let Bytes{size: old_bytes_size, mut data } = self;
let Bytes { size: old_bytes_size, mut data } = self;
let (last_data_index, last_element_size) = BytesTrait::locate(old_bytes_size);

if last_element_size == 0 {
Expand Down
24 changes: 12 additions & 12 deletions src/bytes/src/tests/test_bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -439,53 +439,53 @@ fn test_bytes_append() {

// append_u128_packed
bytes.append_u128_packed(0x101112131415161718, 9);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 9, "append_u128_packed_1_size");
assert_eq!(*data[0], 0x10111213141516171800000000000000, "append_u128_packed_1_value_1");
bytes = Bytes { size: size, data: data };

bytes.append_u128_packed(0x101112131415161718, 9);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 18, "append_u128_packed_2_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u128_packed_2_value_1");
assert_eq!(*data[1], 0x17180000000000000000000000000000, "append_u128_packed_2_value_2");
bytes = Bytes { size: size, data: data };

// append_u8
bytes.append_u8(0x01);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 19, "append_u8_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u8_value_1");
assert_eq!(*data[1], 0x17180100000000000000000000000000, "append_u8_value_2");
bytes = Bytes { size: size, data: data };

// append_u16
bytes.append_u16(0x0102);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 21, "append_u16_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u16_value_1");
assert_eq!(*data[1], 0x17180101020000000000000000000000, "append_u16_value_2");
bytes = Bytes { size: size, data: data };

// append_u32
bytes.append_u32(0x01020304);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 25, "append_u32_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u32_value_1");
assert_eq!(*data[1], 0x17180101020102030400000000000000, "append_u32_value_2");
bytes = Bytes { size: size, data: data };

// append_usize
bytes.append_usize(0x01);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 29, "append_usize_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_usize_value_1");
assert_eq!(*data[1], 0x17180101020102030400000001000000, "append_usize_value_2");
bytes = Bytes { size: size, data: data };

// append_u64
bytes.append_u64(0x030405060708);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 37, "append_u64_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u64_value_1");
assert_eq!(*data[1], 0x17180101020102030400000001000003, "append_u64_value_2");
Expand All @@ -494,7 +494,7 @@ fn test_bytes_append() {

// append_u128
bytes.append_u128(0x101112131415161718);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 53, "append_u128_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u128_value_1");
assert_eq!(*data[1], 0x17180101020102030400000001000003, "append_u128_value_2");
Expand All @@ -504,7 +504,7 @@ fn test_bytes_append() {

// append_u256
bytes.append_u256(u256 { low: 0x01020304050607, high: 0x010203040506070809 });
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 85, "append_u256_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_u256_value_1");
assert_eq!(*data[1], 0x17180101020102030400000001000003, "append_u256_value_2");
Expand All @@ -519,7 +519,7 @@ fn test_bytes_append() {
.try_into()
.unwrap();
bytes.append_address(address);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 117, "append_address_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "append_address_value_1");
assert_eq!(*data[1], 0x17180101020102030400000001000003, "append_address_value_2");
Expand Down Expand Up @@ -554,7 +554,7 @@ fn test_bytes_concat() {
let other = BytesTrait::new(46, array);

bytes.concat(@other);
let Bytes{size, mut data } = bytes;
let Bytes { size, mut data } = bytes;
assert_eq!(size, 163, "concat_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "concat_value_1");
assert_eq!(*data[1], 0x17180101020102030400000001000003, "concat_value_2");
Expand All @@ -572,7 +572,7 @@ fn test_bytes_concat() {
// empty bytes concat
let mut empty_bytes = BytesTrait::new_empty();
empty_bytes.concat(@bytes);
let Bytes{size, data } = empty_bytes;
let Bytes { size, data } = empty_bytes;

assert_eq!(size, 163, "concat_size");
assert_eq!(*data[0], 0x10111213141516171810111213141516, "concat_value_1");
Expand Down
35 changes: 7 additions & 28 deletions src/data_structures/src/array_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ impl ArrayImpl<T, +Copy<T>, +Drop<T>> of ArrayTraitExt<T> {

fn pop_front_n(ref self: Array<T>, mut n: usize) {
// Can't do self.span().pop_front_n();
loop {
if n == 0 {
break;
}
while (n != 0) {
match self.pop_front() {
Option::Some(_) => { n -= 1; },
Option::None => { break; },
Expand All @@ -68,18 +65,12 @@ impl ArrayImpl<T, +Copy<T>, +Drop<T>> of ArrayTraitExt<T> {
let mut ret = array![];
let mut i = 0;

loop {
if (i == self.len()) {
break;
}
while (i != self.len()) {
ret.append(*self[i]);
i += 1;
};
i = 0;
loop {
if (i == a.len()) {
break;
}
while (i != a.len()) {
ret.append(*a[i]);
i += 1;
};
Expand Down Expand Up @@ -130,10 +121,7 @@ impl ArrayImpl<T, +Copy<T>, +Drop<T>> of ArrayTraitExt<T> {

impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
fn pop_front_n(ref self: Span<T>, mut n: usize) {
loop {
if n == 0 {
break;
}
while (n != 0) {
match self.pop_front() {
Option::Some(_) => { n -= 1; },
Option::None => { break; },
Expand All @@ -142,10 +130,7 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
}

fn pop_back_n(ref self: Span<T>, mut n: usize) {
loop {
if n == 0 {
break;
}
while (n != 0) {
match self.pop_back() {
Option::Some(_) => { n -= 1; },
Option::None => { break; },
Expand Down Expand Up @@ -181,18 +166,12 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
let mut ret = array![];
let mut i = 0;

loop {
if (i == self.len()) {
break;
}
while (i != self.len()) {
ret.append(*self[i]);
i += 1;
};
i = 0;
loop {
if (i == a.len()) {
break;
}
while (i != a.len()) {
ret.append(*a[i]);
i += 1;
};
Expand Down
17 changes: 7 additions & 10 deletions src/data_structures/src/bit_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl BitArrayImpl of BitArrayTrait {

fn write_word_be_u256(ref self: BitArray, word: u256, length: usize) {
assert(length <= 256, 'illegal length');
let u256{low, high } = word;
let u256 { low, high } = word;
if length > 128 {
self.write_word_be(high.into(), length - 128);
self.write_word_be(low.into(), 128);
Expand All @@ -244,7 +244,7 @@ impl BitArrayImpl of BitArrayTrait {

fn write_word_be_u512(ref self: BitArray, word: u512, length: usize) {
assert(length <= 512, 'illegal length');
let u512{limb0, limb1, limb2, limb3 } = word;
let u512 { limb0, limb1, limb2, limb3 } = word;

if length > 384 {
self.write_word_be(limb3.into(), length - 384);
Expand Down Expand Up @@ -272,10 +272,7 @@ impl BitArrayImpl of BitArrayTrait {
let mut bit_offset = 0_usize;
let mut byte_offset = 0_usize;
let mut result: Option<felt252> = Option::Some(0);
loop {
if bit_offset == bit_limit && byte_offset == byte_limit {
break;
}
while (bit_offset != bit_limit || byte_offset != byte_limit) {
match self.pop_front() {
Option::Some(bit) => {
if bit {
Expand Down Expand Up @@ -359,7 +356,7 @@ impl BitArrayImpl of BitArrayTrait {

fn write_word_le(ref self: BitArray, word: felt252, length: usize) {
assert(length <= 248, 'illegal length');
let u256{low, high } = word.into();
let u256 { low, high } = word.into();
if length > 128 {
self._write_word_le_recursive(low, 128);
self._write_word_le_recursive(high, length - 128);
Expand All @@ -370,7 +367,7 @@ impl BitArrayImpl of BitArrayTrait {

fn write_word_le_u256(ref self: BitArray, word: u256, length: usize) {
assert(length <= 256, 'illegal length');
let u256{low, high } = word;
let u256 { low, high } = word;
if length > 128 {
self.write_word_le(low.into(), 128);
self.write_word_le(high.into(), length - 128);
Expand All @@ -381,7 +378,7 @@ impl BitArrayImpl of BitArrayTrait {

fn write_word_le_u512(ref self: BitArray, word: u512, length: usize) {
assert(length <= 512, 'illegal length');
let u512{limb0, limb1, limb2, limb3 } = word;
let u512 { limb0, limb1, limb2, limb3 } = word;
if length > 384 {
self.write_word_le(limb0.into(), 128);
self.write_word_le(limb1.into(), 128);
Expand Down Expand Up @@ -498,7 +495,7 @@ fn shift_bit(number: usize) -> u8 {

#[inline(always)]
fn select(word: felt252, byte_index: usize, bit_index: usize) -> bool {
let u256{low, high } = word.into();
let u256 { low, high } = word.into();
let shifted_bytes = if byte_index >= BYTES_IN_U128 {
high / one_shift_left_bytes_u128(byte_index - BYTES_IN_U128)
} else {
Expand Down
24 changes: 9 additions & 15 deletions src/data_structures/src/byte_appender.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ trait ByteAppenderSupportTrait<T> {
impl ByteAppenderSupportArrayU8Impl of ByteAppenderSupportTrait<Array<u8>> {
fn append_bytes_be(ref self: Array<u8>, bytes: felt252, mut count: usize) {
assert(count <= 16, 'count too big');
let u256{low, high: _high } = bytes.into();
loop {
if count == 0 {
break;
}
let u256 { low, high: _high } = bytes.into();
while (count != 0) {
let next = (low / one_shift_left_bytes_u128(count - 1)) % 0x100;
// Unwrap safe by definition of modulus operation 0x100
self.append(next.try_into().unwrap());
Expand All @@ -37,12 +34,9 @@ impl ByteAppenderSupportArrayU8Impl of ByteAppenderSupportTrait<Array<u8>> {

fn append_bytes_le(ref self: Array<u8>, bytes: felt252, count: usize) {
assert(count <= 16, 'count too big');
let u256{mut low, high: _high } = bytes.into();
let u256 { mut low, high: _high } = bytes.into();
let mut index = 0;
loop {
if index == count {
break;
}
while (index != count) {
let (remaining_bytes, next) = DivRem::div_rem(low, 0x100_u128.try_into().unwrap());
low = remaining_bytes;
// Unwrap safe by definition of remainder from division by 0x100
Expand All @@ -61,7 +55,7 @@ impl ByteAppenderSupportByteArrayImpl of ByteAppenderSupportTrait<ByteArray> {
#[inline(always)]
fn append_bytes_le(ref self: ByteArray, bytes: felt252, count: usize) {
assert(count <= 16, 'count too big');
let u256{low, high: _high } = bytes.into();
let u256 { low, high: _high } = bytes.into();
let (reversed, _) = reversing(low, count, 0x100);
self.append_word(reversed.into(), count);
}
Expand Down Expand Up @@ -188,27 +182,27 @@ impl ByteAppenderImpl<T, +Drop<T>, +ByteAppenderSupportTrait<T>> of ByteAppender
}

fn append_u256(ref self: T, word: u256) {
let u256{low, high } = word;
let u256 { low, high } = word;
self.append_u128(high);
self.append_u128(low);
}

fn append_u256_le(ref self: T, word: u256) {
let u256{low, high } = word;
let u256 { low, high } = word;
self.append_u128_le(low);
self.append_u128_le(high);
}

fn append_u512(ref self: T, word: u512) {
let u512{limb0, limb1, limb2, limb3 } = word;
let u512 { limb0, limb1, limb2, limb3 } = word;
self.append_u128(limb3);
self.append_u128(limb2);
self.append_u128(limb1);
self.append_u128(limb0);
}

fn append_u512_le(ref self: T, word: u512) {
let u512{limb0, limb1, limb2, limb3 } = word;
let u512 { limb0, limb1, limb2, limb3 } = word;
self.append_u128_le(limb0);
self.append_u128_le(limb1);
self.append_u128_le(limb2);
Expand Down
17 changes: 4 additions & 13 deletions src/data_structures/src/tests/bit_array_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use integer::u512;
fn test_append_bit() {
let mut ba: BitArray = Default::default();
let mut c = 250;
loop {
if c == 0 {
break;
}
while (c != 0) {
ba.append_bit(true);
ba.append_bit(false);
c -= 1;
Expand Down Expand Up @@ -272,20 +269,14 @@ fn test_stress_test() {
let mut ba: BitArray = Default::default();
let mut index = 0;
let limit = 20;
loop {
if index == limit {
break;
}
while (index != limit) {
let value: felt252 = index.into();
ba.write_word_be(value, 248);
ba.write_word_le(value, 248);
index += 1;
};
index = 0;
loop {
if index == limit {
break;
}
while (index != limit) {
let value = ba.read_word_be(248).unwrap();
assert!(value == index.into(), "not equal");
let value = ba.read_word_le(248).unwrap();
Expand Down Expand Up @@ -336,7 +327,7 @@ fn test_serde_ser_deser() {
// helpers
fn sample_bit_array() -> BitArray {
let sample: felt252 = BoundedInt::<u128>::max().into() - 1;
let u256{low, high: _ } = sample.into();
let u256 { low, high: _ } = sample.into();
let ba = BitArray {
data: array![],
current: low.into() * one_shift_left_bytes_felt252(15),
Expand Down
5 changes: 1 addition & 4 deletions src/data_structures/src/tests/byte_array_ext_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ fn test_span_u8_into_byte_array() {
fn test_byte_array_into_array_u8() {
let array: Array<u8> = test_byte_array_64().into();
let mut index = 0_usize;
loop {
if index == 0x40 {
break;
}
while (index != 64) {
assert!((*array[index]).into() == index + 1, "unexpected result");
index += 1;
}
Expand Down
Loading

0 comments on commit e7b6957

Please sign in to comment.