Skip to content

Commit

Permalink
Updating the repo to latest edition (#290)
Browse files Browse the repository at this point in the history
STILL DRAFT WORKING ON IT
## 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):

This PR updates the repository to use the latest edition.
This might have some breaking changes as some struct/fn/enums might now
not be public by default but if any, it should be easy to identify and
fix.
BREAKING some data serialization
  • Loading branch information
gaetbout authored Apr 12, 2024
1 parent bd822fb commit d175c45
Show file tree
Hide file tree
Showing 108 changed files with 1,562 additions and 1,543 deletions.
1 change: 1 addition & 0 deletions src/ascii/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "alexandria_ascii"
version = "0.1.0"
description = "utilities for working with ascii values"
homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/ascii"
edition = "2023_11"

[tool]
fmt.workspace = true
Expand Down
56 changes: 25 additions & 31 deletions src/ascii/src/integer.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use alexandria_data_structures::array_ext::ArrayTraitExt;
use core::num::traits::Zero;

trait ToAsciiTrait<T, U> {
pub trait ToAsciiTrait<T, U> {
fn to_ascii(self: T) -> U;
}

// converts integers into an array of its individual ascii values
trait ToAsciiArrayTrait<T> {
pub trait ToAsciiArrayTrait<T> {
fn to_ascii_array(self: T) -> Array<felt252>;
fn to_inverse_ascii_array(self: T) -> Array<felt252>;
}
Expand All @@ -19,7 +20,7 @@ impl ToAsciiArrayTraitImpl<
+Into<T, felt252>,
+TryInto<felt252, T>,
+TryInto<T, NonZero<T>>,
+Zeroable<T>,
+Zero<T>,
+Drop<T>,
+Copy<T>,
> of ToAsciiArrayTrait<T> {
Expand Down Expand Up @@ -59,7 +60,7 @@ impl SmallIntegerToAsciiTraitImpl<
+Into<T, felt252>,
+TryInto<felt252, T>,
+TryInto<T, NonZero<T>>,
+Zeroable<T>,
+Zero<T>,
+Drop<T>,
+Copy<T>,
> of ToAsciiTrait<T, felt252> {
Expand Down Expand Up @@ -91,7 +92,7 @@ impl BigIntegerToAsciiTraitImpl<
+Into<T, felt252>,
+TryInto<felt252, T>,
+TryInto<T, NonZero<T>>,
+Zeroable<T>,
+Zero<T>,
+Drop<T>,
+Copy<T>,
> of ToAsciiTrait<T, Array<felt252>> {
Expand Down Expand Up @@ -174,33 +175,26 @@ 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;
loop {
match inverse_ascii_arr.pop_back() {
Option::Some(val) => {
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
};
},
Option::None(_) => {
// if ascii is 0 it means we have already appended the first ascii
// and there's no need to append it again
if ascii.is_non_zero() {
data.append(ascii);
}
break;
},
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;
};
index += 1;
};

if ascii.is_non_zero() {
data.append(ascii);
}
data
}
}
2 changes: 1 addition & 1 deletion src/ascii/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod integer;
pub mod integer;

#[cfg(test)]
mod tests;
Expand Down
20 changes: 10 additions & 10 deletions src/ascii/src/tests/test_ascii_integer.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alexandria_ascii::ToAsciiTrait;
use integer::BoundedInt;
use core::integer::BoundedInt;

#[test]
#[available_gas(2000000000)]
Expand All @@ -23,7 +23,7 @@ fn u256_to_ascii() {
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");
// ---------------------------- 62 cahr u256 test --------------------------- //
// ---------------------------- 62 char u256 test --------------------------- //
let ascii: Array<felt252> = 11579208923731619542357098500868790785326998466564056403945758_u256
.to_ascii();
assert_eq!(ascii.len(), 2, "u256 31 char wrong len");
Expand Down Expand Up @@ -59,41 +59,41 @@ fn u128_to_ascii() {
fn u64_to_ascii() {
// ------------------------------ max u64 test ------------------------------ //
let num: u64 = BoundedInt::max();
assert_eq!(num.to_ascii(), '18446744073709551615', "incorect u64 max felt");
assert_eq!(num.to_ascii(), '18446744073709551615', "incorrect u64 max felt");
// ------------------------------ min u64 test ------------------------------ //
let num: u64 = BoundedInt::min();
assert_eq!(num.to_ascii(), '0', "incorect u64 min felt");
assert_eq!(num.to_ascii(), '0', "incorrect u64 min felt");
}

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

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

#[test]
#[available_gas(2000000)]
fn u8_to_ascii() {
// ------------------------------- max u8 test ------------------------------ //
let num: u8 = BoundedInt::max();
assert_eq!(num.to_ascii(), '255', "incorect u8 max felt");
assert_eq!(num.to_ascii(), '255', "incorrect u8 max felt");
// ------------------------------- min u8 test ------------------------------ //
let num: u8 = BoundedInt::min();
assert_eq!(num.to_ascii(), '0', "incorect u8 min felt");
assert_eq!(num.to_ascii(), '0', "incorrect u8 min felt");
}
1 change: 1 addition & 0 deletions src/bytes/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "alexandria_bytes"
version = "0.1.0"
description = "An implementation similar to Solidity bytes written in Cairo 1"
homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/bytes"
edition = "2023_11"

[tool]
fmt.workspace = true
Expand Down
91 changes: 49 additions & 42 deletions src/bytes/src/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alexandria_bytes::utils::{
};
use alexandria_math::sha256::sha256;
use alexandria_math::{U128BitShift, U256BitShift};
use core::byte_array::ByteArrayTrait;
use starknet::ContractAddress;

/// Bytes is a dynamic array of u128, where each element contains 16 bytes.
Expand Down Expand Up @@ -31,26 +32,31 @@ const BYTES_PER_ELEMENT: usize = 16;
/// - size: the number of bytes in the Bytes
/// - data: the data of the Bytes
#[derive(Drop, Clone, PartialEq, Serde)]
struct Bytes {
pub struct Bytes {
size: usize,
data: Array<u128>
}

trait BytesTrait {
pub impl BytesIndex of IndexView<Bytes, usize, @u128> {
fn index(self: @Bytes, index: usize) -> @u128 {
self.data[index]
}
}


pub trait BytesTrait {
/// Create a Bytes from an array of u128
fn new(size: usize, data: Array<u128>) -> Bytes;
/// Create an empty Bytes
fn new_empty() -> Bytes;
/// Create a Bytes with size bytes 0
fn zero(size: usize) -> Bytes;
/// Create a Bytes from ByteArray ( Array<bytes31> )
fn from_byte_array(bytes: ByteArray) -> Bytes;
/// Create a ByteArray from Bytes
fn to_byte_array(self: Bytes) -> ByteArray;
/// Locate offset in Bytes
fn locate(offset: usize) -> (usize, usize);
/// Get Bytes size
fn size(self: @Bytes) -> usize;
/// Get data
fn data(self: Bytes) -> Array<u128>;
/// update specific value (1 bytes) at specific offset
fn update_at(ref self: Bytes, offset: usize, value: u8);
/// Read value with size bytes from Bytes, and packed into u128
Expand Down Expand Up @@ -144,42 +150,6 @@ impl BytesImpl of BytesTrait {
Bytes { size, data }
}

fn from_byte_array(mut bytes: ByteArray) -> Bytes {
let mut res = BytesTrait::new_empty();
loop {
match bytes.data.pop_front() {
Option::Some(val) => res.append_bytes31(val),
Option::None => { break; }
}
};
// Last elem
if bytes.pending_word_len != 0 {
let mut val: u256 = bytes.pending_word.into();
// Only append the right-aligned bytes of the last word ( using specified length )
val = U256BitShift::shl(val, 8 * (32 - bytes.pending_word_len.into()));
res.concat(@BytesTrait::new(bytes.pending_word_len, array![val.high, val.low]));
}
res
}

fn to_byte_array(self: Bytes) -> ByteArray {
let mut res: ByteArray = Default::default();
let mut offset = 0;
while offset < self
.size() {
if offset + 31 <= self.size() {
let (new_offset, value) = self.read_bytes31(offset);
res.append_word(value.into(), 31);
offset = new_offset;
} else {
let (new_offset, value) = self.read_u8(offset);
res.append_byte(value);
offset = new_offset;
}
};
res
}

/// Locate offset in Bytes
/// Arguments:
/// - offset: the offset in Bytes
Expand All @@ -197,6 +167,11 @@ impl BytesImpl of BytesTrait {
*self.size
}


fn data(self: Bytes) -> Array<u128> {
self.data
}

/// update specific value (1 bytes) at specific offset
fn update_at(ref self: Bytes, offset: usize, value: u8) {
assert(offset < self.size(), 'update out of bound');
Expand Down Expand Up @@ -581,3 +556,35 @@ impl BytesImpl of BytesTrait {
u8_array_to_u256(output.span())
}
}

pub impl ByteArrayIntoBytes of Into<ByteArray, Bytes> {
fn into(self: ByteArray) -> Bytes {
let mut res = BytesTrait::new_empty();
let mut len = 0;
while len < self.len() {
res.append_u8(self[len]);
len += 1;
};
res
}
}

pub impl BytesIntoByteArray of Into<Bytes, ByteArray> {
fn into(self: Bytes) -> ByteArray {
let mut res: ByteArray = Default::default();
let mut offset = 0;
while offset < self
.size() {
if offset + 31 <= self.size() {
let (new_offset, value) = self.read_bytes31(offset);
res.append_word(value.into(), 31);
offset = new_offset;
} else {
let (new_offset, value) = self.read_u8(offset);
res.append_byte(value);
offset = new_offset;
}
};
res
}
}
6 changes: 3 additions & 3 deletions src/bytes/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod bytes;
pub mod bytes;

#[cfg(test)]
mod tests;
mod utils;
pub mod utils;

use bytes::{Bytes, BytesTrait};
pub use bytes::{Bytes, BytesTrait, BytesIndex};
Loading

0 comments on commit d175c45

Please sign in to comment.