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
use std::io;

use crate::{
    AtomicConfig, Config, Context, ContextUnit, Element, IndexedWitness, Preamble, Scalar,
};

/// PLONK polynomial expression representation with its selectors and witnesses.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Polynomial {
    qm: Scalar,
    ql: Scalar,
    qr: Scalar,
    qd: Scalar,
    qc: Scalar,
    qo: Scalar,
    pi: Scalar,
    qarith: Scalar,
    qlogic: Scalar,
    qvariable_add: Scalar,
    a: IndexedWitness,
    b: IndexedWitness,
    d: IndexedWitness,
    o: IndexedWitness,
    re: bool,
}

impl Polynomial {
    // TODO refactor into type, as clippy suggests
    #[allow(clippy::too_many_arguments)]
    /// Create a new polynomial
    pub const fn new(
        qm: Scalar,
        ql: Scalar,
        qr: Scalar,
        qd: Scalar,
        qc: Scalar,
        qo: Scalar,
        pi: Scalar,
        qarith: Scalar,
        qlogic: Scalar,
        qvariable_add: Scalar,
        a: IndexedWitness,
        b: IndexedWitness,
        d: IndexedWitness,
        o: IndexedWitness,
        re: bool,
    ) -> Self {
        Self {
            qm,
            ql,
            qr,
            qd,
            qc,
            qo,
            pi,
            qarith,
            qlogic,
            qvariable_add,
            a,
            b,
            d,
            o,
            re,
        }
    }

    /// Check if the polynomial evaluation is ok
    pub const fn is_ok(&self) -> bool {
        self.re
    }

    // TODO refactor into type, as clippy suggests
    #[allow(clippy::type_complexity)]
    /// Fetch the constraint internals
    pub const fn internals(
        &self,
    ) -> (
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &Scalar,
        &IndexedWitness,
        &IndexedWitness,
        &IndexedWitness,
        &IndexedWitness,
        bool,
    ) {
        (
            &self.qm,
            &self.ql,
            &self.qr,
            &self.qd,
            &self.qc,
            &self.qo,
            &self.pi,
            &self.qarith,
            &self.qlogic,
            &self.qvariable_add,
            &self.a,
            &self.b,
            &self.d,
            &self.o,
            self.re,
        )
    }
}

impl Element for Polynomial {
    type Config = Config;

    fn zeroed() -> Self {
        Self::default()
    }

    fn len(config: &Self::Config) -> usize {
        10 * Scalar::len(config) + 4 * IndexedWitness::len(config) + bool::len(&AtomicConfig)
    }

    fn to_buffer(&self, config: &Self::Config, context: &mut ContextUnit, buf: &mut [u8]) {
        let buf = self.qm.encode(config, context, buf);
        let buf = self.ql.encode(config, context, buf);
        let buf = self.qr.encode(config, context, buf);
        let buf = self.qd.encode(config, context, buf);
        let buf = self.qc.encode(config, context, buf);
        let buf = self.qo.encode(config, context, buf);
        let buf = self.pi.encode(config, context, buf);
        let buf = self.qarith.encode(config, context, buf);
        let buf = self.qlogic.encode(config, context, buf);
        let buf = self.qvariable_add.encode(config, context, buf);
        let buf = self.a.encode(config, context, buf);
        let buf = self.b.encode(config, context, buf);
        let buf = self.d.encode(config, context, buf);
        let buf = self.o.encode(config, context, buf);
        let _ = self.re.encode(&AtomicConfig, context, buf);
    }

    fn try_from_buffer_in_place<S>(
        &mut self,
        config: &Self::Config,
        context: &mut Context<S>,
        buf: &[u8],
    ) -> io::Result<()>
    where
        S: io::Read + io::Seek,
    {
        Self::validate_buffer_len(config, buf.len())?;

        let buf = self.qm.try_decode_in_place(config, context, buf)?;
        let buf = self.ql.try_decode_in_place(config, context, buf)?;
        let buf = self.qr.try_decode_in_place(config, context, buf)?;
        let buf = self.qd.try_decode_in_place(config, context, buf)?;
        let buf = self.qc.try_decode_in_place(config, context, buf)?;
        let buf = self.qo.try_decode_in_place(config, context, buf)?;
        let buf = self.pi.try_decode_in_place(config, context, buf)?;
        let buf = self.qarith.try_decode_in_place(config, context, buf)?;
        let buf = self.qlogic.try_decode_in_place(config, context, buf)?;
        let buf = self
            .qvariable_add
            .try_decode_in_place(config, context, buf)?;
        let buf = self.a.try_decode_in_place(config, context, buf)?;
        let buf = self.b.try_decode_in_place(config, context, buf)?;
        let buf = self.d.try_decode_in_place(config, context, buf)?;
        let buf = self.o.try_decode_in_place(config, context, buf)?;
        let _ = self.re.try_decode_in_place(&AtomicConfig, context, buf)?;

        Ok(())
    }

    fn validate(&self, preamble: &Preamble) -> io::Result<()> {
        self.qm.validate(preamble)?;
        self.ql.validate(preamble)?;
        self.qr.validate(preamble)?;
        self.qd.validate(preamble)?;
        self.qc.validate(preamble)?;
        self.qo.validate(preamble)?;
        self.pi.validate(preamble)?;
        self.qarith.validate(preamble)?;
        self.qlogic.validate(preamble)?;
        self.qvariable_add.validate(preamble)?;
        self.a.validate(preamble)?;
        self.b.validate(preamble)?;
        self.d.validate(preamble)?;
        self.o.validate(preamble)?;
        self.re.validate(preamble)?;

        Ok(())
    }
}

#[test]
fn validate_works() {
    Polynomial::zeroed()
        .validate(&Default::default())
        .expect("default config validate should pass");
}