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
6 #ifndef __ZETH_CIRCUITS_MERKLE_PATH_AUTHENTICATOR_TCC__
7 #define __ZETH_CIRCUITS_MERKLE_PATH_AUTHENTICATOR_TCC__
12 template<typename FieldT, typename HashTreeT>
13 merkle_path_authenticator<FieldT, HashTreeT>::merkle_path_authenticator(
14 libsnark::protoboard<FieldT> &pb,
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)
29 template<typename FieldT, typename HashTreeT>
30 void merkle_path_authenticator<FieldT, HashTreeT>::generate_r1cs_constraints()
32 // We ensure the computed root is constrained
33 merkle_path_compute<FieldT, HashTreeT>::generate_r1cs_constraints();
35 // We ensure, if bool_enforce is 1, that the expected root matches the
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"));
43 template<typename FieldT, typename HashTreeT>
44 void merkle_path_authenticator<FieldT, HashTreeT>::generate_r1cs_witness()
46 merkle_path_compute<FieldT, HashTreeT>::generate_r1cs_witness();
49 template<typename FieldT, typename HashTreeT>
50 bool merkle_path_authenticator<FieldT, HashTreeT>::is_valid()
52 return this->pb.val(this->result()) == this->pb.val(m_expected_root);
55 } // namespace libzeth
57 #endif // __ZETH_CIRCUITS_MERKLE_PATH_AUTHENTICATOR_TCC__