Skip to content

Commit

Permalink
Poseidon plain proof
Browse files Browse the repository at this point in the history
  • Loading branch information
spapinistarkware committed Jul 8, 2024
1 parent c9f154f commit e3b99fb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
23 changes: 20 additions & 3 deletions crates/prover/src/core/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn evaluate_and_commit_on_trace<B: Backend + MerkleOps<MerkleHasher>>(
Ok((commitment_scheme, interaction_elements))
}

pub fn generate_proof<B: Backend + MerkleOps<MerkleHasher>>(
// TODO(spapini): Rename to prove, and rename prover to prove_and_commit.
pub fn prove_without_commit<B: Backend + MerkleOps<MerkleHasher>>(
air: &impl AirProver<B>,
channel: &mut Channel,
interaction_elements: &InteractionElements,
Expand Down Expand Up @@ -180,7 +181,7 @@ pub fn prove<B: Backend + MerkleOps<MerkleHasher>>(
.collect_vec(),
);

generate_proof(
prove_without_commit(
&air,
channel,
&interaction_elements,
Expand Down Expand Up @@ -220,6 +221,22 @@ pub fn verify(
.map(|v| SecureField::from(*v))
.collect_vec(),
);
verify_without_commit(
air,
channel,
&interaction_elements,
&mut commitment_scheme,
proof,
)
}

pub fn verify_without_commit(
air: &impl Air,
channel: &mut Blake2sChannel,
interaction_elements: &InteractionElements,
commitment_scheme: &mut CommitmentSchemeVerifier,
proof: StarkProof,
) -> Result<(), VerificationError> {
let random_coeff = channel.draw_felt();

// Read composition polynomial commitment.
Expand Down Expand Up @@ -249,7 +266,7 @@ pub fn verify(
oods_point,
&trace_oods_values,
random_coeff,
&interaction_elements,
interaction_elements,
&proof.lookup_values,
)
{
Expand Down
58 changes: 51 additions & 7 deletions crates/prover/src/examples/poseidon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,20 @@ mod tests {
use tracing::{span, Level};

use super::N_LOG_INSTANCES_PER_ROW;
use crate::core::air::AirExt;
use crate::core::backend::simd::SimdBackend;
use crate::core::channel::{Blake2sChannel, Channel};
use crate::core::fields::m31::BaseField;
use crate::core::fields::IntoSlice;
use crate::core::prover::{prove, verify};
use crate::core::pcs::{CommitmentSchemeProver, CommitmentSchemeVerifier};
use crate::core::poly::circle::{CanonicCoset, PolyOps};
use crate::core::prover::{prove_without_commit, verify_without_commit, LOG_BLOWUP_FACTOR};
use crate::core::vcs::blake2_hash::Blake2sHasher;
use crate::core::vcs::hasher::Hasher;
use crate::core::InteractionElements;
use crate::examples::poseidon::{
apply_internal_round_matrix, apply_m4, gen_trace, PoseidonAir, PoseidonComponent,
LOG_EXPAND,
};
use crate::math::matrix::{RowMajorMatrix, SquareMatrix};

Expand Down Expand Up @@ -504,16 +509,55 @@ mod tests {
.parse::<u32>()
.unwrap();
let log_n_rows = log_n_instances - N_LOG_INSTANCES_PER_ROW as u32;
let component = PoseidonComponent { log_n_rows };
let span = span!(Level::INFO, "Trace generation").entered();
let trace = gen_trace(component.log_column_size());

// Precompute twiddles.
let span = span!(Level::INFO, "Precompute twiddles").entered();
let twiddles = SimdBackend::precompute_twiddles(
CanonicCoset::new(log_n_rows + LOG_EXPAND + LOG_BLOWUP_FACTOR)
.circle_domain()
.half_coset,
);
span.exit();

// Setup protocol.
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[])));
let air = PoseidonAir { component };
let proof = prove::<SimdBackend>(&air, channel, trace).unwrap();
let commitment_scheme = &mut CommitmentSchemeProver::new(LOG_BLOWUP_FACTOR);

// Trace.
let span = span!(Level::INFO, "Trace").entered();
let span1 = span!(Level::INFO, "Generation").entered();
let trace = gen_trace(log_n_rows);
span1.exit();
commitment_scheme.commit_on_evals(trace, channel, &twiddles);
span.exit();

// Prove constraints.
let component = PoseidonComponent { log_n_rows };
let air = PoseidonAir { component };
let proof = prove_without_commit::<SimdBackend>(
&air,
channel,
&InteractionElements::default(),
&twiddles,
commitment_scheme,
)
.unwrap();

// Verify.
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[])));
verify(proof, &air, channel).unwrap();
let commitment_scheme = &mut CommitmentSchemeVerifier::new();

// Decommit.
let sizes = air.column_log_sizes();
commitment_scheme.commit(proof.commitments[0], &sizes[0], channel);

verify_without_commit(
&air,
channel,
&InteractionElements::default(),
commitment_scheme,
proof,
)
.unwrap();
}
}

0 comments on commit e3b99fb

Please sign in to comment.