Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a VerifierState #13

Merged
merged 11 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions plonky_block_proof_gen/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Hardcoded circuit constants to be used when generating the prover circuits.

/// Default range to be used for the `ArithmeticStark` table.
pub(crate) const DEFAULT_ARITHMETIC_RANGE: Range<usize> = 16..20;
/// Default range to be used for the `BytePackingStark` table.
pub(crate) const DEFAULT_BYTE_PACKING_RANGE: Range<usize> = 10..20;
/// Default range to be used for the `CpuStark` table.
pub(crate) const DEFAULT_CPU_RANGE: Range<usize> = 12..22;
/// Default range to be used for the `KeccakStark` table.
pub(crate) const DEFAULT_KECCAK_RANGE: Range<usize> = 14..17;
/// Default range to be used for the `KeccakSpongeStark` table.
pub(crate) const DEFAULT_KECCAK_SPONGE_RANGE: Range<usize> = 9..14;
/// Default range to be used for the `LogicStark` table.
pub(crate) const DEFAULT_LOGIC_RANGE: Range<usize> = 12..16;
/// Default range to be used for the `MemoryStark` table.
pub(crate) const DEFAULT_MEMORY_RANGE: Range<usize> = 17..25;
15 changes: 15 additions & 0 deletions plonky_block_proof_gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,23 @@
//! curr_block_agg_proof: &GeneratedAggProof,
//! ) -> ProofGenResult<GeneratedBlockProof> { ... }
//! ```
//!
//! ## Verifying block proofs
//!
//! The `ProverState` can be used to verify any block proofs emitted with the
//! same set of circuits.
//! However, because the prover state can be quite heavy, the necessary verifier
//! data to verify block proofs can be saved independently into a
//! `VerifierState`, to allow anyone to easily verify block proofs.

pub(crate) mod constants;
pub mod proof_gen;
pub mod proof_types;
pub mod prover_state;
pub mod types;
pub mod verifier_state;

// Re-exports

pub use prover_state::ProverState;
pub use verifier_state::VerifierState;
17 changes: 9 additions & 8 deletions plonky_block_proof_gen/src/prover_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use log::info;
use paste::paste;
use plonky2_evm::{all_stark::AllStark, config::StarkConfig};

use crate::constants::*;
use crate::types::AllRecursiveCircuits;

