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

Blake2s u32 native proof of work issue #43

Closed
Closed
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
6 changes: 3 additions & 3 deletions src/air/public_input.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl PublicInputImpl of PublicInputTrait {
i += 1;
};

let mut hash_data = ArrayTrait::<u32>::new();
let mut hash_data = ArrayTrait::<u8>::new();
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.log_n_steps).into());
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.rc_min).into());
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.rc_max).into());
Expand Down Expand Up @@ -85,10 +85,10 @@ impl PublicInputImpl of PublicInputTrait {

ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.padding_addr).into());
ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.padding_value).into());
hash_data.append(1 + self.continuous_page_headers.len());
hash_data.append_big_endian(1 + self.continuous_page_headers.len());

// Main page.
hash_data.append(self.main_page.len());
hash_data.append_big_endian(self.main_page.len());
ArrayAppendTrait::<
_, u256
>::append_big_endian(ref hash_data, main_page_hash_state.finalize().into());
Expand Down
11 changes: 5 additions & 6 deletions src/channel/channel.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl ChannelImpl of ChannelTrait {
}

fn random_uint256_to_prover(ref self: Channel) -> u256 {
let mut hash_data = ArrayTrait::<u32>::new();
let mut hash_data = ArrayTrait::<u8>::new();
hash_data.append_big_endian(self.digest);
hash_data.append_big_endian(self.counter);
self.counter += 1;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl ChannelImpl of ChannelTrait {
}

fn read_felt_from_prover(ref self: Channel, value: felt252) {
let mut hash_data = ArrayTrait::<u32>::new();
let mut hash_data = ArrayTrait::<u8>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
hash_data.append_big_endian(self.digest + 1);
Expand All @@ -76,7 +76,7 @@ impl ChannelImpl of ChannelTrait {
}

fn read_felt_vector_from_prover(ref self: Channel, values: Span<felt252>) {
let mut hash_data = ArrayTrait::<u32>::new();
let mut hash_data = ArrayTrait::<u8>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
hash_data.append_big_endian(self.digest + 1);
Expand All @@ -95,15 +95,14 @@ impl ChannelImpl of ChannelTrait {
}

fn read_uint64_from_prover(ref self: Channel, value: u64) {
let mut hash_data = ArrayTrait::<u32>::new();
let mut hash_data = ArrayTrait::<u8>::new();

assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1');
hash_data.append_big_endian(self.digest + 1);

let low: u32 = (value % 0x100000000).try_into().unwrap();
let high: u32 = (value / 0x100000000).try_into().unwrap();
hash_data.append(high.flip_endianness());
hash_data.append(low.flip_endianness());
hash_data.append_big_endian(value);

self.digest = blake2s(hash_data).flip_endianness();
self.counter = 0;
Expand Down
116 changes: 115 additions & 1 deletion src/common/array_append.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ impl ArrayU32AppendFelt of ArrayAppendTrait<u32, felt252> {
}
}

// 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 ArrayU32AppendU128 of ArrayAppendTrait<u32, u128> {
fn append_little_endian(ref self: Array<u32>, mut element: u128) {
let mut i = 4;
Expand Down Expand Up @@ -103,4 +115,106 @@ impl ArrayU32AppendU64 of ArrayAppendTrait<u32, u64> {
}
}
}
}
}

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);
}
}

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;
}
}
}
}
41 changes: 26 additions & 15 deletions src/common/blake2s.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cairo_verifier::common::array_append::ArrayAppendTrait;
use cairo_verifier::common::flip_endianness::FlipEndiannessTrait;


