Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
merkle_path_selector.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/merkle_tree.hpp
4 // https://github.com/HarryR/ethsnarks/blob/master/src/gadgets/merkle_tree.cpp
5 
6 #ifndef __ZETH_CIRCUITS_MERKLE_PATH_SELECTOR_HPP___
7 #define __ZETH_CIRCUITS_MERKLE_PATH_SELECTOR_HPP___
8 
9 #include <libsnark/gadgetlib1/gadgets/basic_gadgets.hpp>
10 
11 // Depending on the address bit, output the correct left/right inputs
12 // for the merkle path authentication hash
13 //
14 // 0 = left
15 // 1 = right
16 //
17 // There are two variables which make up each element of the path,
18 // the `input` and the `pathvar`, the input is the leaf or the
19 // output from the last hash, and the path var is part of the merkle
20 // tree path.
21 //
22 // The `is_right` parameter decides if the `input` is on the left or
23 // right of the hash. These are decided in-circuit using the following
24 // method:
25 //
26 // Left = ((1-is_right) * input) + (is_right * pathvar)
27 // Right = (is_right * input) + ((1 - is_right) * pathvar)
28 //
29 // Each component is split into a & b sides, then added together
30 // so the correct variable ends up in the right or left hand side.
31 
32 namespace libzeth
33 {
34 
35 template<typename FieldT>
36 class merkle_path_selector : public libsnark::gadget<FieldT>
37 {
38 public:
39  // The hash of the previous level or the leaf
40  const libsnark::pb_variable<FieldT> input;
41  // The authentication node of the current level
42  const libsnark::pb_variable<FieldT> pathvar;
43  // The bit of the current level from the leaf address
44  const libsnark::pb_variable<FieldT> is_right;
45 
46  // The first input of the next hash to compute
47  libsnark::pb_variable<FieldT> left;
48  // The seconde input of the next hash to compute
49  libsnark::pb_variable<FieldT> right;
50 
52  libsnark::protoboard<FieldT> &pb,
53  // The hash of the previous level or the leaf
54  const libsnark::pb_variable<FieldT> &input,
55  // The authentication node of the current level
56  const libsnark::pb_variable<FieldT> &pathvar,
57  // The bit of the current level from the leaf address
58  const libsnark::pb_variable<FieldT> &is_right,
59  const std::string &annotation_prefix);
60 
62  void generate_r1cs_witness();
63 
64  // Returns the first input (left) of the next hash to compute
65  const libsnark::pb_variable<FieldT> &get_left();
66 
67  // Returns the second input (right) of the next hash to compute
68  const libsnark::pb_variable<FieldT> &get_right();
69 };
70 
71 } // namespace libzeth
72 
74 
75 #endif // __ZETH_CIRCUITS_MERKLE_PATH_SELECTOR_HPP___
libzeth::merkle_path_selector::is_right
const libsnark::pb_variable< FieldT > is_right
Definition: merkle_path_selector.hpp:44
libzeth::merkle_path_selector::left
libsnark::pb_variable< FieldT > left
Definition: merkle_path_selector.hpp:47
libzeth::merkle_path_selector::get_left
const libsnark::pb_variable< FieldT > & get_left()
libzeth
Definition: binary_operation.hpp:15
libzeth::merkle_path_selector
Definition: merkle_path_selector.hpp:36
libzeth::merkle_path_selector::input
const libsnark::pb_variable< FieldT > input
Definition: merkle_path_selector.hpp:40
libzeth::merkle_path_selector::right
libsnark::pb_variable< FieldT > right
Definition: merkle_path_selector.hpp:49
libzeth::merkle_path_selector::generate_r1cs_witness
void generate_r1cs_witness()
libzeth::merkle_path_selector::get_right
const libsnark::pb_variable< FieldT > & get_right()
merkle_path_selector.tcc
libzeth::merkle_path_selector::merkle_path_selector
merkle_path_selector(libsnark::protoboard< FieldT > &pb, const libsnark::pb_variable< FieldT > &input, const libsnark::pb_variable< FieldT > &pathvar, const libsnark::pb_variable< FieldT > &is_right, const std::string &annotation_prefix)
libzeth::merkle_path_selector::generate_r1cs_constraints
void generate_r1cs_constraints()
libzeth::merkle_path_selector::pathvar
const libsnark::pb_variable< FieldT > pathvar
Definition: merkle_path_selector.hpp:42