1 #include <gtest/gtest.h>
3 #include "test/data/merkle_roots.json.h"
4 #include "test/data/merkle_roots_empty.json.h"
5 #include "test/data/merkle_serialization.json.h"
6 #include "test/data/merkle_witness_serialization.json.h"
7 #include "test/data/merkle_path.json.h"
13 #include "utilstrencodings.h"
15 #include "serialize.h"
18 #include "zcash/IncrementalMerkleTree.hpp"
19 #include "zcash/util.h"
21 #include "libsnark/common/default_types/r1cs_ppzksnark_pp.hpp"
22 #include "libsnark/zk_proof_systems/ppzksnark/r1cs_ppzksnark/r1cs_ppzksnark.hpp"
23 #include "libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_gadget.hpp"
24 #include "libsnark/gadgetlib1/gadgets/merkle_tree/merkle_tree_check_read_gadget.hpp"
26 #include <boost/foreach.hpp>
28 #include "json/json_spirit_reader_template.h"
29 #include "json/json_spirit_utils.h"
30 #include "json/json_spirit_writer_template.h"
32 using namespace json_spirit;
34 read_json(const std::string& jsondata)
38 if (!read_string(jsondata, v) || v.type() != array_type)
46 //#define PRINT_JSON 1
49 using namespace libsnark;
53 void expect_deser_same(const T& expected)
55 CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION);
58 auto serialized_size = ss1.size();
63 CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
66 ASSERT_TRUE(serialized_size == ss2.size());
67 ASSERT_TRUE(memcmp(&*ss1.begin(), &*ss2.begin(), serialized_size) == 0);
71 void expect_deser_same(const ZCTestingIncrementalWitness& expected)
73 // Cannot check this; IncrementalWitness cannot be
74 // deserialized because it can only be constructed by
75 // IncrementalMerkleTree, and it does not yet have a
76 // canonical serialized representation.
80 void expect_deser_same(const libzcash::MerklePath& expected)
82 // This deserialization check is pointless for MerklePath,
83 // since we only serialize it to check it against test
84 // vectors. See `expect_test_vector` for that. Also,
85 // it doesn't seem that vector<bool> can be properly
86 // deserialized by Bitcoin's serialization code.
89 template<typename T, typename U>
90 void expect_test_vector(T& it, const U& expected)
92 expect_deser_same(expected);
94 CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION);
99 std::cout << HexStr(ss1.begin(), ss1.end()) << "\",\n";
101 std::string raw = (it++)->get_str();
102 CDataStream ss2(ParseHex(raw), SER_NETWORK, PROTOCOL_VERSION);
104 ASSERT_TRUE(ss1.size() == ss2.size());
105 ASSERT_TRUE(memcmp(&*ss1.begin(), &*ss2.begin(), ss1.size()) == 0);
109 template<typename A, typename B, typename C>
110 void expect_ser_test_vector(B& b, const C& c, const A& tree) {
111 expect_test_vector<B, C>(b, c);
114 template<typename Tree, typename Witness>
115 void test_tree(Array root_tests, Array ser_tests, Array witness_ser_tests, Array path_tests) {
116 Array::iterator root_iterator = root_tests.begin();
117 Array::iterator ser_iterator = ser_tests.begin();
118 Array::iterator witness_ser_iterator = witness_ser_tests.begin();
119 Array::iterator path_iterator = path_tests.begin();
121 uint256 test_commitment = uint256S("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
125 // The root of the tree at this point is expected to be the root of the
127 ASSERT_TRUE(tree.root() == Tree::empty_root());
129 // We need to witness at every single point in the tree, so
130 // that the consistency of the tree and the merkle paths can
132 vector<Witness> witnesses;
134 for (size_t i = 0; i < 16; i++) {
136 witnesses.push_back(tree.witness());
138 // Now append a commitment to the tree
139 tree.append(test_commitment);
141 // Check tree root consistency
142 expect_test_vector(root_iterator, tree.root());
144 // Check serialization of tree
145 expect_ser_test_vector(ser_iterator, tree, tree);
147 bool first = true; // The first witness can never form a path
148 BOOST_FOREACH(Witness& wit, witnesses)
150 // Append the same commitment to all the witnesses
151 wit.append(test_commitment);
154 ASSERT_THROW(wit.path(), std::runtime_error);
156 auto path = wit.path();
159 expect_test_vector(path_iterator, path);
161 typedef Fr<default_r1cs_ppzksnark_pp> FieldT;
163 protoboard<FieldT> pb;
164 pb_variable_array<FieldT> positions;
165 digest_variable<FieldT> commitment(pb, 256, "commitment");
166 digest_variable<FieldT> root(pb, 256, "root");
167 positions.allocate(pb, INCREMENTAL_MERKLE_TREE_DEPTH_TESTING, "pos");
168 merkle_authentication_path_variable<FieldT, sha256_two_to_one_hash_gadget<FieldT>> authvars(pb, INCREMENTAL_MERKLE_TREE_DEPTH_TESTING, "auth");
169 merkle_tree_check_read_gadget<FieldT, sha256_two_to_one_hash_gadget<FieldT>> auth(
170 pb, INCREMENTAL_MERKLE_TREE_DEPTH_TESTING, positions, commitment, root, authvars, ONE, "path"
172 commitment.generate_r1cs_constraints();
173 root.generate_r1cs_constraints();
174 authvars.generate_r1cs_constraints();
175 auth.generate_r1cs_constraints();
177 std::vector<bool> commitment_bv;
179 std::vector<unsigned char> commitment_v(test_commitment.begin(), test_commitment.end());
180 commitment_bv = convertBytesVectorToVector(commitment_v);
183 size_t path_index = convertVectorToInt(path.index);
185 commitment.bits.fill_with_bits(pb, bit_vector(commitment_bv));
186 positions.fill_with_bits_of_ulong(pb, path_index);
188 authvars.generate_r1cs_witness(path_index, path.authentication_path);
189 auth.generate_r1cs_witness();
191 std::vector<bool> root_bv;
193 uint256 witroot = wit.root();
194 std::vector<unsigned char> root_v(witroot.begin(), witroot.end());
195 root_bv = convertBytesVectorToVector(root_v);
198 root.bits.fill_with_bits(pb, bit_vector(root_bv));
200 ASSERT_TRUE(pb.is_satisfied());
202 root_bv[0] = !root_bv[0];
203 root.bits.fill_with_bits(pb, bit_vector(root_bv));
205 ASSERT_TRUE(!pb.is_satisfied());
209 // Check witness serialization
210 expect_ser_test_vector(witness_ser_iterator, wit, tree);
212 ASSERT_TRUE(wit.root() == tree.root());
219 // Tree should be full now
220 ASSERT_THROW(tree.append(uint256()), std::runtime_error);
222 BOOST_FOREACH(Witness& wit, witnesses)
224 ASSERT_THROW(wit.append(uint256()), std::runtime_error);
229 TEST(merkletree, vectors) {
230 libsnark::default_r1cs_ppzksnark_pp::init_public_params();
232 Array root_tests = read_json(std::string(json_tests::merkle_roots, json_tests::merkle_roots + sizeof(json_tests::merkle_roots)));
233 Array ser_tests = read_json(std::string(json_tests::merkle_serialization, json_tests::merkle_serialization + sizeof(json_tests::merkle_serialization)));
234 Array witness_ser_tests = read_json(std::string(json_tests::merkle_witness_serialization, json_tests::merkle_witness_serialization + sizeof(json_tests::merkle_witness_serialization)));
235 Array path_tests = read_json(std::string(json_tests::merkle_path, json_tests::merkle_path + sizeof(json_tests::merkle_path)));
237 test_tree<ZCTestingIncrementalMerkleTree, ZCTestingIncrementalWitness>(root_tests, ser_tests, witness_ser_tests, path_tests);
240 TEST(merkletree, emptyroots) {
241 Array empty_roots = read_json(std::string(json_tests::merkle_roots_empty, json_tests::merkle_roots_empty + sizeof(json_tests::merkle_roots_empty)));
242 Array::iterator root_iterator = empty_roots.begin();
244 libzcash::EmptyMerkleRoots<64, libzcash::SHA256Compress> emptyroots;
246 for (size_t depth = 0; depth <= 64; depth++) {
247 expect_test_vector(root_iterator, emptyroots.empty_root(depth));
250 // Double check that we're testing (at least) all the empty roots we'll use.
251 ASSERT_TRUE(INCREMENTAL_MERKLE_TREE_DEPTH <= 64);
254 TEST(merkletree, emptyroot) {
255 // This literal is the depth-20 empty tree root with the bytes reversed to
256 // account for the fact that uint256S() loads a big-endian representation of
257 // an integer which converted to little-endian internally.
258 uint256 expected = uint256S("59d2cde5e65c1414c32ba54f0fe4bdb3d67618125286e6a191317917c812c6d7");
260 ASSERT_TRUE(ZCIncrementalMerkleTree::empty_root() == expected);
263 TEST(merkletree, deserializeInvalid) {
264 // attempt to deserialize a small tree from a serialized large tree
265 // (exceeds depth well-formedness check)
266 ZCIncrementalMerkleTree newTree;
268 for (size_t i = 0; i < 16; i++) {
269 newTree.append(uint256S("54d626e08c1c802b305dad30b7e54a82f102390cc92c7d4db112048935236e9c"));
272 newTree.append(uint256S("54d626e08c1c802b305dad30b7e54a82f102390cc92c7d4db112048935236e9c"));
274 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
277 ZCTestingIncrementalMerkleTree newTreeSmall;
278 ASSERT_THROW({ss >> newTreeSmall;}, std::ios_base::failure);
281 TEST(merkletree, deserializeInvalid2) {
282 // the most ancestral parent is empty
284 ParseHex("0155b852781b9995a44c939b64e441ae2724b96f99c8f4fb9a141cfc9842c4b0e3000100"),
289 ZCIncrementalMerkleTree tree;
290 ASSERT_THROW(ss >> tree, std::ios_base::failure);
293 TEST(merkletree, deserializeInvalid3) {
294 // left doesn't exist but right does
296 ParseHex("000155b852781b9995a44c939b64e441ae2724b96f99c8f4fb9a141cfc9842c4b0e300"),
301 ZCIncrementalMerkleTree tree;
302 ASSERT_THROW(ss >> tree, std::ios_base::failure);
305 TEST(merkletree, deserializeInvalid4) {
306 // left doesn't exist but a parent does
308 ParseHex("000001018695873d63ec0bceeadb5bf4ccc6723ac803c1826fc7cfb34fc76180305ae27d"),
313 ZCIncrementalMerkleTree tree;
314 ASSERT_THROW(ss >> tree, std::ios_base::failure);
317 TEST(merkletree, testZeroElements) {
318 for (int start = 0; start < 20; start++) {
319 ZCIncrementalMerkleTree newTree;
321 ASSERT_TRUE(newTree.root() == ZCIncrementalMerkleTree::empty_root());
323 for (int i = start; i > 0; i--) {
324 newTree.append(uint256S("54d626e08c1c802b305dad30b7e54a82f102390cc92c7d4db112048935236e9c"));
327 uint256 oldroot = newTree.root();
329 // At this point, appending tons of null objects to the tree
330 // should preserve its root.
332 for (int i = 0; i < 100; i++) {
333 newTree.append(uint256());
336 ASSERT_TRUE(newTree.root() == oldroot);