jf_relation/gates/
mod.rs

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
// Copyright (c) 2022 Espresso Systems (espressosys.com)
// This file is part of the Jellyfish library.

// You should have received a copy of the MIT License
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

//! Module for various circuit gates.
use ark_ff::Field;
use ark_std::boxed::Box;
use core::fmt;
use downcast_rs::{impl_downcast, Downcast};
use dyn_clone::DynClone;

use crate::constants::{GATE_WIDTH, N_MUL_SELECTORS};

mod arithmetic;
mod ecc;
mod logic;
mod lookup;

pub use arithmetic::*;
pub use ecc::*;
pub use logic::*;
pub use lookup::*;

/// Describes a gate with getter for all selectors configuration
pub trait Gate<F: Field>: Downcast + DynClone {
    /// Get the name of a gate.
    fn name(&self) -> &'static str;
    /// Selectors for linear combination.
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        [F::zero(); GATE_WIDTH]
    }
    /// Selectors for Rescue hashes.
    fn q_hash(&self) -> [F; GATE_WIDTH] {
        [F::zero(); GATE_WIDTH]
    }
    /// Selectors for multiplication.
    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
        [F::zero(); N_MUL_SELECTORS]
    }
    /// The selector for elliptic curve operation.
    fn q_ecc(&self) -> F {
        F::zero()
    }
    /// Constant selector.
    fn q_c(&self) -> F {
        F::zero()
    }
    /// Output wire selector.
    fn q_o(&self) -> F {
        F::zero()
    }
    /// UltraPlonk lookup selector.
    fn q_lookup(&self) -> F {
        F::zero()
    }
    /// UltraPlonk lookup domain separation selector.
    fn q_dom_sep(&self) -> F {
        F::zero()
    }
    /// UltraPlonk table keys.
    fn table_key(&self) -> F {
        F::zero()
    }
    /// UltraPlonk table domain separation ids
    fn table_dom_sep(&self) -> F {
        F::zero()
    }
}
impl_downcast!(Gate<F> where F: Field);

impl<F: Field> Clone for Box<dyn Gate<F>> {
    fn clone(&self) -> Box<dyn Gate<F>> {
        dyn_clone::clone_box(&**self)
    }
}

impl<F: Field> fmt::Debug for (dyn Gate<F> + 'static) {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // TODO: (alex) add more context for debug
        f.write_str(self.name())
    }
}

/// A empty gate for circuit padding
#[derive(Debug, Clone)]
pub struct PaddingGate;

impl<F> Gate<F> for PaddingGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Padding Gate"
    }
}