openvm_sdk/keygen/
dummy.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::sync::Arc;

use openvm_circuit::{
    arch::{
        instructions::{
            exe::VmExe, instruction::Instruction, program::Program, SystemOpcode::TERMINATE,
            VmOpcode,
        },
        SingleSegmentVmExecutor, VirtualMachine, VmComplexTraceHeights, VmConfig, VmExecutor,
    },
    system::program::trace::VmCommittedExe,
    utils::next_power_of_two_or_zero,
};
use openvm_native_circuit::NativeConfig;
use openvm_native_compiler::ir::DIGEST_SIZE;
use openvm_native_recursion::hints::Hintable;
use openvm_rv32im_circuit::Rv32ImConfig;
use openvm_stark_sdk::{
    config::{
        baby_bear_poseidon2::BabyBearPoseidon2Engine,
        fri_params::standard_fri_params_with_100_bits_conjectured_security, FriParameters,
    },
    engine::StarkFriEngine,
    openvm_stark_backend::{
        config::StarkGenericConfig, p3_field::FieldAlgebra, prover::types::Proof, Chip,
    },
};

use crate::{
    prover::vm::{
        local::VmLocalProver, types::VmProvingKey, ContinuationVmProof, ContinuationVmProver,
        SingleSegmentVmProver,
    },
    verifier::{
        internal::types::InternalVmVerifierInput,
        leaf::{types::LeafVmVerifierInput, LeafVmVerifierConfig},
        root::types::RootVmVerifierInput,
    },
    NonRootCommittedExe, F, SC,
};

/// Returns:
/// - trace heights ordered by AIR ID
/// - internal ordering of trace heights.
///
/// All trace heights are rounded to the next power of two (or 0 -> 0).
pub(super) fn compute_root_proof_heights(
    root_vm_config: NativeConfig,
    root_exe: VmExe<F>,
    dummy_internal_proof: &Proof<SC>,
) -> (Vec<usize>, VmComplexTraceHeights) {
    let num_user_public_values = root_vm_config.system.num_public_values - 2 * DIGEST_SIZE;
    let root_input = RootVmVerifierInput {
        proofs: vec![dummy_internal_proof.clone()],
        public_values: vec![F::ZERO; num_user_public_values],
    };
    let vm = SingleSegmentVmExecutor::new(root_vm_config);
    let res = vm
        .execute_and_compute_heights(root_exe, root_input.write())
        .unwrap();
    let air_heights: Vec<_> = res
        .air_heights
        .into_iter()
        .map(next_power_of_two_or_zero)
        .collect();
    let mut internal_heights = res.internal_heights;
    internal_heights.round_to_next_power_of_two_or_zero();
    (air_heights, internal_heights)
}

pub(super) fn dummy_internal_proof(
    internal_vm_pk: Arc<VmProvingKey<SC, NativeConfig>>,
    internal_exe: Arc<NonRootCommittedExe>,
    leaf_proof: Proof<SC>,
) -> Proof<SC> {
    let mut internal_inputs = InternalVmVerifierInput::chunk_leaf_or_internal_proofs(
        internal_exe.get_program_commit().into(),
        &[leaf_proof],
        1,
    );
    let internal_input = internal_inputs.pop().unwrap();
    let internal_prover = VmLocalProver::<SC, NativeConfig, BabyBearPoseidon2Engine>::new(
        internal_vm_pk,
        internal_exe,
    );
    SingleSegmentVmProver::prove(&internal_prover, internal_input.write())
}

pub(super) fn dummy_internal_proof_riscv_app_vm(
    leaf_vm_pk: Arc<VmProvingKey<SC, NativeConfig>>,
    internal_vm_pk: Arc<VmProvingKey<SC, NativeConfig>>,
    internal_exe: Arc<NonRootCommittedExe>,
    num_public_values: usize,
) -> Proof<SC> {
    let fri_params = standard_fri_params_with_100_bits_conjectured_security(1);
    let leaf_proof = dummy_leaf_proof_riscv_app_vm(leaf_vm_pk, num_public_values, fri_params);
    dummy_internal_proof(internal_vm_pk, internal_exe, leaf_proof)
}

#[allow(dead_code)]
pub fn dummy_leaf_proof<VC: VmConfig<F>>(
    leaf_vm_pk: Arc<VmProvingKey<SC, NativeConfig>>,
    app_vm_pk: Arc<VmProvingKey<SC, VC>>,
    overridden_heights: Option<VmComplexTraceHeights>,
) -> Proof<SC>
where
    VC::Executor: Chip<SC>,
    VC::Periphery: Chip<SC>,
{
    let app_proof = dummy_app_proof_impl(app_vm_pk.clone(), overridden_heights);
    dummy_leaf_proof_impl(leaf_vm_pk, app_vm_pk, &app_proof)
}

