bool
    IncrementalMerkleNode::fromCompactRepresentation(IncrementalMerkleTreeCompact &rep, uint32_t pos)
    {
        bool result = false;

        // Do nothing at the bottom level
        if (this->isLeaf()) {
            return true;
        }

        // If we have any subtrees (or this tree already has stuff in it), clean it out.
        if (this->left) {
            delete this->left;
            this->left = NULL;
        }
        if (this->right) {
            delete this->right;
            this->right = NULL;
        }
        this->subtreeFull = this->subtreePruned = false;

        // If the hashList[nodeDepth] is true, insert the next hash into the left tree
        // and mark it full AND pruned. Then recurse to the right.
        if (rep.hashList.at(this->nodeDepth) == true) {
			// Create a left node
			this->left = new IncrementalMerkleNode(this->nodeDepth + 1, this->treeHeight);

            // Fill the left node with the value and mark it full/pruned
            std::vector<bool> hash(SHA256_BLOCK_SIZE * 8, 0);
            convertBytesVectorToVector(rep.hashVec.at(pos), hash);
            this->left->value = hash;
            this->left->subtreePruned = this->left->subtreeFull = true;

            // Create a right node and recurse on it (incrementing pos)
            this->right = new IncrementalMerkleNode(this->nodeDepth + 1, this->treeHeight);
            result = this->right->fromCompactRepresentation(rep, pos + 1);
        } else if (this->nodeDepth < (this->treeHeight - 1)) {
			// Otherwise --
			// * If we're about to create a leaf level, do nothing.
			// * Else create a left node and recurse on it.
			this->left = new IncrementalMerkleNode(this->nodeDepth + 1, this->treeHeight);

            // Otherwise recurse on the left node. Do not increment pos.
            result = this->left->fromCompactRepresentation(rep, pos);
        }

        // Update the hash value of this node
        this->updateHashValue();

        return result;
    }
Пример #2
0
MerklePath IncrementalMerkleTree<Depth, Hash>::path(std::deque<Hash> filler_hashes) const {
    if (!left) {
        throw std::runtime_error("can't create an authentication path for the beginning of the tree");
    }

    PathFiller<Depth, Hash> filler(filler_hashes);

    std::vector<Hash> path;
    std::vector<bool> index;

    if (right) {
        index.push_back(true);
        path.push_back(*left);
    } else {
        index.push_back(false);
        path.push_back(filler.next(0));
    }

    size_t d = 1;

    BOOST_FOREACH(const boost::optional<Hash>& parent, parents) {
        if (parent) {
            index.push_back(true);
            path.push_back(*parent);
        } else {
            index.push_back(false);
            path.push_back(filler.next(d));
        }

        d++;
    }

    while (d < Depth) {
        index.push_back(false);
        path.push_back(filler.next(d));
        d++;
    }

    std::vector<std::vector<bool>> merkle_path;
    BOOST_FOREACH(Hash b, path)
    {
        std::vector<unsigned char> hashv(b.begin(), b.end());

        merkle_path.push_back(convertBytesVectorToVector(hashv));
    }
	bool
    IncrementalMerkleTree::insertElement(const std::vector<unsigned char> &hashV, std::vector<unsigned char> &index) {

		// Create a temporary vector to hold hashV
		std::vector<bool> hashVBool(hashV.size() * 8);
		convertBytesVectorToVector(hashV, hashVBool);

        // Create a temporary vector to hold the index
		std::vector<bool> indexBool(this->treeHeight, 0);

        // Insert the element
        bool result = this->insertElement(hashVBool, indexBool);

		// Convert the returned vector
		index.resize(index.size() / 8); // this might need to include a ceil
		convertVectorToBytesVector(indexBool, index);

		return result;
    }