Skip to content

Commit

Permalink
Upgrade to Cairo 2.5.3 and other refactors (#271)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

## Pull Request type

<!-- Please try to limit your pull request to one type; submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

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

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Issue Number: N/A

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

-
-
-

## Does this introduce a breaking change?

- [ ] Yes
- [ ] No

<!-- If this does introduce a breaking change, please describe the
impact and migration path for existing applications below. -->

## Other information

<!-- Any other information that is important to this PR, such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
delaaxe authored Feb 8, 2024
1 parent 39d20fb commit 800f5ad
Show file tree
Hide file tree
Showing 82 changed files with 2,376 additions and 2,547 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.4.0
scarb 2.5.3
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ description = "Community maintained Cairo and Starknet libraries"
homepage = "https://github.com/keep-starknet-strange/alexandria/"

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

[workspace.tool.fmt]
sort-module-level-items = true
Expand Down
10 changes: 2 additions & 8 deletions src/ascii/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ impl ToAsciiArrayTraitImpl<
}

let mut num = self;
loop {
if num.is_zero() {
break;
}
while num.is_non_zero() {
let (quotient, remainder) = DivRem::div_rem(
num, TryInto::<felt252, T>::try_into(10).unwrap().try_into().expect('Division by 0')
);
Expand Down Expand Up @@ -153,10 +150,7 @@ impl U256ToAsciiArrayTraitImpl of ToAsciiArrayTrait<u256> {
return new_arr;
}
let mut num = self;
loop {
if num.is_zero() {
break;
}
while num != 0 {
let (quotient, remainder) = DivRem::div_rem(
num, 10_u256.try_into().expect('Division by 0')
);
Expand Down
52 changes: 26 additions & 26 deletions src/ascii/src/tests/test_ascii_integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ fn u256_to_ascii() {
// 115792089237316195423570985008687907853269984665640564039457584007913129639935`.
let num: u256 = BoundedInt::max();
let ascii: Array<felt252> = num.to_ascii();
assert(ascii.len() == 3, 'max u256 wrong len');
assert(*ascii.at(0) == '1157920892373161954235709850086', 'max u256 wrong first felt');
assert(*ascii.at(1) == '8790785326998466564056403945758', 'max u256 wrong second felt');
assert(*ascii.at(2) == '4007913129639935', 'max u256 wrong third felt');
assert_eq!(ascii.len(), 3, "max u256 wrong len");
assert_eq!(*ascii.at(0), '1157920892373161954235709850086', "max u256 wrong first felt");
assert_eq!(*ascii.at(1), '8790785326998466564056403945758', "max u256 wrong second felt");
assert_eq!(*ascii.at(2), '4007913129639935', "max u256 wrong third felt");
// ------------------------------ min u256 test ----------------------------- //
let num: u256 = BoundedInt::min();
let ascii: Array<felt252> = num.to_ascii();

assert(ascii.len() == 1, 'min u256 wrong len');
assert(*ascii.at(0) == '0', 'min u256 wrong felt');
assert_eq!(ascii.len(), 1, "min u256 wrong len");
assert_eq!(*ascii.at(0), '0', "min u256 wrong felt");
// ---------------------------- 31 char u256 test --------------------------- //
let ascii: Array<felt252> = 1157920892373161954235709850086_u256.to_ascii();
assert(ascii.len() == 1, 'u256 31 char wrong len');
assert(*ascii.at(0) == '1157920892373161954235709850086', '31 char u256 wrong felt');
assert_eq!(ascii.len(), 1, "u256 31 char wrong len");
assert_eq!(*ascii.at(0), '1157920892373161954235709850086', "31 char u256 wrong felt");
// ---------------------------- 62 cahr u256 test --------------------------- //
let ascii: Array<felt252> = 11579208923731619542357098500868790785326998466564056403945758_u256
.to_ascii();
assert(ascii.len() == 2, 'u256 31 char wrong len');
assert(*ascii.at(0) == '1157920892373161954235709850086', '31 char u256 wrong felt');
assert(*ascii.at(1) == '8790785326998466564056403945758', '62 char u256 wrong felt');
assert_eq!(ascii.len(), 2, "u256 31 char wrong len");
assert_eq!(*ascii.at(0), '1157920892373161954235709850086', "31 char u256 wrong felt");
assert_eq!(*ascii.at(1), '8790785326998466564056403945758', "62 char u256 wrong felt");
}

#[test]
Expand All @@ -39,61 +39,61 @@ fn u128_to_ascii() {
let num: u128 = BoundedInt::max();
let ascii: Array<felt252> = num.to_ascii();

assert(ascii.len() == 2, 'max u128 wrong len');
assert(*ascii.at(0) == '3402823669209384634633746074317', 'max u128 wrong first felt');
assert(*ascii.at(1) == '68211455', 'max u128 wrong second felt');
assert_eq!(ascii.len(), 2, "max u128 wrong len");
assert_eq!(*ascii.at(0), '3402823669209384634633746074317', "max u128 wrong first felt");
assert_eq!(*ascii.at(1), '68211455', "max u128 wrong second felt");
// ------------------------------ min u128 test ----------------------------- //
let num: u128 = BoundedInt::min();
let ascii: Array<felt252> = num.to_ascii();

assert(ascii.len() == 1, 'min u128 wrong len');
assert(*ascii.at(0) == '0', 'min u128 wrong felt');
assert_eq!(ascii.len(), 1, "min u128 wrong len");
assert_eq!(*ascii.at(0), '0', "min u128 wrong felt");
// ---------------------------- 31 char u128 test --------------------------- //
let ascii: Array<felt252> = 3402823669209384634633746074317_u128.to_ascii();
assert(ascii.len() == 1, 'u128 31 char wrong len');
assert(*ascii.at(0) == '3402823669209384634633746074317', '31 char u128 wrong felt');
assert_eq!(ascii.len(), 1, "u128 31 char wrong len");
assert_eq!(*ascii.at(0), '3402823669209384634633746074317', "31 char u128 wrong felt");
}

#[test]
#[available_gas(2000000)]
fn u64_to_ascii() {
// ------------------------------ max u64 test ------------------------------ //
let num: u64 = BoundedInt::max();
assert(num.to_ascii() == '18446744073709551615', 'incorect u64 max felt');
assert_eq!(num.to_ascii(), '18446744073709551615', "incorect u64 max felt");
// ------------------------------ min u64 test ------------------------------ //
let num: u64 = BoundedInt::min();
assert(num.to_ascii() == '0', 'incorect u64 min felt');
assert_eq!(num.to_ascii(), '0', "incorect u64 min felt");
}

#[test]
#[available_gas(2000000)]
fn u32_to_ascii() {
// ------------------------------ max u32 test ------------------------------ //
let num: u32 = BoundedInt::max();
assert(num.to_ascii() == '4294967295', 'incorect u32 max felt');
assert_eq!(num.to_ascii(), '4294967295', "incorect u32 max felt");
// ------------------------------ min u32 test ------------------------------ //
let num: u32 = BoundedInt::min();
assert(num.to_ascii() == '0', 'incorect u32 min felt');
assert_eq!(num.to_ascii(), '0', "incorect u32 min felt");
}

#[test]
#[available_gas(2000000)]
fn u16_to_ascii() {
// ------------------------------ max u16 test ------------------------------ //
let num: u16 = BoundedInt::max();
assert(num.to_ascii() == '65535', 'incorect u16 max felt');
assert_eq!(num.to_ascii(), '65535', "incorect u16 max felt");
// ------------------------------ min u16 test ------------------------------ //
let num: u16 = BoundedInt::min();
assert(num.to_ascii() == '0', 'incorect u16 min felt');
assert_eq!(num.to_ascii(), '0', "incorect u16 min felt");
}

#[test]
#[available_gas(2000000)]
fn u8_to_ascii() {
// ------------------------------- max u8 test ------------------------------ //
let num: u8 = BoundedInt::max();
assert(num.to_ascii() == '255', 'incorect u8 max felt');
assert_eq!(num.to_ascii(), '255', "incorect u8 max felt");
// ------------------------------- min u8 test ------------------------------ //
let num: u8 = BoundedInt::min();
assert(num.to_ascii() == '0', 'incorect u8 min felt');
assert_eq!(num.to_ascii(), '0', "incorect u8 min felt");
}
40 changes: 10 additions & 30 deletions src/bytes/src/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ impl BytesImpl of BytesTrait {
data_len += 1;
}

loop {
if data_len == 0 {
break;
};
while data_len != 0 {
data.append(0_u128);
data_len -= 1;
};
Expand Down Expand Up @@ -192,10 +189,8 @@ impl BytesImpl of BytesTrait {
// if value in two elements, read them and join them
let (element_index, element_offset) = BytesTrait::locate(offset);
let value_in_one_element = element_offset + size <= BYTES_PER_ELEMENT;
let mut value = 0;
if value_in_one_element {
value =
read_sub_u128(*self.data[element_index], BYTES_PER_ELEMENT, element_offset, size);
let value = if value_in_one_element {
read_sub_u128(*self.data[element_index], BYTES_PER_ELEMENT, element_offset, size)
} else {
let (_, end_element_offset) = BytesTrait::locate(offset + size);
let left = read_sub_u128(
Expand All @@ -207,8 +202,8 @@ impl BytesImpl of BytesTrait {
let right = read_sub_u128(
*self.data[element_index + 1], BYTES_PER_ELEMENT, 0, end_element_offset
);
value = u128_join(left, right, end_element_offset);
}
u128_join(left, right, end_element_offset)
};
(offset + size, value)
}

Expand All @@ -223,14 +218,11 @@ impl BytesImpl of BytesTrait {
}
let mut offset = offset;
let mut i = array_length;
loop {
while i != 0 {
let (new_offset, value) = self.read_u128_packed(offset, element_size);
array.append(value);
offset = new_offset;
i -= 1;
if i == 0 {
break;
};
};
(offset, array)
}
Expand Down Expand Up @@ -313,14 +305,11 @@ impl BytesImpl of BytesTrait {

let mut offset = offset;
let mut i = array_length;
loop {
while i != 0 {
let (new_offset, value) = self.read_u256(offset);
array.append(value);
offset = new_offset;
i -= 1;
if i == 0 {
break;
};
};
(offset, array)
}
Expand All @@ -339,10 +328,7 @@ impl BytesImpl of BytesTrait {
// read full array element for sub_bytes
let mut offset = offset;
let mut sub_bytes_full_array_len = size / BYTES_PER_ELEMENT;
loop {
if sub_bytes_full_array_len == 0 {
break;
};
while sub_bytes_full_array_len != 0 {
let (new_offset, value) = self.read_u128(offset);
array.append(value);
offset = new_offset;
Expand Down Expand Up @@ -477,10 +463,7 @@ impl BytesImpl of BytesTrait {
// read full array element for other
let mut offset = 0;
let mut sub_bytes_full_array_len = *other.size / BYTES_PER_ELEMENT;
loop {
if sub_bytes_full_array_len == 0 {
break;
};
while sub_bytes_full_array_len != 0 {
let (new_offset, value) = other.read_u128(offset);
self.append_u128(value);
offset = new_offset;
Expand Down Expand Up @@ -516,10 +499,7 @@ impl BytesImpl of BytesTrait {
let mut hash_data: Array<u8> = array![];
let mut i: usize = 0;
let mut offset: usize = 0;
loop {
if i == self.size() {
break;
}
while i != self.size() {
let (new_offset, hash_data_item) = self.read_u8(offset);
hash_data.append(hash_data_item);
offset = new_offset;
Expand Down
Loading

0 comments on commit 800f5ad

Please sign in to comment.