| // Copyright 2026 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // https://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| // | |
| #ifndef THIRD_PARTY_CREDENTIO_BINDINGS_MERKLE_H_ | |
| #define THIRD_PARTY_CREDENTIO_BINDINGS_MERKLE_H_ | |
| #include <cstdint> | |
| #include <optional> | |
| #include <string> | |
| #include <vector> | |
| #include "absl/status/statusor.h" | |
| #include "crypto/hash.h" | |
| #include "proto/bmff_based_hash_assertion.pb.h" | |
| #include "riegeli/bytes/reader.h" | |
| namespace credentio { | |
| struct DerivedTreeData { | |
| int64_t full_leaf_count; | |
| int64_t full_hashes_count; | |
| int64_t delta_rows; | |
| bool operator==(const DerivedTreeData& other) const { | |
| return full_leaf_count == other.full_leaf_count && | |
| full_hashes_count == other.full_hashes_count && | |
| delta_rows == other.delta_rows; | |
| } | |
| }; | |
| // Derives the data needed to validate a Merkle Tree from the leaf count, hashes | |
| // count, and auxiliary data count. | |
| absl::StatusOr<DerivedTreeData> DeriveMerkleTreeData(int64_t leaf_count, | |
| int64_t hashes_count, | |
| int64_t auxiliary_count); | |
| // Derives the block sizes for a given Merkle map and total leaf data size. | |
| absl::StatusOr<std::vector<int64_t>> DeriveMerkleBlockSizes( | |
| const BmffMerkle& merkle, uint64_t mdat_box_size); | |
| using MerkleTree = std::vector<std::vector<std::string>>; | |
| // Options for creating a Merkle Tree. | |
| // - `fixed_block_size`: blocks of equal sizes | |
| // - `variable_block_sizes`: blocks of the specified sizes | |
| // - neither: the entire input string will treated as a single block | |
| // - both: then an error will be returned | |
| struct CreateMerkleTreeOptions { | |
| std::optional<int64_t> fixed_block_size = std::nullopt; | |
| std::vector<int64_t> variable_block_sizes = {}; | |
| }; | |
| // Creates a Merkle Tree from a riegeli::Reader with the options indicating | |
| // how the input should be broken into blocks. | |
| absl::StatusOr<MerkleTree> CreateMerkleTree( | |
| HasherFactory* factory, riegeli::Reader& input, int64_t starting_offset, | |
| int64_t length, CreateMerkleTreeOptions options = {}); | |
| // Creates a Merkle Tree from a vector of strings, each which represent a single | |
| // leaf node. | |
| absl::StatusOr<MerkleTree> CreateMerkleTree(HasherFactory* factory, | |
| riegeli::Reader& input, | |
| int64_t starting_offset, | |
| int64_t length, | |
| std::vector<int64_t> block_sizes); | |
| } // namespace credentio | |
| #endif // THIRD_PARTY_CREDENTIO_BINDINGS_MERKLE_H_ |