Skip to content

Commit

Permalink
chore: rename *ProvingKey to *PartialProvingKey (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang authored May 28, 2024
1 parent e527dc4 commit 6db59d9
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 198 deletions.
34 changes: 16 additions & 18 deletions chips/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::{iter, sync::Arc};

use afs_chips::{range, range_gate, xor_bits, xor_limbs};
use afs_stark_backend::prover::USE_DEBUG_BUILDER;
use afs_stark_backend::rap::AnyRap;
use afs_stark_backend::verifier::VerificationError;
use afs_test_utils::config::baby_bear_poseidon2::run_simple_test_no_pis;
use afs_test_utils::interaction::dummy_interaction_air::DummyInteractionAir;
use afs_test_utils::utils::create_seeded_rng;
use afs_test_utils::{
config::baby_bear_poseidon2::run_simple_test,
interaction::dummy_interaction_air::DummyInteractionAir,
};
use p3_baby_bear::BabyBear;
use p3_field::AbstractField;
use p3_matrix::dense::{DenseMatrix, RowMajorMatrix};
Expand Down Expand Up @@ -72,7 +70,7 @@ fn test_list_range_checker() {
.chain(iter::once(range_trace))
.collect::<Vec<DenseMatrix<BabyBear>>>();

run_simple_test(all_chips, all_traces).expect("Verification failed");
run_simple_test_no_pis(all_chips, all_traces).expect("Verification failed");
}

#[test]
Expand Down Expand Up @@ -123,7 +121,7 @@ fn test_xor_bits_chip() {
.chain(iter::once(xor_chip_trace))
.collect::<Vec<DenseMatrix<BabyBear>>>();

run_simple_test(all_chips, all_traces).expect("Verification failed");
run_simple_test_no_pis(all_chips, all_traces).expect("Verification failed");
}

#[test]
Expand Down Expand Up @@ -165,7 +163,7 @@ fn negative_test_xor_bits_chip() {
4,
);

let result = run_simple_test(
let result = run_simple_test_no_pis(
vec![&dummy_requester, &*xor_chip],
vec![dummy_trace, xor_chip_trace],
);
Expand Down Expand Up @@ -248,7 +246,7 @@ fn test_xor_limbs_chip() {
.chain(iter::once(xor_lookup_chip_trace))
.collect::<Vec<DenseMatrix<BabyBear>>>();

run_simple_test(all_chips, all_traces).expect("Verification failed");
run_simple_test_no_pis(all_chips, all_traces).expect("Verification failed");
}

#[test]
Expand Down Expand Up @@ -304,7 +302,7 @@ fn negative_test_xor_limbs_chip() {
let xor_limbs_chip_trace = xor_chip.generate_trace();
let xor_lookup_chip_trace = xor_chip.xor_lookup_chip.generate_trace();

let result = run_simple_test(
let result = run_simple_test_no_pis(
vec![&requester, &xor_chip, &xor_chip.xor_lookup_chip],
vec![requester_trace, xor_limbs_chip_trace, xor_lookup_chip_trace],
);
Expand Down Expand Up @@ -376,7 +374,7 @@ fn test_range_gate_chip() {
.chain(iter::once(range_trace))
.collect::<Vec<DenseMatrix<BabyBear>>>();

run_simple_test(all_chips, all_traces).expect("Verification failed");
run_simple_test_no_pis(all_chips, all_traces).expect("Verification failed");
}

#[test]
Expand Down Expand Up @@ -404,12 +402,12 @@ fn negative_test_range_gate_chip() {
2,
);

let result = catch_unwind(AssertUnwindSafe(|| {
run_simple_test(vec![&range_checker], vec![range_trace]).expect("Verification failed");
}));

assert!(
result.is_err(),
"Expected AIR constraints to be violated, but they passed"
USE_DEBUG_BUILDER.with(|debug| {
*debug.lock().unwrap() = false;
});
assert_eq!(
run_simple_test_no_pis(vec![&range_checker], vec![range_trace]),
Err(VerificationError::OodEvaluationMismatch),
"Expected constraint to fail"
);
}
18 changes: 9 additions & 9 deletions stark-backend/src/keygen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
};

use self::types::{
create_commit_to_air_graph, MultiStarkProvingKey, ProverOnlySinglePreprocessedData,
StarkProvingKey, StarkVerifyingKey, TraceWidth, VerifierSinglePreprocessedData,
create_commit_to_air_graph, MultiStarkPartialProvingKey, ProverOnlySinglePreprocessedData,
StarkPartialProvingKey, StarkPartialVerifyingKey, TraceWidth, VerifierSinglePreprocessedData,
};

/// Constants for interactive AIRs
Expand All @@ -30,22 +30,22 @@ pub struct MultiStarkKeygenBuilder<'a, SC: StarkGenericConfig> {
/// `placeholder_main_matrix_in_commit[commit_idx][mat_idx] =` matrix width, it is used to store
/// a placeholder of a main trace matrix that must be committed during proving
placeholder_main_matrix_in_commit: Vec<Vec<usize>>,
pk: MultiStarkProvingKey<SC>,
partial_pk: MultiStarkPartialProvingKey<SC>,
}

impl<'a, SC: StarkGenericConfig> MultiStarkKeygenBuilder<'a, SC> {
pub fn new(config: &'a SC) -> Self {
Self {
config,
pk: MultiStarkProvingKey::empty(),
partial_pk: MultiStarkPartialProvingKey::empty(),
placeholder_main_matrix_in_commit: vec![vec![]],
}
}

/// Generates proving key, resetting the state of the builder.
/// The verifying key can be obtained from the proving key.
pub fn generate_pk(&mut self) -> MultiStarkProvingKey<SC> {
let mut pk = std::mem::take(&mut self.pk);
pub fn generate_partial_pk(&mut self) -> MultiStarkPartialProvingKey<SC> {
let mut pk = std::mem::take(&mut self.partial_pk);
// Determine global num challenges to sample
let num_phases = pk
.per_air
Expand Down Expand Up @@ -168,7 +168,7 @@ impl<'a, SC: StarkGenericConfig> MultiStarkKeygenBuilder<'a, SC> {
&num_exposed_values,
);
let quotient_degree = 1 << log_quotient_degree;
let vk = StarkVerifyingKey {
let vk = StarkPartialVerifyingKey {
degree,
preprocessed_data: prep_verifier_data,
width,
Expand All @@ -178,12 +178,12 @@ impl<'a, SC: StarkGenericConfig> MultiStarkKeygenBuilder<'a, SC> {
num_exposed_values_after_challenge: num_exposed_values,
num_challenges_to_sample,
};
let pk = StarkProvingKey {
let pk = StarkPartialProvingKey {
vk,
preprocessed_data: prep_prover_data,
};

self.pk.per_air.push(pk);
self.partial_pk.per_air.push(pk);
}

/// Default way to add a single Interactive AIR.
Expand Down
28 changes: 14 additions & 14 deletions stark-backend/src/keygen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub struct TraceWidth {
serialize = "PcsProverData<SC>: Serialize",
deserialize = "PcsProverData<SC>: Deserialize<'de>"
))]
pub struct StarkProvingKey<SC: StarkGenericConfig> {
pub struct StarkPartialProvingKey<SC: StarkGenericConfig> {
/// Verifying key
pub vk: StarkVerifyingKey<SC>,
pub vk: StarkPartialVerifyingKey<SC>,
/// Prover only data for preprocessed trace
pub preprocessed_data: Option<ProverOnlySinglePreprocessedData<SC>>,
}
Expand All @@ -41,7 +41,7 @@ pub struct StarkProvingKey<SC: StarkGenericConfig> {
serialize = "Com<SC>: Serialize",
deserialize = "Com<SC>: Deserialize<'de>"
))]
pub struct StarkVerifyingKey<SC: StarkGenericConfig> {
pub struct StarkPartialVerifyingKey<SC: StarkGenericConfig> {
/// Height of trace matrix.
pub degree: usize,
/// Preprocessed trace data, if any
Expand Down Expand Up @@ -101,8 +101,8 @@ pub struct VerifierSinglePreprocessedData<SC: StarkGenericConfig> {
serialize = "PcsProverData<SC>: Serialize",
deserialize = "PcsProverData<SC>: Deserialize<'de>"
))]
pub struct MultiStarkProvingKey<SC: StarkGenericConfig> {
pub per_air: Vec<StarkProvingKey<SC>>,
pub struct MultiStarkPartialProvingKey<SC: StarkGenericConfig> {
pub per_air: Vec<StarkPartialProvingKey<SC>>,
/// Number of multi-matrix commitments that hold commitments to the partitioned main trace matrices across all AIRs.
pub num_main_trace_commitments: usize,
/// Mapping from commit_idx to global AIR index for matrix in commitment, in oder.
Expand All @@ -112,13 +112,13 @@ pub struct MultiStarkProvingKey<SC: StarkGenericConfig> {
pub num_challenges_to_sample: Vec<usize>,
}

impl<SC: StarkGenericConfig> Default for MultiStarkProvingKey<SC> {
impl<SC: StarkGenericConfig> Default for MultiStarkPartialProvingKey<SC> {
fn default() -> Self {
Self::empty()
}
}

impl<SC: StarkGenericConfig> MultiStarkProvingKey<SC> {
impl<SC: StarkGenericConfig> MultiStarkPartialProvingKey<SC> {
/// Empty with 1 main trace commitment
pub fn empty() -> Self {
Self {
Expand All @@ -132,7 +132,7 @@ impl<SC: StarkGenericConfig> MultiStarkProvingKey<SC> {
}

pub fn new(
per_air: Vec<StarkProvingKey<SC>>,
per_air: Vec<StarkPartialProvingKey<SC>>,
num_main_trace_commitments: usize,
num_challenges_to_sample: Vec<usize>,
) -> Self {
Expand All @@ -150,8 +150,8 @@ impl<SC: StarkGenericConfig> MultiStarkProvingKey<SC> {
}
}

pub fn vk(&self) -> MultiStarkVerifyingKey<SC> {
MultiStarkVerifyingKey {
pub fn partial_vk(&self) -> MultiStarkPartialVerifyingKey<SC> {
MultiStarkPartialVerifyingKey {
per_air: self.per_air.iter().map(|pk| pk.vk.clone()).collect(),
main_commit_to_air_graph: self.main_commit_to_air_graph.clone(),
num_main_trace_commitments: self.num_main_trace_commitments,
Expand Down Expand Up @@ -186,8 +186,8 @@ impl<SC: StarkGenericConfig> MultiStarkProvingKey<SC> {
serialize = "Com<SC>: Serialize",
deserialize = "Com<SC>: Deserialize<'de>"
))]
pub struct MultiStarkVerifyingKey<SC: StarkGenericConfig> {
pub per_air: Vec<StarkVerifyingKey<SC>>,
pub struct MultiStarkPartialVerifyingKey<SC: StarkGenericConfig> {
pub per_air: Vec<StarkPartialVerifyingKey<SC>>,
/// Number of multi-matrix commitments that hold commitments to the partitioned main trace matrices across all AIRs.
pub num_main_trace_commitments: usize,
/// Mapping from commit_idx to global AIR index for matrix in commitment, in oder.
Expand All @@ -197,9 +197,9 @@ pub struct MultiStarkVerifyingKey<SC: StarkGenericConfig> {
pub num_challenges_to_sample: Vec<usize>,
}

impl<SC: StarkGenericConfig> MultiStarkVerifyingKey<SC> {
impl<SC: StarkGenericConfig> MultiStarkPartialVerifyingKey<SC> {
pub fn new(
per_air: Vec<StarkVerifyingKey<SC>>,
per_air: Vec<StarkPartialVerifyingKey<SC>>,
num_main_trace_commitments: usize,
num_challenges_to_sample: Vec<usize>,
) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions stark-backend/src/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
air_builders::debug::check_constraints::check_constraints,
commit::CommittedSingleMatrixView,
config::{Com, PcsProof, PcsProverData},
keygen::types::MultiStarkProvingKey,
keygen::types::MultiStarkPartialProvingKey,
prover::trace::SingleRapCommittedTraceView,
rap::AnyRap,
};
Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'c, SC: StarkGenericConfig> MultiTraceStarkProver<'c, SC> {
pub fn prove<'a>(
&self,
challenger: &mut SC::Challenger,
pk: &'a MultiStarkProvingKey<SC>,
pk: &'a MultiStarkPartialProvingKey<SC>,
main_trace_data: MultiAirCommittedTraceData<'a, SC>,
public_values: &'a [Vec<Val<SC>>],
) -> Proof<SC>
Expand Down Expand Up @@ -277,7 +277,7 @@ impl<'c, SC: StarkGenericConfig> MultiTraceStarkProver<'c, SC> {
pub fn prove_raps_with_committed_traces<'a>(
&self,
challenger: &mut SC::Challenger,
pk: &'a MultiStarkProvingKey<SC>,
partial_pk: &'a MultiStarkPartialProvingKey<SC>,
raps: Vec<&'a dyn AnyRap<SC>>,
trace_views: Vec<SingleRapCommittedTraceView<'a, SC>>,
main_pcs_data: &[(Com<SC>, &PcsProverData<SC>)],
Expand All @@ -303,7 +303,7 @@ impl<'c, SC: StarkGenericConfig> MultiTraceStarkProver<'c, SC> {
let alpha: SC::Challenge = challenger.sample_ext_element();
tracing::debug!("alpha: {alpha:?}");

let quotient_degrees = pk
let quotient_degrees = partial_pk
.per_air
.iter()
.map(|pk| pk.vk.quotient_degree)
Expand Down Expand Up @@ -348,7 +348,7 @@ impl<'c, SC: StarkGenericConfig> MultiTraceStarkProver<'c, SC> {

let main_data: Vec<_> = main_pcs_data
.iter()
.zip_eq(&pk.main_commit_to_air_graph.commit_to_air_index)
.zip_eq(&partial_pk.main_commit_to_air_graph.commit_to_air_index)
.map(|((_, data), mat_to_air_index)| {
let domains = mat_to_air_index
.iter()
Expand Down
4 changes: 2 additions & 2 deletions stark-backend/src/prover/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tracing::info_span;
use crate::{
commit::CommittedSingleMatrixView,
config::{Com, PcsProverData},
keygen::types::MultiStarkVerifyingKey,
keygen::types::MultiStarkPartialVerifyingKey,
rap::AnyRap,
};

Expand Down Expand Up @@ -52,7 +52,7 @@ impl<'a, SC: StarkGenericConfig> TraceCommitmentBuilder<'a, SC> {

pub fn view<'b>(
&'b self,
vk: &MultiStarkVerifyingKey<SC>,
vk: &MultiStarkPartialVerifyingKey<SC>,
airs: Vec<&'b dyn AnyRap<SC>>,
) -> MultiAirCommittedTraceData<'b, SC>
where
Expand Down
6 changes: 3 additions & 3 deletions stark-backend/src/verifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod error;
pub use error::*;

use crate::{
keygen::types::MultiStarkVerifyingKey,
keygen::types::MultiStarkPartialVerifyingKey,
prover::{opener::AdjacentOpenedValues, types::Proof},
rap::AnyRap,
};
Expand All @@ -34,7 +34,7 @@ impl<'c, SC: StarkGenericConfig> MultiTraceStarkVerifier<'c, SC> {
pub fn verify(
&self,
challenger: &mut SC::Challenger,
vk: MultiStarkVerifyingKey<SC>,
vk: MultiStarkPartialVerifyingKey<SC>,
raps: Vec<&dyn AnyRap<SC>>,
proof: Proof<SC>,
public_values: &[Vec<Val<SC>>],
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<'c, SC: StarkGenericConfig> MultiTraceStarkVerifier<'c, SC> {
pub fn verify_raps(
&self,
challenger: &mut SC::Challenger,
vk: MultiStarkVerifyingKey<SC>,
vk: MultiStarkPartialVerifyingKey<SC>,
raps: Vec<&dyn AnyRap<SC>>,
proof: Proof<SC>,
public_values: &[Vec<Val<SC>>],
Expand Down
4 changes: 2 additions & 2 deletions stark-backend/tests/cached_lookup/instrumented.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::{self, File};

use afs_stark_backend::{keygen::types::MultiStarkVerifyingKey, prover::types::Proof};
use afs_stark_backend::{keygen::types::MultiStarkPartialVerifyingKey, prover::types::Proof};
use afs_test_utils::{
config::{
baby_bear_poseidon2::{self, engine_from_perm},
Expand All @@ -26,7 +26,7 @@ use super::prove::{get_data_sizes, prove, BenchParams};

fn instrumented_verify<SC: StarkGenericConfig, E: StarkEngineWithHashInstrumentation<SC>>(
engine: &mut E,
vk: MultiStarkVerifyingKey<SC>,
vk: MultiStarkPartialVerifyingKey<SC>,
air: DummyInteractionAir,
proof: Proof<SC>,
pis: Vec<Vec<Val<SC>>>,
Expand Down
10 changes: 5 additions & 5 deletions stark-backend/tests/cached_lookup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ pub fn prove_and_verify_indexless_lookups(
);
// Auto-adds sender matrix
keygen_builder.add_air(&sender_air, sender_degree, 0);
let pk = keygen_builder.generate_pk();
let vk = pk.vk();
let partial_pk = keygen_builder.generate_partial_pk();
let partial_vk = partial_pk.partial_vk();

let prover = MultiTraceStarkProver::new(&config);
// Must add trace matrices in the same order as above
Expand All @@ -95,19 +95,19 @@ pub fn prove_and_verify_indexless_lookups(
trace_builder.load_trace(sender_trace);
trace_builder.commit_current();

let main_trace_data = trace_builder.view(&vk, vec![&receiver_air, &sender_air]);
let main_trace_data = trace_builder.view(&partial_vk, vec![&receiver_air, &sender_air]);
let pis = vec![vec![]; 2];

let mut challenger = config::baby_bear_poseidon2::Challenger::new(perm.clone());
let proof = prover.prove(&mut challenger, &pk, main_trace_data, &pis);
let proof = prover.prove(&mut challenger, &partial_pk, main_trace_data, &pis);

// Verify the proof:
// Start from clean challenger
let mut challenger = config::baby_bear_poseidon2::Challenger::new(perm.clone());
let verifier = MultiTraceStarkVerifier::new(prover.config);
verifier.verify(
&mut challenger,
vk,
partial_vk,
vec![&receiver_air, &sender_air],
proof,
&pis,
Expand Down
Loading

0 comments on commit 6db59d9

Please sign in to comment.