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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! This module implements an abstraction to keep track of the powers of alphas.
//! As a recap, alpha is a challenge sent by the verifier in PLONK,
//! and is used to aggregate multiple constraints into a single polynomial.
//! It is important that different constraints use different powers of alpha,
//! as otherwise they can interact and potentially cancel one another.
//! (The proof is in the use of the Schwartz-Zippel lemma.)
//! As such, we want two properties from this:
//!
//! - we should keep track of a mapping between type of constraint and range of powers
//! - when powers of alphas are used, we should ensure that no more no less are used
//!
//! We use powers of alpha in two different places in the codebase:
//!
//! - when creating the index, we do not know alpha at this point so we
//!   simply keep track of what constraints will use what powers
//! - when creating a proof or verifying a proof, at this point we know alpha
//!   so we can use the mapping we created during the creation of the index.
//!
//! For this to work, we use the type [Alphas] to register ranges of powers of alpha,
//! for the various [ArgumentType]s.
//!

use crate::circuits::{argument::ArgumentType, gate::GateType};
use ark_ff::Field;
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    fmt::Display,
    iter::{Cloned, Skip, Take},
    ops::Range,
    slice::Iter,
    thread,
};

// ------------------------------------------

/// This type can be used to create a mapping between powers of alpha and constraint types.
/// See [Self::default] to create one,
/// and [Self::register] to register a new mapping.
/// Once you know the alpha value, you can convert this type to a [Alphas].
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct Alphas<F> {
    /// The next power of alpha to use
    /// the end result will be [1, alpha^{next_power - 1}]
    next_power: u32,
    /// The mapping between constraint types and powers of alpha
    mapping: HashMap<ArgumentType, (u32, u32)>,
    /// The powers of alpha: 1, alpha, alpha^2, etc.
    /// If set to [Some], you can't register new constraints.
    alphas: Option<Vec<F>>,
}

impl<F: Field> Alphas<F> {
    /// Registers a new [ArgumentType],
    /// associating it with a number `powers` of powers of alpha.
    /// This function will panic if you register the same type twice.
    pub fn register(&mut self, ty: ArgumentType, powers: u32) {
        if self.alphas.is_some() {
            panic!("you cannot register new constraints once initialized with a field element");
        }

        // gates are a special case, as we reuse the same power of alpha
        // across all of them (they're mutually exclusive)
        let ty = if matches!(ty, ArgumentType::Gate(_)) {
            // the zero gate is not used, so we default to it
            ArgumentType::Gate(GateType::Zero)
        } else {
            ty
        };

        if self.mapping.insert(ty, (self.next_power, powers)).is_some() {
            panic!("cannot re-register {ty:?}");
        }

        self.next_power = self
            .next_power
            .checked_add(powers)
            .expect("too many powers of alphas were registered");
    }

    /// Returns a range of exponents, for a given [ArgumentType], upperbounded by `num`.
    /// Note that this function will panic if you did not register enough powers of alpha.
    pub fn get_exponents(
        &self,
        ty: ArgumentType,
        num: u32,
    ) -> MustConsumeIterator<Range<u32>, u32> {
        let ty = if matches!(ty, ArgumentType::Gate(_)) {
            ArgumentType::Gate(GateType::Zero)
        } else {
            ty
        };

        let range = self
            .mapping
            .get(&ty)
            .unwrap_or_else(|| panic!("constraint {ty:?} was not registered"));

        if num > range.1 {
            panic!(
                "you asked for {num} exponents, but only registered {} for {:?}",
                range.1, ty
            );
        }

        let start = range.0;
        let end = start + num;

        MustConsumeIterator {
            inner: start..end,
            debug_info: ty,
        }
    }

    /// Instantiates the ranges with an actual field element `alpha`.
    /// Once you call this function, you cannot register new constraints via [Self::register].
    pub fn instantiate(&mut self, alpha: F) {
        let mut last_power = F::one();
        let mut alphas = Vec::with_capacity(self.next_power as usize);
        alphas.push(F::one());
        for _ in 1..self.next_power {
            last_power *= alpha;
            alphas.push(last_power);
        }
        self.alphas = Some(alphas);
    }

    /// This function allows us to retrieve the powers of alpha, upperbounded by `num`
    pub fn get_alphas(
        &self,
        ty: ArgumentType,
        num: u32,
    ) -> MustConsumeIterator<Cloned<Take<Skip<Iter<F>>>>, F> {
        let ty = if matches!(ty, ArgumentType::Gate(_)) {
            ArgumentType::Gate(GateType::Zero)
        } else {
            ty
        };

        let range = self
            .mapping
            .get(&ty)
            .unwrap_or_else(|| panic!("constraint {ty:?} was not registered"));

        if num > range.1 {
            panic!(
                "you asked for {num} alphas, but only {} are available for {:?}",
                range.1, ty
            );
        }

        match &self.alphas {
            None => panic!("you must call instantiate with an actual field element first"),
            Some(alphas) => {
                let alphas_range = alphas
                    .iter()
                    .skip(range.0 as usize)
                    .take(num as usize)
                    .cloned();
                MustConsumeIterator {
                    inner: alphas_range,
                    debug_info: ty,
                }
            }
        }
    }
}

