Skip to content

Commit

Permalink
fix: multi field rate and serialization (#1575)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirhemo authored Sep 30, 2024
1 parent a183772 commit 7fc5975
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 348 deletions.
484 changes: 178 additions & 306 deletions Cargo.lock

Large diffs are not rendered by default.

61 changes: 41 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,50 @@ sp1-lib = { path = "crates/zkvm/lib", version = "2.0.0", default-features = fals
sp1-zkvm = { path = "crates/zkvm/entrypoint", version = "2.0.0", default-features = false }

# p3
p3-air = "0.1.3-succinct"
p3-field = "0.1.3-succinct"
p3-commit = "0.1.3-succinct"
p3-matrix = "0.1.3-succinct"
p3-baby-bear = { version = "0.1.3-succinct", features = ["nightly-features"] }
p3-util = "0.1.3-succinct"
p3-challenger = "0.1.3-succinct"
p3-dft = "0.1.3-succinct"
p3-fri = "0.1.3-succinct"
p3-goldilocks = "0.1.3-succinct"
p3-keccak = "0.1.3-succinct"
p3-keccak-air = "0.1.3-succinct"
p3-blake3 = "0.1.3-succinct"
p3-mds = "0.1.3-succinct"
p3-merkle-tree = "0.1.3-succinct"
p3-poseidon2 = "0.1.3-succinct"
p3-symmetric = "0.1.3-succinct"
p3-uni-stark = "0.1.3-succinct"
p3-maybe-rayon = "0.1.3-succinct"
p3-bn254-fr = "0.1.3-succinct"
# p3-air = "0.1.3-succinct"
# p3-field = "0.1.3-succinct"
# p3-commit = "0.1.3-succinct"
# p3-matrix = "0.1.3-succinct"
# p3-baby-bear = { version = "0.1.3-succinct", features = ["nightly-features"] }
# p3-util = "0.1.3-succinct"
# p3-challenger = "0.1.3-succinct"
# p3-dft = "0.1.3-succinct"
# p3-fri = "0.1.3-succinct"
# p3-goldilocks = "0.1.3-succinct"
# p3-keccak = "0.1.3-succinct"
# p3-keccak-air = "0.1.3-succinct"
# p3-blake3 = "0.1.3-succinct"
# p3-mds = "0.1.3-succinct"
# p3-merkle-tree = "0.1.3-succinct"
# p3-poseidon2 = "0.1.3-succinct"
# p3-symmetric = "0.1.3-succinct"
# p3-uni-stark = "0.1.3-succinct"
# p3-maybe-rayon = "0.1.3-succinct"
# p3-bn254-fr = "0.1.3-succinct"

# For local development.

p3-air = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-field = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-commit = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-matrix = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-baby-bear = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-util = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-challenger = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-dft = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-fri = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-goldilocks = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-keccak = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-keccak-air = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-blake3 = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-mds = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-merkle-tree = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-poseidon2 = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-symmetric = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-uni-stark = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-maybe-rayon = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }
p3-bn254-fr = { git = "https://github.com/Plonky3/Plonky3", branch = "sp1-v3" }

# p3-air = { path = "../Plonky3/air" }
# p3-field = { path = "../Plonky3/field" }
# p3-commit = { path = "../Plonky3/commit" }
Expand Down
2 changes: 1 addition & 1 deletion crates/recursion/circuit-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sp1-primitives = { workspace = true }
sp1-recursion-gnark-ffi = { workspace = true }

itertools = "0.13.0"
serde = { version = "1.0.204", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
bincode = "1.3.3"
rand = "0.8.5"
tracing = "0.1.40"
Expand Down
19 changes: 11 additions & 8 deletions crates/recursion/circuit-v2/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use sp1_recursion_compiler::{
use sp1_recursion_core_v2::{
air::ChallengerPublicValues,
runtime::{HASH_RATE, PERMUTATION_WIDTH},
stark::config::{OUTER_MULTI_FIELD_CHALLENGER_DIGEST_SIZE, OUTER_MULTI_FIELD_CHALLENGER_RATE},
NUM_BITS,
};

// Constants for the Multifield challenger.
pub const SPONGE_SIZE: usize = 3;
pub const DIGEST_SIZE: usize = 1;
pub const RATE: usize = 16;
pub const POSEIDON_2_BB_RATE: usize = 16;

// use crate::{DigestVariable, VerifyingKeyVariable};

Expand Down Expand Up @@ -256,7 +255,7 @@ impl<C: Config> MultiField32ChallengerVariable<C> {
}

pub fn duplexing(&mut self, builder: &mut Builder<C>) {
assert!(self.input_buffer.len() <= self.num_f_elms * SPONGE_SIZE);
assert!(self.input_buffer.len() <= self.num_f_elms * OUTER_MULTI_FIELD_CHALLENGER_RATE);

for (i, f_chunk) in self.input_buffer.chunks(self.num_f_elms).enumerate() {
self.sponge_state[i] = reduce_32(builder, f_chunk);
Expand All @@ -279,15 +278,15 @@ impl<C: Config> MultiField32ChallengerVariable<C> {
self.output_buffer.clear();

self.input_buffer.push(value);
if self.input_buffer.len() == self.num_f_elms * SPONGE_SIZE {
if self.input_buffer.len() == self.num_f_elms * OUTER_MULTI_FIELD_CHALLENGER_RATE {
self.duplexing(builder);
}
}

pub fn observe_commitment(
&mut self,
builder: &mut Builder<C>,
value: [Var<C::N>; DIGEST_SIZE],
value: [Var<C::N>; OUTER_MULTI_FIELD_CHALLENGER_DIGEST_SIZE],
) {
for val in value {
let f_vals: Vec<Felt<C::F>> = split_32(builder, val, self.num_f_elms);
Expand Down Expand Up @@ -353,10 +352,14 @@ impl<C: Config> CanObserveVariable<C, Felt<C::F>> for MultiField32ChallengerVari
}
}

impl<C: Config> CanObserveVariable<C, [Var<C::N>; DIGEST_SIZE]>
impl<C: Config> CanObserveVariable<C, [Var<C::N>; OUTER_MULTI_FIELD_CHALLENGER_DIGEST_SIZE]>
for MultiField32ChallengerVariable<C>
{
fn observe(&mut self, builder: &mut Builder<C>, value: [Var<C::N>; DIGEST_SIZE]) {
fn observe(
&mut self,
builder: &mut Builder<C>,
value: [Var<C::N>; OUTER_MULTI_FIELD_CHALLENGER_DIGEST_SIZE],
) {
self.observe_commitment(builder, value)
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/recursion/circuit-v2/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ use sp1_recursion_compiler::{
circuit::CircuitV2Builder,
ir::{Builder, Config, DslIr, Felt, Var},
};
use sp1_recursion_core_v2::stark::config::outer_perm;
use sp1_recursion_core_v2::stark::config::{outer_perm, OUTER_MULTI_FIELD_CHALLENGER_WIDTH};
use sp1_recursion_core_v2::{stark::config::BabyBearPoseidon2Outer, DIGEST_SIZE};
use sp1_recursion_core_v2::{HASH_RATE, PERMUTATION_WIDTH};
use sp1_stark::baby_bear_poseidon2::BabyBearPoseidon2;
use sp1_stark::inner_perm;

use crate::challenger::SPONGE_SIZE;
use crate::{
challenger::{reduce_32, RATE},
challenger::{reduce_32, POSEIDON_2_BB_RATE},
select_chain, CircuitConfig,
};

Expand Down Expand Up @@ -168,9 +167,9 @@ impl<C: CircuitConfig<F = BabyBear, N = Bn254Fr, Bit = Var<Bn254Fr>>> FieldHashe
assert!(C::N::bits() == p3_bn254_fr::Bn254Fr::bits());
assert!(C::F::bits() == p3_baby_bear::BabyBear::bits());
let num_f_elms = C::N::bits() / C::F::bits();
let mut state: [Var<C::N>; SPONGE_SIZE] =
let mut state: [Var<C::N>; OUTER_MULTI_FIELD_CHALLENGER_WIDTH] =
[builder.eval(C::N::zero()), builder.eval(C::N::zero()), builder.eval(C::N::zero())];
for block_chunk in &input.iter().chunks(RATE) {
for block_chunk in &input.iter().chunks(POSEIDON_2_BB_RATE) {
for (chunk_id, chunk) in (&block_chunk.chunks(num_f_elms)).into_iter().enumerate() {
let chunk = chunk.copied().collect::<Vec<_>>();
state[chunk_id] = reduce_32(builder, chunk.as_slice());
Expand All @@ -185,7 +184,7 @@ impl<C: CircuitConfig<F = BabyBear, N = Bn254Fr, Bit = Var<Bn254Fr>>> FieldHashe
builder: &mut Builder<C>,
input: [Self::DigestVariable; 2],
) -> Self::DigestVariable {
let state: [Var<C::N>; SPONGE_SIZE] =
let state: [Var<C::N>; OUTER_MULTI_FIELD_CHALLENGER_WIDTH] =
[builder.eval(input[0][0]), builder.eval(input[1][0]), builder.eval(C::N::zero())];
builder.push_op(DslIr::CircuitPoseidon2Permute(state));
[state[0]; BN254_DIGEST_SIZE]
Expand Down
7 changes: 6 additions & 1 deletion crates/recursion/circuit-v2/src/machine/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use p3_commit::Mmcs;
use p3_field::AbstractField;
use p3_matrix::dense::RowMajorMatrix;

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sp1_recursion_compiler::ir::{Builder, Ext, Felt, SymbolicFelt};

use sp1_recursion_core_v2::{
Expand All @@ -24,7 +25,8 @@ use sp1_recursion_core_v2::{
use sp1_stark::{
air::{MachineAir, POSEIDON_NUM_WORDS, PV_DIGEST_NUM_WORDS},
baby_bear_poseidon2::BabyBearPoseidon2,
ProofShape, ShardProof, StarkGenericConfig, StarkMachine, StarkVerifyingKey, Word, DIGEST_SIZE,
Dom, ProofShape, ShardProof, StarkGenericConfig, StarkMachine, StarkVerifyingKey, Word,
DIGEST_SIZE,
};

use crate::{
Expand Down Expand Up @@ -61,6 +63,9 @@ pub struct SP1CompressWitnessVariable<
}

/// An input layout for the reduce verifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound(serialize = "ShardProof<SC>: Serialize, Dom<SC>: Serialize"))]
#[serde(bound(deserialize = "ShardProof<SC>: Deserialize<'de>, Dom<SC>: DeserializeOwned"))]
pub struct SP1CompressWitnessValues<SC: StarkGenericConfig> {
pub vks_and_proofs: Vec<(StarkVerifyingKey<SC>, ShardProof<SC>)>,
pub is_complete: bool,
Expand Down
7 changes: 5 additions & 2 deletions crates/recursion/circuit-v2/src/machine/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use p3_commit::Mmcs;
use p3_field::AbstractField;
use p3_matrix::dense::RowMajorMatrix;

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sp1_core_machine::{
cpu::MAX_CPU_LOG_DEGREE,
riscv::{RiscvAir, MAX_LOG_NUMBER_OF_SHARDS},
Expand All @@ -20,7 +21,7 @@ use sp1_recursion_core_v2::air::PV_DIGEST_NUM_WORDS;
use sp1_stark::{
air::{PublicValues, POSEIDON_NUM_WORDS},
baby_bear_poseidon2::BabyBearPoseidon2,
ProofShape, StarkMachine, Word,
Dom, ProofShape, StarkMachine, Word,
};

use sp1_stark::{ShardProof, StarkGenericConfig, StarkVerifyingKey};
Expand Down Expand Up @@ -54,7 +55,9 @@ pub struct SP1RecursionWitnessVariable<
pub is_first_shard: Felt<C::F>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound(serialize = "ShardProof<SC>: Serialize, Dom<SC>: Serialize"))]
#[serde(bound(deserialize = "ShardProof<SC>: Deserialize<'de>, Dom<SC>: DeserializeOwned"))]
pub struct SP1RecursionWitnessValues<SC: StarkGenericConfig> {
pub vk: StarkVerifyingKey<SC>,
pub shard_proofs: Vec<ShardProof<SC>>,
Expand Down
11 changes: 10 additions & 1 deletion crates/recursion/circuit-v2/src/machine/deferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::{
borrow::{Borrow, BorrowMut},
};

use serde::{de::DeserializeOwned, Deserialize, Serialize};

use p3_air::Air;
use p3_baby_bear::BabyBear;
use p3_commit::Mmcs;
Expand All @@ -15,7 +17,7 @@ use sp1_recursion_compiler::ir::{Builder, Ext, Felt};
use sp1_stark::{
air::{MachineAir, POSEIDON_NUM_WORDS},
baby_bear_poseidon2::BabyBearPoseidon2,
ShardProof, StarkMachine, StarkVerifyingKey, Word,
Dom, ShardProof, StarkMachine, StarkVerifyingKey, Word,
};

use sp1_recursion_core_v2::{
Expand Down Expand Up @@ -46,6 +48,13 @@ pub struct SP1DeferredShape {
height: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound(
serialize = "SC::Challenger: Serialize, ShardProof<SC>: Serialize, Dom<SC>: Serialize, [SC::Val; DIGEST_SIZE]: Serialize, SC::Digest: Serialize"
))]
#[serde(bound(
deserialize = "SC::Challenger: Deserialize<'de>, ShardProof<SC>: Deserialize<'de>, Dom<SC>: DeserializeOwned, [SC::Val; DIGEST_SIZE]: Deserialize<'de>, SC::Digest: Deserialize<'de>"
))]
pub struct SP1DeferredWitnessValues<SC: BabyBearFriConfig + FieldHasher<BabyBear>> {
pub vks_and_proofs: Vec<(StarkVerifyingKey<SC>, ShardProof<SC>)>,
pub vk_merkle_data: SP1MerkleProofWitnessValues<SC>,
Expand Down
4 changes: 4 additions & 0 deletions crates/recursion/circuit-v2/src/machine/vkey_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use p3_baby_bear::BabyBear;
use p3_commit::Mmcs;
use p3_field::AbstractField;
use p3_matrix::dense::RowMajorMatrix;
use serde::{Deserialize, Serialize};
use sp1_recursion_compiler::ir::{Builder, Felt};
use sp1_recursion_core_v2::DIGEST_SIZE;
use sp1_stark::{
Expand Down Expand Up @@ -54,6 +55,9 @@ pub struct SP1MerkleProofWitnessVariable<
}

/// An input layout for the reduce verifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound(serialize = "SC::Digest: Serialize"))]
#[serde(bound(deserialize = "SC::Digest: Deserialize<'de>"))]
pub struct SP1MerkleProofWitnessValues<SC: FieldHasher<BabyBear>> {
pub vk_merkle_proofs: Vec<MerkleProof<BabyBear, SC>>,
pub values: Vec<SC::Digest>,
Expand Down
4 changes: 3 additions & 1 deletion crates/recursion/circuit-v2/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pub struct MerkleTree<F: Field, HV: FieldHasher<F>> {
}
pub struct VcsError;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound(serialize = "HV::Digest: Serialize"))]
#[serde(bound(deserialize = "HV::Digest: Deserialize<'de>"))]
pub struct MerkleProof<F: Field, HV: FieldHasher<F>> {
pub index: usize,
pub path: Vec<HV::Digest>,
Expand Down
12 changes: 11 additions & 1 deletion crates/recursion/core/src/stark/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ use super::{poseidon2::bn254_poseidon2_rc3, utils};

pub const DIGEST_SIZE: usize = 1;

pub const OUTER_MULTI_FIELD_CHALLENGER_WIDTH: usize = 3;
pub const OUTER_MULTI_FIELD_CHALLENGER_RATE: usize = 2;
pub const OUTER_MULTI_FIELD_CHALLENGER_DIGEST_SIZE: usize = 1;

/// A configuration for outer recursion.
pub type OuterVal = BabyBear;
pub type OuterChallenge = BinomialExtensionField<OuterVal, 4>;
Expand All @@ -30,7 +34,13 @@ pub type OuterCompress = TruncatedPermutation<OuterPerm, 2, 1, 3>;
pub type OuterValMmcs = FieldMerkleTreeMmcs<BabyBear, Bn254Fr, OuterHash, OuterCompress, 1>;
pub type OuterChallengeMmcs = ExtensionMmcs<OuterVal, OuterChallenge, OuterValMmcs>;
pub type OuterDft = Radix2DitParallel;
pub type OuterChallenger = MultiField32Challenger<OuterVal, Bn254Fr, OuterPerm, 3>;
pub type OuterChallenger = MultiField32Challenger<
OuterVal,
Bn254Fr,
OuterPerm,
OUTER_MULTI_FIELD_CHALLENGER_WIDTH,
OUTER_MULTI_FIELD_CHALLENGER_RATE,
>;
pub type OuterPcs = TwoAdicFriPcs<OuterVal, OuterDft, OuterValMmcs, OuterChallengeMmcs>;

pub type OuterQueryProof = QueryProof<OuterChallenge, OuterChallengeMmcs>;
Expand Down
4 changes: 3 additions & 1 deletion crates/stark/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ pub trait StarkGenericConfig: 'static + Send + Sync + Serialize + DeserializeOwn
/// The challenger (Fiat-Shamir) implementation used.
type Challenger: FieldChallenger<Val<Self>>
+ CanObserve<<Self::Pcs as Pcs<Self::Challenge, Self::Challenger>>::Commitment>
+ CanSample<Self::Challenge>;
+ CanSample<Self::Challenge>
+ Serialize
+ DeserializeOwned;

/// Get the PCS used by this configuration.
fn pcs(&self) -> &Self::Pcs;
Expand Down

0 comments on commit 7fc5975

Please sign in to comment.