aboutsummaryrefslogtreecommitdiff
path: root/pkg/merkle/merkle.go
blob: ea58b3291758fbd69566353185abeb410c31cc6d (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// package merkle provides hashing operations that can be used to verify a
// Sigsum log's Merkle tree.  The exact hash strategy is defined in RFC 6962.
package merkle

import (
	"bytes"
	"crypto/sha256"
	"fmt"
)

type Prefix uint8

const (
	PrefixLeafNode Prefix = iota
	PrefixInteriorNode
)

var (
	prefixLeafNode     = []byte{byte(PrefixLeafNode)}
	prefixInteriorNode = []byte{byte(PrefixInteriorNode)}
)

const (
	HashSize = sha256.Size
)

type Hash [HashSize]byte

func HashFn(b []byte) *Hash {
	var ret Hash
	h := sha256.Sum256(b)
	copy(ret[:], h[:])
	return &ret
}

func HashLeafNode(leaf []byte) *Hash {
	var ret Hash
	h := sha256.New()
	h.Write(prefixLeafNode)
	h.Write(leaf)
	copy(ret[:], h.Sum(nil))
	return &ret
}

func HashInteriorNode(left, right Hash) *Hash {
	var ret Hash
	h := sha256.New()
	h.Write(prefixInteriorNode)
	h.Write(left[:])
	h.Write(right[:])
	copy(ret[:], h.Sum(nil))
	return &ret
}

// VerifyConsistency verifies that a Merkle tree is consistent.  The algorithm
// used is in RFC 9162, §2.1.4.2.  It is the same proof technique as RFC 6962.
func VerifyConsistency(oldSize, newSize uint64, oldRoot, newRoot Hash, path []Hash) error {
	// Step 1
	if len(path) == 0 {
		return fmt.Errorf("proof input is malformed: no path")
	}

	// Step2
	if pow2(oldSize) {
		path = append([]Hash{oldRoot}, path...)
	}

	// Step 3
	fn := oldSize - 1
	sn := newSize - 1

	// Step 4
	for lsb(fn) {
		fn = rshift(fn)
		sn = rshift(sn)
	}

	// Step 5
	fr := path[0]
	sr := path[0]

	// Step 6
	for _, c := range path[1:] {
		// Step 6(a)
		if sn == 0 {
			return fmt.Errorf("proof input is malformed: reached root too soon")
		}

		// Step 6(b)
		if lsb(fn) || fn == sn {
			// Step 6(b), i
			fr = *HashInteriorNode(c, fr)
			// Step 6(b), ii
			sr = *HashInteriorNode(c, sr)
			// Step 6(b), iii
			if !lsb(fn) {
				for {
					fn = rshift(fn)
					sn = rshift(sn)

					if lsb(fn) || fn == 0 {
						break
					}
				}
			}
		} else {
			// Step 6(b), i
			sr = *HashInteriorNode(sr, c)
		}

		// Step 6(c)
		fn = rshift(fn)
		sn = rshift(sn)
	}

	// Step 7
	if sn != 0 {
		return fmt.Errorf("proof input is malformed: never reached the root")
	}
	if !bytes.Equal(fr[:], oldRoot[:]) {
		return fmt.Errorf("invalid proof: old root mismatch")
	}
	if !bytes.Equal(sr[:], newRoot[:]) {
		return fmt.Errorf("invalid proof: new root mismatch")
	}
	return nil
}

// VerifyInclusion verifies that something is in a Merkle tree.  The algorithm
// used is in RFC 9162, §2.1.3.2.  It is the same proof technique as RFC 6962.
func VerifyInclusion(leaf Hash, index, size uint64, root Hash, path []Hash) error {
	// Step 1
	if index >= size {
		return fmt.Errorf("proof input is malformed: index out of range")
	}

	// Step 2
	fn := index
	sn := size - 1

	// Step 3
	r := leaf

	// Step 4
	for _, p := range path {
		// Step 4(a)
		if sn == 0 {
			return fmt.Errorf("proof input is malformed: reached root too soon")
		}

		// Step 4(b)
		if lsb(fn) || fn == sn {
			// Step 4(b), i
			r = *HashInteriorNode(p, r)

			// Step 4(b), ii
			if !lsb(fn) {
				for {
					fn = rshift(fn)
					sn = rshift(sn)

					if lsb(fn) || fn == 0 {
						break
					}
				}
			}
		} else {
			// Step 4(b), i
			r = *HashInteriorNode(r, p)
		}

		// Step 4(c)
		fn = rshift(fn)
		sn = rshift(sn)
	}

	// Step 5
	if sn != 0 {
		return fmt.Errorf("proof input is malformed: never reached the root")
	}
	if !bytes.Equal(r[:], root[:]) {
		return fmt.Errorf("invalid proof: root mismatch")
	}
	return nil
}

// lsb outputs true if the least significant bit is set
func lsb(num uint64) bool {
	return (num & 1) != 0
}

// pow2 outputs true if the number is a power of 2
func pow2(num uint64) bool {
	return (num & (num - 1)) == 0
}

// rshift returns the right-shifted number
func rshift(num uint64) uint64 {
	return num >> 1
}