Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
262 changes: 130 additions & 132 deletions Cargo.lock

Large diffs are not rendered by default.

94 changes: 48 additions & 46 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,30 @@ debug-assertions = true

[workspace.dependencies]
rustc-hash = "2.1.0"
# Pinned to a rev, not a branch. These crates supply the field, the challenger and
# the Merkle tree -- the transcript -- so a branch pin lets `cargo update` change
# every challenge, hence every vk, with no diff to review. This rev is the one the
# production prover builds against.
p3-air = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-field = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-challenger = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-commit = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-dft = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-matrix = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-maybe-rayon = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-util = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-fri = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-koala-bear = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-circle = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-keccak = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-keccak-air = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-merkle-tree = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-mersenne-31 = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-monty-31 = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-poseidon2 = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-symmetric = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }
p3-bn254-fr = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2", package = "p3-bn254" }
p3-uni-stark = { git = "https://github.com/ProjectZKM/Plonky3", rev = "4dd0d47a8176503fbaa7293c9f0e3b20004e16b2" }

# p3-air = { path = "../patch/Plonky3/air" }
# p3-field = { path = "../patch/Plonky3/field" }
# p3-challenger = { path = "../patch/Plonky3/challenger" }
# p3-commit = { path = "../patch/Plonky3/commit" }
# p3-dft = { path = "../patch/Plonky3/dft" }
# p3-matrix = { path = "../patch/Plonky3/matrix" }
# p3-maybe-rayon = { path = "../patch/Plonky3/maybe-rayon" }
# p3-util = { path = "../patch/Plonky3/util" }
# p3-fri = { path = "../patch/Plonky3/fri" }
# p3-koala-bear = { path = "../patch/Plonky3/koala-bear" }
# p3-circle = { path = "../patch/Plonky3/circle" }
# p3-keccak = { path = "../patch/Plonky3/keccak" }
# p3-keccak-air = { path = "../patch/Plonky3/keccak-air" }
# p3-mds = { path = "../patch/Plonky3/mds" }
# p3-merkle-tree = { path = "../patch/Plonky3/merkle-tree" }
# p3-mersenne-31 = { path = "../patch/Plonky3/mersenne-31" }
# p3-monty-31 = { path = "../patch/Plonky3/monty-31" }
# p3-poseidon2 = { path = "../patch/Plonky3/poseidon2" }
# p3-symmetric = { path = "../patch/Plonky3/symmetric" }
# p3-bn254-fr = { path = "../patch/Plonky3/bn254-fr" }
# p3-uni-stark = { path = "../patch/Plonky3/uni-stark" }
# These crates define the fields, challengers and Merkle trees used by the transcript.
# For Git dependencies, Cargo.lock records the revisions selected during resolution.
# Enabling the local path overrides below uses checkout contents instead. Dependency
# changes can change the transcript and verifying keys.
p3-air = { git = "https://github.com/ProjectZKM/Plonky3" }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add the latest rev for Plonky3 deps?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need

p3-field = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-challenger = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-commit = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-dft = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-matrix = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-maybe-rayon = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-util = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-fri = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-koala-bear = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-circle = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-keccak = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-keccak-air = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-merkle-tree = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-mersenne-31 = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-monty-31 = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-poseidon2 = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-symmetric = { git = "https://github.com/ProjectZKM/Plonky3" }
p3-bn254-fr = { git = "https://github.com/ProjectZKM/Plonky3", package = "p3-bn254" }
p3-uni-stark = { git = "https://github.com/ProjectZKM/Plonky3" }

itertools = "0.13.0"
tracing = "0.1.37"
Expand Down Expand Up @@ -137,3 +115,27 @@ hashbrown = "0.14.5"
zkm-lib = { path = "crates/zkvm/lib" }
zkm-primitives = { path = "crates/primitives" }

