Skip to main content

sp1_prover/worker/prover/
air_prover.rs

1use std::{future::Future, sync::Arc};
2
3use slop_challenger::IopCtx;
4use sp1_hypercube::{
5    prover::{AirProver, PcsProof, Program, ProverPermit, ProverSemaphore, ProvingKey, Record},
6    Chip, Machine, MachineVerifyingKey, ShardContext, ShardContextProof, ShardProof,
7};
8
9/// A prover for an AIR.
10pub trait AirProverWorker<GC: IopCtx, SC: ShardContext<GC>, P: AirProver<GC, SC>>:
11    'static + Send + Sync
12{
13    /// Setup from a program.
14    ///
15    /// The setup phase produces a verifying key.
16    #[allow(clippy::type_complexity)]
17    fn setup(
18        &self,
19        program: Arc<Program<GC, SC>>,
20        setup_permits: ProverSemaphore,
21    ) -> impl Future<Output = (Arc<ProvingKey<GC, SC, P>>, MachineVerifyingKey<GC>)> + Send;
22
23    /// Get the machine.
24    fn machine(&self) -> &Machine<GC::F, SC::Air>;
25
26    /// Setup and prove a shard.
27    fn setup_and_prove_shard(
28        &self,
29        program: Arc<Program<GC, SC>>,
30        record: Record<GC, SC>,
31        vk: Option<MachineVerifyingKey<GC>>,
32        prover_permits: ProverSemaphore,
33    ) -> impl Future<Output = (MachineVerifyingKey<GC>, ShardContextProof<GC, SC>, ProverPermit)> + Send;
34    /// Setup and prove a shard.
35    fn prove_shard_with_pk(
36        &self,
37        pk: Arc<ProvingKey<GC, SC, P>>,
38        record: Record<GC, SC>,
39        prover_permits: ProverSemaphore,
40    ) -> impl Future<Output = (ShardProof<GC, PcsProof<GC, SC>>, ProverPermit)> + Send;
41    /// Get all the chips in the machine.
42    fn all_chips(&self) -> &[Chip<GC::F, SC::Air>] {
43        self.machine().chips()
44    }
45}
46
47impl<GC, SC, P> AirProverWorker<GC, SC, P> for P
48where
49    GC: IopCtx,
50    SC: ShardContext<GC>,
51    P: AirProver<GC, SC>,
52{
53    async fn setup(
54        &self,
55        program: Arc<Program<GC, SC>>,
56        setup_permits: ProverSemaphore,
57    ) -> (Arc<ProvingKey<GC, SC, P>>, MachineVerifyingKey<GC>) {
58        let (preprocessed, vk) = self.setup(program, setup_permits).await;
59        (preprocessed.pk, vk)
60    }
61
62    /// Get the machine.
63    fn machine(&self) -> &Machine<GC::F, SC::Air> {
64        AirProver::machine(self)
65    }
66
67    /// Setup and prove a shard.
68    async fn setup_and_prove_shard(
69        &self,
70        program: Arc<Program<GC, SC>>,
71        record: Record<GC, SC>,
72        vk: Option<MachineVerifyingKey<GC>>,
73        prover_permits: ProverSemaphore,
74    ) -> (MachineVerifyingKey<GC>, ShardProof<GC, PcsProof<GC, SC>>, ProverPermit) {
75        AirProver::setup_and_prove_shard(self, program, record, vk, prover_permits).await
76    }
77
78    /// Prove a shard from a given pk.
79    async fn prove_shard_with_pk(
80        &self,
81        pk: Arc<ProvingKey<GC, SC, P>>,
82        record: Record<GC, SC>,
83        prover_permits: ProverSemaphore,
84    ) -> (ShardProof<GC, PcsProof<GC, SC>>, ProverPermit) {
85        AirProver::prove_shard_with_pk(self, pk, record, prover_permits).await
86    }
87}