|
| 1 | +// Copyright 2022 Google LLC. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package merkle |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/transparency-dev/merkle/compact" |
| 21 | + "github.com/transparency-dev/merkle/proof" |
| 22 | +) |
| 23 | + |
| 24 | +type HashGetter interface { |
| 25 | + GetConsistencyProof(first, second uint64) ([][]byte, error) |
| 26 | + GetLeafHashes(begin, end uint64) ([][]byte, error) |
| 27 | +} |
| 28 | + |
| 29 | +func GetCompactRange(rf *compact.RangeFactory, begin, end, size uint64, hg HashGetter) (*compact.Range, error) { |
| 30 | + if begin > size || end > size { |
| 31 | + return nil, fmt.Errorf("[%d, %d) out of range in %d", begin, end, size) |
| 32 | + } |
| 33 | + if begin >= end { |
| 34 | + return rf.NewEmptyRange(begin), nil |
| 35 | + } |
| 36 | + |
| 37 | + if size <= 3 || end == 1 { |
| 38 | + hashes, err := hg.GetLeafHashes(begin, end) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("GetLeafHashes(%d, %d): %v", begin, end, err) |
| 41 | + } |
| 42 | + if got, want := uint64(len(hashes)), end-begin; got != want { |
| 43 | + return nil, fmt.Errorf("GetLeafHashes(%d, %d): %d hashes, want %d", begin, end, got, want) |
| 44 | + } |
| 45 | + r := rf.NewEmptyRange(begin) |
| 46 | + for _, h := range hashes { |
| 47 | + if err := r.Append(h, nil); err != nil { |
| 48 | + return nil, fmt.Errorf("Append: %v", err) |
| 49 | + } |
| 50 | + } |
| 51 | + return r, nil |
| 52 | + } |
| 53 | + // size >= 4 && end >= 2 |
| 54 | + |
| 55 | + known := make(map[compact.NodeID][]byte) |
| 56 | + |
| 57 | + store := func(nodes proof.Nodes, hashes [][]byte) error { |
| 58 | + _, b, e := nodes.Ephem() |
| 59 | + wantSize := len(nodes.IDs) - (e - b) |
| 60 | + if b != e { |
| 61 | + wantSize++ |
| 62 | + } |
| 63 | + if got := len(hashes); got != wantSize { |
| 64 | + return fmt.Errorf("proof size mismatch: got %d, want %d", got, wantSize) |
| 65 | + } |
| 66 | + |
| 67 | + idx := 0 |
| 68 | + for _, hash := range hashes { |
| 69 | + if idx == b && b+1 < e { |
| 70 | + idx = e - 1 |
| 71 | + continue |
| 72 | + } |
| 73 | + known[nodes.IDs[idx]] = hash |
| 74 | + idx++ |
| 75 | + } |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + newRange := func(begin, end uint64) (*compact.Range, error) { |
| 80 | + size := compact.RangeSize(begin, end) |
| 81 | + ids := compact.RangeNodes(begin, end, make([]compact.NodeID, 0, size)) |
| 82 | + hashes := make([][]byte, 0, len(ids)) |
| 83 | + for _, id := range ids { |
| 84 | + if hash, ok := known[id]; ok { |
| 85 | + hashes = append(hashes, hash) |
| 86 | + } else { |
| 87 | + return nil, fmt.Errorf("hash not known: %+v", id) |
| 88 | + } |
| 89 | + } |
| 90 | + return rf.NewRange(begin, end, hashes) |
| 91 | + } |
| 92 | + |
| 93 | + fetch := func(first, second uint64) error { |
| 94 | + nodes, err := proof.Consistency(first, second) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("proof.Consistency: %v", err) |
| 97 | + } |
| 98 | + hashes, err := hg.GetConsistencyProof(first, second) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("GetConsistencyProof(%d, %d): %v", first, second, err) |
| 101 | + } |
| 102 | + store(nodes, hashes) |
| 103 | + return nil |
| 104 | + } |
| 105 | + |
| 106 | + mid, _ := compact.Decompose(begin, end) |
| 107 | + mid += begin |
| 108 | + if err := fetch(begin, mid); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + if begin == 0 && end == 2 || end == 3 { |
| 113 | + if err := fetch(3, 4); err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + } |
| 117 | + if end <= 3 { |
| 118 | + return newRange(begin, end) |
| 119 | + } |
| 120 | + // end >= 4 |
| 121 | + |
| 122 | + if (end-1)&(end-2) != 0 { // end-1 is not a power of 2. |
| 123 | + if err := fetch(end-1, end); err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + r, err := newRange(begin, end-1) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + if err := r.Append(known[compact.NewNodeID(0, end-1)], nil); err != nil { |
| 131 | + return nil, fmt.Errorf("Append: %v", err) |
| 132 | + } |
| 133 | + return r, nil |
| 134 | + } |
| 135 | + |
| 136 | + // At this point: end >= 4, end-1 is a power of 2; thus, end-2 is not a power of 2. |
| 137 | + if err := fetch(end-2, end); err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + r := rf.NewEmptyRange(begin) |
| 141 | + if end-2 > begin { |
| 142 | + var err error |
| 143 | + if r, err = newRange(begin, end-2); err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + } |
| 147 | + for index := r.End(); index < end; index++ { |
| 148 | + if err := r.Append(known[compact.NewNodeID(0, index)], nil); err != nil { |
| 149 | + return nil, fmt.Errorf("Append: %v", err) |
| 150 | + } |
| 151 | + } |
| 152 | + return r, nil |
| 153 | +} |
0 commit comments