diff --git a/src/deserialization.cairo b/src/deserialization.cairo index 3af48f620..c1e2347f3 100644 --- a/src/deserialization.cairo +++ b/src/deserialization.cairo @@ -1,3 +1,4 @@ +use core::traits::TryInto; use core::array::ArrayTrait; use cairo_verifier::{ air::{ @@ -5,9 +6,12 @@ use cairo_verifier::{ traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness} }, fri::{fri_config::FriConfig, fri::{FriUnsentCommitment, FriWitness, FriLayerWitness}}, - table_commitment::{TableCommitmentConfig, TableCommitmentWitness, TableDecommitment}, + stark::{StarkProof, StarkConfig, StarkUnsentCommitment, StarkWitness}, + table_commitment::{ + TableCommitmentConfig, TableCommitmentWitness, TableDecommitment, TableUnsentCommitment + }, proof_of_work::{config::ProofOfWorkConfig, proof_of_work::ProofOfWorkUnsentCommitment}, - vector_commitment::vector_commitment::VectorCommitmentWitness, + vector_commitment::vector_commitment::{VectorCommitmentConfig, VectorCommitmentWitness}, }; #[derive(Drop, Serde)] @@ -17,6 +21,51 @@ struct StarkProofWithSerde { unsent_commitment: StarkUnsentCommitmentWithSerde, witness: StarkWitnessWithSerde, } +// impl IntoStarkProof of Into { +// fn into(self: StarkProofWithSerde) -> StarkProof { +// StarkProof { +// config: StarkConfig { +// traces: TracesConfig { +// original: TableCommitmentConfig { +// columns: *self.config.traces.original.columns, +// vector: VectorCommitmentConfig { +// height: *self.config.traces.original.vector.height, +// verifier_friendly_commitment_layers: *self.config.traces.original.vector.verifier_friendly_commitment_layers, +// } +// }, +// interaction: TableCommitmentConfig { +// columns: *self.config.traces.interaction.columns, +// vector: VectorCommitmentConfig { +// height: *self.config.traces.interaction.vector.height, +// verifier_friendly_commitment_layers: *self.config.traces.interaction.vector.verifier_friendly_commitment_layers, +// } +// }, +// }, +// composition: TableCommitmentConfig { +// columns: *self.config.composition.columns, +// vector: VectorCommitmentConfig { +// height: *self.config.composition.vector.height, +// verifier_friendly_commitment_layers: *self.config.composition.vector.verifier_friendly_commitment_layers, +// } +// }, +// fri: FriConfig { +// log_input_size: *self.config.fri.log_input_size, +// n_layers: *self.config.fri.n_layers, +// inner_layers: self.config.fri.inner_layers.span().map(|x| *x), +// fri_step_sizes: self.config.fri.fri_step_sizes.span().map(|x| *x), +// log_last_layer_degree_bound: *self.config.fri.log_last_layer_degree_bound, +// }, +// proof_of_work: ProofOfWorkConfig { +// n_bits: *self.config.proof_of_work.n_bits, +// }, +// log_trace_domain_size: *self.config.log_trace_domain_size, +// n_queries: *self.config.n_queries, +// log_n_cosets: *self.config.log_n_cosets, +// n_verifier_friendly_commitment_layers: *self.config.n_verifier_friendly_commitment_layers, +// }, +// } +// } +// } #[derive(Drop, Serde)] struct PublicInputWithSerde { @@ -51,23 +100,72 @@ struct StarkConfigWithSerde { // Number of layers that use a verifier friendly hash in each commitment. n_verifier_friendly_commitment_layers: felt252, } +// impl IntoStarkConfig of Into + #[derive(Drop, Serde)] struct TracesConfigWithSerde { original: TableCommitmentConfigWithSerde, interaction: TableCommitmentConfigWithSerde, } +impl IntoTracesConfig of Into { + fn into(self: TracesConfigWithSerde) -> TracesConfig { + TracesConfig { + original: TableCommitmentConfig { + n_columns: self.original.n_columns, + vector: VectorCommitmentConfig { + height: self.original.vector.height, + n_verifier_friendly_commitment_layers: self + .original + .vector + .n_verifier_friendly_commitment_layers, + } + }, + interaction: TableCommitmentConfig { + n_columns: self.interaction.n_columns, + vector: VectorCommitmentConfig { + height: self.interaction.vector.height, + n_verifier_friendly_commitment_layers: self + .interaction + .vector + .n_verifier_friendly_commitment_layers, + } + }, + } + } +} #[derive(Drop, Serde)] struct TableCommitmentConfigWithSerde { - columns: felt252, + n_columns: felt252, vector: VectorCommitmentConfigWithSerde, } +impl IntoTableCommitmentConfig of Into { + fn into(self: TableCommitmentConfigWithSerde) -> TableCommitmentConfig { + TableCommitmentConfig { + n_columns: self.n_columns, + vector: VectorCommitmentConfig { + height: self.vector.height, + n_verifier_friendly_commitment_layers: self + .vector + .n_verifier_friendly_commitment_layers, + } + } + } +} #[derive(Drop, Serde)] struct VectorCommitmentConfigWithSerde { height: felt252, - verifier_friendly_commitment_layers: felt252, + n_verifier_friendly_commitment_layers: felt252, +} +impl IntoVectorCommitmentConfig of Into { + fn into(self: VectorCommitmentConfigWithSerde) -> VectorCommitmentConfig { + VectorCommitmentConfig { + height: self.height, + n_verifier_friendly_commitment_layers: self.n_verifier_friendly_commitment_layers, + } + } } #[derive(Drop, Serde)] @@ -84,12 +182,47 @@ struct FriConfigWithSerde { fri_step_sizes: Array, log_last_layer_degree_bound: felt252, } +impl IntoFriConfig of Into { + fn into(self: FriConfigWithSerde) -> FriConfig { + let mut inner_layers = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == self.inner_layers.len() { + break; + } + + inner_layers + .append( + TableCommitmentConfig { + n_columns: *self.inner_layers.at(i), + vector: VectorCommitmentConfig { + height: *self.inner_layers.at(i + 1), + n_verifier_friendly_commitment_layers: *self.inner_layers.at(i + 2), + } + } + ); + i += 3; + }; + FriConfig { + log_input_size: self.log_input_size, + n_layers: self.n_layers, + inner_layers: inner_layers.span(), + fri_step_sizes: self.fri_step_sizes.span(), + log_last_layer_degree_bound: self.log_last_layer_degree_bound, + } + } +} #[derive(Drop, Serde)] struct ProofOfWorkConfigWithSerde { // Proof of work difficulty (number of bits required to be 0). n_bits: felt252, } +impl IntoProofOfWorkConfig of Into { + fn into(self: ProofOfWorkConfigWithSerde) -> ProofOfWorkConfig { + ProofOfWorkConfig { n_bits: self.n_bits.try_into().unwrap(), } + } +} #[derive(Drop, Serde)] struct StarkUnsentCommitmentWithSerde { @@ -99,23 +232,66 @@ struct StarkUnsentCommitmentWithSerde { fri: FriUnsentCommitmentWithSerde, proof_of_work: ProofOfWorkUnsentCommitmentWithSerde, } +impl IntoStarkUnsentCommitment of Into { + fn into(self: StarkUnsentCommitmentWithSerde) -> StarkUnsentCommitment { + StarkUnsentCommitment { + traces: self.traces.into(), + composition: self.composition, + oods_values: self.oods_values.span(), + fri: self.fri.into(), + proof_of_work: self.proof_of_work.into(), + } + } +} #[derive(Drop, Serde)] struct TracesUnsentCommitmentWithSerde { original: felt252, interaction: felt252, } +impl IntoTracesUnsentCommitment of Into { + fn into(self: TracesUnsentCommitmentWithSerde) -> TracesUnsentCommitment { + TracesUnsentCommitment { + original: TableUnsentCommitment { vector: self.original }, + interaction: TableUnsentCommitment { vector: self.original }, + } + } +} #[derive(Drop, Serde)] struct FriUnsentCommitmentWithSerde { inner_layers: Array, last_layer_coefficients: Array, } +impl IntoFriUnsentCommitment of Into { + fn into(self: FriUnsentCommitmentWithSerde) -> FriUnsentCommitment { + let mut inner_layers = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == self.inner_layers.len() { + break; + } + inner_layers.append(TableUnsentCommitment { vector: *self.inner_layers[i] }); + i += 1; + }; + FriUnsentCommitment { + inner_layers: inner_layers.span(), + last_layer_coefficients: self.last_layer_coefficients.span(), + } + } +} #[derive(Drop, Serde)] struct ProofOfWorkUnsentCommitmentWithSerde { nonce: felt252, } +impl IntoProofOfWorkUnsentCommitment of Into< + ProofOfWorkUnsentCommitmentWithSerde, ProofOfWorkUnsentCommitment +> { + fn into(self: ProofOfWorkUnsentCommitmentWithSerde) -> ProofOfWorkUnsentCommitment { + ProofOfWorkUnsentCommitment { nonce: self.nonce.try_into().unwrap(), } + } +} #[derive(Drop, Serde)] struct StarkWitnessWithSerde { @@ -126,18 +302,40 @@ struct StarkWitnessWithSerde { composition_witness: TableCommitmentWitnessWithSerde, fri_witness: FriWitnessWithSerde, } +// impl IntoStarkWitness of Into { +// fn into(self: StarkWitnessWithSerde) -> StarkWitness { +// StarkWitness { +// traces_decommitment: self.traces_decommitment.into(), +// traces_witness: self.traces_witness.into(), +// interaction: self.interaction.into(), +// composition_decommitment: self.composition_decommitment.into(), +// composition_witness: self.composition_witness.into(), +// fri_witness: self.fri_witness.into(), +// } +// } +// } #[derive(Drop, Serde)] struct TracesDecommitmentWithSerde { original: TableDecommitmentWithSerde, interaction: TableDecommitmentWithSerde, } +impl IntoTracesDecommitment of Into { + fn into(self: TracesDecommitmentWithSerde) -> TracesDecommitment { + TracesDecommitment { original: self.original.into(), interaction: self.interaction.into(), } + } +} #[derive(Drop, Serde)] struct TableDecommitmentWithSerde { n_values: felt252, values: Array, } +impl IntoTableDecommitment of Into { + fn into(self: TableDecommitmentWithSerde) -> TableDecommitment { + TableDecommitment { values: self.values.span(), } + } +} #[derive(Drop, Serde)] struct TracesWitnessWithSerde { @@ -148,12 +346,24 @@ struct TracesWitnessWithSerde { struct TableCommitmentWitnessWithSerde { vector: VectorCommitmentWitnessWithSerde, } +impl IntoTableCommitmentWitness of Into { + fn into(self: TableCommitmentWitnessWithSerde) -> TableCommitmentWitness { + TableCommitmentWitness { vector: self.vector.into(), } + } +} #[derive(Drop, Serde)] struct VectorCommitmentWitnessWithSerde { n_authentications: felt252, authentications: Array, } +impl IntoVectorCommitmentWitness of Into< + VectorCommitmentWitnessWithSerde, VectorCommitmentWitness +> { + fn into(self: VectorCommitmentWitnessWithSerde) -> VectorCommitmentWitness { + VectorCommitmentWitness { authentications: self.authentications.span(), } + } +} #[derive(Drop, Serde)] struct FriWitnessWithSerde { diff --git a/src/stark.cairo b/src/stark.cairo index 4fa1da390..a65ad411d 100644 --- a/src/stark.cairo +++ b/src/stark.cairo @@ -75,7 +75,7 @@ impl StarkConfigImpl of StarkConfigTrait { struct StarkUnsentCommitment { traces: TracesUnsentCommitment, composition: felt252, - oods_values: Array, + oods_values: Span, fri: FriUnsentCommitment, proof_of_work: ProofOfWorkUnsentCommitment, }