Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
merkle_path_authenticator.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/merkle_tree.hpp
4 // https://github.com/HarryR/ethsnarks/blob/master/src/gadgets/merkle_tree.cpp
5 
6 #ifndef __ZETH_CIRCUITS_MERKLE_PATH_AUTHENTICATOR_TCC__
7 #define __ZETH_CIRCUITS_MERKLE_PATH_AUTHENTICATOR_TCC__
8 
9 namespace libzeth
10 {
11 
12 template<typename FieldT, typename HashTreeT>
13 merkle_path_authenticator<FieldT, HashTreeT>::merkle_path_authenticator(
14  libsnark::protoboard<FieldT> &pb,
15  const size_t depth,
16  const libsnark::pb_variable_array<FieldT> &address_bits,
17  const libsnark::pb_variable<FieldT> leaf,
18  const libsnark::pb_variable<FieldT> expected_root,
19  const libsnark::pb_variable_array<FieldT> &path,
20  const libsnark::pb_variable<FieldT> &bool_enforce,
21  const std::string &annotation_prefix)
22  : merkle_path_compute<FieldT, HashTreeT>(
23  pb, depth, address_bits, leaf, path, annotation_prefix)
24  , m_expected_root(expected_root)
25  , value_enforce(bool_enforce)
26 {
27 }
28 
29 template<typename FieldT, typename HashTreeT>
30 void merkle_path_authenticator<FieldT, HashTreeT>::generate_r1cs_constraints()
31 {
32  // We ensure the computed root is constrained
33  merkle_path_compute<FieldT, HashTreeT>::generate_r1cs_constraints();
34 
35  // We ensure, if bool_enforce is 1, that the expected root matches the
36  // computed one
37  this->pb.add_r1cs_constraint(
38  libsnark::r1cs_constraint<FieldT>(
39  this->result() - this->m_expected_root, this->value_enforce, 0),
40  FMT(this->annotation_prefix, " expected_root authenticator"));
41 }
42 
43 template<typename FieldT, typename HashTreeT>
44 void merkle_path_authenticator<FieldT, HashTreeT>::generate_r1cs_witness()
45 {
46  merkle_path_compute<FieldT, HashTreeT>::generate_r1cs_witness();
47 }
48 
49 template<typename FieldT, typename HashTreeT>
50 bool merkle_path_authenticator<FieldT, HashTreeT>::is_valid()
51 {
52  return this->pb.val(this->result()) == this->pb.val(m_expected_root);
53 }
54 
55 } // namespace libzeth
56 
57 #endif // __ZETH_CIRCUITS_MERKLE_PATH_AUTHENTICATOR_TCC__