openvm_stark_sdk/config/
baby_bear_bytehash.rs

1use openvm_stark_backend::{
2    config::StarkConfig,
3    interaction::fri_log_up::FriLogUpPhase,
4    p3_challenger::{HashChallenger, SerializingChallenger32},
5    p3_commit::ExtensionMmcs,
6    p3_field::extension::BinomialExtensionField,
7};
8use p3_baby_bear::BabyBear;
9use p3_dft::Radix2DitParallel;
10use p3_fri::{FriConfig, TwoAdicFriPcs};
11use p3_merkle_tree::MerkleTreeMmcs;
12use p3_symmetric::{CompressionFunctionFromHasher, CryptographicHasher, SerializingHasher32};
13
14use super::FriParameters;
15use crate::{
16    config::{
17        fri_params::SecurityParameters, log_up_params::log_up_security_params_baby_bear_100_bits,
18    },
19    engine::{StarkEngine, StarkFriEngine},
20};
21
22type Val = BabyBear;
23type Challenge = BinomialExtensionField<Val, 4>;
24
25// Generic over H: CryptographicHasher<u8, [u8; 32]>
26type FieldHash<H> = SerializingHasher32<H>;
27type Compress<H> = CompressionFunctionFromHasher<H, 2, 32>;
28// type InstrCompress<H> = Instrumented<Compress<H>>;
29
30type ValMmcs<H> = MerkleTreeMmcs<Val, u8, FieldHash<H>, Compress<H>, 32>;
31type ChallengeMmcs<H> = ExtensionMmcs<Val, Challenge, ValMmcs<H>>;
32type Dft = Radix2DitParallel<Val>;
33type Challenger<H> = SerializingChallenger32<Val, HashChallenger<u8, H, 32>>;
34
35type Pcs<H> = TwoAdicFriPcs<Val, Dft, ValMmcs<H>, ChallengeMmcs<H>>;
36
37type RapPhase<H> = FriLogUpPhase<Val, Challenge, Challenger<H>>;
38
39pub type BabyBearByteHashConfig<H> = StarkConfig<Pcs<H>, RapPhase<H>, Challenge, Challenger<H>>;
40
41pub struct BabyBearByteHashEngine<H>
42where
43    H: CryptographicHasher<u8, [u8; 32]> + Clone,
44{
45    pub fri_params: FriParameters,
46    pub config: BabyBearByteHashConfig<H>,
47    pub byte_hash: H,
48    pub max_constraint_degree: usize,
49}
50
51impl<H> StarkEngine<BabyBearByteHashConfig<H>> for BabyBearByteHashEngine<H>
52where
53    H: CryptographicHasher<u8, [u8; 32]> + Clone + Send + Sync,
54{
55    fn config(&self) -> &BabyBearByteHashConfig<H> {
56        &self.config
57    }
58
59    fn max_constraint_degree(&self) -> Option<usize> {
60        Some(self.max_constraint_degree)
61    }
62
63    fn new_challenger(&self) -> Challenger<H> {
64        Challenger::from_hasher(vec![], self.byte_hash.clone())
65    }
66}
67
68/// `pcs_log_degree` is the upper bound on the log_2(PCS polynomial degree).
69pub fn default_engine<H>(byte_hash: H) -> BabyBearByteHashEngine<H>
70where
71    H: CryptographicHasher<u8, [u8; 32]> + Clone,
72{
73    engine_from_byte_hash(byte_hash, SecurityParameters::standard_fast())
74}
75
76pub fn engine_from_byte_hash<H>(
77    byte_hash: H,
78    security_params: SecurityParameters,
79) -> BabyBearByteHashEngine<H>
80where
81    H: CryptographicHasher<u8, [u8; 32]> + Clone,
82{
83    let fri_params = security_params.fri_params;
84    let max_constraint_degree = fri_params.max_constraint_degree();
85    let config = config_from_byte_hash(byte_hash.clone(), security_params);
86    BabyBearByteHashEngine {
87        config,
88        byte_hash,
89        fri_params,
90        max_constraint_degree,
91    }
92}
93
94pub fn config_from_byte_hash<H>(
95    byte_hash: H,
96    security_params: SecurityParameters,
97) -> BabyBearByteHashConfig<H>
98where
99    H: CryptographicHasher<u8, [u8; 32]> + Clone,
100{
101    let field_hash = FieldHash::new(byte_hash.clone());
102    let compress = Compress::new(byte_hash);
103    let val_mmcs = ValMmcs::new(field_hash, compress);
104    let challenge_mmcs = ChallengeMmcs::new(val_mmcs.clone());
105    let dft = Dft::default();
106    let SecurityParameters {
107        fri_params,
108        log_up_params,
109    } = security_params;
110    let fri_config = FriConfig {
111        log_blowup: fri_params.log_blowup,
112        log_final_poly_len: fri_params.log_final_poly_len,
113        num_queries: fri_params.num_queries,
114        proof_of_work_bits: fri_params.proof_of_work_bits,
115        mmcs: challenge_mmcs,
116    };
117    let pcs = Pcs::new(dft, val_mmcs, fri_config);
118    let rap_phase = FriLogUpPhase::new(log_up_params);
119    BabyBearByteHashConfig::new(pcs, rap_phase)
120}
121
122pub trait BabyBearByteHashEngineWithDefaultHash<H>
123where
124    H: CryptographicHasher<u8, [u8; 32]> + Clone,
125{
126    fn default_hash() -> H;
127}
128
129impl<H: CryptographicHasher<u8, [u8; 32]> + Clone + Send + Sync>
130    StarkFriEngine<BabyBearByteHashConfig<H>> for BabyBearByteHashEngine<H>
131where
132    BabyBearByteHashEngine<H>: BabyBearByteHashEngineWithDefaultHash<H>,
133{
134    fn new(fri_params: FriParameters) -> Self {
135        let security_params = SecurityParameters {
136            fri_params,
137            log_up_params: log_up_security_params_baby_bear_100_bits(),
138        };
139        engine_from_byte_hash(Self::default_hash(), security_params)
140    }
141    fn fri_params(&self) -> FriParameters {
142        self.fri_params
143    }
144}