jf_relation/gates/
mod.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the Jellyfish library.
3
4// You should have received a copy of the MIT License
5// along with the Jellyfish library. If not, see <https://mit-license.org/>.
6
7//! Module for various circuit gates.
8use ark_ff::Field;
9use ark_std::boxed::Box;
10use core::fmt;
11use downcast_rs::{impl_downcast, Downcast};
12use dyn_clone::DynClone;
13
14use crate::constants::{GATE_WIDTH, N_MUL_SELECTORS};
15
16mod arithmetic;
17mod ecc;
18mod logic;
19mod lookup;
20
21pub use arithmetic::*;
22pub use ecc::*;
23pub use logic::*;
24pub use lookup::*;
25
26/// Describes a gate with getter for all selectors configuration
27pub trait Gate<F: Field>: Downcast + DynClone {
28    /// Get the name of a gate.
29    fn name(&self) -> &'static str;
30    /// Selectors for linear combination.
31    fn q_lc(&self) -> [F; GATE_WIDTH] {
32        [F::zero(); GATE_WIDTH]
33    }
34    /// Selectors for Rescue hashes.
35    fn q_hash(&self) -> [F; GATE_WIDTH] {
36        [F::zero(); GATE_WIDTH]
37    }
38    /// Selectors for multiplication.
39    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
40        [F::zero(); N_MUL_SELECTORS]
41    }
42    /// The selector for elliptic curve operation.
43    fn q_ecc(&self) -> F {
44        F::zero()
45    }
46    /// Constant selector.
47    fn q_c(&self) -> F {
48        F::zero()
49    }
50    /// Output wire selector.
51    fn q_o(&self) -> F {
52        F::zero()
53    }
54    /// UltraPlonk lookup selector.
55    fn q_lookup(&self) -> F {
56        F::zero()
57    }
58    /// UltraPlonk lookup domain separation selector.
59    fn q_dom_sep(&self) -> F {
60        F::zero()
61    }
62    /// UltraPlonk table keys.
63    fn table_key(&self) -> F {
64        F::zero()
65    }
66    /// UltraPlonk table domain separation ids
67    fn table_dom_sep(&self) -> F {
68        F::zero()
69    }
70}
71impl_downcast!(Gate<F> where F: Field);
72
73impl<F: Field> Clone for Box<dyn Gate<F>> {
74    fn clone(&self) -> Box<dyn Gate<F>> {
75        dyn_clone::clone_box(&**self)
76    }
77}
78
79impl<F: Field> fmt::Debug for (dyn Gate<F> + 'static) {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        // TODO: (alex) add more context for debug
82        f.write_str(self.name())
83    }
84}
85
86/// A empty gate for circuit padding
87#[derive(Debug, Clone)]
88pub struct PaddingGate;
89
90impl<F> Gate<F> for PaddingGate
91where
92    F: Field,
93{
94    fn name(&self) -> &'static str {
95        "Padding Gate"
96    }
97}