Skip to main content

sp1_hypercube/verifier/
config.rs

1use serde::{Deserialize, Serialize};
2use slop_algebra::AbstractField;
3use slop_challenger::VariableLengthChallenger;
4use slop_challenger::{CanObserve, IopCtx};
5
6use crate::septic_digest::SepticDigest;
7
8#[allow(clippy::disallowed_types)]
9use slop_basefold::Poseidon2KoalaBear16BasefoldConfig;
10
11#[allow(clippy::disallowed_types)]
12/// The basefold configuration (field, extension field, challenger, tensor commitment scheme)
13/// for SP1.
14pub type SP1BasefoldConfig = Poseidon2KoalaBear16BasefoldConfig;
15
16#[allow(clippy::disallowed_types)]
17pub use slop_koala_bear::Poseidon2KoalaBearConfig;
18
19#[allow(clippy::disallowed_types)]
20/// The Merkle tree configuration for SP1.
21pub type SP1MerkleTreeConfig = Poseidon2KoalaBearConfig;
22
23/// A specification of preprocessed polynomial batch dimensions.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
25pub struct ChipDimensions<T> {
26    /// The height of the preprocessed polynomial.
27    pub height: T,
28    /// The number of polynomials in the preprocessed batch.
29    pub num_polynomials: T,
30}
31
32/// A configuration regarding untrusted programs and trap handler.
33#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
34pub struct UntrustedConfig<T> {
35    /// Whether or not untrusted programs are enabled on the program.
36    pub enable_untrusted_programs: T,
37    /// Whether or not a trap handler exists.
38    #[cfg(feature = "mprotect")]
39    pub enable_trap_handler: T,
40    /// The `address`, `address + 8`, `address + 16` values of the trap context.
41    #[cfg(feature = "mprotect")]
42    pub trap_context: [[T; 3]; 3],
43    /// The region of memory where mprotect syscall may be called.
44    #[cfg(feature = "mprotect")]
45    pub untrusted_memory: [[T; 3]; 2],
46}
47
48impl<T: AbstractField> UntrustedConfig<T> {
49    /// A dummy config with all zeros
50    #[must_use]
51    pub fn zero() -> Self {
52        Self {
53            enable_untrusted_programs: T::zero(),
54            #[cfg(feature = "mprotect")]
55            enable_trap_handler: T::zero(),
56            #[cfg(feature = "mprotect")]
57            trap_context: [
58                [T::zero(), T::zero(), T::zero()],
59                [T::zero(), T::zero(), T::zero()],
60                [T::zero(), T::zero(), T::zero()],
61            ],
62            #[cfg(feature = "mprotect")]
63            untrusted_memory: [
64                [T::zero(), T::zero(), T::zero()],
65                [T::zero(), T::zero(), T::zero()],
66            ],
67        }
68    }
69}
70
71/// A verifying key.
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct MachineVerifyingKey<C: IopCtx> {
74    /// The start pc of the program.
75    pub pc_start: [C::F; 3],
76    /// The starting global digest of the program, after incorporating the initial memory.
77    pub initial_global_cumulative_sum: SepticDigest<C::F>,
78    /// The preprocessed commitments.
79    pub preprocessed_commit: C::Digest,
80    /// Metadata on configuration regarding untrusted programs.
81    pub untrusted_config: UntrustedConfig<C::F>,
82}
83
84impl<C: IopCtx> PartialEq for MachineVerifyingKey<C> {
85    fn eq(&self, other: &Self) -> bool {
86        self.pc_start == other.pc_start
87            && self.initial_global_cumulative_sum == other.initial_global_cumulative_sum
88            && self.preprocessed_commit == other.preprocessed_commit
89            && self.untrusted_config == other.untrusted_config
90    }
91}
92
93impl<C: IopCtx> Eq for MachineVerifyingKey<C> {}
94
95impl<C: IopCtx> MachineVerifyingKey<C> {
96    /// Observes the values of the proving key into the challenger.
97    pub fn observe_into(&self, challenger: &mut C::Challenger) {
98        challenger.observe(self.preprocessed_commit);
99        challenger.observe_constant_length_slice(&self.pc_start);
100        challenger.observe_constant_length_slice(&self.initial_global_cumulative_sum.0.x.0);
101        challenger.observe_constant_length_slice(&self.initial_global_cumulative_sum.0.y.0);
102        challenger.observe(self.untrusted_config.enable_untrusted_programs);
103        #[cfg(feature = "mprotect")]
104        challenger.observe(self.untrusted_config.enable_trap_handler);
105        #[cfg(feature = "mprotect")]
106        challenger.observe_constant_length_slice(self.untrusted_config.trap_context.as_flattened());
107        #[cfg(feature = "mprotect")]
108        challenger
109            .observe_constant_length_slice(self.untrusted_config.untrusted_memory.as_flattened());
110        // Observe the padding.
111        challenger.observe_constant_length_slice(&[C::F::zero(); 6]);
112    }
113}