# Optional local overrides for the sibling Plonky3 checkout.
# [patch."https://github.com/ProjectZKM/Plonky3"]
# p3-air = { path = "../Plonky3/air" }
# p3-bn254 = { path = "../Plonky3/bn254" }
# p3-challenger = { path = "../Plonky3/challenger" }
# p3-circle = { path = "../Plonky3/circle" }
# p3-commit = { path = "../Plonky3/commit" }
# p3-dft = { path = "../Plonky3/dft" }
# p3-field = { path = "../Plonky3/field" }
# p3-fri = { path = "../Plonky3/fri" }
# p3-keccak = { path = "../Plonky3/keccak" }
# p3-keccak-air = { path = "../Plonky3/keccak-air" }
# p3-koala-bear = { path = "../Plonky3/koala-bear" }
# p3-matrix = { path = "../Plonky3/matrix" }
# p3-maybe-rayon = { path = "../Plonky3/maybe-rayon" }
# p3-mds = { path = "../Plonky3/mds" }
# p3-merkle-tree = { path = "../Plonky3/merkle-tree" }
# p3-mersenne-31 = { path = "../Plonky3/mersenne-31" }
# p3-monty-31 = { path = "../Plonky3/monty-31" }
# p3-multilinear-util = { path = "../Plonky3/multilinear-util" }
# p3-poseidon1 = { path = "../Plonky3/poseidon1" }
# p3-poseidon2 = { path = "../Plonky3/poseidon2" }
# p3-symmetric = { path = "../Plonky3/symmetric" }
# p3-uni-stark = { path = "../Plonky3/uni-stark" }
1 change: 1 addition & 0 deletions crates/core/machine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ num = { version = "0.4.3" }
p3-air = { workspace = true }
p3-koala-bear = { workspace = true }
p3-challenger = { workspace = true }
p3-commit = { workspace = true }
p3-field = { workspace = true }
p3-keccak-air = { workspace = true }
p3-matrix = { workspace = true }
Expand Down
19 changes: 15 additions & 4 deletions crates/core/machine/examples/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use zkm_core_executor::{Executor, Program};
use zkm_core_machine::{
io::ZKMStdin,
mips::MipsAir,
utils::{run_test, run_test_io, setup_cli_logger},
utils::{run_test_io, setup_cli_logger},
};
use zkm_pcs::air::MachineAir;
use zkm_pcs::MachineRecord;
Expand Down Expand Up @@ -72,6 +72,15 @@ const REPRO_ENV: &[&str] = &[
"ZKM_SKIP_PROGRAM_BUILD",
];

/// Input the built-in fixtures read from stdin; empty for every other program.
fn fixture_stdin(name: &str) -> ZKMStdin {
let mut stdin = ZKMStdin::new();
if name == "fibonacci" {
stdin.write(&10u32);
}
stdin
}

