Skip to content

Commit 611b94f

Browse files
Generic Merkle Channel in Prover (#466)
1 parent 61d9f6b commit 611b94f

4 files changed

Lines changed: 61 additions & 29 deletions

File tree

crates/cairo_air/src/privacy_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use num_traits::Zero;
1919
use stwo::core::fields::qm31::QM31;
2020
use stwo::core::fri::FriConfig;
2121
use stwo::core::pcs::PcsConfig;
22+
use stwo::core::vcs_lifted::blake2_merkle::Blake2sM31MerkleHasher;
2223

2324
use crate::privacy::{privacy_cairo_verifier_config, privacy_components};
2425
use crate::test::verify_cairo_with_component_set;
@@ -28,7 +29,7 @@ use crate::verify::build_cairo_verifier_circuit;
2829
/// Verifies with a circuit a proof of execution of another circuit.
2930
fn verify_circuit_proof(
3031
preprocessed_circuit: &PreprocessedCircuit,
31-
circuit_proof: CircuitProof,
32+
circuit_proof: CircuitProof<Blake2sM31MerkleHasher>,
3233
preprocessed_root: HashValue<QM31>,
3334
) -> Context<QM31> {
3435
let preprocessed_column_ids = preprocessed_circuit.preprocessed_trace.ids();

crates/circuit_prover/src/prover.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use circuits_stark_verifier::proof_from_stark_proof::{
1818
use itertools::chain;
1919
use num_traits::Zero;
2020
use stwo::core::air::Component;
21-
use stwo::core::channel::Blake2sM31Channel;
22-
use stwo::core::channel::Channel;
21+
use stwo::core::channel::{Channel, MerkleChannel};
2322
use stwo::core::fields::qm31::QM31;
2423
use stwo::core::pcs::PcsConfig;
2524
use stwo::core::poly::circle::CanonicCoset;
@@ -28,6 +27,7 @@ use stwo::core::proof_of_work::GrindOps;
2827
use stwo::core::utils::MaybeOwned;
2928
use stwo::core::vcs_lifted::blake2_merkle::Blake2sM31MerkleChannel;
3029
use stwo::core::vcs_lifted::blake2_merkle::Blake2sM31MerkleHasher;
30+
use stwo::core::vcs_lifted::merkle_hasher::MerkleHasherLifted;
3131
use stwo::prover::CommitmentSchemeProver;
3232
use stwo::prover::CommitmentTreeProver;
3333
use stwo::prover::ComponentProver;
@@ -39,13 +39,13 @@ use stwo::prover::{ProvingError, prove_ex};
3939

4040
const COMPOSITION_POLYNOMIAL_LOG_DEGREE_BOUND: u32 = 1;
4141

42-
pub struct CircuitProof {
42+
pub struct CircuitProof<H: MerkleHasherLifted> {
4343
pub pcs_config: PcsConfig,
4444
pub claim: CircuitClaim,
4545
pub interaction_pow_nonce: u64,
4646
pub interaction_claim: CircuitInteractionClaim,
4747
pub components: Vec<Box<dyn Component>>,
48-
pub stark_proof: Result<ExtendedStarkProof<Blake2sM31MerkleHasher>, ProvingError>,
48+
pub stark_proof: Result<ExtendedStarkProof<H>, ProvingError>,
4949
pub channel_salt: u32,
5050
}
5151

@@ -82,7 +82,25 @@ pub fn prove_circuit_assignment(
8282
preprocessed_circuit: &PreprocessedCircuit,
8383
base_column_pool: &BaseColumnPool<SimdBackend>,
8484
pcs_config: PcsConfig,
85-
) -> CircuitProof {
85+
) -> CircuitProof<Blake2sM31MerkleHasher> {
86+
prove_circuit_assignment_with_channel::<Blake2sM31MerkleChannel>(
87+
values,
88+
preprocessed_circuit,
89+
base_column_pool,
90+
pcs_config,
91+
)
92+
}
93+
94+
pub fn prove_circuit_assignment_with_channel<MC>(
95+
values: &[QM31],
96+
preprocessed_circuit: &PreprocessedCircuit,
97+
base_column_pool: &BaseColumnPool<SimdBackend>,
98+
pcs_config: PcsConfig,
99+
) -> CircuitProof<MC::H>
100+
where
101+
MC: MerkleChannel,
102+
SimdBackend: stwo::prover::backend::BackendForChannel<MC>,
103+
{
86104
let trace_log_size = preprocessed_circuit.params.trace_log_size;
87105
let lifting_log_size = trace_log_size + pcs_config.fri_config.log_blowup_factor;
88106
let pcs_config = PcsConfig { lifting_log_size: Some(lifting_log_size), ..pcs_config };
@@ -106,7 +124,7 @@ pub fn prove_circuit_assignment(
106124
let preprocessed_trace_polys = SimdBackend::interpolate_columns(preprocessed_trace, &twiddles);
107125

108126
let store_polynomials_coefficients = true;
109-
let preprocessed_tree = CommitmentTreeProver::<SimdBackend, Blake2sM31MerkleChannel>::new(
127+
let preprocessed_tree = CommitmentTreeProver::<SimdBackend, MC>::new(
110128
preprocessed_trace_polys,
111129
pcs_config.fri_config.log_blowup_factor,
112130
&twiddles,
@@ -115,7 +133,7 @@ pub fn prove_circuit_assignment(
115133
base_column_pool,
116134
);
117135

118-
prove_circuit_with_precompute(
136+
prove_circuit_with_precompute::<MC>(
119137
base_column_pool,
120138
&twiddles,
121139
preprocessed_circuit,
@@ -125,14 +143,18 @@ pub fn prove_circuit_assignment(
125143
)
126144
}
127145

128-
pub fn prove_circuit_with_precompute<'a>(
146+
pub fn prove_circuit_with_precompute<'a, MC>(
129147
base_column_pool: &BaseColumnPool<SimdBackend>,
130148
twiddles: &TwiddleTree<SimdBackend>,
131149
preprocessed_circuit: &PreprocessedCircuit,
132-
preprocessed_tree: MaybeOwned<'a, CommitmentTreeProver<SimdBackend, Blake2sM31MerkleChannel>>,
150+
preprocessed_tree: MaybeOwned<'a, CommitmentTreeProver<SimdBackend, MC>>,
133151
values: &[QM31],
134152
pcs_config: PcsConfig,
135-
) -> CircuitProof {
153+
) -> CircuitProof<MC::H>
154+
where
155+
MC: MerkleChannel,
156+
SimdBackend: stwo::prover::backend::BackendForChannel<MC>,
157+
{
136158
let PreprocessedCircuit { preprocessed_trace, params } = preprocessed_circuit;
137159
let CircuitParams { first_permutation_row, n_blake_gates, output_addresses, .. } = params;
138160
let trace_generator = TraceGenerator {
@@ -142,18 +164,17 @@ pub fn prove_circuit_with_precompute<'a>(
142164
};
143165

144166
// Setup protocol.
145-
let channel = &mut Blake2sM31Channel::default();
167+
let channel = &mut MC::C::default();
146168

147169
// Mix channel salt. Note that we first reduce it modulo `M31::P`, then cast it as QM31.
148170
let channel_salt = 0_u32;
149171
channel.mix_felts(&[channel_salt.into()]);
150172
pcs_config.mix_into(channel);
151-
let mut commitment_scheme =
152-
CommitmentSchemeProver::<SimdBackend, Blake2sM31MerkleChannel>::with_memory_pool(
153-
pcs_config,
154-
twiddles,
155-
base_column_pool,
156-
);
173+
let mut commitment_scheme = CommitmentSchemeProver::<SimdBackend, MC>::with_memory_pool(
174+
pcs_config,
175+
twiddles,
176+
base_column_pool,
177+
);
157178

158179
commitment_scheme.set_store_polynomials_coefficients();
159180

@@ -225,7 +246,7 @@ pub fn prove_circuit_with_precompute<'a>(
225246
}
226247

227248
pub fn prepare_circuit_proof_for_circuit_verifier(
228-
circuit_proof: CircuitProof,
249+
circuit_proof: CircuitProof<Blake2sM31MerkleHasher>,
229250
proof_config: &ProofConfig,
230251
) -> (Proof<QM31>, CircuitPublicData) {
231252
let CircuitProof {

crates/circuit_prover/src/prover_test.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use stwo::core::channel::Channel;
2121
use stwo::core::fields::qm31::QM31;
2222
use stwo::core::pcs::{CommitmentSchemeVerifier, PcsConfig, TreeVec};
2323
use stwo::core::vcs_lifted::blake2_merkle::Blake2sM31MerkleChannel;
24-
24+
use stwo::core::vcs_lifted::blake2_merkle::Blake2sM31MerkleHasher;
2525
// Not a power of 2 so that we can test component padding.
2626
const N: usize = 1030;
2727

@@ -105,7 +105,10 @@ pub fn build_m31_to_u32_context() -> Context<QM31> {
105105

106106
/// Verifies a [`CircuitProof`] using the stwo verifier. Asserts that the proof is valid
107107
/// and that the logup sum is zero.
108-
fn stwo_verify(circuit_proof: CircuitProof, preprocessed_circuit: &PreprocessedCircuit) {
108+
fn stwo_verify(
109+
circuit_proof: CircuitProof<Blake2sM31MerkleHasher>,
110+
preprocessed_circuit: &PreprocessedCircuit,
111+
) {
109112
let CircuitProof {
110113
components,
111114
claim,
@@ -231,7 +234,7 @@ fn test_prove_and_stark_verify_m31_to_u32_context() {
231234
/// Verifies a [`CircuitProof`] using the circuit verifier. Requires the expected
232235
/// `preprocessed_root` of the preprocessed trace.
233236
fn circuit_verify(
234-
circuit_proof: CircuitProof,
237+
circuit_proof: CircuitProof<Blake2sM31MerkleHasher>,
235238
preprocessed_circuit: &PreprocessedCircuit,
236239
preprocessed_root: [u32; 8],
237240
) {

crates/circuit_prover/src/witness/trace.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ use circuit_common::preprocessed::PreProcessedTrace;
2525
use itertools::Itertools;
2626
use num_traits::Zero;
2727
use rayon::scope;
28+
use stwo::core::channel::MerkleChannel;
2829
use stwo::core::fields::qm31::QM31;
29-
use stwo::core::vcs_lifted::blake2_merkle::Blake2sM31MerkleChannel;
3030
use stwo::prover::TreeBuilder;
31+
use stwo::prover::backend::BackendForChannel;
3132
use stwo::prover::backend::simd::SimdBackend;
3233
use stwo::prover::poly::circle::PolyOps;
3334
use stwo::prover::poly::twiddles::TwiddleTree;
@@ -36,14 +37,17 @@ pub struct TraceGenerator {
3637
pub qm31_ops_trace_generator: Qm31OpsTraceGenerator,
3738
}
3839

39-
pub fn write_trace(
40+
pub fn write_trace<MC: MerkleChannel>(
4041
context_values: &[QM31],
4142
preprocessed_trace: Arc<PreProcessedTrace>,
4243
output_addresses: &[usize],
43-
tree_builder: &mut TreeBuilder<'_, '_, SimdBackend, Blake2sM31MerkleChannel>,
44+
tree_builder: &mut TreeBuilder<'_, '_, SimdBackend, MC>,
4445
trace_generator: &TraceGenerator,
4546
twiddles: &TwiddleTree<SimdBackend>,
46-
) -> (CircuitClaim, CircuitInteractionClaimGenerator) {
47+
) -> (CircuitClaim, CircuitInteractionClaimGenerator)
48+
where
49+
SimdBackend: BackendForChannel<MC>,
50+
{
4751
let preprocessed_trace_ref = preprocessed_trace.as_ref();
4852

4953
// Parent scope: eq/qm31_ops traces run as spawns alongside everything else.
@@ -445,13 +449,16 @@ pub struct CircuitInteractionClaimGenerator {
445449
pub range_check_16: range_check_16::InteractionClaimGenerator,
446450
}
447451

448-
pub fn write_interaction_trace(
452+
pub fn write_interaction_trace<MC: MerkleChannel>(
449453
circuit_claim: &CircuitClaim,
450454
circuit_interaction_claim_generator: CircuitInteractionClaimGenerator,
451-
tree_builder: &mut TreeBuilder<'_, '_, SimdBackend, Blake2sM31MerkleChannel>,
455+
tree_builder: &mut TreeBuilder<'_, '_, SimdBackend, MC>,
452456
interaction_elements: &CircuitInteractionElements,
453457
twiddles: &TwiddleTree<SimdBackend>,
454-
) -> CircuitInteractionClaim {
458+
) -> CircuitInteractionClaim
459+
where
460+
SimdBackend: BackendForChannel<MC>,
461+
{
455462
let CircuitClaim { log_sizes, output_values: _ } = circuit_claim;
456463
let mut component_log_size_iter = log_sizes.iter();
457464

0 commit comments

Comments
 (0)