From ca56a2f23e58803c8e9d31902ea905a3a05c9d3b Mon Sep 17 00:00:00 2001 From: Filip Krawczyk Date: Sat, 10 Feb 2024 00:11:05 +0100 Subject: [PATCH] Merge keccak and blake verifier versions --- src/air/public_input.cairo | 2 +- src/channel/channel.cairo | 2 +- src/common/array_append.cairo | 66 +++++++++++++++++++ src/common/hasher.cairo | 22 +++++-- src/proof_of_work/proof_of_work.cairo | 7 +- src/table_commitment/table_commitment.cairo | 2 +- src/vector_commitment/vector_commitment.cairo | 2 +- 7 files changed, 89 insertions(+), 14 deletions(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index b977539b1..09abab494 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -57,7 +57,7 @@ impl PublicInputImpl of PublicInputTrait { .update_with(AddrValueSize * self.main_page.len()); let main_page_hash = main_page_hash_state.finalize(); - let mut hash_data = ArrayTrait::::new(); // TODO: u64 for keccak + let mut hash_data = ArrayTrait::new(); // u32 for blake, u64 for keccak 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()); diff --git a/src/channel/channel.cairo b/src/channel/channel.cairo index 653f9fd3c..5798e0a44 100644 --- a/src/channel/channel.cairo +++ b/src/channel/channel.cairo @@ -65,7 +65,7 @@ impl ChannelImpl of ChannelTrait { } fn read_truncated_hash_from_prover(ref self: Channel, value: felt252) { - let mut hash_data = ArrayTrait::new(); // u32 for blake, u64 for keccak + let mut hash_data = ArrayTrait::::new(); // u32 for blake, u64 for keccak assert(self.digest.low != BoundedU128::max(), 'digest low is 2^128-1'); hash_data.append_big_endian(self.digest + 1); diff --git a/src/common/array_append.cairo b/src/common/array_append.cairo index 652efe8a7..38953720f 100644 --- a/src/common/array_append.cairo +++ b/src/common/array_append.cairo @@ -179,3 +179,69 @@ impl ArrayU8AppendU16 of ArrayAppendTrait { self.append(low); } } + +impl ArrayU64AppendU256 of ArrayAppendTrait { + fn append_little_endian(ref self: Array, element: u256) { + self.append_little_endian(element.low); + self.append_little_endian(element.high); + } + + fn append_big_endian(ref self: Array, element: u256) { + self.append_big_endian(element.high); + self.append_big_endian(element.low); + } +} + +impl ArrayU64AppendU128 of ArrayAppendTrait { + fn append_little_endian(ref self: Array, mut element: u128) { + let (high, low) = u128_split(element); + self.append(low); + self.append(high); + } + + fn append_big_endian(ref self: Array, mut element: u128) { + let (high, low) = u128_split(element); + self.append(high.flip_endianness()); + self.append(low.flip_endianness()); + } +} + +impl ArrayU64AppendU64 of ArrayAppendTrait { + fn append_little_endian(ref self: Array, mut element: u64) { + self.append(element); + } + + fn append_big_endian(ref self: Array, mut element: u64) { + self.append(element.flip_endianness()); + } +} + +impl ArrayU64AppendFelt of ArrayAppendTrait { + fn append_little_endian(ref self: Array, element: felt252) { + self.append_little_endian(Into::::into(element)); + } + + fn append_big_endian(ref self: Array, element: felt252) { + self.append_big_endian(Into::::into(element)); + } +} + +impl ArrayU64AppendFeltSpan of ArrayAppendTrait> { + fn append_little_endian(ref self: Array, mut element: Span) { + loop { + match element.pop_front() { + Option::Some(element) => { self.append_big_endian(*element); }, + Option::None => { break; } + } + }; + } + + fn append_big_endian(ref self: Array, mut element: Span) { + loop { + match element.pop_front() { + Option::Some(element) => { self.append_big_endian(*element); }, + Option::None => { break; } + } + }; + } +} \ No newline at end of file diff --git a/src/common/hasher.cairo b/src/common/hasher.cairo index 686e27f27..a6b9fe53a 100644 --- a/src/common/hasher.cairo +++ b/src/common/hasher.cairo @@ -1,8 +1,11 @@ -use cairo_verifier::common::{blake2s::blake2s, blake2s::truncated_blake2s, blake2s_u8::blake2s as blake2s_u8}; +use cairo_verifier::common::{blake2s::blake2s, blake2s::truncated_blake2s, blake2s_u8::blake2s as blake2s_u8, flip_endianness::FlipEndiannessTrait}; // blake hasher -// fn hash_u8(data: Array) -> u256 { +// fn hash_n_bytes(mut data: Array, n: u8, hash_len: bool) -> u256 { +// if hash_len { +// data.append(n); +// } // blake2s_u8(data) // } @@ -16,12 +19,19 @@ use cairo_verifier::common::{blake2s::blake2s, blake2s::truncated_blake2s, blake // keccak hasher -fn hash_u8(data: Array) -> u256 { - +fn hash_n_bytes(mut data: Array, n: u8, hash_len: bool) -> u256 { + if hash_len { + keccak::cairo_keccak(ref data, n.into(), 1) + } else { + keccak::cairo_keccak(ref data, 0, 0) + } } -fn hash_truncated(data: Array) -> felt252 { - +fn hash_truncated(mut data: Array) -> felt252 { + (keccak::cairo_keccak(ref data, 0, 0) + .flip_endianness() % 0x10000000000000000000000000000000000000000) + .try_into() + .unwrap() } fn hash(mut data: Array) -> u256 { diff --git a/src/proof_of_work/proof_of_work.cairo b/src/proof_of_work/proof_of_work.cairo index b637d2a5f..ee1c5374e 100644 --- a/src/proof_of_work/proof_of_work.cairo +++ b/src/proof_of_work/proof_of_work.cairo @@ -1,7 +1,7 @@ use cairo_verifier::{ common::{ flip_endianness::FlipEndiannessTrait, array_print::{SpanPrintTrait, ArrayPrintTrait}, - hasher::hash_u8, array_append::ArrayAppendTrait, math::pow, + hasher::hash_n_bytes, array_append::ArrayAppendTrait, math::pow, }, channel::channel::{Channel, ChannelTrait}, proof_of_work::config::{ProofOfWorkConfig} }; @@ -29,8 +29,7 @@ fn verify_proof_of_work(digest: u256, n_bits: u8, nonce: u64) { let mut init_hash_data = ArrayTrait::new(); // u8 with blake, u64 with keccak init_hash_data.append_big_endian(MAGIC); init_hash_data.append_big_endian(digest); - init_hash_data.append(n_bits); - let init_hash = hash_u8(init_hash_data).flip_endianness(); + let init_hash = hash_n_bytes(init_hash_data, n_bits.into(), true).flip_endianness(); // Compute Hash(init_hash || nonce ) // 32 bytes || 8 bytes @@ -39,7 +38,7 @@ fn verify_proof_of_work(digest: u256, n_bits: u8, nonce: u64) { let mut hash_data = ArrayTrait::new(); // u8 with blake, u64 with keccak hash_data.append_big_endian(init_hash); hash_data.append_big_endian(nonce); - let hash = hash_u8(hash_data).flip_endianness(); + let hash = hash_n_bytes(hash_data, 0, false).flip_endianness(); let work_limit = pow(2, 128 - n_bits.into()); assert( diff --git a/src/table_commitment/table_commitment.cairo b/src/table_commitment/table_commitment.cairo index 89bfd486e..947693af2 100644 --- a/src/table_commitment/table_commitment.cairo +++ b/src/table_commitment/table_commitment.cairo @@ -124,7 +124,7 @@ fn generate_vector_queries( *values[i * n_columns] } else { let slice = values.slice(i * n_columns, n_columns); - let mut data: Array = ArrayTrait::new(); + let mut data = ArrayTrait::new(); // u32 for blake, u64 for keccak data.append_big_endian(slice); hash_truncated(data) }; diff --git a/src/vector_commitment/vector_commitment.cairo b/src/vector_commitment/vector_commitment.cairo index bb95f95bd..af325e5ca 100644 --- a/src/vector_commitment/vector_commitment.cairo +++ b/src/vector_commitment/vector_commitment.cairo @@ -162,7 +162,7 @@ fn hash_blake_or_pedersen(x: felt252, y: felt252, is_verifier_friendly: bool) -> if is_verifier_friendly { PedersenTrait::new(x).update(y).finalize() } else { - let mut data = ArrayTrait::::new(); + let mut data = ArrayTrait::new(); // u32 for blake, u64 for keccak data.append_big_endian(x); data.append_big_endian(y); hash_truncated(data)