Skip to content

Commit

Permalink
add encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinones committed Nov 5, 2024
1 parent 89c4121 commit f10d2e9
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 163 deletions.
56 changes: 56 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "alexandria_bytes"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2"
dependencies = [
"alexandria_data_structures",
"alexandria_math",
]

[[package]]
name = "alexandria_data_structures"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2"
dependencies = [
"alexandria_encoding",
]

[[package]]
name = "alexandria_encoding"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2"
dependencies = [
"alexandria_bytes",
"alexandria_math",
"alexandria_numeric",
]

[[package]]
name = "alexandria_math"
version = "0.2.1"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2"

[[package]]
name = "alexandria_numeric"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2"
dependencies = [
"alexandria_math",
"alexandria_searching",
]

[[package]]
name = "alexandria_searching"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2#7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "cairo_template"
version = "0.1.0"
dependencies = [
"alexandria_encoding",
"graffiti",
"openzeppelin",
]

[[package]]
name = "graffiti"
version = "0.1.0"
source = "git+https://github.com/ponderingdemocritus/graffiti?rev=bc569531791dbc71c6cd8d9bc154c34eedad31fe#bc569531791dbc71c6cd8d9bc154c34eedad31fe"

[[package]]
name = "openzeppelin"
version = "0.18.0"
Expand Down
2 changes: 2 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edition = "2024_07"

[dependencies]
openzeppelin = "0.18.0"
alexandria_encoding = { git = "https://github.com/keep-starknet-strange/alexandria.git", rev = "7c19379ab6cf0f8b48be3c5b118ffddd7e26a4d2" }
graffiti = { git = "https://github.com/ponderingdemocritus/graffiti", rev = "bc569531791dbc71c6cd8d9bc154c34eedad31fe" }

[dev-dependencies]
cairo_test = "2.8.4"
Expand Down
8 changes: 8 additions & 0 deletions src/beast_stats.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[derive(Drop, Serde, starknet::Store)]
pub struct BeastStats {
pub tier: u8,
pub level: u8,
pub health: u32,
pub attack: u32,
pub type_beast: u8
}
230 changes: 230 additions & 0 deletions src/encoding.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
use core::num::traits::Bounded;
// use integer::{u32_as_non_zero, U32TryIntoNonZero};

pub fn u128_split(input: u128) -> (u64, u64) {
let (high, low) = core::integer::u128_safe_divmod(
input, 0x10000000000000000_u128.try_into().unwrap()
);
(high.try_into().unwrap(), low.try_into().unwrap())
}

#[inline(always)]
fn get_base64_char_set() -> Span<u8> {
let mut result = array![
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'+',
'/'
];
result.span()
}

pub fn bytes_base64_encode(_bytes: ByteArray) -> ByteArray {
encode_bytes(_bytes, get_base64_char_set())
}

fn encode_bytes(mut bytes: ByteArray, base64_chars: Span<u8>) -> ByteArray {
let mut result: ByteArray = "";
if bytes.len() == 0 {
return result;
}
let mut p: u8 = 0;
let c = bytes.len() % 3;
if c == 1 {
p = 2;
bytes.append_byte(0_u8);
bytes.append_byte(0_u8);
} else if c == 2 {
p = 1;
bytes.append_byte(0_u8);
}

let mut i = 0;
let bytes_len = bytes.len();
let last_iteration = bytes_len - 3;
loop {
if i == bytes_len {
break;
}
let n: u32 = (bytes.at(i).unwrap()).into()
* 65536 | (bytes.at(i + 1).unwrap()).into()
* 256 | (bytes.at(i + 2).unwrap()).into();
let e1 = (n / 262144) & 63;
let e2 = (n / 4096) & 63;
let e3 = (n / 64) & 63;
let e4 = n & 63;

if i == last_iteration {
if p == 2 {
result.append_byte(*base64_chars[e1]);
result.append_byte(*base64_chars[e2]);
result.append_byte('=');
result.append_byte('=');
} else if p == 1 {
result.append_byte(*base64_chars[e1]);
result.append_byte(*base64_chars[e2]);
result.append_byte(*base64_chars[e3]);
result.append_byte('=');
} else {
result.append_byte(*base64_chars[e1]);
result.append_byte(*base64_chars[e2]);
result.append_byte(*base64_chars[e3]);
result.append_byte(*base64_chars[e4]);
}
} else {
result.append_byte(*base64_chars[e1]);
result.append_byte(*base64_chars[e2]);
result.append_byte(*base64_chars[e3]);
result.append_byte(*base64_chars[e4]);
}

i += 3;
};
result
}


trait BytesUsedTrait<T> {
/// Returns the number of bytes used to represent a `T` value.
/// # Arguments
/// * `self` - The value to check.
/// # Returns
/// The number of bytes used to represent the value.
fn bytes_used(self: T) -> u8;
}

impl U8BytesUsedTraitImpl of BytesUsedTrait<u8> {
fn bytes_used(self: u8) -> u8 {
if self == 0 {
return 0;
}

return 1;
}
}

impl USizeBytesUsedTraitImpl of BytesUsedTrait<usize> {
fn bytes_used(self: usize) -> u8 {
if self < 0x10000 { // 256^2
if self < 0x100 { // 256^1
if self == 0 {
return 0;
} else {
return 1;
};
}
return 2;
} else {
if self < 0x1000000 { // 256^3
return 3;
}
return 4;
}
}
}

impl U64BytesUsedTraitImpl of BytesUsedTrait<u64> {
fn bytes_used(self: u64) -> u8 {
if self <= Bounded::<u32>::MAX.into() { // 256^4
return BytesUsedTrait::<u32>::bytes_used(self.try_into().unwrap());
} else {
if self < 0x1000000000000 { // 256^6
if self < 0x10000000000 {
if self < 0x100000000 {
return 4;
}
return 5;
}
return 6;
} else {
if self < 0x100000000000000 { // 256^7
return 7;
} else {
return 8;
}
}
}
}
}


impl U128BytesTraitUsedImpl of BytesUsedTrait<u128> {
fn bytes_used(self: u128) -> u8 {
let (u64high, u64low) = u128_split(self);
if u64high == 0 {
return BytesUsedTrait::<u64>::bytes_used(u64low.try_into().unwrap());
} else {
return BytesUsedTrait::<u64>::bytes_used(u64high.try_into().unwrap()) + 8;
}
}
}

impl U256BytesUsedTraitImpl of BytesUsedTrait<u256> {
fn bytes_used(self: u256) -> u8 {
if self.high == 0 {
return BytesUsedTrait::<u128>::bytes_used(self.low.try_into().unwrap());
} else {
return BytesUsedTrait::<u128>::bytes_used(self.high.try_into().unwrap()) + 16;
}
}
}
Loading

0 comments on commit f10d2e9

Please sign in to comment.