| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package tlog |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto/sha256" |
| 18 |
"encoding/base64" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"math/bits" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
type Hash [HashSize]byte |
| 26 |
|
| 27 |
|
| 28 |
const HashSize = 32 |
| 29 |
|
| 30 |
|
| 31 |
func (h Hash) String() string { |
| 32 |
return base64.StdEncoding.EncodeToString(h[:]) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func (h Hash) MarshalJSON() ([]byte, error) { |
| 37 |
return []byte(`"` + h.String() + `"`), nil |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func (h *Hash) UnmarshalJSON(data []byte) error { |
| 42 |
if len(data) != 1+44+1 || data[0] != '"' || data[len(data)-2] != '=' || data[len(data)-1] != '"' { |
| 43 |
return errors.New("cannot decode hash") |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
var tmp Hash |
| 54 |
n, err := base64.RawStdEncoding.Decode(tmp[:], data[1:len(data)-2]) |
| 55 |
if err != nil || n != HashSize { |
| 56 |
return errors.New("cannot decode hash") |
| 57 |
} |
| 58 |
*h = tmp |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func ParseHash(s string) (Hash, error) { |
| 64 |
data, err := base64.StdEncoding.DecodeString(s) |
| 65 |
if err != nil || len(data) != HashSize { |
| 66 |
return Hash{}, fmt.Errorf("malformed hash") |
| 67 |
} |
| 68 |
var h Hash |
| 69 |
copy(h[:], data) |
| 70 |
return h, nil |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
). |
| 75 |
func maxpow2(n int64) (k int64, l int) { |
| 76 |
l = 0 |
| 77 |
for 1<<uint(l+1) < n { |
| 78 |
l++ |
| 79 |
} |
| 80 |
return 1 << uint(l), l |
| 81 |
} |
| 82 |
|
| 83 |
var zeroPrefix = []byte{0x00} |
| 84 |
|
| 85 |
|
| 86 |
func RecordHash(data []byte) Hash { |
| 87 |
|
| 88 |
|
| 89 |
h := sha256.New() |
| 90 |
h.Write(zeroPrefix) |
| 91 |
h.Write(data) |
| 92 |
var h1 Hash |
| 93 |
h.Sum(h1[:0]) |
| 94 |
return h1 |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func NodeHash(left, right Hash) Hash { |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
var buf [1 + HashSize + HashSize]byte |
| 104 |
buf[0] = 0x01 |
| 105 |
copy(buf[1:], left[:]) |
| 106 |
copy(buf[1+HashSize:], right[:]) |
| 107 |
return sha256.Sum256(buf[:]) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
func StoredHashIndex(level int, n int64) int64 { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
for l := level; l > 0; l-- { |
| 125 |
n = 2*n + 1 |
| 126 |
} |
| 127 |
|
| 128 |
). |
| 129 |
i := int64(0) |
| 130 |
for ; n > 0; n >>= 1 { |
| 131 |
i += n |
| 132 |
} |
| 133 |
|
| 134 |
return i + int64(level) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
func SplitStoredHashIndex(index int64) (level int, n int64) { |
| 140 |
|
| 141 |
|
| 142 |
]. |
| 143 |
n = index / 2 |
| 144 |
indexN := StoredHashIndex(0, n) |
| 145 |
if indexN > index { |
| 146 |
panic("bad math") |
| 147 |
} |
| 148 |
for { |
| 149 |
|
| 150 |
x := indexN + 1 + int64(bits.TrailingZeros64(uint64(n+1))) |
| 151 |
if x > index { |
| 152 |
break |
| 153 |
} |
| 154 |
n++ |
| 155 |
indexN = x |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
level = int(index - indexN) |
| 160 |
return level, n >> uint(level) |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
func StoredHashCount(n int64) int64 { |
| 166 |
if n == 0 { |
| 167 |
return 0 |
| 168 |
} |
| 169 |
|
| 170 |
numHash := StoredHashIndex(0, n-1) + 1 |
| 171 |
|
| 172 |
for i := uint64(n - 1); i&1 != 0; i >>= 1 { |
| 173 |
numHash++ |
| 174 |
} |
| 175 |
return numHash |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
s, |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
func StoredHashes(n int64, data []byte, r HashReader) ([]Hash, error) { |
| 186 |
return StoredHashesForRecordHash(n, RecordHash(data), r) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func StoredHashesForRecordHash(n int64, h Hash, r HashReader) ([]Hash, error) { |
| 192 |
|
| 193 |
hashes := []Hash{h} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
m := int(bits.TrailingZeros64(uint64(n + 1))) |
| 199 |
indexes := make([]int64, m) |
| 200 |
for i := 0; i < m; i++ { |
| 201 |
|
| 202 |
|
| 203 |
indexes[m-1-i] = StoredHashIndex(i, n>>uint(i)-1) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
old, err := r.ReadHashes(indexes) |
| 208 |
if err != nil { |
| 209 |
return nil, err |
| 210 |
} |
| 211 |
if len(old) != len(indexes) { |
| 212 |
return nil, fmt.Errorf("tlog: ReadHashes(%d indexes) = %d hashes", len(indexes), len(old)) |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
for i := 0; i < m; i++ { |
| 217 |
h = NodeHash(old[m-1-i], h) |
| 218 |
hashes = append(hashes, h) |
| 219 |
} |
| 220 |
return hashes, nil |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
type HashReader interface { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
ReadHashes(indexes []int64) ([]Hash, error) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
type HashReaderFunc func([]int64) ([]Hash, error) |
| 235 |
|
| 236 |
func (f HashReaderFunc) ReadHashes(indexes []int64) ([]Hash, error) { |
| 237 |
return f(indexes) |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
s. |
| 244 |
|
| 245 |
func TreeHash(n int64, r HashReader) (Hash, error) { |
| 246 |
if n == 0 { |
| 247 |
return Hash{}, nil |
| 248 |
} |
| 249 |
indexes := subTreeIndex(0, n, nil) |
| 250 |
hashes, err := r.ReadHashes(indexes) |
| 251 |
if err != nil { |
| 252 |
return Hash{}, err |
| 253 |
} |
| 254 |
if len(hashes) != len(indexes) { |
| 255 |
return Hash{}, fmt.Errorf("tlog: ReadHashes(%d indexes) = %d hashes", len(indexes), len(hashes)) |
| 256 |
} |
| 257 |
hash, hashes := subTreeHash(0, n, hashes) |
| 258 |
if len(hashes) != 0 { |
| 259 |
panic("tlog: bad index math in TreeHash") |
| 260 |
} |
| 261 |
return hash, nil |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
func subTreeIndex(lo, hi int64, need []int64) []int64 { |
| 269 |
|
| 270 |
for lo < hi { |
| 271 |
k, level := maxpow2(hi - lo + 1) |
| 272 |
if lo&(k-1) != 0 { |
| 273 |
panic("tlog: bad math in subTreeIndex") |
| 274 |
} |
| 275 |
need = append(need, StoredHashIndex(level, lo>>uint(level))) |
| 276 |
lo += k |
| 277 |
} |
| 278 |
return need |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
func subTreeHash(lo, hi int64, hashes []Hash) (Hash, []Hash) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
numTree := 0 |
| 291 |
for lo < hi { |
| 292 |
k, _ := maxpow2(hi - lo + 1) |
| 293 |
if lo&(k-1) != 0 || lo >= hi { |
| 294 |
panic("tlog: bad math in subTreeHash") |
| 295 |
} |
| 296 |
numTree++ |
| 297 |
lo += k |
| 298 |
} |
| 299 |
|
| 300 |
if len(hashes) < numTree { |
| 301 |
panic("tlog: bad index math in subTreeHash") |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
h := hashes[numTree-1] |
| 306 |
for i := numTree - 2; i >= 0; i-- { |
| 307 |
h = NodeHash(hashes[i], h) |
| 308 |
} |
| 309 |
return h, hashes[numTree:] |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
.” |
| 314 |
type RecordProof []Hash |
| 315 |
|
| 316 |
|
| 317 |
func ProveRecord(t, n int64, r HashReader) (RecordProof, error) { |
| 318 |
if t < 0 || n < 0 || n >= t { |
| 319 |
return nil, fmt.Errorf("tlog: invalid inputs in ProveRecord") |
| 320 |
} |
| 321 |
indexes := leafProofIndex(0, t, n, nil) |
| 322 |
if len(indexes) == 0 { |
| 323 |
return RecordProof{}, nil |
| 324 |
} |
| 325 |
hashes, err := r.ReadHashes(indexes) |
| 326 |
if err != nil { |
| 327 |
return nil, err |
| 328 |
} |
| 329 |
if len(hashes) != len(indexes) { |
| 330 |
return nil, fmt.Errorf("tlog: ReadHashes(%d indexes) = %d hashes", len(indexes), len(hashes)) |
| 331 |
} |
| 332 |
|
| 333 |
p, hashes := leafProof(0, t, n, hashes) |
| 334 |
if len(hashes) != 0 { |
| 335 |
panic("tlog: bad index math in ProveRecord") |
| 336 |
} |
| 337 |
return p, nil |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
func leafProofIndex(lo, hi, n int64, need []int64) []int64 { |
| 345 |
|
| 346 |
if !(lo <= n && n < hi) { |
| 347 |
panic("tlog: bad math in leafProofIndex") |
| 348 |
} |
| 349 |
if lo+1 == hi { |
| 350 |
return need |
| 351 |
} |
| 352 |
if k, _ := maxpow2(hi - lo); n < lo+k { |
| 353 |
need = leafProofIndex(lo, lo+k, n, need) |
| 354 |
need = subTreeIndex(lo+k, hi, need) |
| 355 |
} else { |
| 356 |
need = subTreeIndex(lo, lo+k, need) |
| 357 |
need = leafProofIndex(lo+k, hi, n, need) |
| 358 |
} |
| 359 |
return need |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
func leafProof(lo, hi, n int64, hashes []Hash) (RecordProof, []Hash) { |
| 366 |
|
| 367 |
if !(lo <= n && n < hi) { |
| 368 |
panic("tlog: bad math in leafProof") |
| 369 |
} |
| 370 |
|
| 371 |
if lo+1 == hi { |
| 372 |
|
| 373 |
|
| 374 |
return RecordProof{}, hashes |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
var p RecordProof |
| 380 |
var th Hash |
| 381 |
if k, _ := maxpow2(hi - lo); n < lo+k { |
| 382 |
|
| 383 |
p, hashes = leafProof(lo, lo+k, n, hashes) |
| 384 |
th, hashes = subTreeHash(lo+k, hi, hashes) |
| 385 |
} else { |
| 386 |
|
| 387 |
th, hashes = subTreeHash(lo, lo+k, hashes) |
| 388 |
p, hashes = leafProof(lo+k, hi, n, hashes) |
| 389 |
} |
| 390 |
return append(p, th), hashes |
| 391 |
} |
| 392 |
|
| 393 |
var errProofFailed = errors.New("invalid transparency proof") |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
func CheckRecord(p RecordProof, t int64, th Hash, n int64, h Hash) error { |
| 398 |
if t < 0 || n < 0 || n >= t { |
| 399 |
return fmt.Errorf("tlog: invalid inputs in CheckRecord") |
| 400 |
} |
| 401 |
th2, err := runRecordProof(p, 0, t, n, h) |
| 402 |
if err != nil { |
| 403 |
return err |
| 404 |
} |
| 405 |
if th2 == th { |
| 406 |
return nil |
| 407 |
} |
| 408 |
return errProofFailed |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
func runRecordProof(p RecordProof, lo, hi, n int64, leafHash Hash) (Hash, error) { |
| 415 |
|
| 416 |
if !(lo <= n && n < hi) { |
| 417 |
panic("tlog: bad math in runRecordProof") |
| 418 |
} |
| 419 |
|
| 420 |
if lo+1 == hi { |
| 421 |
|
| 422 |
|
| 423 |
if len(p) != 0 { |
| 424 |
return Hash{}, errProofFailed |
| 425 |
} |
| 426 |
return leafHash, nil |
| 427 |
} |
| 428 |
|
| 429 |
if len(p) == 0 { |
| 430 |
return Hash{}, errProofFailed |
| 431 |
} |
| 432 |
|
| 433 |
k, _ := maxpow2(hi - lo) |
| 434 |
if n < lo+k { |
| 435 |
th, err := runRecordProof(p[:len(p)-1], lo, lo+k, n, leafHash) |
| 436 |
if err != nil { |
| 437 |
return Hash{}, err |
| 438 |
} |
| 439 |
return NodeHash(th, p[len(p)-1]), nil |
| 440 |
} else { |
| 441 |
th, err := runRecordProof(p[:len(p)-1], lo+k, hi, n, leafHash) |
| 442 |
if err != nil { |
| 443 |
return Hash{}, err |
| 444 |
} |
| 445 |
return NodeHash(p[len(p)-1], th), nil |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
.” |
| 452 |
type TreeProof []Hash |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
func ProveTree(t, n int64, h HashReader) (TreeProof, error) { |
| 457 |
if t < 1 || n < 1 || n > t { |
| 458 |
return nil, fmt.Errorf("tlog: invalid inputs in ProveTree") |
| 459 |
} |
| 460 |
indexes := treeProofIndex(0, t, n, nil) |
| 461 |
if len(indexes) == 0 { |
| 462 |
return TreeProof{}, nil |
| 463 |
} |
| 464 |
hashes, err := h.ReadHashes(indexes) |
| 465 |
if err != nil { |
| 466 |
return nil, err |
| 467 |
} |
| 468 |
if len(hashes) != len(indexes) { |
| 469 |
return nil, fmt.Errorf("tlog: ReadHashes(%d indexes) = %d hashes", len(indexes), len(hashes)) |
| 470 |
} |
| 471 |
|
| 472 |
p, hashes := treeProof(0, t, n, hashes) |
| 473 |
if len(hashes) != 0 { |
| 474 |
panic("tlog: bad index math in ProveTree") |
| 475 |
} |
| 476 |
return p, nil |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
func treeProofIndex(lo, hi, n int64, need []int64) []int64 { |
| 483 |
|
| 484 |
if !(lo < n && n <= hi) { |
| 485 |
panic("tlog: bad math in treeProofIndex") |
| 486 |
} |
| 487 |
|
| 488 |
if n == hi { |
| 489 |
if lo == 0 { |
| 490 |
return need |
| 491 |
} |
| 492 |
return subTreeIndex(lo, hi, need) |
| 493 |
} |
| 494 |
|
| 495 |
if k, _ := maxpow2(hi - lo); n <= lo+k { |
| 496 |
need = treeProofIndex(lo, lo+k, n, need) |
| 497 |
need = subTreeIndex(lo+k, hi, need) |
| 498 |
} else { |
| 499 |
need = subTreeIndex(lo, lo+k, need) |
| 500 |
need = treeProofIndex(lo+k, hi, n, need) |
| 501 |
} |
| 502 |
return need |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
func treeProof(lo, hi, n int64, hashes []Hash) (TreeProof, []Hash) { |
| 509 |
|
| 510 |
if !(lo < n && n <= hi) { |
| 511 |
panic("tlog: bad math in treeProof") |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
if n == hi { |
| 516 |
if lo == 0 { |
| 517 |
|
| 518 |
|
| 519 |
return TreeProof{}, hashes |
| 520 |
} |
| 521 |
th, hashes := subTreeHash(lo, hi, hashes) |
| 522 |
return TreeProof{th}, hashes |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
var p TreeProof |
| 528 |
var th Hash |
| 529 |
if k, _ := maxpow2(hi - lo); n <= lo+k { |
| 530 |
|
| 531 |
p, hashes = treeProof(lo, lo+k, n, hashes) |
| 532 |
th, hashes = subTreeHash(lo+k, hi, hashes) |
| 533 |
} else { |
| 534 |
|
| 535 |
th, hashes = subTreeHash(lo, lo+k, hashes) |
| 536 |
p, hashes = treeProof(lo+k, hi, n, hashes) |
| 537 |
} |
| 538 |
return append(p, th), hashes |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
func CheckTree(p TreeProof, t int64, th Hash, n int64, h Hash) error { |
| 544 |
if t < 1 || n < 1 || n > t { |
| 545 |
return fmt.Errorf("tlog: invalid inputs in CheckTree") |
| 546 |
} |
| 547 |
h2, th2, err := runTreeProof(p, 0, t, n, h) |
| 548 |
if err != nil { |
| 549 |
return err |
| 550 |
} |
| 551 |
if th2 == th && h2 == h { |
| 552 |
return nil |
| 553 |
} |
| 554 |
return errProofFailed |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
func runTreeProof(p TreeProof, lo, hi, n int64, old Hash) (Hash, Hash, error) { |
| 562 |
|
| 563 |
if !(lo < n && n <= hi) { |
| 564 |
panic("tlog: bad math in runTreeProof") |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
if n == hi { |
| 569 |
if lo == 0 { |
| 570 |
if len(p) != 0 { |
| 571 |
return Hash{}, Hash{}, errProofFailed |
| 572 |
} |
| 573 |
return old, old, nil |
| 574 |
} |
| 575 |
if len(p) != 1 { |
| 576 |
return Hash{}, Hash{}, errProofFailed |
| 577 |
} |
| 578 |
return p[0], p[0], nil |
| 579 |
} |
| 580 |
|
| 581 |
if len(p) == 0 { |
| 582 |
return Hash{}, Hash{}, errProofFailed |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
k, _ := maxpow2(hi - lo) |
| 587 |
if n <= lo+k { |
| 588 |
oh, th, err := runTreeProof(p[:len(p)-1], lo, lo+k, n, old) |
| 589 |
if err != nil { |
| 590 |
return Hash{}, Hash{}, err |
| 591 |
} |
| 592 |
return oh, NodeHash(th, p[len(p)-1]), nil |
| 593 |
} else { |
| 594 |
oh, th, err := runTreeProof(p[:len(p)-1], lo+k, hi, n, old) |
| 595 |
if err != nil { |
| 596 |
return Hash{}, Hash{}, err |
| 597 |
} |
| 598 |
return NodeHash(p[len(p)-1], oh), NodeHash(p[len(p)-1], th), nil |
| 599 |
} |
| 600 |
} |