2 // Content taken and adapted from:
3 // https://github.com/HarryR/ethsnarks/blob/master/src/gadgets/mimc.hpp
5 #ifndef __ZETH_CIRCUITS_MIMC_MP_TCC__
6 #define __ZETH_CIRCUITS_MIMC_MP_TCC__
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)
21 // Adding x+y to the output of the permutation yields the Miyaguchi-Preneel
24 // result = permutation(x, y) + x + y
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")));
32 template<typename FieldT, typename PermutationT>
33 void MiMC_mp_gadget<FieldT, PermutationT>::generate_r1cs_constraints()
35 permutation_gadget->generate_r1cs_constraints();
38 template<typename FieldT, typename PermutationT>
39 void MiMC_mp_gadget<FieldT, PermutationT>::generate_r1cs_witness() const
41 permutation_gadget->generate_r1cs_witness();
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)
48 libsnark::protoboard<FieldT> pb;
50 libsnark::pb_variable<FieldT> pb_x;
51 libsnark::pb_variable<FieldT> pb_y;
52 libsnark::pb_variable<FieldT> result;
54 // Allocates and fill with the x and y
55 pb_x.allocate(pb, "x");
58 pb_y.allocate(pb, "y");
61 result.allocate(pb, "result");
63 // Initialize the Hash
64 MiMC_mp_gadget<FieldT, PermutationT> mimc_hasher(
65 pb, pb_x, pb_y, result, " mimc_hash");
68 mimc_hasher.generate_r1cs_constraints();
69 mimc_hasher.generate_r1cs_witness();
72 return pb.val(result);
75 } // namespace libzeth
77 #endif // __ZETH_CIRCUITS_MIMC_MP_TCC__