1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/cert/merkle_audit_proof.h"
6
7 #include "base/check_op.h"
8
9 namespace net::ct {
10
CalculateAuditPathLength(uint64_t leaf_index,uint64_t tree_size)11 uint64_t CalculateAuditPathLength(uint64_t leaf_index, uint64_t tree_size) {
12 // RFC6962, section 2.1.1, describes audit paths.
13 // Algorithm taken from
14 // https://github.com/google/certificate-transparency-rfcs/blob/c8844de6bd0b5d3d16bac79865e6edef533d760b/dns/draft-ct-over-dns.md#retrieve-merkle-audit-proof-from-log-by-leaf-hash.
15 CHECK_LT(leaf_index, tree_size);
16 uint64_t length = 0;
17 uint64_t index = leaf_index;
18 uint64_t last_node = tree_size - 1;
19
20 while (last_node != 0) {
21 if ((index % 2 != 0) || index != last_node)
22 ++length;
23 index /= 2;
24 last_node /= 2;
25 }
26
27 return length;
28 }
29
30 MerkleAuditProof::MerkleAuditProof() = default;
31
32 MerkleAuditProof::MerkleAuditProof(const MerkleAuditProof& other) = default;
33
MerkleAuditProof(uint64_t leaf_index,uint64_t tree_size,const std::vector<std::string> & audit_path)34 MerkleAuditProof::MerkleAuditProof(uint64_t leaf_index,
35 uint64_t tree_size,
36 const std::vector<std::string>& audit_path)
37 : leaf_index(leaf_index), tree_size(tree_size), nodes(audit_path) {}
38
39 MerkleAuditProof::~MerkleAuditProof() = default;
40
41 } // namespace net::ct
42