impl<T> Display for Alphas<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for arg in [
            ArgumentType::Gate(GateType::Zero),
            ArgumentType::Permutation,
            //            ArgumentType::Lookup,
        ] {
            let name = if matches!(arg, ArgumentType::Gate(_)) {
                "gates".to_string()
            } else {
                format!("{arg:?}")
            };
            let range = self
                .mapping
                .get(&arg)
                .expect("you need to register all arguments before calling display");
            writeln!(
                f,
                "* **{}**. Offset starts at {} and {} powers of $\\alpha$ are used",
                name, range.0, range.1
            )?;
        }

        Ok(())
    }
}

// ------------------------------------------

/// Wrapper around an iterator that warns you if not consumed entirely.
#[derive(Debug)]
pub struct MustConsumeIterator<I, T>
where
    I: Iterator<Item = T>,
    T: std::fmt::Display,
{
    inner: I,
    debug_info: ArgumentType,
}

impl<I, T> Iterator for MustConsumeIterator<I, T>
where
    I: Iterator<Item = T>,
    T: std::fmt::Display,
{
    type Item = I::Item;
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<I, T> Drop for MustConsumeIterator<I, T>
where
    I: Iterator<Item = T>,
    T: std::fmt::Display,
{
    fn drop(&mut self) {
        if let Some(v) = self.inner.next() {
            if thread::panicking() {
                eprintln!("the registered number of powers of alpha for {:?} is too large, you haven't used alpha^{} (absolute power of alpha)", self.debug_info,
                v);
            } else {
                panic!("the registered number of powers of alpha for {:?} is too large, you haven't used alpha^{} (absolute power of alpha)", self.debug_info,
                v);
            }
        }
    }
}

// ------------------------------------------

#[cfg(test)]
mod tests {
    use std::{fs, path::Path};

    use super::*;
    use crate::circuits::gate::GateType;
    use mina_curves::pasta::{Fp, Vesta};

    // testing [Builder]

    #[test]
    fn incorrect_alpha_powers() {
        let mut alphas = Alphas::<Fp>::default();
        alphas.register(ArgumentType::Gate(GateType::Poseidon), 3);

        let mut powers = alphas.get_exponents(ArgumentType::Gate(GateType::Poseidon), 3);
        assert_eq!(powers.next(), Some(0));
        assert_eq!(powers.next(), Some(1));
        assert_eq!(powers.next(), Some(2));

        alphas.register(ArgumentType::Permutation, 3);
        let mut powers = alphas.get_exponents(ArgumentType::Permutation, 3);

        assert_eq!(powers.next(), Some(3));
        assert_eq!(powers.next(), Some(4));
        assert_eq!(powers.next(), Some(5));
    }

    #[test]
    #[should_panic]
    fn register_after_instantiating() {
        let mut alphas = Alphas::<Fp>::default();
        alphas.instantiate(Fp::from(1));
        alphas.register(ArgumentType::Gate(GateType::Poseidon), 3);
    }

    #[test]
    #[should_panic]
    fn didnt_use_all_alpha_powers() {
        let mut alphas = Alphas::<Fp>::default();
        alphas.register(ArgumentType::Permutation, 7);
        let mut powers = alphas.get_exponents(ArgumentType::Permutation, 3);
        powers.next();
    }

    #[test]
    #[should_panic]
    fn registered_alpha_powers_for_some_constraint_twice() {
        let mut alphas = Alphas::<Fp>::default();
        alphas.register(ArgumentType::Gate(GateType::Poseidon), 2);
        alphas.register(ArgumentType::Gate(GateType::ForeignFieldMul), 3);
    }

    #[test]
    fn powers_of_alpha() {
        let mut alphas = Alphas::default();
        alphas.register(ArgumentType::Gate(GateType::Poseidon), 4);
        let mut powers = alphas.get_exponents(ArgumentType::Gate(GateType::Poseidon), 4);

        assert_eq!(powers.next(), Some(0));
        assert_eq!(powers.next(), Some(1));
        assert_eq!(powers.next(), Some(2));
        assert_eq!(powers.next(), Some(3));

        let alpha = Fp::from(2);
        alphas.instantiate(alpha);

        let mut alphas = alphas.get_alphas(ArgumentType::Gate(GateType::Poseidon), 4);
        assert_eq!(alphas.next(), Some(1.into()));
        assert_eq!(alphas.next(), Some(2.into()));
        assert_eq!(alphas.next(), Some(4.into()));
        assert_eq!(alphas.next(), Some(8.into()));
    }

    // useful for the spec

    use crate::{
        circuits::{gate::CircuitGate, wires::Wire},
        linearization::expr_linearization,
        prover_index::testing::new_index_for_test,
    };

    #[test]
    fn get_alphas_for_spec() {
        let gates = vec![CircuitGate::<Fp>::zero(Wire::for_row(0)); 2];
        let index = new_index_for_test::<Vesta>(gates, 0);
        let (_linearization, powers_of_alpha) =
            expr_linearization::<Fp>(Some(&index.cs.feature_flags), true);
        // make sure this is present in the specification
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
        let spec_path = Path::new(&manifest_dir)
            .join("..")
            .join("book")
            .join("specifications")
            .join("kimchi")
            .join("template.md");

        let spec = fs::read_to_string(spec_path).unwrap();
        if !spec.contains(&powers_of_alpha.to_string()) {
            panic!(
                "the specification of kimchi must contain the following paragraph:\n\n{powers_of_alpha}\n\n"
            );
        }
    }
}