-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtlogx_test.go
More file actions
99 lines (86 loc) · 2.42 KB
/
Copy pathtlogx_test.go
File metadata and controls
99 lines (86 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package torchwood_test
import (
"reflect"
"slices"
"testing"
"filippo.io/torchwood"
"golang.org/x/mod/sumdb/tlog"
)
func TestRightEdge(t *testing.T) {
tests := []struct {
n int64
want []int64
}{
{0, nil},
{13, []int64{
tlog.StoredHashIndex(3, 0),
tlog.StoredHashIndex(2, 2),
tlog.StoredHashIndex(0, 12),
}},
{16, []int64{
tlog.StoredHashIndex(4, 0),
}},
{1 << 62, []int64{ // 2^62, the maximum supported tree size
tlog.StoredHashIndex(62, 0),
}},
}
for _, test := range tests {
if got := torchwood.RightEdge(test.n); !reflect.DeepEqual(got, test.want) {
t.Errorf("RightEdge(%d) = %v; want %v", test.n, got, test.want)
}
}
// A tree size beyond 2^62 must panic rather than loop forever in maxpow2.
func() {
defer func() {
if recover() == nil {
t.Errorf("RightEdge(1<<62 + 1) did not panic")
}
}()
torchwood.RightEdge(1<<62 + 1)
}()
}
func TestHashProof(t *testing.T) {
var hashes []tlog.Hash
hashReader := tlog.HashReaderFunc(func(i []int64) ([]tlog.Hash, error) {
var out []tlog.Hash
for _, j := range i {
out = append(out, hashes[j])
}
return out, nil
})
for n := range int64(256) {
newHashes, err := tlog.StoredHashes(n, []byte{byte(n)}, hashReader)
fatalIfErr(t, err)
hashes = append(hashes, newHashes...)
rootHash, err := tlog.TreeHash(n+1, hashReader)
fatalIfErr(t, err)
for i := range len(hashes) {
proof, err := torchwood.ProveHash(n+1, int64(i), hashReader)
fatalIfErr(t, err)
if err := torchwood.CheckHash(proof, n+1, rootHash, int64(i), hashes[i]); err != nil {
t.Errorf("hash proof mismatch for size %d, hash index %d: %v", n+1, i, err)
}
if l, k := tlog.SplitStoredHashIndex(int64(i)); l == 0 {
recordProof, err := tlog.ProveRecord(n+1, k, hashReader)
fatalIfErr(t, err)
if !slices.Equal([]tlog.Hash(proof), recordProof) {
t.Errorf("record proof mismatch for size %d, record index %d", n+1, k)
}
}
}
}
// Oversized tree size must be rejected, not hang. (ProveHash/CheckHash
// delegate the bound to ProveSubtree/CheckSubtree.)
if _, err := torchwood.ProveHash(1<<62+1, 0, hashReader); err == nil {
t.Errorf("ProveHash with t > 2^62: nil error")
}
if err := torchwood.CheckHash(nil, 1<<62+1, tlog.Hash{}, 0, tlog.Hash{}); err == nil {
t.Errorf("CheckHash with t > 2^62: nil error")
}
}
func fatalIfErr(t *testing.T, err error) {
if err != nil {
t.Helper()
t.Fatalf("unexpected error: %v", err)
}
}