-
Notifications
You must be signed in to change notification settings - Fork 37
/
multiproof.go
130 lines (115 loc) · 4.18 KB
/
multiproof.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright © 2018 - 2023 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package merkletree
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
"github.com/wealdtech/go-merkletree/v2/blake2b"
)
// MultiProof is a single structure containing multiple proofs of a Merkle tree.
type MultiProof struct {
// Values is the number of values in the Merkle tree.
Values uint64
// Hashes are indexed hashes of values that cannot be calculated from the index data
Hashes map[uint64][]byte
// Indices are the indices of the data that can be proved with the hashes
Indices []uint64
salt bool
// if sorted is true, the hash values are sorted before hashing branch nodes
sorted bool
hash HashType
}
// NewMultiProof creates a new multiproof using the provided information.
func NewMultiProof(params ...Parameter) (*MultiProof, error) {
parameters, err := parseAndCheckMultiProofParameters(params...)
if err != nil {
return nil, errors.Wrap(err, "problem with parameters")
}
return &MultiProof{
Values: parameters.values,
Hashes: parameters.hashes,
Indices: parameters.indices,
salt: parameters.salt,
sorted: parameters.sorted,
hash: parameters.hash,
}, nil
}
// Verify verifies a multiproof.
func (p *MultiProof) Verify(data [][]byte, root []byte) (bool, error) {
// Step 1 create hashes for all values.
var proofHash []byte
indexSalt := make([]byte, 4)
for i, index := range p.Indices {
if p.salt {
binary.BigEndian.PutUint32(indexSalt, uint32(index))
proofHash = p.hash.Hash(data[i], indexSalt)
} else {
proofHash = p.hash.Hash(data[i])
}
p.Hashes[index+p.Values] = proofHash
}
// Step 2 calculate values up the tree.
for i := p.Values - 1; i > 0; i-- {
_, exists := p.Hashes[i]
if exists {
continue
}
child1, exists := p.Hashes[i*2]
if !exists {
continue
}
child2, exists := p.Hashes[i*2+1]
if !exists {
continue
}
if p.sorted && bytes.Compare(child1, child2) == 1 {
p.Hashes[i] = p.hash.Hash(child2, child1)
} else {
p.Hashes[i] = p.hash.Hash(child1, child2)
}
}
return bytes.Equal(p.Hashes[1], root), nil
}
// VerifyMultiProof verifies multiple Merkle tree proofs for pieces of data using the default hash type.
// The proof and path are as per Merkle tree's GenerateProof(), and root is the root hash of the tree against which the proof is to
// be verified. Note that this does not require the Merkle tree to verify the proof, only its root; this allows for checking
// against historical trees without having to instantiate them.
//
// This returns true if the proof is verified, otherwise false.
//
// Deprecated: please use MultiProof.Verify(...)
func VerifyMultiProof(data [][]byte, salt bool, proof *MultiProof, root []byte) (bool, error) {
return VerifyMultiProofUsing(data, salt, proof, root, blake2b.New())
}
// VerifyMultiProofUsing verifies multiple Merkle tree proofs for pieces of data using the provided hash type.
// The proof and is as per Merkle tree's GenerateProof(), and root is the root hash of the tree against which the proof is to
// be verified. Note that this does not require the Merkle tree to verify the proof, only its root; this allows for checking
// against historical trees without having to instantiate them.
//
// This returns true if the proof is verified, otherwise false.
//
// Deprecated: please use MultiProof.Verify(...)
func VerifyMultiProofUsing(data [][]byte, salt bool, proof *MultiProof, root []byte, hashType HashType) (bool, error) {
mp, err := NewMultiProof(
WithSalt(salt),
WithHashType(hashType),
WithIndices(proof.Indices),
WithHashes(proof.Hashes),
WithValues(proof.Values),
)
if err != nil {
return false, err
}
return mp.Verify(data, root)
}