/// Accepts a built-in fixture name or a path to any guest ELF, so the same
/// harness works for a one-line repro and for a real workload.
fn resolve(name: &str) -> Program {
Expand Down Expand Up @@ -114,6 +123,7 @@ fn main() {
match cmd.as_str() {
"execute" => {
let mut rt = Executor::new(program(), ZKMCoreOpts::default());
rt.write_vecs(&fixture_stdin(&name).buffer);
rt.run().expect("execution failed");
tracing::info!("shards = {}", rt.records.len());
tracing::info!("global_clk = {}", rt.state.global_clk);
Expand Down Expand Up @@ -153,7 +163,8 @@ fn main() {
}
}
"prove" => {
run_test::<CpuProver<_, _>>(program()).expect("prove + verify failed");
run_test_io::<CpuProver<_, _>>(program(), fixture_stdin(&name))
.expect("prove + verify failed");
tracing::info!("prove + verify OK");
}
"widths" => {
Expand All @@ -166,7 +177,7 @@ fn main() {
(
MachineAir::<KoalaBear>::name(c),
p3_air::BaseAir::<KoalaBear>::width(c).max(1),
MachineAir::<KoalaBear>::preprocessed_width(c),
p3_air::BaseAir::<KoalaBear>::preprocessed_width(c),
)
})
.collect();
Expand Down Expand Up @@ -216,7 +227,7 @@ fn main() {
static_rows.push((
name,
p3_air::BaseAir::<KoalaBear>::width(c).max(1),
MachineAir::<KoalaBear>::preprocessed_width(c),
p3_air::BaseAir::<KoalaBear>::preprocessed_width(c),
sends,
recvs,
padded,
Expand Down
6 changes: 5 additions & 1 deletion crates/core/machine/src/bytes/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ use zkm_core_executor::ByteOpcode;
use zkm_pcs::air::ZKMAirBuilder;

use super::{
columns::{ByteMultCols, BytePreprocessedCols, NUM_BYTE_MULT_COLS},
columns::{ByteMultCols, BytePreprocessedCols, NUM_BYTE_MULT_COLS, NUM_BYTE_PREPROCESSED_COLS},
ByteChip,
};

impl<F: Field> BaseAir<F> for ByteChip<F> {
fn width(&self) -> usize {
NUM_BYTE_MULT_COLS
}

fn preprocessed_width(&self) -> usize {
NUM_BYTE_PREPROCESSED_COLS
}
}

// The deferred-shard area cut charges every shard this table's main columns.
Expand Down
6 changes: 1 addition & 5 deletions crates/core/machine/src/bytes/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use zkm_pcs::air::MachineAir;
use crate::{utils::zeroed_f_vec, CoreChipError};

use super::{
columns::{ByteMultCols, NUM_BYTE_MULT_COLS, NUM_BYTE_PREPROCESSED_COLS},
columns::{ByteMultCols, NUM_BYTE_MULT_COLS},
ByteChip,
};

Expand All @@ -25,10 +25,6 @@ impl<F: PrimeField32> MachineAir<F> for ByteChip<F> {
"Byte".to_string()
}

fn preprocessed_width(&self) -> usize {
NUM_BYTE_PREPROCESSED_COLS
}

fn generate_preprocessed_trace(&self, _program: &Self::Program) -> Option<RowMajorMatrix<F>> {
let trace = Self::trace();
Some(trace)
Expand Down
18 changes: 17 additions & 1 deletion crates/core/machine/src/mips/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ pub mod tests {
.chips()
.iter()
.find(|c| <_ as MachineAir<KoalaBear>>::name(*c) == name)
.map(<_ as MachineAir<KoalaBear>>::preprocessed_width)
.map(<_ as p3_air::BaseAir<KoalaBear>>::preprocessed_width)
.unwrap_or_else(|| panic!("{name} is not a chip of the core machine"))
};

Expand Down Expand Up @@ -1634,3 +1634,19 @@ pub mod tests {
run_test_with_stdin::<CpuProver<_, _>>(program, fib_stdin()).unwrap();
}
}

#[cfg(test)]
mod preprocessed_width_tests {
use super::*;
use p3_koala_bear::KoalaBear;
use zkm_core_executor::Program;
use zkm_pcs::air::assert_preprocessed_widths_match;

#[test]
fn preprocessed_widths_match_generated_traces() {
assert_preprocessed_widths_match::<KoalaBear, _>(
MipsAir::<KoalaBear>::chips(),
&Program::default(),
);
}
}
8 changes: 4 additions & 4 deletions crates/core/machine/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ impl<F: PrimeField32> MachineAir<F> for ProgramChip {
"Program".to_string()
}

fn preprocessed_width(&self) -> usize {
NUM_PROGRAM_PREPROCESSED_COLS
}

fn generate_preprocessed_trace(&self, program: &Self::Program) -> Option<RowMajorMatrix<F>> {
debug_assert!(!program.instructions.is_empty(), "empty program");
let nb_rows = program.instructions.len();
Expand Down Expand Up @@ -150,6 +146,10 @@ impl<F> BaseAir<F> for ProgramChip {
fn width(&self) -> usize {
NUM_PROGRAM_MULT_COLS
}

fn preprocessed_width(&self) -> usize {
NUM_PROGRAM_PREPROCESSED_COLS
}
}

// The deferred-shard area cut charges every shard this table's main columns.
Expand Down
8 changes: 4 additions & 4 deletions crates/core/machine/src/range/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ impl<F: PrimeField32> MachineAir<F> for RangeChip<F> {
"Range".to_string()
}

fn preprocessed_width(&self) -> usize {
NUM_RANGE_PREPROCESSED_COLS
}

fn generate_preprocessed_trace(&self, _program: &Self::Program) -> Option<RowMajorMatrix<F>> {
Some(Self::preprocessed_trace())
}
Expand Down Expand Up @@ -115,6 +111,10 @@ impl<F: Field> BaseAir<F> for RangeChip<F> {
fn width(&self) -> usize {
NUM_RANGE_MULT_COLS
}

fn preprocessed_width(&self) -> usize {
NUM_RANGE_PREPROCESSED_COLS
}
}

impl<AB: ZKMAirBuilder<F: Field>> Air<AB> for RangeChip<AB::F> {
Expand Down
8 changes: 5 additions & 3 deletions crates/core/machine/src/utils/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ pub fn uni_stark_prove<SC, A>(
trace: RowMajorMatrix<SC::Val>,
) -> Proof<UniConfig<SC>>
where
SC: StarkGenericConfig,
SC: StarkGenericConfig<Val = p3_koala_bear::KoalaBear>,
SC::Pcs: p3_commit::UnivariateStarkPcs<SC::Challenge, SC::Challenger>,
A: Air<p3_uni_stark::SymbolicAirBuilder<SC::Val>>
+ for<'a> Air<p3_uni_stark::ProverConstraintFolder<'a, UniConfig<SC>>>
+ for<'a> Air<p3_air::DebugConstraintBuilder<'a, SC::Val>>,
{
p3_uni_stark::prove(&UniConfig(config.clone()), air, trace, &[])
p3_uni_stark::prove(&UniConfig(config.clone()), air, trace, &[]).unwrap()
}

/// `p3_uni_stark::verify` over a single hand-built AIR fixture.
Expand All @@ -145,7 +146,8 @@ pub fn uni_stark_verify<SC, A>(
proof: &Proof<UniConfig<SC>>,
) -> Result<(), p3_uni_stark::VerificationError<p3_uni_stark::PcsError<UniConfig<SC>>>>
where
SC: StarkGenericConfig,
SC: StarkGenericConfig<Val = p3_koala_bear::KoalaBear>,
SC::Pcs: p3_commit::UnivariateStarkPcs<SC::Challenge, SC::Challenger>,
A: Air<p3_uni_stark::SymbolicAirBuilder<SC::Val>>
+ for<'a> Air<p3_uni_stark::VerifierConstraintFolder<'a, UniConfig<SC>>>
+ for<'a> Air<p3_air::DebugConstraintBuilder<'a, SC::Val>>,
Expand Down
26 changes: 13 additions & 13 deletions crates/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream {
}
});

let preprocessed_width_arms = variants.iter().map(|(variant_name, field)| {
let field_ty = &field.ty;
quote! {
#name::#variant_name(x) => <#field_ty as p3_air::BaseAir<F>>::preprocessed_width(x)
}
});

let base_air = quote! {
impl #impl_generics p3_air::BaseAir<F> for #name #ty_generics #where_clause {
fn width(&self) -> usize {
Expand All @@ -139,6 +146,12 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream {
}
}

fn preprocessed_width(&self) -> usize {
match self {
#(#preprocessed_width_arms,)*
}
}

fn preprocessed_trace(&self) -> Option<p3_matrix::dense::RowMajorMatrix<F>> {
unreachable!("A machine air should use the preprocessed trace from the `MachineAir` trait")
}
Expand All @@ -152,13 +165,6 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream {
}
});

let preprocessed_width_arms = variants.iter().map(|(variant_name, field)| {
let field_ty = &field.ty;
quote! {
#name::#variant_name(x) => <#field_ty as zkm_pcs::air::MachineAir<F>>::preprocessed_width(x)
}
});

let generate_preprocessed_trace_arms = variants.iter().map(|(variant_name, field)| {
let field_ty = &field.ty;
quote! {
Expand Down Expand Up @@ -229,12 +235,6 @@ pub fn machine_air_derive(input: TokenStream) -> TokenStream {
}
}

fn preprocessed_width(&self) -> usize {
match self {
#(#preprocessed_width_arms,)*
}
}

fn generate_preprocessed_trace(
&self,
program: &#program_path,
Expand Down
4 changes: 2 additions & 2 deletions crates/fv/picus/src/picus_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl AirBuilder for PicusBuilder {
type PreprocessedWindow = RowMajorMatrix<Self::Var>;
type MainWindow = RowMajorMatrix<Self::Var>;
type PublicVar = SymbolicVariable<Felt>;
type PeriodicVar = SymbolicVariable<Felt>;

fn main(&self) -> Self::MainWindow {
self.main.clone()
Expand All @@ -156,8 +157,7 @@ impl AirBuilder for PicusBuilder {
SymbolicExpression::Leaf(BaseLeaf::IsLastRow)
}

fn is_transition_window(&self, size: usize) -> Self::Expr {
assert_eq!(size, 2, "PicusBuilder only supports a transition window of size 2");
fn is_transition(&self) -> Self::Expr {
SymbolicExpression::Leaf(BaseLeaf::IsTransition)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/pcs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ strum = "0.26.3"
strum_macros = "0.26.4"
hashbrown = { version = "0.14.5", features = ["serde", "inline-more"] }
arrayref = "0.3.8"
num-bigint = { version = "0.4.3", default-features = false }
num-bigint = { version = "0.5.1", default-features = false }
rayon-scan = "0.1.1"
rayon = "1.12"

Expand Down
Loading
Loading