halo2_axiom/poly/
query.rs

1use std::fmt::Debug;
2
3use super::commitment::{Blind, MSM};
4use crate::{
5    arithmetic::eval_polynomial,
6    poly::{Coeff, Polynomial},
7};
8use halo2curves::CurveAffine;
9
10pub trait Query<F>: Sized + Clone + Send + Sync {
11    type Commitment: PartialEq + Copy + Send + Sync;
12    type Eval: Clone + Default + Debug;
13
14    fn get_point(&self) -> F;
15    fn get_eval(&self) -> Self::Eval;
16    fn get_commitment(&self) -> Self::Commitment;
17}
18
19/// A polynomial query at a point
20#[derive(Debug, Clone)]
21pub struct ProverQuery<'com, C: CurveAffine> {
22    /// point at which polynomial is queried
23    pub(crate) point: C::Scalar,
24    /// coefficients of polynomial
25    pub(crate) poly: &'com Polynomial<C::Scalar, Coeff>,
26    /// blinding factor of polynomial
27    pub(crate) blind: Blind<C::Scalar>,
28}
29
30#[doc(hidden)]
31#[derive(Copy, Clone)]
32pub struct PolynomialPointer<'com, C: CurveAffine> {
33    pub(crate) poly: &'com Polynomial<C::Scalar, Coeff>,
34    pub(crate) blind: Blind<C::Scalar>,
35}
36
37impl<'com, C: CurveAffine> PartialEq for PolynomialPointer<'com, C> {
38    fn eq(&self, other: &Self) -> bool {
39        std::ptr::eq(self.poly, other.poly)
40    }
41}
42
43impl<'com, C: CurveAffine> Query<C::Scalar> for ProverQuery<'com, C> {
44    type Commitment = PolynomialPointer<'com, C>;
45    type Eval = C::Scalar;
46
47    fn get_point(&self) -> C::Scalar {
48        self.point
49    }
50    fn get_eval(&self) -> Self::Eval {
51        eval_polynomial(&self.poly[..], self.get_point())
52    }
53    fn get_commitment(&self) -> Self::Commitment {
54        PolynomialPointer {
55            poly: self.poly,
56            blind: self.blind,
57        }
58    }
59}
60
61impl<'com, C: CurveAffine, M: MSM<C>> VerifierQuery<'com, C, M> {
62    /// Create a new verifier query based on a commitment
63    pub fn new_commitment(commitment: &'com C, point: C::Scalar, eval: C::Scalar) -> Self {
64        VerifierQuery {
65            point,
66            eval,
67            commitment: CommitmentReference::Commitment(commitment),
68        }
69    }
70
71    /// Create a new verifier query based on a linear combination of commitments
72    pub fn new_msm(msm: &'com M, point: C::Scalar, eval: C::Scalar) -> VerifierQuery<'com, C, M> {
73        VerifierQuery {
74            point,
75            eval,
76            commitment: CommitmentReference::MSM(msm),
77        }
78    }
79}
80
81/// A polynomial query at a point
82#[derive(Debug)]
83pub struct VerifierQuery<'com, C: CurveAffine, M: MSM<C>> {
84    /// point at which polynomial is queried
85    pub(crate) point: C::Scalar,
86    /// commitment to polynomial
87    pub(crate) commitment: CommitmentReference<'com, C, M>,
88    /// evaluation of polynomial at query point
89    pub(crate) eval: C::Scalar,
90}
91
92impl<'com, C: CurveAffine, M: MSM<C>> Clone for VerifierQuery<'com, C, M> {
93    fn clone(&self) -> Self {
94        Self {
95            point: self.point,
96            commitment: self.commitment,
97            eval: self.eval,
98        }
99    }
100}
101
102#[allow(clippy::upper_case_acronyms)]
103#[derive(Clone, Debug)]
104pub enum CommitmentReference<'r, C: CurveAffine, M: MSM<C>> {
105    Commitment(&'r C),
106    MSM(&'r M),
107}
108
109impl<'r, C: CurveAffine, M: MSM<C>> Copy for CommitmentReference<'r, C, M> {}
110
111impl<'r, C: CurveAffine, M: MSM<C>> PartialEq for CommitmentReference<'r, C, M> {
112    #![allow(clippy::vtable_address_comparisons)]
113    fn eq(&self, other: &Self) -> bool {
114        match (self, other) {
115            (&CommitmentReference::Commitment(a), &CommitmentReference::Commitment(b)) => {
116                std::ptr::eq(a, b)
117            }
118            (&CommitmentReference::MSM(a), &CommitmentReference::MSM(b)) => std::ptr::eq(a, b),
119            _ => false,
120        }
121    }
122}
123
124impl<'com, C: CurveAffine, M: MSM<C>> Query<C::Scalar> for VerifierQuery<'com, C, M> {
125    type Eval = C::Scalar;
126    type Commitment = CommitmentReference<'com, C, M>;
127
128    fn get_point(&self) -> C::Scalar {
129        self.point
130    }
131    fn get_eval(&self) -> C::Scalar {
132        self.eval
133    }
134    fn get_commitment(&self) -> Self::Commitment {
135        self.commitment
136    }
137}