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

feat/proof_of_work #42

Merged
merged 12 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/common.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod blake2s;
mod blake2s_u8;
mod flip_endianness;
mod from_span;
mod horner_eval;
Expand Down
144 changes: 144 additions & 0 deletions src/common/array_append.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,147 @@ impl ArrayU32AppendU128 of ArrayAppendTrait<u32, u128> {
}
}
}

impl ArrayU32AppendU64 of ArrayAppendTrait<u32, u64> {
fn append_little_endian(ref self: Array<u32>, mut element: u64) {
let mut i = 2;
loop {
if i != 0 {
i -= 1;
let (q, r) = DivRem::div_rem(element, U64maxU32.try_into().unwrap());
self.append(r.try_into().unwrap());
element = q;
} else {
break;
}
}
}

fn append_big_endian(ref self: Array<u32>, mut element: u64) {
let mut array = ArrayTrait::<u32>::new();
array.append_little_endian(element);
let mut i = array.len();
loop {
if i != 0 {
i -= 1;
self.append((*array.at(i)).flip_endianness());
} else {
break;
}
}
}
}

impl ArrayU8AppendU256 of ArrayAppendTrait<u8, u256> {
fn append_little_endian(ref self: Array<u8>, element: u256) {
self.append_little_endian(element.low);
self.append_little_endian(element.high);
}

fn append_big_endian(ref self: Array<u8>, element: u256) {
self.append_big_endian(element.high);
self.append_big_endian(element.low);
}
}

// input's MSB is padded with 0s
// (internally felt252 is converted to u256)
impl ArrayU8AppendFelt of ArrayAppendTrait<u8, felt252> {
fn append_little_endian(ref self: Array<u8>, element: felt252) {
self.append_little_endian(Into::<felt252, u256>::into(element));
}

fn append_big_endian(ref self: Array<u8>, element: felt252) {
self.append_big_endian(Into::<felt252, u256>::into(element));
}
}

impl ArrayU8AppendU128 of ArrayAppendTrait<u8, u128> {
fn append_little_endian(ref self: Array<u8>, mut element: u128) {
let mut i = 16;
loop {
if i != 0 {
i -= 1;
let (q, r) = DivRem::div_rem(element, U128maxU8.try_into().unwrap());
self.append(r.try_into().unwrap());
element = q;
} else {
break;
}
}
}

fn append_big_endian(ref self: Array<u8>, mut element: u128) {
let mut array = ArrayTrait::<u8>::new();
array.append_little_endian(element);
let mut i = array.len();
loop {
if i != 0 {
i -= 1;
self.append(*array.at(i));
} else {
break;
}
}
}
}

impl ArrayU8AppendU64 of ArrayAppendTrait<u8, u64> {
fn append_little_endian(ref self: Array<u8>, mut element: u64) {
let mut i = 8;
loop {
if i != 0 {
i -= 1;
let (q, r) = DivRem::div_rem(element, U64maxU8.try_into().unwrap());
self.append(r.try_into().unwrap());
element = q;
} else {
break;
}
}
}

fn append_big_endian(ref self: Array<u8>, mut element: u64) {
let mut array = ArrayTrait::<u8>::new();
array.append_little_endian(element);
let mut i = array.len();
loop {
if i != 0 {
i -= 1;
self.append(*array.at(i));
} else {
break;
}
}
}
}

impl ArrayU8AppendU32 of ArrayAppendTrait<u8, u32> {
fn append_little_endian(ref self: Array<u8>, mut element: u32) {
let mut i = 4;
loop {
if i != 0 {
i -= 1;
let (q, r) = DivRem::div_rem(element, U32maxU8.try_into().unwrap());
self.append(r.try_into().unwrap());
element = q;
} else {
break;
}
}
}

fn append_big_endian(ref self: Array<u8>, mut element: u32) {
let mut array = ArrayTrait::<u8>::new();
array.append_little_endian(element);
let mut i = array.len();
loop {
if i != 0 {
i -= 1;
self.append(*array.at(i));
} else {
break;
}
}
}
}
Loading