/// Plonky2 proving state. Note that this is generally going to be massive in
Expand All @@ -32,17 +33,17 @@ pub struct ProverStateBuilder {

impl Default for ProverStateBuilder {
fn default() -> Self {
// These ranges are somewhat arbitrary, but should be enough for testing
// The default ranges are somewhat arbitrary, but should be enough for testing
// purposes against most transactions.
// Some heavy contract deployments may require bumping these ranges though.
Self {
arithmetic_circuit_size: 16..20,
byte_packing_circuit_size: 10..20,
cpu_circuit_size: 12..22,
keccak_circuit_size: 14..17,
keccak_sponge_circuit_size: 9..14,
logic_circuit_size: 12..16,
memory_circuit_size: 17..25,
arithmetic_circuit_size: DEFAULT_ARITHMETIC_RANGE,
byte_packing_circuit_size: DEFAULT_BYTE_PACKING_RANGE,
cpu_circuit_size: DEFAULT_CPU_RANGE,
keccak_circuit_size: DEFAULT_KECCAK_RANGE,
keccak_sponge_circuit_size: DEFAULT_KECCAK_SPONGE_RANGE,
logic_circuit_size: DEFAULT_LOGIC_RANGE,
memory_circuit_size: DEFAULT_MEMORY_RANGE,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions plonky_block_proof_gen/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ pub type AllRecursiveCircuits = plonky2_evm::fixed_recursive_verifier::AllRecurs
PoseidonGoldilocksConfig,
2,
>;

/// A type alias for the verifier data necessary to verify succinct block
/// proofs.
/// While the prover state [`AllRecursiveCircuits`] can also verify proofs, this
/// [`VerifierData`] is much lighter, allowing anyone to verify block proofs,
/// regardless of the underlying hardware.
pub type VerifierData =
plonky2::plonk::circuit_data::VerifierCircuitData<GoldilocksField, PoseidonGoldilocksConfig, 2>;
110 changes: 110 additions & 0 deletions plonky_block_proof_gen/src/verifier_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! This module defines the `VerifierState`, that contains the necessary data to
//! handle succinct block proofs verification.

use std::ops::Range;

use log::info;
use paste::paste;
use plonky2_evm::{all_stark::AllStark, config::StarkConfig};

use crate::constants::*;
use crate::{
prover_state::ProverState,
types::{AllRecursiveCircuits, VerifierData},
};

/// Plonky2 verifier state.
///
/// The default generation requires generating all the verifier data before
/// extracting the verifier-related data, which can take a long time and require
/// a large amount of memory.
pub struct VerifierState {
///
pub state: VerifierData,
}
dvdplm marked this conversation as resolved.
Show resolved Hide resolved

/// Builder for the verifier state.
#[derive(Debug)]
pub struct VerifierStateBuilder {
arithmetic_circuit_size: Range<usize>,
byte_packing_circuit_size: Range<usize>,
cpu_circuit_size: Range<usize>,
keccak_circuit_size: Range<usize>,
keccak_sponge_circuit_size: Range<usize>,
logic_circuit_size: Range<usize>,
memory_circuit_size: Range<usize>,
}

impl Default for VerifierStateBuilder {
fn default() -> Self {
// The default ranges are somewhat arbitrary, but should be enough for testing
// purposes against most transactions.
// Some heavy contract deployments may require bumping these ranges though.
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
Self {
arithmetic_circuit_size: DEFAULT_ARITHMETIC_RANGE,
byte_packing_circuit_size: DEFAULT_BYTE_PACKING_RANGE,
cpu_circuit_size: DEFAULT_CPU_RANGE,
keccak_circuit_size: DEFAULT_KECCAK_RANGE,
keccak_sponge_circuit_size: DEFAULT_KECCAK_SPONGE_RANGE,
logic_circuit_size: DEFAULT_LOGIC_RANGE,
memory_circuit_size: DEFAULT_MEMORY_RANGE,
}
}
}

macro_rules! define_set_circuit_size_method {
dvdplm marked this conversation as resolved.
Show resolved Hide resolved
($name:ident) => {
paste! {
/// Specifies a range of degrees to be supported for this STARK
/// table's associated recursive circuits.
pub fn [<set_ $name _circuit_size>](mut self, size: Range<usize>) -> Self {
self.[<$name _circuit_size>] = size;
self
}
}
};
}

impl VerifierStateBuilder {
define_set_circuit_size_method!(arithmetic);
define_set_circuit_size_method!(byte_packing);
define_set_circuit_size_method!(cpu);
define_set_circuit_size_method!(keccak);
define_set_circuit_size_method!(keccak_sponge);
define_set_circuit_size_method!(logic);
define_set_circuit_size_method!(memory);

// TODO: Consider adding async version?
/// Instantiate the verifier state from the builder. Note that this is a
/// very expensive call!
pub fn build(self) -> VerifierState {
info!("Initializing Plonky2 aggregation verifier state (This may take a while)...");

let state = AllRecursiveCircuits::new(
&AllStark::default(),
&[
self.arithmetic_circuit_size,
self.byte_packing_circuit_size,
self.cpu_circuit_size,
self.keccak_circuit_size,
self.keccak_sponge_circuit_size,
self.logic_circuit_size,
self.memory_circuit_size,
],
&StarkConfig::standard_fast_config(),
);

info!("Finished initializing Plonky2 aggregation verifier state!");

VerifierState { state }
}
}

/// Extracts the verifier state from the entire prover state.
impl From<ProverState> for VerifierState {
fn from(prover_state: ProverState) -> Self {
VerifierState {
state: prover_state.state.final_verifier_data(),
}
}
}
Nashtare marked this conversation as resolved.
Show resolved Hide resolved