This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
readers.go
70 lines (66 loc) · 2.23 KB
/
readers.go
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
package merkletree
import (
"errors"
"hash"
"io"
)
// ReadAll will read segments of size 'segmentSize' and push them into the tree
// until EOF is reached. Success will return 'err == nil', not 'err == EOF'. No
// padding is added to the data, so the last element may be smaller than
// 'segmentSize'.
func (t *Tree) ReadAll(r io.Reader, segmentSize int) error {
for {
segment := make([]byte, segmentSize)
n, readErr := io.ReadFull(r, segment)
if readErr == io.EOF {
// All data has been read.
break
} else if readErr == io.ErrUnexpectedEOF {
// This is the last segment, and there aren't enough bytes to fill
// the entire segment. Note that the next call will return io.EOF.
segment = segment[:n]
} else if readErr != nil {
return readErr
}
t.Push(segment)
}
return nil
}
// ReaderRoot returns the Merkle root of the data read from the reader, where
// each leaf is 'segmentSize' long and 'h' is used as the hashing function. All
// leaves will be 'segmentSize' bytes except the last leaf, which will not be
// padded out if there are not enough bytes remaining in the reader.
func ReaderRoot(r io.Reader, h hash.Hash, segmentSize int) (root []byte, err error) {
tree := New(h)
err = tree.ReadAll(r, segmentSize)
if err != nil {
return
}
root = tree.Root()
return
}
// BuildReaderProof returns a proof that certain data is in the merkle tree
// created by the data in the reader. The merkle root, set of proofs, and the
// number of leaves in the Merkle tree are all returned. All leaves will we
// 'segmentSize' bytes except the last leaf, which will not be padded out if
// there are not enough bytes remaining in the reader.
func BuildReaderProof(r io.Reader, h hash.Hash, segmentSize int, index uint64) (root []byte, proofSet [][]byte, numLeaves uint64, err error) {
tree := New(h)
err = tree.SetIndex(index)
if err != nil {
// This code should be unreachable - SetIndex will only return an error
// if the tree is not empty, and yet the tree should be empty at this
// point.
panic(err)
}
err = tree.ReadAll(r, segmentSize)
if err != nil {
return
}
root, proofSet, _, numLeaves = tree.Prove()
if len(proofSet) == 0 {
err = errors.New("index was not reached while creating proof")
return
}
return
}