Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mimc_permutation.hpp
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_PERMUTATION_HPP__
6 #define __ZETH_CIRCUITS_MIMC_PERMUTATION_HPP__
7 
9 
10 namespace libzeth
11 {
12 
19 template<typename FieldT, size_t Exponent, size_t NumRounds>
20 class MiMC_permutation_gadget : public libsnark::gadget<FieldT>
21 {
22 private:
23  // Round constants only available up to some maximum number of rounds. Note
24  // that the instantiations used in this library do not use all 93 round
25  // constants, but other instantiations may (in particular, see
26  // instantiations for other curves in
27  // https://github.com/clearmatics/zecale).
28  static const size_t MaxRounds = 93;
29  static_assert(
30  NumRounds <= MaxRounds, "NumRounds must be less than MaxRounds");
31 
32  // Instantiate round gadget with exponent = Exponent
34 
35  // Vector of round constants
36  static std::vector<FieldT> round_constants;
37  static bool round_constants_initialized;
38 
39  // Vector of intermediate result values
40  std::array<libsnark::pb_variable<FieldT>, NumRounds> round_results;
41 
42  // Vector of MiMC round_gadgets
43  std::vector<RoundT> round_gadgets;
44 
45  // Common initialization
47  libsnark::protoboard<FieldT> &pb,
48  const libsnark::pb_linear_combination<FieldT> &msg,
49  const libsnark::pb_linear_combination<FieldT> &key,
50  const libsnark::pb_variable<FieldT> &result,
51  const libsnark::pb_linear_combination<FieldT> &add_to_result,
52  const bool add_to_result_is_valid,
53  const std::string &annotation_prefix);
54 
55 public:
57  libsnark::protoboard<FieldT> &pb,
58  const libsnark::pb_linear_combination<FieldT> &msg,
59  const libsnark::pb_linear_combination<FieldT> &key,
60  const libsnark::pb_variable<FieldT> &result,
61  const std::string &annotation_prefix = "MiMC_permutation_gadget");
62 
64  libsnark::protoboard<FieldT> &pb,
65  const libsnark::pb_linear_combination<FieldT> &msg,
66  const libsnark::pb_linear_combination<FieldT> &key,
67  const libsnark::pb_variable<FieldT> &result,
68  const libsnark::pb_linear_combination<FieldT> &add_to_result,
69  const std::string &annotation_prefix = "MiMC_permutation_gadget");
70 
72  void generate_r1cs_witness() const;
73 
74  // Constants vector initialization
75  void setup_sha3_constants();
76 };
77 
78 } // namespace libzeth
79 
81 
82 #endif // __ZETH_CIRCUITS_MIMC_PERMUTATION_HPP__
mimc_permutation.tcc
libzeth
Definition: binary_operation.hpp:15
libzeth::MiMC_permutation_gadget
Definition: mimc_permutation.hpp:20
libzeth::MiMC_permutation_gadget::setup_sha3_constants
void setup_sha3_constants()
libzeth::MiMC_permutation_gadget::generate_r1cs_constraints
void generate_r1cs_constraints()
mimc_round.hpp
libzeth::MiMC_permutation_gadget::generate_r1cs_witness
void generate_r1cs_witness() const
libzeth::MiMC_round_gadget
Definition: mimc_round.hpp:17