Skip to main content

zinc_piop/combined_poly_resolver/
structs.rs

1use crypto_primitives::PrimeField;
2use zinc_transcript::traits::{ConstTranscribable, GenTranscribable, Transcribable};
3use zinc_utils::add;
4
5use crate::combined_poly_resolver::CombinedPolyResolverError;
6
7/// The proof type of the combined polynomial resolver subprotocol.
8///
9/// Note: the sumcheck proof now lives at the protocol
10/// level as part of `MultiDegreeSumcheckProof`.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Proof<F: PrimeField> {
13    /// The evaluation of the projected trace columns MLEs at the shared point.
14    pub up_evals: Vec<F>,
15    /// The evaluations of the shifted projected trace columns MLEs at the
16    /// shared point.
17    pub down_evals: Vec<F>,
18}
19
20impl<F: PrimeField> GenTranscribable for Proof<F>
21where
22    F::Inner: ConstTranscribable,
23    F::Modulus: ConstTranscribable,
24{
25    fn read_transcription_bytes_exact(bytes: &[u8]) -> Self {
26        let (up_evals, bytes) = Vec::<F>::read_transcription_bytes_subset(bytes);
27        let (down_evals, bytes) = Vec::<F>::read_transcription_bytes_subset(bytes);
28        assert!(bytes.is_empty(), "All bytes should be consumed");
29        Self {
30            up_evals,
31            down_evals,
32        }
33    }
34
35    fn write_transcription_bytes_exact(&self, buf: &mut [u8]) {
36        let buf = self.up_evals.write_transcription_bytes_subset(buf);
37        let buf = self.down_evals.write_transcription_bytes_subset(buf);
38        assert!(buf.is_empty(), "Entire buffer should be used");
39    }
40}
41
42impl<F: PrimeField> Transcribable for Proof<F>
43where
44    F::Inner: ConstTranscribable,
45    F::Modulus: ConstTranscribable,
46{
47    fn get_num_bytes(&self) -> usize {
48        add!(
49            2 * u32::NUM_BYTES,
50            add!(
51                self.up_evals.get_num_bytes(),
52                self.down_evals.get_num_bytes()
53            )
54        )
55    }
56}
57
58impl<F: PrimeField> Proof<F> {
59    /// Check if `up_evals` and `down_evals` vectors have the expected lengths.
60    pub fn validate_evaluation_sizes(
61        &self,
62        num_cols: usize,
63        num_down_cols: usize,
64    ) -> Result<(), CombinedPolyResolverError<F>> {
65        if self.up_evals.len() != num_cols {
66            return Err(CombinedPolyResolverError::WrongUpEvalsNumber {
67                got: self.up_evals.len(),
68                expected: num_cols,
69            });
70        }
71
72        if self.down_evals.len() != num_down_cols {
73            return Err(CombinedPolyResolverError::WrongDownEvalsNumber {
74                got: self.down_evals.len(),
75                expected: num_down_cols,
76            });
77        }
78
79        Ok(())
80    }
81}
82
83pub struct ProverState<F: PrimeField> {
84    /// The shared evaluation point yielded by the multi-degree sumcheck.
85    pub evaluation_point: Vec<F>,
86}
87
88/// Ancillary data produced by `prepare_sumcheck_group` and consumed by
89/// `finalize_prover`. Holds everything needed to extract `up_evals` /
90/// `down_evals` after the shared sumcheck completes.
91pub struct CprProverAncillary {
92    /// Number of trace (up) columns — used to split the flat evals vec.
93    pub num_cols: usize,
94    /// Number of shifted (down) columns.
95    pub num_down_cols: usize,
96    /// Number of variables — used to index the last challenge.
97    pub num_vars: usize,
98}
99
100/// Ancillary data produced by `prepare_verifier` and consumed by
101/// `finalize_verifier`. Holds state that bridges the pre-sumcheck and
102/// post-sumcheck halves of the CPR verifier.
103pub struct CprVerifierAncillary<F: PrimeField> {
104    /// Powers of the folding challenge α: [1, α, α², ..., α^{k-1}].
105    pub folding_challenge_powers: Vec<F>,
106    /// Evaluation point from the ideal check subclaim (for eq_r computation).
107    pub ic_evaluation_point: Vec<F>,
108    /// Number of variables (for selector computation).
109    pub num_vars: usize,
110}
111
112/// The claim that is left to be proven after the combined polynomial resolver
113/// verifier has succeeded. It is several evaluation claims about the trace
114/// columns and the shifted trace columns at the same evaluation point.
115#[derive(Clone, Debug)]
116pub struct VerifierSubclaim<F: PrimeField> {
117    /// Evaluation point for the claims.
118    pub evaluation_point: Vec<F>,
119    /// Evaluation claims about the trace columns.
120    pub up_evals: Vec<F>,
121    /// Evaluation claims about the shifted trace columns.
122    pub down_evals: Vec<F>,
123}