Skip to content

Commit

Permalink
Merge pull request #42 from HerodotusDev/feat/proof_of_work
Browse files Browse the repository at this point in the history
feat/proof_of_work
  • Loading branch information
Okm165 authored Jan 9, 2024
2 parents b104078 + 2000bb6 commit 475ab6f
Show file tree
Hide file tree
Showing 12 changed files with 857 additions and 0 deletions.
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

0 comments on commit 475ab6f

Please sign in to comment.