zksnark 0.0.2

An implementation of zkSNARK using groth16.
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use super::super::super::field::Field;
use std::collections::HashMap;

#[cfg(test)]
mod tests;

#[derive(Clone, Copy, Debug)]
pub enum ConnectionType<T>
where
    T: Copy,
{
    Left(T, SubCircuitId),
    Right(T, SubCircuitId),
    Output(SubCircuitId),
}

#[derive(Clone)]
pub struct SubCircuitConnections<T> {
    left_inputs: Vec<(T, WireId)>,
    right_inputs: Vec<(T, WireId)>,
    output: WireId,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct WireId(usize);

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct SubCircuitId(usize);

impl SubCircuitId {
    pub fn inner_id(&self) -> usize {
        self.0
    }
}

pub struct Circuit<T>
where
    T: Copy,
{
    next_wire_id: WireId,
    next_sub_circuit_id: SubCircuitId,

    wire_assignments: HashMap<WireId, Vec<ConnectionType<T>>>,
    sub_circuit_wires: HashMap<SubCircuitId, SubCircuitConnections<T>>,
    wire_values: HashMap<WireId, Option<T>>,
}

/// The purpose of this struct is to build circuits into a representation that
/// can be turned into a `QAP` (Quadratic Arithmetic Program) which is defined
/// in "groth16/mod.rs". I'll start this off by describing what are the building
/// blocks for a `Circuit` are made of:
///
/// # Sub Circuit
///
/// You can think of a circuit as a collection of nodes connected by wires
/// without any cycles (Directed Acyclic Graph). Nodes are made up of
/// "sub circuit" which in reality look like this:
///
/// ```
/// // `Left` input wires-> \|/ \|  <- `Right` input wires
/// //                       +   +  <- Plus operation
/// //                        \ /   <- (implicit connections, not wires)
/// //                         *    <- Multiplication operation
/// //                         |    <- `Output` wire
/// ```
///
/// Lets walk through the example "sub circuit" which is the atomic unit of a
/// `Circuit`. In this example the left plus operation has 3 input wires and the
/// right plus operation has 2 input wires. Every plus operation must have at
/// least one input wire, but may have any number of extra input wires. Each
/// "wire" also has an associated weight and takes some input, unless it is an
/// `Output` wire. The way the "sub circuit" is evaluated is by evaluating all
/// input wires, followed by the plus operation and multiplication operation.
///
/// ```
/// // For Example, given the above "sub circuit" we can evaluate it as follows:
/// //              We have these inputs and weights on the left wires:
/// //                 - [(1,1), (0,2), (2,1)] : [(weights, inputs)]
///
/// //              And here are the inputs and weights on the right wires:
/// //                 - [(0,3), (0,1)] : [(weights, inputs)]
///
/// //              To evaluate the wires we multiply their inputs by their weight:
/// //              and then the plus operation adds the results together:
/// //                 - Left plus operation equals => ((1 * 1) + (0 * 2) + (2 * 1))
/// //                 - Right plus operation equals => ((0 * 3) + (1 * 0))
///
/// //              Finally the multiplication operation multiplies the result
/// //              of the previous two:
/// //                 - (((1 * 1) + (0 * 2) + (2 * 1)) * ((0 * 3) + (1 * 0)))
/// //              
/// //              Thus the output wire would have the value: 0
/// ```
///
/// Note: A single wire may connect to any number of sub circuits including to
/// the same sub circuit multiple times on both its left and right inputs.
/// (Exception, wires must never form a loop anywhere in the `Circuit`)
///
/// # `Circuit`
///
/// A `Circuit` is made up of many connecting sub circuits. To evaluate a
/// `Circuit` means to determine the value of the `Circuit`'s output wires.
/// Which are in turn made up of the sub circuits output wires that do not
/// connect to another sub circuit's input wires. This also means a `Circuit`
/// has some number of input wires which come from the sub circuits with input
/// wires that do not connect to other sub circuit's output wires. `Circuit`s
/// are pure in the sense that the input uniquely determines the output of the
/// `Circuit`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use zksnark::field::z251::Z251;
/// use zksnark::field::*;
/// use zksnark::groth16::circuit::*;
///
/// // Create an empty circuit
/// let mut circuit = Circuit::<Z251>::new();
///
/// // In order to give our sub circuits input we need input wires that the sub
/// // circuits will take as arguments.
/// let input_wire = circuit.new_wire();
///
/// // lets start with a bit checker that returns 0 if the input is 0 or 1 and
/// // some other number in other cases. This is useful after turning the
/// // `Circuit` into a `CircuitInstance` where these wires can be checked to
/// // ensure they evaluate to zero. However, in this module we can only
/// // evaluate the whole `Circuit`
/// let checker_wire = circuit.new_bit_checker(input_wire);
///
/// // First lets give it a zero to check if it does what we think.
/// circuit.set_value(input_wire, Z251::from(0));
/// assert_eq!(circuit.evaluate(checker_wire), Z251::from(0));
///
/// // Next we need to reset all wire values in order to re-evaluate the circuit
/// // and in order to set the inputs to new values.
/// circuit.reset();
///
/// // Same check with 1 as input
/// circuit.set_value(input_wire, Z251::from(1));
/// assert_eq!(circuit.evaluate(checker_wire), Z251::from(0));
///
/// // Just to demonstrate a negative test
/// circuit.reset();
/// circuit.set_value(input_wire, Z251::from(2));
/// assert_ne!(circuit.evaluate(checker_wire), Z251::from(0));
///
/// // This gives you a basic idea of how to use some components, but not what
/// // this actually looks like. Let me draw a picture of what the circuit looks
/// // like now:
///
/// //  (weight 1, input_wire) -> |
/// //                           / \
/// //                           |  \| <- (weight -1, unity_wire)
/// //                           +   +
/// //                            \ /
/// //                             *
/// //                             | <- checker_wire
///
/// // Note: the unity wire, is a wire that always outputs the value 1. You can
/// // think of it as an input wire that is permanently set to an input of one.
///
/// // Now we can add something onto that `checker` wire
/// let not_wire = circuit.new_not(checker_wire);
///
/// // Now it looks like this:
///
/// //    (weight 1, input_wire) -> |
/// //                             / \
/// //                             |  \| <- (weight -1, one wire)
/// //                             +   +
/// //                              \ /
/// //                               *
/// //                               | <- checker_wire
/// // ------------------------------|-----------------------------------------
/// //                               | <- (weight -1, checker_wire)
/// // (weight 1, unity wire) -> |   |/ <- (weight 1, unity_wire)
/// //                           +   +
/// //                            \ /
/// //                             *
/// //                             | <- not_wire
///
/// // Note: the line "----" is to show where the sub circuit starts and ends
/// // conceptually. The checker_wire, when inserted into the input of the
/// // new_not, is the same wire; it just gets assigned a weight.
///
/// // reset, because we have different inputs from before
/// circuit.reset();
/// circuit.set_value(input_wire, Z251::from(0));
/// assert_eq!(circuit.evaluate(not_wire), Z251::from(1));
///
/// // Check that the not's input was either 0 or 1
/// // Note, this call will not need to "evaluate" the checker_wire since it
/// // was already evaluated to get the value of not_wire
/// assert_eq!(circuit.evaluate(checker_wire), Z251::from(0));
/// ```
///
impl<T> Circuit<T>
where
    T: Copy + Field,
{
    pub fn new() -> Self {
        // TODO: Initialise the unity wire value to be one?
        Circuit {
            next_wire_id: WireId(1),
            next_sub_circuit_id: SubCircuitId(0),
            wire_assignments: HashMap::new(),
            sub_circuit_wires: HashMap::new(),
            wire_values: HashMap::new(),
        }
    }

    pub fn unity_wire(&self) -> WireId {
        WireId(0)
    }

    pub fn new_wire(&mut self) -> WireId {
        let next_wire_id = self.next_wire_id;
        self.next_wire_id.0 += 1;
        self.wire_values.insert(next_wire_id, None);
        next_wire_id
    }

    pub fn num_wires(&self) -> usize {
        self.next_wire_id.0
    }

    pub fn value(&self, wire: WireId) -> Option<T> {
        *self
            .wire_values
            .get(&wire)
            .expect("wire is not defined in this circuit")
    }

    pub fn set_value(&mut self, wire: WireId, value: T) {
        self.wire_values.insert(wire, Some(value));
    }

    pub fn wire_assignments(&self) -> &HashMap<WireId, Vec<ConnectionType<T>>> {
        &self.wire_assignments
    }

    pub fn assignments(&self, wire: &WireId) -> &Vec<ConnectionType<T>> {
        self.wire_assignments
            .get(wire)
            .expect("wire id is not defined in this circuit")
    }

    fn insert_connection(&mut self, wire: WireId, connection: ConnectionType<T>) {
        if self.wire_assignments.get(&wire).is_none() {
            self.wire_assignments.insert(wire, vec![connection]);
        } else {
            self.wire_assignments
                .get_mut(&wire)
                .map(|v| v.push(connection));
        }
    }

    pub fn sub_circuits(&self) -> impl Iterator<Item = SubCircuitId> {
        (0..self.next_sub_circuit_id.0).map(|id| SubCircuitId(id))
    }

    pub fn new_sub_circuit(
        &mut self,
        left_inputs: Vec<(T, WireId)>,
        right_inputs: Vec<(T, WireId)>,
    ) -> WireId {
        use self::ConnectionType::{Left, Output, Right};

        let sub_circuit_id = self.next_sub_circuit_id;
        self.next_sub_circuit_id.0 += 1;
        let output_wire = self.new_wire();

        // Update the LHS wire mappings
        for (weight, wire) in left_inputs.clone().into_iter() {
            let connection = Left(weight, sub_circuit_id);
            self.insert_connection(wire, connection);
        }

        // Update the RHS wire mappings
        for (weight, wire) in right_inputs.clone().into_iter() {
            let connection = Right(weight, sub_circuit_id);
            self.insert_connection(wire, connection);
        }

        // Update the output wire mappings
        let connection = Output(sub_circuit_id);
        self.insert_connection(output_wire, connection);

        // Update the sub circuit mapping
        self.sub_circuit_wires.insert(
            sub_circuit_id,
            SubCircuitConnections {
                left_inputs,
                right_inputs,
                output: output_wire,
            },
        );

        output_wire
    }

    fn evaluate_sub_circuit(&mut self, sub_circuit: SubCircuitId) -> T {
        let SubCircuitConnections {
            left_inputs,
            right_inputs,
            ..
        } = self
            .sub_circuit_wires
            .get(&sub_circuit)
            .expect("a sub circuit referenced by a wire should exist")
            .clone();

        let lhs = left_inputs
            .into_iter()
            .fold(T::zero(), |acc, (weight, wire)| {
                acc + weight * self.evaluate(wire)
            });
        let rhs = right_inputs
            .into_iter()
            .fold(T::zero(), |acc, (weight, wire)| {
                acc + weight * self.evaluate(wire)
            });
        lhs * rhs
    }

    pub fn evaluate(&mut self, wire: WireId) -> T {
        use self::ConnectionType::Output;

        if wire == self.unity_wire() {
            return T::one();
        }

        self.wire_values
            .get(&wire)
            .expect("cannot evaluate unknown wire")
            .unwrap_or_else(|| {
                let output_sub_circuit = self
                    .wire_assignments
                    .get(&wire)
                    .expect("a wire must be attached to something")
                    .into_iter()
                    .filter_map(|c| if let &Output(sc) = c { Some(sc) } else { None })
                    .nth(0)
                    .expect("a wire with an unknown value must be the output of a sub circuit");

                let value = self.evaluate_sub_circuit(output_sub_circuit);
                self.wire_values.insert(wire, Some(value));

                value
            })
    }

    /// Clears all of the stored circuit wire values (including those manually
    /// set) so that the same circuit can be reused for different inputs.
    pub fn reset(&mut self) {
        for value in self.wire_values.values_mut() {
            *value = None;
        }
    }

    pub fn new_bit_checker(&mut self, input: WireId) -> WireId {
        let lhs_inputs = vec![(T::one(), input)];
        let rhs_inputs = vec![(T::one(), input), (-T::one(), self.unity_wire())];

        self.new_sub_circuit(lhs_inputs, rhs_inputs)
    }

    pub fn new_not(&mut self, input: WireId) -> WireId {
        let lhs_inputs = vec![(T::one(), self.unity_wire())];
        let rhs_inputs = vec![(T::one(), self.unity_wire()), (-T::one(), input)];

        self.new_sub_circuit(lhs_inputs, rhs_inputs)
    }

    pub fn new_and(&mut self, lhs: WireId, rhs: WireId) -> WireId {
        let lhs_inputs = vec![(T::one(), lhs)];
        let rhs_inputs = vec![(T::one(), rhs)];

        self.new_sub_circuit(lhs_inputs, rhs_inputs)
    }

    pub fn new_or(&mut self, lhs: WireId, rhs: WireId) -> WireId {
        let lhs_and_rhs = self.new_and(lhs, rhs);
        let one = T::one();
        let lhs_inputs = vec![(-one, lhs_and_rhs), (one, lhs), (one, rhs)];
        let rhs_inputs = vec![(one, self.unity_wire())];

        self.new_sub_circuit(lhs_inputs, rhs_inputs)
    }

    pub fn new_xor(&mut self, lhs: WireId, rhs: WireId) -> WireId {
        let one = T::one();
        let lhs_inputs = vec![(one, lhs), (-one, rhs)];
        let rhs_inputs = vec![(one, lhs), (-one, rhs)];

        self.new_sub_circuit(lhs_inputs, rhs_inputs)
    }

    pub fn fan_in<F>(&mut self, inputs: &[WireId], mut gate: F) -> WireId
    where
        F: FnMut(&mut Self, WireId, WireId) -> WireId,
    {
        if inputs.len() < 2 {
            panic!("cannot fan in with fewer than two inputs");
        }
        inputs
            .iter()
            .skip(1)
            .fold(inputs[0], |acc, wire| gate(self, acc, *wire))
    }

    pub fn bitwise_op<F>(&mut self, left: &[WireId], right: &[WireId], mut gate: F) -> Vec<WireId>
    where
        F: FnMut(&mut Self, WireId, WireId) -> WireId,
    {
        assert!(left.len() == right.len());

        left.iter()
            .zip(right.iter())
            .map(|(&l, &r)| gate(self, l, r))
            .collect()
    }
}