From 2731f348dbec456538e64d60b10cc4cb39568e19 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 3 Jan 2024 10:30:44 +0100 Subject: [PATCH 1/7] function declaration --- src/air/public_input.cairo | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index 789c3c192..45a5ca298 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -25,6 +25,13 @@ struct PublicInput { #[generate_trait] impl PublicInputImpl of PublicInputTrait { + + // Computes the hash of the public input, which is used as the initial seed for the Fiat-Shamir heuristic. + fn get_public_input_hash(self: @PublicInput) -> u256 { + + 0 + } + // Returns the ratio between the product of all public memory cells and z^|public_memory|. // This is the value that needs to be at the memory__multi_column_perm__perm__public_memory_prod // member expression. From acfd5ba8fb51eb37b2b054a586b6b01e355e81e7 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 3 Jan 2024 11:01:19 +0100 Subject: [PATCH 2/7] main page pedersen hash --- src/air/public_input.cairo | 34 +++++++++++++++++++++++++++++++++- src/air/public_memory.cairo | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index 45a5ca298..1a34f3244 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -1,7 +1,10 @@ +use core::array::ArrayTrait; use cairo_verifier::air::public_memory::{ Page, PageTrait, ContinuousPageHeader, get_continuous_pages_product }; use cairo_verifier::common::math::{pow, Felt252PartialOrd, Felt252Div}; +use core::pedersen::PedersenTrait; +use core::hash::{HashStateTrait, HashStateExTrait, Hash}; #[derive(Drop)] struct SegmentInfo { @@ -23,12 +26,41 @@ struct PublicInput { continuous_page_headers: Span } +// impl ArrayHash, +HashStateTrait, +Copy, +Drop, +Drop> of Hash, S> { +// fn update_state(mut state: S, value: Array) -> S { +// let mut i: u32 = 0; +// loop { +// if i == value.len() { +// break; +// } + +// state = state.update_with(*value.at(i)); +// }; +// state +// } +// } + #[generate_trait] impl PublicInputImpl of PublicInputTrait { // Computes the hash of the public input, which is used as the initial seed for the Fiat-Shamir heuristic. fn get_public_input_hash(self: @PublicInput) -> u256 { - + // Main page hash. + let mut hash_state = PedersenTrait::new(0); + let mut i: u32 = 0; + loop { + if i == self.main_page.len() { + break; + } + + let page = *self.main_page.at(i); + hash_state = hash_state.update_with((page.address, page.value)); + + i += 1; + }; + + let main_page_hash = hash_state.finalize(); + 0 } diff --git a/src/air/public_memory.cairo b/src/air/public_memory.cairo index 262af7ddc..8bc32ed65 100644 --- a/src/air/public_memory.cairo +++ b/src/air/public_memory.cairo @@ -1,4 +1,4 @@ -#[derive(Drop)] +#[derive(Drop, Copy)] struct AddrValue { address: felt252, value: felt252 From 72917ea355c3cd55ec72e3d5f539f07709501f53 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 3 Jan 2024 11:01:50 +0100 Subject: [PATCH 3/7] fmt --- src/air/public_input.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index 1a34f3244..24b96f586 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -42,7 +42,6 @@ struct PublicInput { #[generate_trait] impl PublicInputImpl of PublicInputTrait { - // Computes the hash of the public input, which is used as the initial seed for the Fiat-Shamir heuristic. fn get_public_input_hash(self: @PublicInput) -> u256 { // Main page hash. From f6bb6af5497539a5f7ef9f9d18d30559f4067a92 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 3 Jan 2024 11:46:57 +0100 Subject: [PATCH 4/7] get_public_input_hash implemented - not tested --- src/air/public_input.cairo | 78 +++++++++++++++++++++++++++---------- src/air/public_memory.cairo | 6 +-- 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index 24b96f586..e9888a2dd 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -1,3 +1,5 @@ +use cairo_verifier::common::flip_endianness::FlipEndiannessTrait; +use cairo_verifier::common::array_append::ArrayAppendTrait; use core::array::ArrayTrait; use cairo_verifier::air::public_memory::{ Page, PageTrait, ContinuousPageHeader, get_continuous_pages_product @@ -5,8 +7,9 @@ use cairo_verifier::air::public_memory::{ use cairo_verifier::common::math::{pow, Felt252PartialOrd, Felt252Div}; use core::pedersen::PedersenTrait; use core::hash::{HashStateTrait, HashStateExTrait, Hash}; +use cairo_verifier::common::blake2s::blake2s; -#[derive(Drop)] +#[derive(Drop, Copy)] struct SegmentInfo { // Start address of the memory segment. begin_addr: felt252, @@ -19,27 +22,15 @@ struct PublicInput { log_n_steps: felt252, rc_min: felt252, rc_max: felt252, + layout: felt252, + dynamic_params: Array, segments: Array, padding_addr: felt252, padding_value: felt252, main_page: Page, - continuous_page_headers: Span + continuous_page_headers: Array } -// impl ArrayHash, +HashStateTrait, +Copy, +Drop, +Drop> of Hash, S> { -// fn update_state(mut state: S, value: Array) -> S { -// let mut i: u32 = 0; -// loop { -// if i == value.len() { -// break; -// } - -// state = state.update_with(*value.at(i)); -// }; -// state -// } -// } - #[generate_trait] impl PublicInputImpl of PublicInputTrait { // Computes the hash of the public input, which is used as the initial seed for the Fiat-Shamir heuristic. @@ -51,16 +42,63 @@ impl PublicInputImpl of PublicInputTrait { if i == self.main_page.len() { break; } - let page = *self.main_page.at(i); hash_state = hash_state.update_with((page.address, page.value)); - i += 1; }; let main_page_hash = hash_state.finalize(); - 0 + let mut hash_data = ArrayTrait::::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()); + ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.layout).into()); + + let mut i: u32 = 0; + loop { + if i == self.dynamic_params.len() { + break; + } + ArrayAppendTrait::< + _, u256 + >::append_big_endian(ref hash_data, (*self.dynamic_params.at(i)).into()); + }; + + let mut i: u32 = 0; + loop { + if i == self.segments.len() { + break; + } + let segment = *self.segments.at(i); + ArrayAppendTrait::< + _, u256 + >::append_big_endian(ref hash_data, segment.begin_addr.into()); + ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, segment.stop_ptr.into()); + }; + + 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(self.continuous_page_headers.len().flip_endianness()); + hash_data.append(self.main_page.len().flip_endianness()); + ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, main_page_hash.into()); + + let mut i: u32 = 0; + loop { + if i == self.continuous_page_headers.len() { + break; + } + let continuous_page = *self.continuous_page_headers.at(i); + ArrayAppendTrait::< + _, u256 + >::append_big_endian(ref hash_data, continuous_page.start_address.into()); + ArrayAppendTrait::< + _, u256 + >::append_big_endian(ref hash_data, continuous_page.size.into()); + ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, continuous_page.hash); + }; + + blake2s(hash_data) } // Returns the ratio between the product of all public memory cells and z^|public_memory|. @@ -88,7 +126,7 @@ impl PublicInputImpl of PublicInputTrait { let main_page_prod = self.main_page.get_product(z, alpha); let (continuous_pages_prod, continuous_pages_total_length) = get_continuous_pages_product( - *self.continuous_page_headers, + self.continuous_page_headers.span(), ); let prod = main_page_prod * continuous_pages_prod; diff --git a/src/air/public_memory.cairo b/src/air/public_memory.cairo index 8bc32ed65..c7482e77b 100644 --- a/src/air/public_memory.cairo +++ b/src/air/public_memory.cairo @@ -15,14 +15,14 @@ type Page = Array; // StarkProof object. // z = interaction_elements.memory_multi_column_perm_perm__interaction_elm // alpha = interaction_elements.memory_multi_column_perm_hash_interaction_elm0 -#[derive(Drop)] +#[derive(Drop, Copy)] struct ContinuousPageHeader { // Start address. - //start_address: felt252, + start_address: felt252, // Size of the page. size: felt252, // Hash of the page. - //hash: u256 + hash: u256, // Cumulative product of the page. prod: felt252, } From 9e30c25cee8f862507ab6f09e5542352b6f80c7e Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 3 Jan 2024 11:50:53 +0100 Subject: [PATCH 5/7] fix --- src/air/public_input.cairo | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index e9888a2dd..039d304ad 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -55,6 +55,7 @@ impl PublicInputImpl of PublicInputTrait { ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.rc_max).into()); ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, (*self.layout).into()); + // Dynamic params. let mut i: u32 = 0; loop { if i == self.dynamic_params.len() { @@ -65,6 +66,7 @@ impl PublicInputImpl of PublicInputTrait { >::append_big_endian(ref hash_data, (*self.dynamic_params.at(i)).into()); }; + // Segments. let mut i: u32 = 0; loop { if i == self.segments.len() { @@ -79,10 +81,12 @@ 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(self.continuous_page_headers.len().flip_endianness()); + hash_data.append((1 + self.continuous_page_headers.len()).flip_endianness()); + // Main page. hash_data.append(self.main_page.len().flip_endianness()); ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, main_page_hash.into()); + // Add the rest of the pages. let mut i: u32 = 0; loop { if i == self.continuous_page_headers.len() { From f4f511f48fc47e1282075c782a25879e1018c5a5 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 3 Jan 2024 12:23:21 +0100 Subject: [PATCH 6/7] fix&fmt --- src/air/public_input.cairo | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index 039d304ad..40d5878db 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -1,13 +1,11 @@ -use cairo_verifier::common::flip_endianness::FlipEndiannessTrait; -use cairo_verifier::common::array_append::ArrayAppendTrait; -use core::array::ArrayTrait; -use cairo_verifier::air::public_memory::{ - Page, PageTrait, ContinuousPageHeader, get_continuous_pages_product +use cairo_verifier::{ + common::{ + flip_endianness::FlipEndiannessTrait, array_append::ArrayAppendTrait, blake2s::blake2s, + math::{pow, Felt252PartialOrd, Felt252Div} + }, + air::public_memory::{Page, PageTrait, ContinuousPageHeader, get_continuous_pages_product} }; -use cairo_verifier::common::math::{pow, Felt252PartialOrd, Felt252Div}; -use core::pedersen::PedersenTrait; -use core::hash::{HashStateTrait, HashStateExTrait, Hash}; -use cairo_verifier::common::blake2s::blake2s; +use core::{pedersen::PedersenTrait, hash::{HashStateTrait, HashStateExTrait, Hash}}; #[derive(Drop, Copy)] struct SegmentInfo { @@ -82,6 +80,7 @@ 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()).flip_endianness()); + // Main page. hash_data.append(self.main_page.len().flip_endianness()); ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, main_page_hash.into()); From 88db9d099cc0bb6ec06405cd6d33835ae8718a99 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Thu, 4 Jan 2024 12:20:34 +0100 Subject: [PATCH 7/7] refactor --- src/air/public_input.cairo | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/air/public_input.cairo b/src/air/public_input.cairo index 40d5878db..8f73c2414 100644 --- a/src/air/public_input.cairo +++ b/src/air/public_input.cairo @@ -34,19 +34,19 @@ impl PublicInputImpl of PublicInputTrait { // Computes the hash of the public input, which is used as the initial seed for the Fiat-Shamir heuristic. fn get_public_input_hash(self: @PublicInput) -> u256 { // Main page hash. - let mut hash_state = PedersenTrait::new(0); + let mut main_page_hash_state = PedersenTrait::new(0); let mut i: u32 = 0; loop { if i == self.main_page.len() { break; } + let page = *self.main_page.at(i); - hash_state = hash_state.update_with((page.address, page.value)); + main_page_hash_state = main_page_hash_state.update_with((page.address, page.value)); + i += 1; }; - let main_page_hash = hash_state.finalize(); - let mut hash_data = ArrayTrait::::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()); @@ -59,9 +59,12 @@ impl PublicInputImpl of PublicInputTrait { if i == self.dynamic_params.len() { break; } + ArrayAppendTrait::< _, u256 >::append_big_endian(ref hash_data, (*self.dynamic_params.at(i)).into()); + + i += 1; }; // Segments. @@ -70,20 +73,25 @@ impl PublicInputImpl of PublicInputTrait { if i == self.segments.len() { break; } + let segment = *self.segments.at(i); ArrayAppendTrait::< _, u256 >::append_big_endian(ref hash_data, segment.begin_addr.into()); ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, segment.stop_ptr.into()); + + i += 1; }; 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()).flip_endianness()); + hash_data.append(1 + self.continuous_page_headers.len()); // Main page. - hash_data.append(self.main_page.len().flip_endianness()); - ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, main_page_hash.into()); + hash_data.append(self.main_page.len()); + ArrayAppendTrait::< + _, u256 + >::append_big_endian(ref hash_data, main_page_hash_state.finalize().into()); // Add the rest of the pages. let mut i: u32 = 0; @@ -91,6 +99,7 @@ impl PublicInputImpl of PublicInputTrait { if i == self.continuous_page_headers.len() { break; } + let continuous_page = *self.continuous_page_headers.at(i); ArrayAppendTrait::< _, u256 @@ -99,6 +108,8 @@ impl PublicInputImpl of PublicInputTrait { _, u256 >::append_big_endian(ref hash_data, continuous_page.size.into()); ArrayAppendTrait::<_, u256>::append_big_endian(ref hash_data, continuous_page.hash); + + i += 1; }; blake2s(hash_data)