Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mimc_mp.tcc
Go to the documentation of this file.
1 // DISCLAIMER:
2 // Content taken and adapted from:
3 // https://github.com/HarryR/ethsnarks/blob/master/src/gadgets/mimc.hpp
4 
5 #ifndef __ZETH_CIRCUITS_MIMC_MP_TCC__
6 #define __ZETH_CIRCUITS_MIMC_MP_TCC__
7 
8 #include "mimc_mp.hpp"
9 
10 namespace libzeth
11 {
12 
13 template<typename FieldT, typename PermutationT>
14 MiMC_mp_gadget<FieldT, PermutationT>::MiMC_mp_gadget(
15  libsnark::protoboard<FieldT> &pb,
16  const libsnark::pb_linear_combination<FieldT> &x,
17  const libsnark::pb_linear_combination<FieldT> &y,
18  const libsnark::pb_variable<FieldT> &result,
19  const std::string &annotation_prefix)
20 {
21  // Adding x+y to the output of the permutation yields the Miyaguchi-Preneel
22  // equation:
23  //
24  // result = permutation(x, y) + x + y
25 
26  libsnark::pb_linear_combination<FieldT> x_plus_y;
27  x_plus_y.assign(pb, x + y);
28  permutation_gadget.reset(new PermutationT(
29  pb, x, y, result, x_plus_y, FMT(annotation_prefix, " MP")));
30 }
31 
32 template<typename FieldT, typename PermutationT>
33 void MiMC_mp_gadget<FieldT, PermutationT>::generate_r1cs_constraints()
34 {
35  permutation_gadget->generate_r1cs_constraints();
36 }
37 
38 template<typename FieldT, typename PermutationT>
39 void MiMC_mp_gadget<FieldT, PermutationT>::generate_r1cs_witness() const
40 {
41  permutation_gadget->generate_r1cs_witness();
42 }
43 
44 // Returns the hash of two elements
45 template<typename FieldT, typename PermutationT>
46 FieldT MiMC_mp_gadget<FieldT, PermutationT>::get_hash(const FieldT x, FieldT y)
47 {
48  libsnark::protoboard<FieldT> pb;
49 
50  libsnark::pb_variable<FieldT> pb_x;
51  libsnark::pb_variable<FieldT> pb_y;
52  libsnark::pb_variable<FieldT> result;
53 
54  // Allocates and fill with the x and y
55  pb_x.allocate(pb, "x");
56  pb.val(pb_x) = x;
57 
58  pb_y.allocate(pb, "y");
59  pb.val(pb_y) = y;
60 
61  result.allocate(pb, "result");
62 
63  // Initialize the Hash
64  MiMC_mp_gadget<FieldT, PermutationT> mimc_hasher(
65  pb, pb_x, pb_y, result, " mimc_hash");
66 
67  // Computes the hash
68  mimc_hasher.generate_r1cs_constraints();
69  mimc_hasher.generate_r1cs_witness();
70 
71  // Returns the hash
72  return pb.val(result);
73 }
74 
75 } // namespace libzeth
76 
77 #endif // __ZETH_CIRCUITS_MIMC_MP_TCC__