Skip to content

Commit 4ba2f58

Browse files
committed
Remove adapter, move test to the proof package
1 parent 2e2444b commit 4ba2f58

File tree

3 files changed

+46
-100
lines changed

3 files changed

+46
-100
lines changed

hasher.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 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+
// LogHasher provides the hash functions needed to compute dense merkle trees.
18+
type LogHasher interface {
19+
// EmptyRoot supports returning a special case for the root of an empty tree.
20+
EmptyRoot() []byte
21+
// HashLeaf computes the hash of a leaf that exists.
22+
HashLeaf(leaf []byte) []byte
23+
// HashChildren computes interior nodes.
24+
HashChildren(l, r []byte) []byte
25+
// Size returns the number of bytes the Hash* functions will return.
26+
Size() int
27+
}

log_verifier.go

-73
This file was deleted.

log_verifier_test.go renamed to proof/verify_test.go

+19-27
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package merkle
15+
package proof
1616

1717
import (
18-
"bytes"
1918
"encoding/hex"
2019
"fmt"
2120
"strings"
@@ -212,23 +211,16 @@ func corruptConsistencyProof(size1, size2 uint64, root1, root2 []byte, proof [][
212211
return ret
213212
}
214213

215-
func verifierCheck(v *LogVerifier, leafIndex, treeSize uint64, proof [][]byte, root, leafHash []byte) error {
216-
// Verify original inclusion proof.
217-
got, err := v.RootFromInclusionProof(leafIndex, treeSize, leafHash, proof)
218-
if err != nil {
219-
return err
220-
}
221-
if !bytes.Equal(got, root) {
222-
return fmt.Errorf("got root:\n%x\nexpected:\n%x", got, root)
223-
}
224-
if err := v.VerifyInclusion(leafIndex, treeSize, leafHash, proof, root); err != nil {
214+
func verifierCheck(nh NodeHasher, leafIndex, treeSize uint64, proof [][]byte, root, leafHash []byte) error {
215+
// FIXME: GetRootFromInclusionProof
216+
if err := VerifyInclusion(nh, leafIndex, treeSize, leafHash, proof, root); err != nil {
225217
return err
226218
}
227219

228220
probes := corruptInclusionProof(leafIndex, treeSize, proof, root, leafHash)
229221
var wrong []string
230222
for _, p := range probes {
231-
if err := v.VerifyInclusion(p.leafIndex, p.treeSize, p.leafHash, p.proof, p.root); err == nil {
223+
if err := VerifyInclusion(nh, p.leafIndex, p.treeSize, p.leafHash, p.proof, p.root); err == nil {
232224
wrong = append(wrong, p.desc)
233225
}
234226
}
@@ -238,9 +230,9 @@ func verifierCheck(v *LogVerifier, leafIndex, treeSize uint64, proof [][]byte, r
238230
return nil
239231
}
240232

241-
func verifierConsistencyCheck(v *LogVerifier, size1, size2 uint64, root1, root2 []byte, proof [][]byte) error {
233+
func verifierConsistencyCheck(nh NodeHasher, size1, size2 uint64, root1, root2 []byte, proof [][]byte) error {
242234
// Verify original consistency proof.
243-
if err := v.VerifyConsistency(size1, size2, root1, root2, proof); err != nil {
235+
if err := VerifyConsistency(nh, size1, size2, proof, root1, root2); err != nil {
244236
return err
245237
}
246238
// For simplicity test only non-trivial proofs that have root1 != root2,
@@ -252,7 +244,7 @@ func verifierConsistencyCheck(v *LogVerifier, size1, size2 uint64, root1, root2
252244
probes := corruptConsistencyProof(size1, size2, root1, root2, proof)
253245
var wrong []string
254246
for _, p := range probes {
255-
if err := v.VerifyConsistency(p.size1, p.size2, p.root1, p.root2, p.proof); err == nil {
247+
if err := VerifyConsistency(nh, p.size1, p.size2, p.proof, p.root1, p.root2); err == nil {
256248
wrong = append(wrong, p.desc)
257249
}
258250
}
@@ -263,10 +255,10 @@ func verifierConsistencyCheck(v *LogVerifier, size1, size2 uint64, root1, root2
263255
}
264256

265257
func TestVerifyInclusionSingleEntry(t *testing.T) {
266-
v := NewLogVerifier(rfc6962.DefaultHasher)
258+
hasher := rfc6962.DefaultHasher
267259
data := []byte("data")
268260
// Root and leaf hash for 1-entry tree are the same.
269-
hash := v.hasher.HashLeaf(data)
261+
hash := hasher.HashLeaf(data)
270262
// The corresponding inclusion proof is empty.
271263
proof := [][]byte{}
272264
emptyHash := []byte{}
@@ -282,7 +274,7 @@ func TestVerifyInclusionSingleEntry(t *testing.T) {
282274
// {emptyHash, emptyHash, true}, // Wrong hash size. FIXME
283275
} {
284276
t.Run(fmt.Sprintf("test:%d", i), func(t *testing.T) {
285-
err := v.VerifyInclusion(0, 1, tc.leaf, proof, tc.root)
277+
err := VerifyInclusion(hasher, 0, 1, tc.leaf, proof, tc.root)
286278
if got, want := err != nil, tc.wantErr; got != want {
287279
t.Errorf("error: %v, want %v", got, want)
288280
}
@@ -291,21 +283,21 @@ func TestVerifyInclusionSingleEntry(t *testing.T) {
291283
}
292284

293285
func TestVerifyInclusion(t *testing.T) {
294-
v := NewLogVerifier(rfc6962.DefaultHasher)
286+
hasher := rfc6962.DefaultHasher
295287
proof := [][]byte{}
296288

297289
probes := []struct {
298290
index, size uint64
299291
}{{0, 0}, {0, 1}, {1, 0}, {2, 1}}
300292
for _, p := range probes {
301293
t.Run(fmt.Sprintf("probe:%d:%d", p.index, p.size), func(t *testing.T) {
302-
if err := v.VerifyInclusion(p.index, p.size, sha256SomeHash, proof, []byte{}); err == nil {
294+
if err := VerifyInclusion(hasher, p.index, p.size, sha256SomeHash, proof, []byte{}); err == nil {
303295
t.Error("Incorrectly verified invalid root/leaf")
304296
}
305-
if err := v.VerifyInclusion(p.index, p.size, []byte{}, proof, sha256EmptyTreeHash); err == nil {
297+
if err := VerifyInclusion(hasher, p.index, p.size, []byte{}, proof, sha256EmptyTreeHash); err == nil {
306298
t.Error("Incorrectly verified invalid root/leaf")
307299
}
308-
if err := v.VerifyInclusion(p.index, p.size, sha256SomeHash, proof, sha256EmptyTreeHash); err == nil {
300+
if err := VerifyInclusion(hasher, p.index, p.size, sha256SomeHash, proof, sha256EmptyTreeHash); err == nil {
309301
t.Error("Incorrectly verified invalid root/leaf")
310302
}
311303
})
@@ -316,15 +308,15 @@ func TestVerifyInclusion(t *testing.T) {
316308
p := inclusionProofs[i]
317309
t.Run(fmt.Sprintf("proof:%d", i), func(t *testing.T) {
318310
leafHash := rfc6962.DefaultHasher.HashLeaf(leaves[p.leaf-1])
319-
if err := verifierCheck(&v, p.leaf-1, p.size, p.proof, roots[p.size-1], leafHash); err != nil {
311+
if err := verifierCheck(hasher, p.leaf-1, p.size, p.proof, roots[p.size-1], leafHash); err != nil {
320312
t.Errorf("verifierCheck(): %s", err)
321313
}
322314
})
323315
}
324316
}
325317

326318
func TestVerifyConsistency(t *testing.T) {
327-
v := NewLogVerifier(rfc6962.DefaultHasher)
319+
hasher := rfc6962.DefaultHasher
328320

329321
root1 := []byte("don't care 1")
330322
root2 := []byte("don't care 2")
@@ -358,7 +350,7 @@ func TestVerifyConsistency(t *testing.T) {
358350
}
359351
for i, p := range tests {
360352
t.Run(fmt.Sprintf("test:%d:size:%d-%d", i, p.size1, p.size2), func(t *testing.T) {
361-
err := verifierConsistencyCheck(&v, p.size1, p.size2, p.root1, p.root2, p.proof)
353+
err := verifierConsistencyCheck(hasher, p.size1, p.size2, p.root1, p.root2, p.proof)
362354
if p.wantErr && err == nil {
363355
t.Errorf("Incorrectly verified")
364356
} else if !p.wantErr && err != nil {
@@ -370,7 +362,7 @@ func TestVerifyConsistency(t *testing.T) {
370362
for i := 0; i < 4; i++ {
371363
p := consistencyProofs[i]
372364
t.Run(fmt.Sprintf("proof:%d", i), func(t *testing.T) {
373-
err := verifierConsistencyCheck(&v, p.size1, p.size2,
365+
err := verifierConsistencyCheck(hasher, p.size1, p.size2,
374366
roots[p.size1-1], roots[p.size2-1], p.proof)
375367
if err != nil {
376368
t.Fatalf("Failed to verify known good proof: %s", err)

0 commit comments

Comments
 (0)