openvm_native_recursion/config/
outer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use openvm_native_compiler::ir::Config;
use openvm_stark_backend::{
    keygen::types::{MultiStarkVerifyingKey, StarkVerifyingKey},
    p3_challenger::MultiField32Challenger,
    p3_commit::ExtensionMmcs,
    p3_field::extension::BinomialExtensionField,
};
use openvm_stark_sdk::{
    config::baby_bear_poseidon2_root::BabyBearPoseidon2RootConfig,
    p3_baby_bear::BabyBear,
    p3_bn254_fr::{Bn254Fr, Poseidon2Bn254},
};
use p3_dft::Radix2DitParallel;
use p3_fri::{BatchOpening, CommitPhaseProofStep, FriProof, QueryProof, TwoAdicFriPcs};
use p3_merkle_tree::MerkleTreeMmcs;
use p3_symmetric::{MultiField32PaddingFreeSponge, TruncatedPermutation};
use serde::{Deserialize, Serialize};

use crate::{
    digest::DigestVal,
    types::{
        MultiStarkVerificationAdvice, StarkVerificationAdvice,
        VerifierSinglePreprocessedDataInProgram,
    },
};

const WIDTH: usize = 3;
const RATE: usize = 16;
const DIGEST_WIDTH: usize = 1;

#[derive(Clone, Default, Debug, Serialize, Deserialize)]
pub struct OuterConfig;

impl Config for OuterConfig {
    type N = Bn254Fr;
    type F = BabyBear;
    type EF = BinomialExtensionField<BabyBear, 4>;
}

/// A configuration for outer recursion.
pub type OuterVal = BabyBear;
pub type OuterChallenge = BinomialExtensionField<OuterVal, 4>;
pub type OuterPerm = Poseidon2Bn254<WIDTH>;
pub type OuterHash =
    MultiField32PaddingFreeSponge<OuterVal, Bn254Fr, OuterPerm, WIDTH, RATE, DIGEST_WIDTH>;
pub type OuterDigest = [Bn254Fr; 1];
pub type OuterCompress = TruncatedPermutation<OuterPerm, 2, 1, WIDTH>;
pub type OuterValMmcs = MerkleTreeMmcs<BabyBear, Bn254Fr, OuterHash, OuterCompress, 1>;
pub type OuterChallengeMmcs = ExtensionMmcs<OuterVal, OuterChallenge, OuterValMmcs>;
pub type OuterDft = Radix2DitParallel<OuterVal>;
pub type OuterChallenger = MultiField32Challenger<OuterVal, Bn254Fr, OuterPerm, WIDTH, 2>;
pub type OuterPcs = TwoAdicFriPcs<OuterVal, OuterDft, OuterValMmcs, OuterChallengeMmcs>;
pub type OuterInputProof = Vec<OuterBatchOpening>;
pub type OuterQueryProof = QueryProof<OuterChallenge, OuterChallengeMmcs, OuterInputProof>;
pub type OuterCommitPhaseStep = CommitPhaseProofStep<OuterChallenge, OuterChallengeMmcs>;
pub type OuterFriProof = FriProof<OuterChallenge, OuterChallengeMmcs, OuterVal, OuterInputProof>;
pub type OuterBatchOpening = BatchOpening<OuterVal, OuterValMmcs>;

pub(crate) fn new_from_outer_vkv2(
    vk: StarkVerifyingKey<BabyBearPoseidon2RootConfig>,
) -> StarkVerificationAdvice<OuterConfig> {
    let StarkVerifyingKey {
        preprocessed_data,
        params,
        quotient_degree,
        symbolic_constraints,
        rap_phase_seq_kind: _,
    } = vk;
    StarkVerificationAdvice {
        preprocessed_data: preprocessed_data.map(|data| {
            let commit: [Bn254Fr; DIGEST_WIDTH] = data.commit.into();
            VerifierSinglePreprocessedDataInProgram {
                commit: DigestVal::N(commit.to_vec()),
            }
        }),
        width: params.width,
        quotient_degree,
        num_public_values: params.num_public_values,
        num_challenges_to_sample: params.num_challenges_to_sample,
        num_exposed_values_after_challenge: params.num_exposed_values_after_challenge,
        symbolic_constraints: symbolic_constraints.constraints,
    }
}

/// Create MultiStarkVerificationAdvice for the outer config.
pub fn new_from_outer_multi_vk(
    vk: &MultiStarkVerifyingKey<BabyBearPoseidon2RootConfig>,
) -> MultiStarkVerificationAdvice<OuterConfig> {
    let num_challenges_to_sample = vk.num_challenges_per_phase();
    let MultiStarkVerifyingKey::<BabyBearPoseidon2RootConfig> { per_air } = vk;
    MultiStarkVerificationAdvice {
        per_air: per_air
            .clone()
            .into_iter()
            .map(new_from_outer_vkv2)
            .collect(),
        num_challenges_to_sample,
    }
}