Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the repo to latest edition #290

Merged
merged 45 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
499c631
ascii
gaetbout Apr 2, 2024
337e592
utils
gaetbout Apr 2, 2024
19b4110
storage
gaetbout Apr 2, 2024
c702c1d
sorting
gaetbout Apr 2, 2024
97a467d
searching
gaetbout Apr 2, 2024
80b7c78
numeric
gaetbout Apr 2, 2024
af8d84f
merkle_tree
gaetbout Apr 2, 2024
fb9cea1
math
gaetbout Apr 2, 2024
3e2d296
linalg
gaetbout Apr 2, 2024
1022c73
encoding
gaetbout Apr 2, 2024
0649699
bytes
gaetbout Apr 2, 2024
b7b1d15
fix tests
gaetbout Apr 2, 2024
a933764
data_structure almost working
gaetbout Apr 3, 2024
bffb2c5
fix build
gaetbout Apr 3, 2024
4d4cb3b
fix tests
gaetbout Apr 3, 2024
d8fa7b7
integer
gaetbout Apr 5, 2024
30705e4
bytes
gaetbout Apr 5, 2024
8cbb420
list + utils
gaetbout Apr 5, 2024
4c2ce56
remove is_equal
gaetbout Apr 5, 2024
f74cb6c
sorting
gaetbout Apr 5, 2024
63450ec
more
gaetbout Apr 5, 2024
fb6c67e
math
gaetbout Apr 5, 2024
71b6af8
linalg
gaetbout Apr 5, 2024
7c31c47
final
gaetbout Apr 5, 2024
2829a95
merge w main
gaetbout Apr 5, 2024
9ba8a18
addressing comments
gaetbout Apr 5, 2024
6fa568e
panic_with_felt to panic macro
gaetbout Apr 5, 2024
1034487
reworking error message
gaetbout Apr 5, 2024
7ba9352
from_byte_array + to_byte_array transformed to an into trait
gaetbout Apr 8, 2024
56da76a
update todo
gaetbout Apr 8, 2024
8a8d5a5
i257 remove pub on struct
gaetbout Apr 9, 2024
1055cfd
list.cairo
gaetbout Apr 9, 2024
df1b7b5
fix tests
gaetbout Apr 9, 2024
c8ba40a
remove pub on bytes data
gaetbout Apr 12, 2024
edbeee8
bitarray remove pub on struct
gaetbout Apr 12, 2024
67aed80
fix Dijkstra
gaetbout Apr 12, 2024
5ea7de1
ed25519
gaetbout Apr 12, 2024
2f3fb25
byte-reader pub
gaetbout Apr 12, 2024
dde2417
Binary node using new
gaetbout Apr 12, 2024
48fa4cc
Binary node using new
gaetbout Apr 12, 2024
a58c941
fix prev push
gaetbout Apr 12, 2024
ca9ada8
storage proof new fn
gaetbout Apr 12, 2024
52e2afd
assert instead of panic
gaetbout Apr 12, 2024
f8e25e5
fix prev push
gaetbout Apr 12, 2024
8520b2f
remive i257_new
gaetbout Apr 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading