示例#1
0
    def _check_merkle_path(self, address: int, mkpath: List[str],
                           mktree: MerkleTree) -> None:
        self.assertEqual(len(mkpath), mktree.depth)
        current = mktree.get_node(0, address)
        for i in range(mktree.depth):
            if address & 1:
                current = MerkleTree.combine(bytes.fromhex(mkpath[i]), current)
            else:
                current = MerkleTree.combine(current, bytes.fromhex(mkpath[i]))
            address = address >> 1

        self.assertEqual(mktree.get_root(), current)
示例#2
0
    def test_combine(self) -> None:
        # Use test vectors used to test the MiMC contract (generated in
        # test_mimc.py)

        left = self._test_vector_to_bytes32(
            3703141493535563179657531719960160174296085208671919316200479060314459804651
        )  # noqa
        right = self._test_vector_to_bytes32(
            15683951496311901749339509118960676303290224812129752890706581988986633412003
        )  # noqa
        expect = self._test_vector_to_bytes32(
            16797922449555994684063104214233396200599693715764605878168345782964540311877
        )  # noqa

        result = MerkleTree.combine(left, right)
        self.assertEqual(expect, result)
示例#3
0
    def _check_tree_nodes(self, leaves: List[bytes],
                          mktree: MerkleTree) -> None:
        def layer_size(layer: int) -> int:
            return int(MERKLE_TREE_TEST_NUM_LEAVES / pow(2, layer))

        # Check layer 0
        _, layer_0 = next(mktree.get_layers())
        self.assertEqual(leaves, layer_0)

        # Check layer `layer`
        for layer in range(1, MERKLE_TREE_TEST_DEPTH):
            for i in range(layer_size(layer)):
                self.assertEqual(
                    MerkleTree.combine(mktree.get_node(layer - 1, 2 * i),
                                       mktree.get_node(layer - 1, 2 * i + 1)),
                    mktree.get_node(layer, i), f"Layer {layer}, node {i}")
示例#4
0
    def test_single_entry(self) -> None:
        mktree_file = join(MERKLE_TREE_TEST_DIR, "single")
        data = TEST_VALUES[0]

        mktree = PersistentMerkleTree.open(mktree_file,
                                           MERKLE_TREE_TEST_NUM_LEAVES)
        mktree.insert(data)
        self.assertEqual(1, mktree.get_num_entries())
        self.assertEqual(data, mktree.get_leaf(0))
        self.assertEqual(ZERO_ENTRY, mktree.get_leaf(1))
        root_1 = mktree.recompute_root()
        self.assertEqual(MerkleTree.combine(data, ZERO_ENTRY),
                         mktree.get_node(1, 0))
        self.assertNotEqual(self._expected_empty(), root_1)
        mktree.save()

        mktree = PersistentMerkleTree.open(mktree_file,
                                           MERKLE_TREE_TEST_NUM_LEAVES)
        self.assertEqual(1, mktree.get_num_entries())
        self.assertEqual(data, mktree.get_leaf(0))
        self.assertEqual(ZERO_ENTRY, mktree.get_leaf(1))
        root_2 = mktree.recompute_root()
        self.assertEqual(root_1, root_2)