pub(super) fn dummy_leaf_proof_riscv_app_vm(
    leaf_vm_pk: Arc<VmProvingKey<SC, NativeConfig>>,
    num_public_values: usize,
    app_fri_params: FriParameters,
) -> Proof<SC> {
    let app_vm_pk = Arc::new(dummy_riscv_app_vm_pk(num_public_values, app_fri_params));
    let app_proof = dummy_app_proof_impl(app_vm_pk.clone(), None);
    dummy_leaf_proof_impl(leaf_vm_pk, app_vm_pk, &app_proof)
}

fn dummy_leaf_proof_impl<VC: VmConfig<F>>(
    leaf_vm_pk: Arc<VmProvingKey<SC, NativeConfig>>,
    app_vm_pk: Arc<VmProvingKey<SC, VC>>,
    app_proof: &ContinuationVmProof<SC>,
) -> Proof<SC> {
    let leaf_program = LeafVmVerifierConfig {
        app_fri_params: app_vm_pk.fri_params,
        app_system_config: app_vm_pk.vm_config.system().clone(),
        compiler_options: Default::default(),
    }
    .build_program(&app_vm_pk.vm_pk.get_vk());
    assert_eq!(
        app_proof.per_segment.len(),
        1,
        "Dummy proof should only have 1 segment"
    );
    let e = BabyBearPoseidon2Engine::new(leaf_vm_pk.fri_params);
    let leaf_exe = Arc::new(VmCommittedExe::<SC>::commit(
        leaf_program.into(),
        e.config.pcs(),
    ));
    let leaf_prover =
        VmLocalProver::<SC, NativeConfig, BabyBearPoseidon2Engine>::new(leaf_vm_pk, leaf_exe);
    let mut leaf_inputs = LeafVmVerifierInput::chunk_continuation_vm_proof(app_proof, 1);
    let leaf_input = leaf_inputs.pop().unwrap();
    SingleSegmentVmProver::prove(&leaf_prover, leaf_input.write_to_stream())
}

fn dummy_riscv_app_vm_pk(
    num_public_values: usize,
    fri_params: FriParameters,
) -> VmProvingKey<SC, Rv32ImConfig> {
    let vm_config = Rv32ImConfig::with_public_values(num_public_values);
    let vm = VirtualMachine::new(BabyBearPoseidon2Engine::new(fri_params), vm_config.clone());
    let vm_pk = vm.keygen();
    VmProvingKey {
        fri_params,
        vm_config,
        vm_pk,
    }
}

fn dummy_app_proof_impl<VC: VmConfig<F>>(
    app_vm_pk: Arc<VmProvingKey<SC, VC>>,
    overridden_heights: Option<VmComplexTraceHeights>,
) -> ContinuationVmProof<SC>
where
    VC::Executor: Chip<SC>,
    VC::Periphery: Chip<SC>,
{
    let fri_params = app_vm_pk.fri_params;
    let dummy_exe = dummy_app_committed_exe(fri_params);
    // Enforce each AIR to have at least 1 row.
    let overridden_heights = if let Some(overridden_heights) = overridden_heights {
        overridden_heights
    } else {
        // We first execute once to get the trace heights from dummy_exe, then pad to powers of 2 (forcing trace height 0 to 1)
        let executor = VmExecutor::new(app_vm_pk.vm_config.clone());
        let mut results = executor
            .execute_segments(dummy_exe.exe.clone(), vec![])
            .unwrap();
        // ASSUMPTION: the dummy exe has only 1 segment
        assert_eq!(results.len(), 1, "dummy exe should have only 1 segment");
        let mut result = results.pop().unwrap();
        result.chip_complex.finalize_memory();
        let mut internal_heights = result.chip_complex.get_internal_trace_heights();
        internal_heights.round_to_next_power_of_two();
        internal_heights
    };
    // For the dummy proof, we must override the trace heights.
    let app_prover =
        VmLocalProver::<SC, VC, BabyBearPoseidon2Engine>::new_with_overridden_trace_heights(
            app_vm_pk,
            dummy_exe,
            Some(overridden_heights),
        );
    ContinuationVmProver::prove(&app_prover, vec![])
}

fn dummy_app_committed_exe(fri_params: FriParameters) -> Arc<NonRootCommittedExe> {
    let program = dummy_app_program();
    let e = BabyBearPoseidon2Engine::new(fri_params);
    Arc::new(VmCommittedExe::<SC>::commit(program.into(), e.config.pcs()))
}

fn dummy_app_program() -> Program<F> {
    let mut ret = Program::from_instructions(&[Instruction::from_isize(
        VmOpcode::with_default_offset(TERMINATE),
        0,
        0,
        0,
        0,
        0,
    )]);
    ret.max_num_public_values = 0;
    ret
}