fn blake2s(data: Array<u32>) -> u256 {
fn blake2s(data: Array<u8>) -> u256 {
let mut state = blake2s_init();
state = blake2s_update(state, data);
blake2s_final(state)
Expand All @@ -13,7 +13,7 @@ fn blake2s(data: Array<u32>) -> u256 {
// hash:
// blake2s(x, y) & ~((1<<96) - 1).
fn truncated_blake2s(x: felt252, y: felt252) -> felt252 {
let mut data = ArrayTrait::<u32>::new();
let mut data = ArrayTrait::<u8>::new();
data.append_big_endian(x);
data.append_big_endian(y);

Expand Down Expand Up @@ -78,7 +78,7 @@ struct blake2s_state {
t0: u32,
t1: u32,
f0: u32,
buf: Array<u32>, // length: 16 (64 bytes)
buf: Array<u8>, // length: 64
buflen: u32,
}

Expand All @@ -96,7 +96,7 @@ fn blake2s_init() -> blake2s_state {
let mut buf = ArrayTrait::new();
let mut i = 0;
loop {
if i == 16 {
if i == 64 {
break;
}
buf.append(0);
Expand All @@ -106,8 +106,19 @@ fn blake2s_init() -> blake2s_state {
blake2s_state { h: blake2s_IV, t0: 0, t1: 0, f0: 0, buf: buf, buflen: 0 }
}

fn blake2s_compress(mut s: blake2s_state, m: Array<u32>) -> blake2s_state {
assert(m.len() == 16, 'in array must have length 16');
fn blake2s_compress(mut s: blake2s_state, in: Array<u8>) -> blake2s_state {
assert(in.len() == 64, 'in array must have length 64');

let mut m: Array<u32> = ArrayTrait::new();

let mut i: u32 = 0;
loop {
if i == 16 {
break;
}
m.append(load32(*in[4 * i + 0], *in[4 * i + 1], *in[4 * i + 2], *in[4 * i + 3]));
i += 1;
};

let mut v0: u32 = *s.h[0];
let mut v1: u32 = *s.h[1];
Expand Down Expand Up @@ -277,13 +288,13 @@ fn blake2s_compress(mut s: blake2s_state, m: Array<u32>) -> blake2s_state {
s
}

fn blake2s_update(mut s: blake2s_state, in: Array<u32>) -> blake2s_state {
fn blake2s_update(mut s: blake2s_state, in: Array<u8>) -> blake2s_state {
let mut in_len = in.len();
let mut in_shift = 0;
let in_span = in.span();
if in_len != 0 {
let left = s.buflen;
let fill = 16 - left;
let fill = 64 - left;
if in_len > fill {
s.buflen = 0;

Expand Down Expand Up @@ -319,7 +330,7 @@ fn blake2s_update(mut s: blake2s_state, in: Array<u32>) -> blake2s_state {
in_len -= fill;

loop {
if in_len <= 16 {
if in_len <= 64_u32 {
break;
}

Expand All @@ -332,7 +343,7 @@ fn blake2s_update(mut s: blake2s_state, in: Array<u32>) -> blake2s_state {
let mut compress_in = ArrayTrait::new();
i = 0;
loop {
if i == 16 {
if i == 64_u32 {
break;
}
compress_in.append(*in_span[in_shift + i]);
Expand All @@ -341,8 +352,8 @@ fn blake2s_update(mut s: blake2s_state, in: Array<u32>) -> blake2s_state {

s = blake2s_compress(s, compress_in);

in_shift += 16;
in_len -= 16;
in_shift += 64_u32;
in_len -= 64_u32;
};
}

Expand All @@ -365,7 +376,7 @@ fn blake2s_update(mut s: blake2s_state, in: Array<u32>) -> blake2s_state {
i += 1;
};
loop {
if new_buf.len() == 16 {
if new_buf.len() == 64_u32 {
break;
}
new_buf.append(0);
Expand All @@ -381,7 +392,7 @@ fn blake2s_final(mut s: blake2s_state) -> u256 {
assert(s.f0 == 0, 'blake2s_is_lastblock');

// blake2s_increment_counter
s.t0 = u32_wrapping_add(s.t0, s.buflen * 4);
s.t0 = u32_wrapping_add(s.t0, s.buflen);
if s.t0 < s.buflen {
s.t1 = u32_wrapping_add(s.t1, 1);
}
Expand All @@ -399,7 +410,7 @@ fn blake2s_final(mut s: blake2s_state) -> u256 {
i += 1;
};
loop {
if i == 16 {
if i == 64 {
break;
}
buf.append(0);
Expand Down
14 changes: 10 additions & 4 deletions src/common/tests/test_blake2s.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cairo_verifier::common::blake2s::{blake2s, truncated_blake2s, load32};

fn get_arr_v1(n: u32) -> Array<u32> {
fn get_arr_v1(n: u32) -> Array<u8> {
let mut arr = ArrayTrait::new();
let mut i: u32 = 1;
loop {
Expand All @@ -13,7 +13,10 @@ fn get_arr_v1(n: u32) -> Array<u32> {
let mut out = ArrayTrait::new();
i = 0;
loop {
out.append(load32(*arr[4 * i], *arr[4 * i + 1], *arr[4 * i + 2], *arr[4 * i + 3]));
out.append(*arr[4 * i]);
out.append(*arr[4 * i + 1]);
out.append(*arr[4 * i + 2]);
out.append(*arr[4 * i + 3]);
i += 1;
if i == n {
break;
Expand All @@ -22,7 +25,7 @@ fn get_arr_v1(n: u32) -> Array<u32> {
out
}

fn get_arr_v2(n: u32) -> Array<u32> {
fn get_arr_v2(n: u32) -> Array<u8> {
let mut arr = ArrayTrait::new();
let mut s: u32 = 1;
let mut i: u32 = 1;
Expand All @@ -39,7 +42,10 @@ fn get_arr_v2(n: u32) -> Array<u32> {
let mut out = ArrayTrait::new();
i = 0;
loop {
out.append(load32(*arr[4 * i], *arr[4 * i + 1], *arr[4 * i + 2], *arr[4 * i + 3]));
out.append(*arr[4 * i]);
out.append(*arr[4 * i + 1]);
out.append(*arr[4 * i + 2]);
out.append(*arr[4 * i + 3]);
i += 1;
if i == n {
break;
Expand Down
Loading