diff --git a/.tool-versions b/.tool-versions index ebe25423..49edbb53 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -scarb 2.5.3 +scarb 2.5.4 diff --git a/Scarb.toml b/Scarb.toml index 9f084f0b..bdd45ec6 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -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 diff --git a/src/bytes/src/bytes.cairo b/src/bytes/src/bytes.cairo index 702bc146..44c102f3 100644 --- a/src/bytes/src/bytes.cairo +++ b/src/bytes/src/bytes.cairo @@ -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 { diff --git a/src/bytes/src/tests/test_bytes.cairo b/src/bytes/src/tests/test_bytes.cairo index 08242c88..0b859cdc 100644 --- a/src/bytes/src/tests/test_bytes.cairo +++ b/src/bytes/src/tests/test_bytes.cairo @@ -439,13 +439,13 @@ 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"); @@ -453,7 +453,7 @@ fn test_bytes_append() { // 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"); @@ -461,7 +461,7 @@ fn test_bytes_append() { // 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"); @@ -469,7 +469,7 @@ fn test_bytes_append() { // 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"); @@ -477,7 +477,7 @@ fn test_bytes_append() { // 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"); @@ -485,7 +485,7 @@ fn test_bytes_append() { // 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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); @@ -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"); diff --git a/src/data_structures/src/array_ext.cairo b/src/data_structures/src/array_ext.cairo index b4d0ee80..75834559 100644 --- a/src/data_structures/src/array_ext.cairo +++ b/src/data_structures/src/array_ext.cairo @@ -44,10 +44,7 @@ impl ArrayImpl, +Drop> of ArrayTraitExt { fn pop_front_n(ref self: Array, 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; }, @@ -68,18 +65,12 @@ impl ArrayImpl, +Drop> of ArrayTraitExt { 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; }; @@ -130,10 +121,7 @@ impl ArrayImpl, +Drop> of ArrayTraitExt { impl SpanImpl, +Drop> of SpanTraitExt { fn pop_front_n(ref self: Span, mut n: usize) { - loop { - if n == 0 { - break; - } + while (n != 0) { match self.pop_front() { Option::Some(_) => { n -= 1; }, Option::None => { break; }, @@ -142,10 +130,7 @@ impl SpanImpl, +Drop> of SpanTraitExt { } fn pop_back_n(ref self: Span, mut n: usize) { - loop { - if n == 0 { - break; - } + while (n != 0) { match self.pop_back() { Option::Some(_) => { n -= 1; }, Option::None => { break; }, @@ -181,18 +166,12 @@ impl SpanImpl, +Drop> of SpanTraitExt { 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; }; diff --git a/src/data_structures/src/bit_array.cairo b/src/data_structures/src/bit_array.cairo index e41d9363..b9e488c0 100644 --- a/src/data_structures/src/bit_array.cairo +++ b/src/data_structures/src/bit_array.cairo @@ -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); @@ -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); @@ -272,10 +272,7 @@ impl BitArrayImpl of BitArrayTrait { let mut bit_offset = 0_usize; let mut byte_offset = 0_usize; let mut result: Option = 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 { @@ -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); @@ -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); @@ -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); @@ -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 { diff --git a/src/data_structures/src/byte_appender.cairo b/src/data_structures/src/byte_appender.cairo index fe2cf80f..079ea583 100644 --- a/src/data_structures/src/byte_appender.cairo +++ b/src/data_structures/src/byte_appender.cairo @@ -23,11 +23,8 @@ trait ByteAppenderSupportTrait { impl ByteAppenderSupportArrayU8Impl of ByteAppenderSupportTrait> { fn append_bytes_be(ref self: Array, 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()); @@ -37,12 +34,9 @@ impl ByteAppenderSupportArrayU8Impl of ByteAppenderSupportTrait> { fn append_bytes_le(ref self: Array, 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 @@ -61,7 +55,7 @@ impl ByteAppenderSupportByteArrayImpl of ByteAppenderSupportTrait { #[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); } @@ -188,19 +182,19 @@ impl ByteAppenderImpl, +ByteAppenderSupportTrait> 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); @@ -208,7 +202,7 @@ impl ByteAppenderImpl, +ByteAppenderSupportTrait> of ByteAppender } 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); diff --git a/src/data_structures/src/tests/bit_array_test.cairo b/src/data_structures/src/tests/bit_array_test.cairo index b6ddace2..9cbe6f67 100644 --- a/src/data_structures/src/tests/bit_array_test.cairo +++ b/src/data_structures/src/tests/bit_array_test.cairo @@ -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; @@ -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(); @@ -336,7 +327,7 @@ fn test_serde_ser_deser() { // helpers fn sample_bit_array() -> BitArray { let sample: felt252 = BoundedInt::::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), diff --git a/src/data_structures/src/tests/byte_array_ext_test.cairo b/src/data_structures/src/tests/byte_array_ext_test.cairo index 1da58653..678d81d3 100644 --- a/src/data_structures/src/tests/byte_array_ext_test.cairo +++ b/src/data_structures/src/tests/byte_array_ext_test.cairo @@ -20,10 +20,7 @@ fn test_span_u8_into_byte_array() { fn test_byte_array_into_array_u8() { let array: Array = 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; } diff --git a/src/data_structures/src/tests/byte_reader_test.cairo b/src/data_structures/src/tests/byte_reader_test.cairo index 2e34229f..1c203985 100644 --- a/src/data_structures/src/tests/byte_reader_test.cairo +++ b/src/data_structures/src/tests/byte_reader_test.cairo @@ -288,7 +288,7 @@ fn test_len_arr() { fn test_read_u256() { let ba = test_byte_array_64(); let mut rd = ba.reader(); - let u256{low: low1, high: high1 } = rd.read_u256().unwrap(); + let u256 { low: low1, high: high1 } = rd.read_u256().unwrap(); assert!(high1 == 0x0102030405060708090a0b0c0d0e0f10_u128, "wrong value for high1"); assert!(low1 == 0x1112131415161718191a1b1c1d1e1f20_u128, "wrong value for low1"); } @@ -298,7 +298,7 @@ fn test_read_u256() { fn test_read_u256_arr() { let ba = test_array_64(); let mut rd = ba.reader(); - let u256{low: low1, high: high1 } = rd.read_u256().unwrap(); + let u256 { low: low1, high: high1 } = rd.read_u256().unwrap(); assert!(high1 == 0x0102030405060708090a0b0c0d0e0f10_u128, "wrong value for high1"); assert!(low1 == 0x1112131415161718191a1b1c1d1e1f20_u128, "wrong value for low1"); } @@ -308,7 +308,7 @@ fn test_read_u256_arr() { fn test_read_u256_le() { let ba = test_byte_array_64(); let mut rd = ba.reader(); - let u256{low: low1, high: high1 } = rd.read_u256_le().unwrap(); + let u256 { low: low1, high: high1 } = rd.read_u256_le().unwrap(); assert!(high1 == 0x201f1e1d1c1b1a191817161514131211_u128, "wrong value for high1"); assert!(low1 == 0x100f0e0d0c0b0a090807060504030201_u128, "wrong value for low1"); } @@ -318,7 +318,7 @@ fn test_read_u256_le() { fn test_read_u256_le_arr() { let ba = test_array_64(); let mut rd = ba.reader(); - let u256{low: low1, high: high1 } = rd.read_u256_le().unwrap(); + let u256 { low: low1, high: high1 } = rd.read_u256_le().unwrap(); assert!(high1 == 0x201f1e1d1c1b1a191817161514131211_u128, "wrong value for high1"); assert!(low1 == 0x100f0e0d0c0b0a090807060504030201_u128, "wrong value for low1"); } @@ -328,7 +328,7 @@ fn test_read_u256_le_arr() { fn test_read_u512() { let ba = test_byte_array_64(); let mut rd = ba.reader(); - let u512{limb0, limb1, limb2, limb3 } = rd.read_u512().unwrap(); + let u512 { limb0, limb1, limb2, limb3 } = rd.read_u512().unwrap(); assert!(limb3 == 0x0102030405060708090a0b0c0d0e0f10_u128, "wrong value for limb3"); assert!(limb2 == 0x1112131415161718191a1b1c1d1e1f20_u128, "wrong value for limb2"); @@ -341,7 +341,7 @@ fn test_read_u512() { fn test_read_u512_arr() { let ba = test_array_64(); let mut rd = ba.reader(); - let u512{limb0, limb1, limb2, limb3 } = rd.read_u512().unwrap(); + let u512 { limb0, limb1, limb2, limb3 } = rd.read_u512().unwrap(); assert!(limb3 == 0x0102030405060708090a0b0c0d0e0f10_u128, "wrong value for limb3"); assert!(limb2 == 0x1112131415161718191a1b1c1d1e1f20_u128, "wrong value for limb2"); @@ -354,7 +354,7 @@ fn test_read_u512_arr() { fn test_read_u512_le() { let ba = test_byte_array_64(); let mut rd = ba.reader(); - let u512{limb0, limb1, limb2, limb3 } = rd.read_u512_le().unwrap(); + let u512 { limb0, limb1, limb2, limb3 } = rd.read_u512_le().unwrap(); assert!(limb0 == 0x100f0e0d0c0b0a090807060504030201_u128, "wrong value for limb0"); assert!(limb1 == 0x201f1e1d1c1b1a191817161514131211_u128, "wrong value for limb1"); assert!(limb2 == 0x302f2e2d2c2b2a292827262524232221_u128, "wrong value for limb2"); @@ -366,7 +366,7 @@ fn test_read_u512_le() { fn test_read_u512_le_arr() { let ba = test_array_64(); let mut rd = ba.reader(); - let u512{limb0, limb1, limb2, limb3 } = rd.read_u512_le().unwrap(); + let u512 { limb0, limb1, limb2, limb3 } = rd.read_u512_le().unwrap(); assert!(limb0 == 0x100f0e0d0c0b0a090807060504030201_u128, "wrong value for limb0"); assert!(limb1 == 0x201f1e1d1c1b1a191817161514131211_u128, "wrong value for limb1"); assert!(limb2 == 0x302f2e2d2c2b2a292827262524232221_u128, "wrong value for limb2"); diff --git a/src/encoding/src/base64.cairo b/src/encoding/src/base64.cairo index 0c034eb8..14f6a9b8 100644 --- a/src/encoding/src/base64.cairo +++ b/src/encoding/src/base64.cairo @@ -69,10 +69,7 @@ fn encode_u8_array(mut bytes: Array, base64_chars: Span) -> Array { let mut i = 0; let bytes_len = bytes.len(); let last_iteration = bytes_len - 3; - loop { - if i == bytes_len { - break; - } + while (i != bytes_len) { let n: u32 = (*bytes[i]).into() * 65536 | (*bytes[i + 1]).into() * 256 | (*bytes[i + 2]).into(); @@ -119,10 +116,7 @@ fn encode_felt(self: felt252, base64_chars: Span) -> Array { result.append(*base64_chars[r3]); num = quotient; } - loop { - if num == 0 { - break; - } + while (num != 0) { let (quotient, remainder) = DivRem::div_rem(num, 16777216_u256.try_into().unwrap()); // Safe since 'remainder' is always less than 16777216 (2^24), // which is within the range of usize (less than 2^32). @@ -137,10 +131,7 @@ fn encode_felt(self: felt252, base64_chars: Span) -> Array { result.append(*base64_chars[r4]); num = quotient; }; - loop { - if result.len() >= 43 { - break; - } + while (result.len() < 43) { result.append('A'); }; result = result.reverse(); diff --git a/src/encoding/src/reversible.cairo b/src/encoding/src/reversible.cairo index 2dcd47b9..16fdaa67 100644 --- a/src/encoding/src/reversible.cairo +++ b/src/encoding/src/reversible.cairo @@ -127,7 +127,7 @@ impl U128ReversibleBits of ReversibleBits { impl U256Reversible of ReversibleBytes { fn reverse_bytes(self: @u256) -> u256 { - let u256{low, high } = *self; + let u256 { low, high } = *self; let (low_reversed, _) = reversing(word: low, size: 16, step: SELECT_BYTE.into()); let (high_reversed, _) = reversing(word: high, size: 16, step: SELECT_BYTE.into()); u256 { low: high_reversed, high: low_reversed } @@ -136,7 +136,7 @@ impl U256Reversible of ReversibleBytes { impl U256ReversibleBits of ReversibleBits { fn reverse_bits(self: @u256) -> u256 { - let u256{low, high } = *self; + let u256 { low, high } = *self; let (low_reversed, _) = reversing(word: low, size: 128, step: SELECT_BIT.into()); let (high_reversed, _) = reversing(word: high, size: 128, step: SELECT_BIT.into()); u256 { low: high_reversed, high: low_reversed } @@ -145,7 +145,7 @@ impl U256ReversibleBits of ReversibleBits { impl U512Reversible of ReversibleBytes { fn reverse_bytes(self: @u512) -> u512 { - let u512{limb0, limb1, limb2, limb3 } = *self; + let u512 { limb0, limb1, limb2, limb3 } = *self; let (limb0_reversed, _) = reversing(word: limb0, size: 16, step: SELECT_BYTE.into()); let (limb1_reversed, _) = reversing(word: limb1, size: 16, step: SELECT_BYTE.into()); let (limb2_reversed, _) = reversing(word: limb2, size: 16, step: SELECT_BYTE.into()); @@ -161,7 +161,7 @@ impl U512Reversible of ReversibleBytes { impl U512ReversibleBits of ReversibleBits { fn reverse_bits(self: @u512) -> u512 { - let u512{limb0, limb1, limb2, limb3 } = *self; + let u512 { limb0, limb1, limb2, limb3 } = *self; let (limb0_reversed, _) = reversing(word: limb0, size: 128, step: SELECT_BIT.into()); let (limb1_reversed, _) = reversing(word: limb1, size: 128, step: SELECT_BIT.into()); let (limb2_reversed, _) = reversing(word: limb2, size: 128, step: SELECT_BIT.into()); @@ -181,7 +181,7 @@ impl Bytes31Reversible of ReversibleBytes { // low: A B -> low_rev: C_rev B_rev // high: C Z -> high_rev: A_rev Z fn reverse_bytes(self: @bytes31) -> bytes31 { - let u256{low, high } = (*self).into(); + let u256 { low, high } = (*self).into(); let (c_rev, _) = reversing(word: high, size: 15, step: SELECT_BYTE.into()); let (low_rev, a) = reversing_partial_result( // pushes b_rev yielding low_rev word: low, onto: c_rev, size: 1, step: SELECT_BYTE.into() @@ -194,7 +194,7 @@ impl Bytes31Reversible of ReversibleBytes { impl Bytes31ReversibleBits of ReversibleBits { fn reverse_bits(self: @bytes31) -> bytes31 { - let u256{low, high } = (*self).into(); + let u256 { low, high } = (*self).into(); let (c_rev, _) = reversing(word: high, size: 120, step: SELECT_BIT.into()); let (low_rev, a) = reversing_partial_result( // pushes b_rev yielding low_rev word: low, onto: c_rev, size: 8, step: SELECT_BIT.into() diff --git a/src/encoding/src/tests/base64_felt_test.cairo b/src/encoding/src/tests/base64_felt_test.cairo index 40eb66e5..5dac0fcc 100644 --- a/src/encoding/src/tests/base64_felt_test.cairo +++ b/src/encoding/src/tests/base64_felt_test.cairo @@ -10,19 +10,13 @@ fn bytes_be(val: felt252) -> Array { let mut result = array![]; let mut num: u256 = val.into(); - loop { - if num == 0 { - break; - } + while (num != 0) { let (quotient, remainder) = DivRem::div_rem(num, 256_u256.try_into().unwrap()); result.append(remainder.try_into().unwrap()); num = quotient; }; - loop { - if result.len() >= 32 { - break; - } - result.append(0_u8); + while (result.len() < 32) { + result.append(0); }; result = result.reverse(); result diff --git a/src/encoding/src/tests/rlp_test.cairo b/src/encoding/src/tests/rlp_test.cairo index 056cb008..3f3d29da 100644 --- a/src/encoding/src/tests/rlp_test.cairo +++ b/src/encoding/src/tests/rlp_test.cairo @@ -122,10 +122,7 @@ fn test_rlp_decode_string_default_value() { #[available_gas(99999999)] fn test_rlp_decode_string() { let mut i = 0; - loop { - if i == 0x80 { - break; - } + while (i != 128) { let arr = array![i]; let res = RLPTrait::decode(arr.span()).unwrap(); @@ -1995,10 +1992,7 @@ fn test_rlp_encode_string_length_between_2_and_55() { fn test_rlp_encode_string_length_exactly_56() { let mut input: Array = Default::default(); let mut i = 0; - loop { - if i == 56 { - break; - } + while (i != 56) { input.append(0x60); i += 1; }; @@ -2009,7 +2003,7 @@ fn test_rlp_encode_string_length_exactly_56() { assert!(*res[0] == 0xb8, "wrong prefix"); assert!(*res[1] == 56, "wrong string length"); let mut i = 2; - loop { + while (i != 58) { if i == 58 { break; } @@ -2024,10 +2018,7 @@ fn test_rlp_encode_string_length_exactly_56() { fn test_rlp_encode_string_length_greater_than_56() { let mut input: Array = Default::default(); let mut i = 0; - loop { - if i == 60 { - break; - } + while (i != 60) { input.append(0x70); i += 1; }; @@ -2038,10 +2029,7 @@ fn test_rlp_encode_string_length_greater_than_56() { assert!(*res[0] == 0xb8, "wrong prefix"); assert!(*res[1] == 60, "wrong length byte"); let mut i = 2; - loop { - if i == 62 { - break; - } + while (i != 62) { assert!(*res[i] == 0x70, "wrong value in sequence"); i += 1; } @@ -2052,10 +2040,7 @@ fn test_rlp_encode_string_length_greater_than_56() { fn test_rlp_encode_string_large_bytearray_inputs() { let mut input: Array = Default::default(); let mut i = 0; - loop { - if i == 500 { - break; - } + while (i != 500) { input.append(0x70); i += 1; }; @@ -2067,10 +2052,7 @@ fn test_rlp_encode_string_large_bytearray_inputs() { assert!(*res[1] == 0x01, "wrong first length byte"); assert!(*res[2] == 0xF4, "wrong second length byte"); let mut i = 3; - loop { - if i == 503 { - break; - } + while (i != 503) { assert!(*res[i] == 0x70, "wrong value in sequence"); i += 1; } diff --git a/src/math/src/aliquot_sum.cairo b/src/math/src/aliquot_sum.cairo index 42df95d2..938a4a03 100644 --- a/src/math/src/aliquot_sum.cairo +++ b/src/math/src/aliquot_sum.cairo @@ -16,14 +16,11 @@ fn aliquot_sum(number: u128) -> u128 { let limit = (number / 2) + 1; let mut index = 1; let mut res = 0; - loop { - if index == limit { - break res; - } - + while (index != limit) { if number % index == 0 { res = res + index; } index += 1; - } + }; + res } diff --git a/src/math/src/ed25519.cairo b/src/math/src/ed25519.cairo index 0473b666..a382c85e 100644 --- a/src/math/src/ed25519.cairo +++ b/src/math/src/ed25519.cairo @@ -226,10 +226,7 @@ fn point_mult(mut scalar: u256, mut P: ExtendedHomogeneousPoint) -> ExtendedHomo let zero_u512 = Default::default(); // Double and add method - loop { - if (scalar == zero_u512) { - break; - } + while (scalar != zero_u512) { if ((scalar.low & 1) == 1) { Q = Q + P; } @@ -265,16 +262,11 @@ fn check_group_equation( } fn verify_signature(msg: Span, signature: Span, pub_key: Span) -> bool { - if (pub_key.len() != PUB_KEY_LEN) { - return false; - } - - if (signature.len() != SIG_LEN) { + if (pub_key.len() != PUB_KEY_LEN || signature.len() != SIG_LEN) { return false; } let r_string = signature.slice(0, SIG_LEN / 2); - let s_string = signature.slice(32, SIG_LEN / 2); let R_opt: Option = r_string.try_into(); if (R_opt.is_none()) { return false; @@ -284,7 +276,7 @@ fn verify_signature(msg: Span, signature: Span, pub_key: Span) -> bo return false; } let R: Point = R_opt.unwrap(); - let S: u256 = s_string.into(); + let S: u256 = signature.slice(32, SIG_LEN / 2).into(); let A_prime: Point = A_prime_opt.unwrap(); let R_extended: ExtendedHomogeneousPoint = R.into(); diff --git a/src/math/src/extended_euclidean_algorithm.cairo b/src/math/src/extended_euclidean_algorithm.cairo index fb055d97..b11214d1 100644 --- a/src/math/src/extended_euclidean_algorithm.cairo +++ b/src/math/src/extended_euclidean_algorithm.cairo @@ -18,17 +18,14 @@ fn extended_euclidean_algorithm(a: u128, b: u128) -> (u128, u128, u128) { let mut old_t = 0; let mut coeff_t = 1; - // Loop until remainder is 0. - loop { - if rem == 0 { - break (old_r, old_s, old_t); - } + while (rem != 0) { let quotient = old_r / rem; update_step(ref rem, ref old_r, quotient); update_step(ref coeff_s, ref old_s, quotient); update_step(ref coeff_t, ref old_t, quotient); - } + }; + (old_r, old_s, old_t) } /// Update the step of the extended Euclidean algorithm. diff --git a/src/math/src/fast_power.cairo b/src/math/src/fast_power.cairo index 23949be0..7a851b49 100644 --- a/src/math/src/fast_power.cairo +++ b/src/math/src/fast_power.cairo @@ -21,11 +21,7 @@ fn fast_power(base: u128, mut power: u128, modulus: u128) -> u128 { let modulus: u256 = modulus.into(); let mut result: u256 = 1; - let res = loop { - if power == 0 { - break result; - } - + while (power != 0) { if power % 2 != 0 { result = (result * base) % modulus; } @@ -34,5 +30,5 @@ fn fast_power(base: u128, mut power: u128, modulus: u128) -> u128 { power = power / 2; }; - res.try_into().expect('value cant be larger than u128') + result.try_into().expect('value cant be larger than u128') } diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index be3f2c53..8487e93f 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -58,14 +58,11 @@ fn pow, +Mul, +Div, +Rem, +PartialEq, +Into, +Drop /// * `felt252` - The number of digits in num of base fn count_digits_of_base(mut num: u128, base: u128) -> u128 { let mut res = 0; - loop { - if num == 0 { - break res; - } else { - num = num / base; - } + while (num != 0) { + num = num / base; res += 1; - } + }; + res } trait BitShift { diff --git a/src/math/src/mod_arithmetics.cairo b/src/math/src/mod_arithmetics.cairo index 5819a8cd..5507d01f 100644 --- a/src/math/src/mod_arithmetics.cairo +++ b/src/math/src/mod_arithmetics.cairo @@ -108,10 +108,7 @@ fn pow_mod(mut base: u256, mut pow: u256, modulo: u256) -> u256 { let mod_non_zero: NonZero = integer::u256_try_as_non_zero(modulo).unwrap(); let mut mult: u512 = u512 { limb0: 0_u128, limb1: 0_u128, limb2: 0_u128, limb3: 0_u128 }; - loop { - if (pow <= 0) { - break base; - } + while (pow != 0) { if ((pow & 1) > 0) { mult = integer::u256_wide_mul(result, base); let (_, res_u256, _, _, _, _, _) = integer::u512_safe_divmod_by_u256( diff --git a/src/math/src/perfect_number.cairo b/src/math/src/perfect_number.cairo index 6df69a5e..554aec12 100644 --- a/src/math/src/perfect_number.cairo +++ b/src/math/src/perfect_number.cairo @@ -15,16 +15,13 @@ fn is_perfect_number(num: u128) -> bool { let mut index = 1; let mut sum = 0; - loop { - if index == num - 1 { - break num == sum; - } - + while (index != num - 1) { if num % index == 0 { sum += index; } index += 1; - } + }; + num == sum } /// Algorithm to determine all the perfect numbers up to a maximum value @@ -36,10 +33,7 @@ fn perfect_numbers(max: u128) -> Array { let mut res = array![]; let mut index = 1; - loop { - if index == max { - break; - } + while (index != max) { if is_perfect_number(index) { res.append(index); } diff --git a/src/math/src/sha256.cairo b/src/math/src/sha256.cairo index a0d7a8a9..ad2c1fe6 100644 --- a/src/math/src/sha256.cairo +++ b/src/math/src/sha256.cairo @@ -50,10 +50,7 @@ fn sha256(mut data: Array) -> Array { // add one data.append(0x80); // add padding - loop { - if (64 * ((data.len() - 1) / 64 + 1)) - 8 == data.len() { - break; - } + while ((64 * ((data.len() - 1) / 64 + 1)) - 8 != data.len()) { data.append(0); }; @@ -151,18 +148,12 @@ fn compression(w: Span, i: usize, k: Span, mut h: Span) -> Span, i: usize) -> Span { let mut j = 0; let mut result = array![]; - loop { - if j >= 16 { - break; - } + while (j < 16) { result.append(*data[i * 16 + j]); j += 1; }; let mut i = 16; - loop { - if i >= 64 { - break; - } + while (i < 64) { let s0 = ssig0(*result[i - 15]); let s1 = ssig1(*result[i - 2]); let res = u32_wrapping_add( diff --git a/src/math/src/sha512.cairo b/src/math/src/sha512.cairo index 7b36ec24..d885a3f5 100644 --- a/src/math/src/sha512.cairo +++ b/src/math/src/sha512.cairo @@ -108,11 +108,7 @@ fn fpow(mut base: u128, mut power: u128) -> u128 { let mut base_u128: u256 = base.into(); let mut result: u256 = 1; - loop { - if power == 0 { - break result; - } - + while (power != 0) { if power % 2 != 0 { result = (result * base_u128); } @@ -149,10 +145,7 @@ fn add_trailing_zeroes(ref data: Array, msg_len: usize) { }; let mut i = 0; - loop { - if (i >= padding_len) { - break; - } + while (i < padding_len) { data.append(0); i += 1; }; @@ -162,10 +155,7 @@ fn from_u8Array_to_WordArray(data: Array) -> Array { let mut new_arr: Array = array![]; let mut i = 0; - loop { - if (i >= data.len()) { - break; - } + while (i < data.len()) { let new_word: u128 = (BitShift::shl((*data[i + 0]).into(), 56) + BitShift::shl((*data[i + 1]).into(), 48) + BitShift::shl((*data[i + 2]).into(), 40) @@ -183,11 +173,8 @@ fn from_u8Array_to_WordArray(data: Array) -> Array { fn from_WordArray_to_u8array(data: Span) -> Array { let mut arr: Array = array![]; - let mut i: usize = 0; - loop { - if (i == data.len()) { - break; - } + let mut i = 0; + while (i != data.len()) { let mut res: u128 = BitShift::shr((*data.at(i).data).into(), 56) & BoundedInt::::max().into(); arr.append(res.try_into().unwrap()); @@ -227,18 +214,13 @@ fn digest_hash(data: Span, msg_len: usize) -> Array { let mut i = 0; - loop { - if (i == block_nb) { - break; - } + while (i != block_nb) { // Prepare message schedule let mut t: usize = 0; let mut W: Array = array![]; - loop { - if t == 80 { - break; - } else if t < 16 { + while (t != 80) { + if t < 16 { W.append(*data.at(i * 16 + t)); } else { let buf = ssig1(*W.at(t - 2)) + *W.at(t - 7) + ssig0(*W.at(t - 15)) + *W.at(t - 16); @@ -257,10 +239,7 @@ fn digest_hash(data: Span, msg_len: usize) -> Array { let mut h = h_7; let mut t: usize = 0; - loop { - if t == 80 { - break; - } + while (t != 80) { let T1 = h + bsig1(e) + ch(e, f, g) + *k.at(t) + *W.at(t); let T2 = bsig0(a) + maj(a, b, c); h = g; diff --git a/src/math/src/tests/ed25519_test.cairo b/src/math/src/tests/ed25519_test.cairo index 22fb2789..b86f2a86 100644 --- a/src/math/src/tests/ed25519_test.cairo +++ b/src/math/src/tests/ed25519_test.cairo @@ -2,16 +2,12 @@ use alexandria_math::ed25519::{p, Point, verify_signature, SpanU8TryIntoPoint}; fn gen_msg() -> Span { // Hello World! - let mut msg: Array = array![ - 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21 - ]; - msg.span() + array!['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'].span() } fn gen_wrong_msg() -> Span { // Hello Bro! - let mut msg: Array = array![0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x42, 0x72, 0x6F, 0x21]; - msg.span() + array!['H', 'e', 'l', 'l', 'o', ' ', 'B', 'r', 'o', '!'].span() } fn gen_sig() -> Span { diff --git a/src/searching/src/bm_search.cairo b/src/searching/src/bm_search.cairo index 584d7c26..fd3beb4f 100644 --- a/src/searching/src/bm_search.cairo +++ b/src/searching/src/bm_search.cairo @@ -31,11 +31,7 @@ fn bm_search(text: @ByteArray, pattern: @ByteArray) -> Array { let mut shift: usize = 0; // Shift value for pattern matching // Perform pattern matching - loop { - if shift > text_len - pattern_len { - break; - } - + while (shift <= text_len - pattern_len) { let mut pattern_index = pattern_len; // Compare characters from right to left diff --git a/src/sorting/src/lib.cairo b/src/sorting/src/lib.cairo index 3a767b87..d03cd063 100644 --- a/src/sorting/src/lib.cairo +++ b/src/sorting/src/lib.cairo @@ -39,17 +39,16 @@ fn is_equal_vec(mut a: Felt252Vec, mut b: Span) -> bool { } let mut compare_id = 0; - let mut is_equal = true; + loop { if compare_id == b.len() { - break; + break true; } if a[compare_id] != *b[compare_id] { - is_equal = false; + break false; } compare_id += 1; - }; - is_equal + } } diff --git a/src/storage/src/tests/list_test.cairo b/src/storage/src/tests/list_test.cairo index 21e5ea00..91e0a4f9 100644 --- a/src/storage/src/tests/list_test.cairo +++ b/src/storage/src/tests/list_test.cairo @@ -272,11 +272,7 @@ mod tests { let max: u32 = 513; // test appending many - loop { - if index == max { - break; - } - + while (index != max) { let append_indexes = contract.do_append(mock_addr, index.into()); assert(append_indexes == (index, index), index.into()); index += 1; @@ -286,11 +282,7 @@ mod tests { // test getting many index = 0; - loop { - if index == max { - break; - } - + while (index != max) { let (some_addr, some_number) = contract.do_get(index); assert!(some_addr.is_some(), "addr is some"); assert_eq!(some_addr.unwrap(), mock_addr, "addr");