-
Notifications
You must be signed in to change notification settings - Fork 13
/
merkle.go
86 lines (73 loc) · 1.85 KB
/
merkle.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
// Copyright (c) 2014-2017 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package merkle
import (
//"github.com/bitmark-inc/bitmarkd/merkledigest"
)
// compute minimum merkle root from a set of transaction ids
//
// structure is:
// 1. N * transaction digests
// 2. level 1..m digests
// 3. merkle root digest
func FullMerkleTree(txIds []Digest) []Digest {
// compute length of ids + all tree levels including root
idCount := len(txIds)
totalLength := 1 // all ids + space for the final root
for n := idCount; n > 1; n = (n + 1) / 2 {
totalLength += n
}
// add initial ids
tree := make([]Digest, totalLength)
copy(tree[:], txIds)
n := idCount
j := 0
for workLength := idCount; workLength > 1; workLength = (workLength + 1) / 2 {
for i := 0; i < workLength; i += 2 {
k := j + 1
if i+1 == workLength {
k = j // compensate for odd number
}
tree[n] = NewDigest(append(tree[j][:], tree[k][:]...))
n += 1
j = k + 1
}
}
return tree
}
// merkle hashing
//
// build a minimised tree
func MinimumMerkleTree(ids []Digest) []Digest {
length := len(ids)
// ensure length is within 24 bit positive number
if length <= 0 || length > 0x1000000 {
return nil
}
// output length = tx[0] + workspace = 1 + len/2
outputLength := length/2 + 1
tree := make([]Digest, outputLength)
tree[0] = ids[0]
// build minimum merkle - two sections 1.ids[]->tree[]; 2.tree[]->tree[]
finish := length
for start := 1; start < finish; start += 1 {
n := start
for i := start; i < finish; i += 2 {
j := i + 1
if j >= finish {
j = i // compensate for odd number
}
var b []byte
if 1 == start {
b = append(ids[i][:], ids[j][:]...)
} else {
b = append(tree[i][:], tree[j][:]...)
}
tree[n] = NewDigest(b)
n += 1
}
finish = n
}
return tree[:finish]
}