1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // A Tile is a description of a transparency log tile.
14 // A tile of height H at level L offset N lists W consecutive hashes
15 // at level H*L of the tree starting at offset N*(2**H).
16 // A complete tile lists 2**H hashes; a partial tile lists fewer.
17 // Note that a tile represents the entire subtree of height H
18 // with those hashes as the leaves. The levels above H*L
19 // can be reconstructed by hashing the leaves.
21 // Each Tile can be encoded as a “tile coordinate path”
22 // of the form tile/H/L/NNN[.p/W].
23 // The .p/W suffix is present only for partial tiles, meaning W < 2**H.
24 // The NNN element is an encoding of N into 3-digit path elements.
25 // All but the last path element begins with an "x".
27 // Tile{H: 3, L: 4, N: 1234067, W: 1}'s path
28 // is tile/3/4/x001/x234/067.p/1, and
29 // Tile{H: 3, L: 4, N: 1234067, W: 8}'s path
30 // is tile/3/4/x001/x234/067.
31 // See Tile's Path method and the ParseTilePath function.
33 // The special level L=-1 holds raw record data instead of hashes.
34 // In this case, the level encodes into a tile path as the path element
35 // "data" instead of "-1".
37 // See also https://golang.org/design/25530-sumdb#checksum-database
38 // and https://research.swtch.com/tlog#tiling_a_log.
40 H int // height of tile (1 ≤ H ≤ 30)
41 L int // level in tiling (-1 ≤ L ≤ 63)
42 N int64 // number within level (0 ≤ N, unbounded)
43 W int // width of tile (1 ≤ W ≤ 2**H; 2**H is complete tile)
46 // TileForIndex returns the tile of fixed height h ≥ 1
47 // and least width storing the given hash storage index.
49 // If h ≤ 0, TileForIndex panics.
50 func TileForIndex(h int, index int64) Tile {
52 panic(fmt.Sprintf("TileForIndex: invalid height %d", h))
54 t, _, _ := tileForIndex(h, index)
58 // tileForIndex returns the tile of height h ≥ 1
59 // storing the given hash index, which can be
60 // reconstructed using tileHash(data[start:end]).
61 func tileForIndex(h int, index int64) (t Tile, start, end int) {
62 level, n := SplitStoredHashIndex(index)
65 level -= t.L * h // now level within tile
66 t.N = n << uint(level) >> uint(t.H)
67 n -= t.N << uint(t.H) >> uint(level) // now n within tile at level
68 t.W = int((n + 1) << uint(level))
69 return t, int(n<<uint(level)) * HashSize, int((n+1)<<uint(level)) * HashSize
72 // HashFromTile returns the hash at the given storage index,
73 // provided that t == TileForIndex(t.H, index) or a wider version,
74 // and data is t's tile data (of length at least t.W*HashSize).
75 func HashFromTile(t Tile, data []byte, index int64) (Hash, error) {
76 if t.H < 1 || t.H > 30 || t.L < 0 || t.L >= 64 || t.W < 1 || t.W > 1<<uint(t.H) {
77 return Hash{}, fmt.Errorf("invalid tile %v", t.Path())
79 if len(data) < t.W*HashSize {
80 return Hash{}, fmt.Errorf("data len %d too short for tile %v", len(data), t.Path())
82 t1, start, end := tileForIndex(t.H, index)
83 if t.L != t1.L || t.N != t1.N || t.W < t1.W {
84 return Hash{}, fmt.Errorf("index %v is in %v not %v", index, t1.Path(), t.Path())
86 return tileHash(data[start:end]), nil
89 // tileHash computes the subtree hash corresponding to the (2^K)-1 hashes in data.
90 func tileHash(data []byte) Hash {
92 panic("bad math in tileHash")
94 if len(data) == HashSize {
100 return NodeHash(tileHash(data[:n]), tileHash(data[n:]))
103 // NewTiles returns the coordinates of the tiles of height h ≥ 1
104 // that must be published when publishing from a tree of
105 // size newTreeSize to replace a tree of size oldTreeSize.
106 // (No tiles need to be published for a tree of size zero.)
108 // If h ≤ 0, TileForIndex panics.
109 func NewTiles(h int, oldTreeSize, newTreeSize int64) []Tile {
111 panic(fmt.Sprintf("NewTiles: invalid height %d", h))
115 for level := uint(0); newTreeSize>>(H*level) > 0; level++ {
116 oldN := oldTreeSize >> (H * level)
117 newN := newTreeSize >> (H * level)
118 for n := oldN >> H; n < newN>>H; n++ {
119 tiles = append(tiles, Tile{H: h, L: int(level), N: n, W: 1 << H})
122 maxW := int(newN - n<<H)
125 minW = int(oldN - n<<H)
127 for w := minW; w <= maxW; w++ {
128 tiles = append(tiles, Tile{H: h, L: int(level), N: n, W: w})
134 // ReadTileData reads the hashes for tile t from r
135 // and returns the corresponding tile data.
136 func ReadTileData(t Tile, r HashReader) ([]byte, error) {
139 size = 1 << uint(t.H)
141 start := t.N << uint(t.H)
142 indexes := make([]int64, size)
143 for i := 0; i < size; i++ {
144 indexes[i] = StoredHashIndex(t.H*t.L, start+int64(i))
147 hashes, err := r.ReadHashes(indexes)
151 if len(hashes) != len(indexes) {
152 return nil, fmt.Errorf("tlog: ReadHashes(%d indexes) = %d hashes", len(indexes), len(hashes))
155 tile := make([]byte, size*HashSize)
156 for i := 0; i < size; i++ {
157 copy(tile[i*HashSize:], hashes[i][:])
162 // To limit the size of any particular directory listing,
163 // we encode the (possibly very large) number N
164 // by encoding three digits at a time.
165 // For example, 123456789 encodes as x123/x456/789.
166 // Each directory has at most 1000 each xNNN, NNN, and NNN.p children,
167 // so there are at most 3000 entries in any one directory.
168 const pathBase = 1000
170 // Path returns a tile coordinate path describing t.
171 func (t Tile) Path() string {
173 nStr := fmt.Sprintf("%03d", n%pathBase)
176 nStr = fmt.Sprintf("x%03d/%s", n%pathBase, nStr)
179 if t.W != 1<<uint(t.H) {
180 pStr = fmt.Sprintf(".p/%d", t.W)
186 L = fmt.Sprintf("%d", t.L)
188 return fmt.Sprintf("tile/%d/%s/%s%s", t.H, L, nStr, pStr)
191 // ParseTilePath parses a tile coordinate path.
192 func ParseTilePath(path string) (Tile, error) {
193 f := strings.Split(path, "/")
194 if len(f) < 4 || f[0] != "tile" {
195 return Tile{}, &badPathError{path}
197 h, err1 := strconv.Atoi(f[1])
203 l, err2 := strconv.Atoi(f[2])
204 if err1 != nil || err2 != nil || h < 1 || l < 0 || h > 30 {
205 return Tile{}, &badPathError{path}
208 if dotP := f[len(f)-2]; strings.HasSuffix(dotP, ".p") {
209 ww, err := strconv.Atoi(f[len(f)-1])
210 if err != nil || ww <= 0 || ww >= w {
211 return Tile{}, &badPathError{path}
214 f[len(f)-2] = dotP[:len(dotP)-len(".p")]
219 for _, s := range f {
220 nn, err := strconv.Atoi(strings.TrimPrefix(s, "x"))
221 if err != nil || nn < 0 || nn >= pathBase {
222 return Tile{}, &badPathError{path}
224 n = n*pathBase + int64(nn)
229 t := Tile{H: h, L: l, N: n, W: w}
230 if path != t.Path() {
231 return Tile{}, &badPathError{path}
236 type badPathError struct {
240 func (e *badPathError) Error() string {
241 return fmt.Sprintf("malformed tile path %q", e.path)
244 // A TileReader reads tiles from a go.sum database log.
245 type TileReader interface {
246 // Height returns the height of the available tiles.
249 // ReadTiles returns the data for each requested tile.
250 // If ReadTiles returns err == nil, it must also return
251 // a data record for each tile (len(data) == len(tiles))
252 // and each data record must be the correct length
253 // (len(data[i]) == tiles[i].W*HashSize).
255 // An implementation of ReadTiles typically reads
256 // them from an on-disk cache or else from a remote
257 // tile server. Tile data downloaded from a server should
258 // be considered suspect and not saved into a persistent
259 // on-disk cache before returning from ReadTiles.
260 // When the client confirms the validity of the tile data,
261 // it will call SaveTiles to signal that they can be safely
262 // written to persistent storage.
263 // See also https://research.swtch.com/tlog#authenticating_tiles.
264 ReadTiles(tiles []Tile) (data [][]byte, err error)
266 // SaveTiles informs the TileReader that the tile data
267 // returned by ReadTiles has been confirmed as valid
268 // and can be saved in persistent storage (on disk).
269 SaveTiles(tiles []Tile, data [][]byte)
272 // TileHashReader returns a HashReader that satisfies requests
273 // by loading tiles of the given tree.
275 // The returned HashReader checks that loaded tiles are
276 // valid for the given tree. Therefore, any hashes returned
277 // by the HashReader are already proven to be in the tree.
278 func TileHashReader(tree Tree, tr TileReader) HashReader {
279 return &tileHashReader{tree: tree, tr: tr}
282 type tileHashReader struct {
287 // tileParent returns t's k'th tile parent in the tiles for a tree of size n.
288 // If there is no such parent, tileParent returns Tile{}.
289 func tileParent(t Tile, k int, n int64) Tile {
291 t.N >>= uint(k * t.H)
293 if max := n >> uint(t.L*t.H); t.N<<uint(t.H)+int64(t.W) >= max {
294 if t.N<<uint(t.H) >= max {
297 t.W = int(max - t.N<<uint(t.H))
302 func (r *tileHashReader) ReadHashes(indexes []int64) ([]Hash, error) {
305 tileOrder := make(map[Tile]int) // tileOrder[tileKey(tiles[i])] = i
308 // Plan to fetch tiles necessary to recompute tree hash.
309 // If it matches, those tiles are authenticated.
310 stx := subTreeIndex(0, r.tree.N, nil)
311 stxTileOrder := make([]int, len(stx))
312 for i, x := range stx {
313 tile, _, _ := tileForIndex(h, x)
314 tile = tileParent(tile, 0, r.tree.N)
315 if j, ok := tileOrder[tile]; ok {
319 stxTileOrder[i] = len(tiles)
320 tileOrder[tile] = len(tiles)
321 tiles = append(tiles, tile)
324 // Plan to fetch tiles containing the indexes,
325 // along with any parent tiles needed
326 // for authentication. For most calls,
327 // the parents are being fetched anyway.
328 indexTileOrder := make([]int, len(indexes))
329 for i, x := range indexes {
330 if x >= StoredHashIndex(0, r.tree.N) {
331 return nil, fmt.Errorf("indexes not in tree")
334 tile, _, _ := tileForIndex(h, x)
336 // Walk up parent tiles until we find one we've requested.
337 // That one will be authenticated.
340 p := tileParent(tile, k, r.tree.N)
341 if j, ok := tileOrder[p]; ok {
343 indexTileOrder[i] = j
349 // Walk back down recording child tiles after parents.
350 // This loop ends by revisiting the tile for this index
351 // (tileParent(tile, 0, r.tree.N)) unless k == 0, in which
352 // case the previous loop did it.
353 for k--; k >= 0; k-- {
354 p := tileParent(tile, k, r.tree.N)
355 if p.W != 1<<uint(p.H) {
356 // Only full tiles have parents.
357 // This tile has a parent, so it must be full.
358 return nil, fmt.Errorf("bad math in tileHashReader: %d %d %v", r.tree.N, x, p)
360 tileOrder[p] = len(tiles)
362 indexTileOrder[i] = len(tiles)
364 tiles = append(tiles, p)
368 // Fetch all the tile data.
369 data, err := r.tr.ReadTiles(tiles)
373 if len(data) != len(tiles) {
374 return nil, fmt.Errorf("TileReader returned bad result slice (len=%d, want %d)", len(data), len(tiles))
376 for i, tile := range tiles {
377 if len(data[i]) != tile.W*HashSize {
378 return nil, fmt.Errorf("TileReader returned bad result slice (%v len=%d, want %d)", tile.Path(), len(data[i]), tile.W*HashSize)
382 // Authenticate the initial tiles against the tree hash.
383 // They are arranged so that parents are authenticated before children.
384 // First the tiles needed for the tree hash.
385 th, err := HashFromTile(tiles[stxTileOrder[len(stx)-1]], data[stxTileOrder[len(stx)-1]], stx[len(stx)-1])
389 for i := len(stx) - 2; i >= 0; i-- {
390 h, err := HashFromTile(tiles[stxTileOrder[i]], data[stxTileOrder[i]], stx[i])
396 if th != r.tree.Hash {
397 // The tiles do not support the tree hash.
398 // We know at least one is wrong, but not which one.
399 return nil, fmt.Errorf("downloaded inconsistent tile")
402 // Authenticate full tiles against their parents.
403 for i := len(stx); i < len(tiles); i++ {
405 p := tileParent(tile, 1, r.tree.N)
406 j, ok := tileOrder[p]
408 return nil, fmt.Errorf("bad math in tileHashReader %d %v: lost parent of %v", r.tree.N, indexes, tile)
410 h, err := HashFromTile(p, data[j], StoredHashIndex(p.L*p.H, tile.N))
412 return nil, fmt.Errorf("bad math in tileHashReader %d %v: lost hash of %v: %v", r.tree.N, indexes, tile, err)
414 if h != tileHash(data[i]) {
415 return nil, fmt.Errorf("downloaded inconsistent tile")
419 // Now we have all the tiles needed for the requested hashes,
420 // and we've authenticated the full tile set against the trusted tree hash.
421 r.tr.SaveTiles(tiles, data)
423 // Pull out the requested hashes.
424 hashes := make([]Hash, len(indexes))
425 for i, x := range indexes {
426 j := indexTileOrder[i]
427 h, err := HashFromTile(tiles[j], data[j], x)
429 return nil, fmt.Errorf("bad math in tileHashReader %d %v: lost hash %v: %v", r.tree.N, indexes, x, err)