Skip to content

Commit

Permalink
Upgrade to 2.7.0 (#321)
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:

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


## What is the new behavior?

Made some function deprecated.
Upgrade the code to 2.7
Fixed all warnings
Tried to make some fn generic (didn't look through all of em) 
Replaced some while pop_front to for loop
Updated all assert to assert! and removed their error message as the
default one is a good enough default value
  • Loading branch information
gaetbout authored Aug 14, 2024
1 parent bcdca70 commit e1b0805
Show file tree
Hide file tree
Showing 117 changed files with 2,595 additions and 2,819 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.6.4
scarb 2.7.0
5 changes: 3 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ name = "alexandria"
version = "0.1.0"
description = "Community maintained Cairo and Starknet libraries"
homepage = "https://github.com/keep-starknet-strange/alexandria/"
cairo-version = "2.6.3"
cairo-version = "2.7.0"

[workspace.dependencies]
starknet = "2.6.3"
starknet = "2.7.0"
cairo_test = "2.7.0"

[workspace.tool.fmt]
sort-module-level-items = true
Expand Down
5 changes: 4 additions & 1 deletion packages/ascii/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ edition = "2023_11"
fmt.workspace = true

[dependencies]
alexandria_data_structures = { path = "../data_structures" }
alexandria_data_structures = { path = "../data_structures" }

[dev-dependencies]
cairo_test.workspace = true
48 changes: 22 additions & 26 deletions packages/ascii/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ impl ToAsciiArrayTraitImpl<
}

let mut num = self;
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')
);
new_arr.append(remainder.into() + 48);
num = quotient;
};
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')
);
new_arr.append(remainder.into() + 48);
num = quotient;
};
new_arr
}
}
Expand Down Expand Up @@ -110,8 +108,8 @@ impl BigIntegerToAsciiTraitImpl<
match inverse_ascii_arr.pop_back() {
Option::Some(val) => {
let new_ascii = ascii * 256 + *val;
// if index is at 30 it means we have reached the max size of felt252 at 31 characters
// so we append the current ascii and reset the ascii to 0
// if index is at 30 it means we have reached the max size of felt252 at 31
// characters so we append the current ascii and reset the ascii to 0
ascii = if index == 30 {
data.append(new_ascii);
0
Expand Down Expand Up @@ -175,22 +173,20 @@ impl U256ToAsciiTraitImpl of ToAsciiTrait<u256, Array<felt252>> {
let mut inverse_ascii_arr = self.to_inverse_ascii_array().span();
let mut index = 0;
let mut ascii: felt252 = 0;
while let Option::Some(val) = inverse_ascii_arr
.pop_back() {
let new_ascii = ascii * 256 + *val;
// if index is currently at 30 it means we have processed the number for index 31
// this means we have reached the max size of felt252 at 31 characters
// so we append the current ascii and reset the ascii to 0
// do the same at index 61 as well because max u256 is 78 characters
ascii =
if index == 30 || index == 61 {
data.append(new_ascii);
0
} else {
new_ascii
};
index += 1;
while let Option::Some(val) = inverse_ascii_arr.pop_back() {
let new_ascii = ascii * 256 + *val;
// if index is currently at 30 it means we have processed the number for index 31
// this means we have reached the max size of felt252 at 31 characters
// so we append the current ascii and reset the ascii to 0
// do the same at index 61 as well because max u256 is 78 characters
ascii = if index == 30 || index == 61 {
data.append(new_ascii);
0
} else {
new_ascii
};
index += 1;
};

if ascii.is_non_zero() {
data.append(ascii);
Expand Down
78 changes: 39 additions & 39 deletions packages/ascii/src/tests/test_ascii_integer.cairo
Original file line number Diff line number Diff line change
@@ -1,99 +1,99 @@
use alexandria_ascii::ToAsciiTrait;
use core::integer::BoundedInt;
use core::num::traits::Bounded;

#[test]
#[available_gas(2000000000)]
fn u256_to_ascii() {
// ------------------------------ max u256 test ----------------------------- //
// max u256 int in cairo is GitHub Copilot: The maximum u256 number in Cairo is `
// 115792089237316195423570985008687907853269984665640564039457584007913129639935`.
let num: u256 = BoundedInt::max();
let num: u256 = Bounded::MAX;
let ascii: Array<felt252> = num.to_ascii();
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");
assert_eq!(ascii.len(), 3);
assert_eq!(*ascii.at(0), '1157920892373161954235709850086');
assert_eq!(*ascii.at(1), '8790785326998466564056403945758');
assert_eq!(*ascii.at(2), '4007913129639935');
// ------------------------------ min u256 test ----------------------------- //
let num: u256 = BoundedInt::min();
let num: u256 = Bounded::MIN;
let ascii: Array<felt252> = num.to_ascii();

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

#[test]
#[available_gas(2000000)]
fn u128_to_ascii() {
// ------------------------------ max u128 test ----------------------------- //
// max u128 int in cairo is 340282366920938463463374607431768211455
let num: u128 = BoundedInt::max();
let num: u128 = Bounded::MAX;
let ascii: Array<felt252> = num.to_ascii();

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");
assert_eq!(ascii.len(), 2);
assert_eq!(*ascii.at(0), '3402823669209384634633746074317');
assert_eq!(*ascii.at(1), '68211455');
// ------------------------------ min u128 test ----------------------------- //
let num: u128 = BoundedInt::min();
let num: u128 = Bounded::MIN;
let ascii: Array<felt252> = num.to_ascii();

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

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

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

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

#[test]
#[available_gas(2000000)]
fn u8_to_ascii() {
// ------------------------------- max u8 test ------------------------------ //
let num: u8 = BoundedInt::max();
assert_eq!(num.to_ascii(), '255', "incorrect u8 max felt");
let num: u8 = Bounded::MAX;
assert_eq!(num.to_ascii(), '255');
// ------------------------------- min u8 test ------------------------------ //
let num: u8 = BoundedInt::min();
assert_eq!(num.to_ascii(), '0', "incorrect u8 min felt");
let num: u8 = Bounded::MIN;
assert_eq!(num.to_ascii(), '0');
}
3 changes: 3 additions & 0 deletions packages/bytes/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ fmt.workspace = true
alexandria_math = { path = "../math" }
alexandria_data_structures = { path = "../data_structures" }
starknet.workspace = true

[dev-dependencies]
cairo_test.workspace = true
Loading

0 comments on commit e1b0805

Please sign in to comment.