Skip to main content

mdnt_support/circuit/
configuration.rs

1//! Traits related to circuit configuration.
2
3/// Helper trait that enables composing types that can handle circuit
4/// configuration.
5pub trait AutoConfigure<CS, Output = Self> {
6    /// Creates an instance of self using the constraint system.
7    fn configure(meta: &mut CS) -> Output;
8}
9
10/// Creates an implementation of [`AutoConfigure`].
11#[macro_export]
12macro_rules! auto_conf_impl {
13    ($T:ty, $method:ident) => {
14        $crate::auto_conf_impl!($T, $method, midnight_proofs);
15    };
16    ($T:ty, $method:ident, $proofs:ident) => {
17        impl<F: ff::Field> AutoConfigure<$proofs::plonk::ConstraintSystem<F>, $T> for $T {
18            fn configure(meta: &mut $proofs::plonk::ConstraintSystem<F>) -> $T {
19                meta.$method()
20            }
21        }
22    };
23}
24
25impl<CS, T, const N: usize> AutoConfigure<CS> for [T; N]
26where
27    T: AutoConfigure<CS>,
28{
29    fn configure(meta: &mut CS) -> [T; N] {
30        std::array::from_fn(|_| T::configure(meta))
31    }
32}
33
34impl<CS> AutoConfigure<CS> for () {
35    fn configure(_: &mut CS) -> Self {
36        ()
37    }
38}
39
40macro_rules! tuple_auto_conf_impl {
41    () => {
42        // Do nothing
43    };
44    ($h:ident $(,$t:ident)* $(,)?) => {
45        tuple_auto_conf_impl!($( $t, )*);
46
47        impl<CS, $h, $( $t, )*> AutoConfigure<CS> for ( $h, $( $t, )* )
48        where
49            $h: AutoConfigure<CS,  $h>,
50            $( $t: AutoConfigure<CS,  $t>, )*
51        {
52            fn configure(meta: &mut CS) -> Self {
53                (
54                    $h::configure(meta),
55                    $( $t::configure(meta), )*
56                )
57            }
58        }
59    };
60}
61
62tuple_auto_conf_impl!(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12);