jf_relation/gates/
arithmetic.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
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
// 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/>.

//! Implementation of arithmetic gates

use super::Gate;
use crate::constants::{GATE_WIDTH, N_MUL_SELECTORS};
use ark_ff::Field;

/// A constant gate
#[derive(Debug, Clone)]
pub struct ConstantGate<F: Field>(pub(crate) F);

impl<F> Gate<F> for ConstantGate<F>
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Constant Gate"
    }
    fn q_c(&self) -> F {
        self.0
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// An addition gate
#[derive(Debug, Clone)]
pub struct AdditionGate;

impl<F> Gate<F> for AdditionGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Addition Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        [F::one(), F::one(), F::zero(), F::zero()]
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// Adding a variable by a constant.
#[derive(Debug, Clone)]
pub struct ConstantAdditionGate<F: Field>(pub(crate) F);

impl<F> Gate<F> for ConstantAdditionGate<F>
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Constant addition Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        [F::one(), F::zero(), F::zero(), F::zero()]
    }
    fn q_c(&self) -> F {
        self.0
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// A subtraction gate
#[derive(Debug, Clone)]
pub struct SubtractionGate;

impl<F> Gate<F> for SubtractionGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Subtraction Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        [F::one(), -F::one(), F::zero(), F::zero()]
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// A multiplication gate
#[derive(Debug, Clone)]
pub struct MultiplicationGate;

impl<F> Gate<F> for MultiplicationGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Multiplication Gate"
    }
    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
        [F::one(), F::zero()]
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// A mul constant gate.
/// Multiply the first variable with the constant.
#[derive(Debug, Clone)]
pub struct ConstantMultiplicationGate<F>(pub(crate) F);

impl<F> Gate<F> for ConstantMultiplicationGate<F>
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Mul constant Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        [self.0, F::zero(), F::zero(), F::zero()]
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// A boolean gate, selectors identical to `MultiplicationGate`, achieve through
/// constraining a * a = a
#[derive(Debug, Clone)]
pub struct BoolGate;

impl<F> Gate<F> for BoolGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Check Boolean Gate"
    }
    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
        MultiplicationGate.q_mul()
    }
    fn q_o(&self) -> F {
        MultiplicationGate.q_o()
    }
}

/// An equality gate, selectors identical to `SubtractionGate`, achieve through
/// constraining a - b = 0
#[derive(Debug, Clone)]
pub struct EqualityGate;

impl<F> Gate<F> for EqualityGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Check Equality Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        SubtractionGate.q_lc()
    }
    fn q_o(&self) -> F {
        SubtractionGate.q_o()
    }
}

/// An I/O gate for public inputs
#[derive(Debug, Clone)]
pub struct IoGate;

impl<F> Gate<F> for IoGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Public I/O Gate"
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// Gate for checking a value is the fifth root of another
#[derive(Debug, Clone)]
pub struct FifthRootGate;

impl<F: Field> Gate<F> for FifthRootGate {
    fn name(&self) -> &'static str {
        "Raise to the inverse of 5 power Gate"
    }

    fn q_hash(&self) -> [F; GATE_WIDTH] {
        [F::one(), F::zero(), F::zero(), F::zero()]
    }

    fn q_o(&self) -> F {
        F::one()
    }
}

/// A deg-2 polynomial gate
#[derive(Clone)]
pub struct QuadPolyGate<F: Field> {
    pub(crate) q_lc: [F; GATE_WIDTH],
    pub(crate) q_mul: [F; N_MUL_SELECTORS],
    pub(crate) q_o: F,
    pub(crate) q_c: F,
}
impl<F> Gate<F> for QuadPolyGate<F>
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Deg-2 Polynomial Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        self.q_lc
    }
    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
        self.q_mul
    }
    fn q_o(&self) -> F {
        self.q_o
    }
    fn q_c(&self) -> F {
        self.q_c
    }
}

/// A linear combination gate
#[derive(Clone)]
pub struct LinCombGate<F: Field> {
    pub(crate) coeffs: [F; GATE_WIDTH],
}
impl<F> Gate<F> for LinCombGate<F>
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Linear Combination Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        self.coeffs
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// A multiplication-then-addition gate
#[derive(Clone)]
pub struct MulAddGate<F: Field> {
    pub(crate) coeffs: [F; N_MUL_SELECTORS],
}
impl<F> Gate<F> for MulAddGate<F>
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Multiplication-then-addition Gate"
    }
    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
        self.coeffs
    }
    fn q_o(&self) -> F {
        F::one()
    }
}

/// A gate for conditional selection
#[derive(Clone)]
pub struct CondSelectGate;

impl<F> Gate<F> for CondSelectGate
where
    F: Field,
{
    fn name(&self) -> &'static str {
        "Conditional Selection Gate"
    }
    fn q_lc(&self) -> [F; GATE_WIDTH] {
        [F::zero(), F::one(), F::zero(), F::zero()]
    }
    fn q_mul(&self) -> [F; N_MUL_SELECTORS] {
        [-F::one(), F::one()]
    }
    fn q_o(&self) -> F {
        F::one()
    }
}