-
Notifications
You must be signed in to change notification settings - Fork 6
/
compute.go
129 lines (116 loc) · 4.23 KB
/
compute.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
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package factom
import (
"crypto/sha256"
"crypto/sha512"
merkle "github.com/AdamSLevy/go-merkle"
)
// ComputeDBlockHeaderHash returns sha256(data[:DBlockHeaderSize]).
func ComputeDBlockHeaderHash(data []byte) Bytes32 {
return sha256.Sum256(data[:DBlockHeaderSize])
}
// ComputeEBlockHeaderHash returns sha256(data[:EBlockHeaderSize]).
func ComputeEBlockHeaderHash(data []byte) Bytes32 {
return sha256.Sum256(data[:EBlockHeaderSize])
}
// ComputeFBlockKeyMR returns the Key Merkle root of the FBlock.
func ComputeFBlockKeyMR(elements [][]byte) (Bytes32, error) {
tree := merkle.NewTreeWithOpts(merkle.TreeOptions{DoubleOddNodes: true, DisableHashLeaves: true})
if err := tree.Generate(elements, sha256.New()); err != nil {
return Bytes32{}, err
}
root := tree.Root()
var keyMR Bytes32
copy(keyMR[:], root.Hash)
return keyMR, nil
}
// ComputeFBlockBodyMR returns the merkle root of the tree created with
// elements as leaves, where the leaves are hashed.
func ComputeFBlockBodyMR(elements [][]byte) (Bytes32, error) {
tree := merkle.NewTreeWithOpts(merkle.TreeOptions{DoubleOddNodes: true})
if err := tree.Generate(elements, sha256.New()); err != nil {
return Bytes32{}, err
}
root := tree.Root()
var bodyMR Bytes32
copy(bodyMR[:], root.Hash)
return bodyMR, nil
}
// ComputeDBlockBodyMR returns the merkle root of the tree created with
// elements as leaves, where the leaves are hashed.
func ComputeDBlockBodyMR(elements [][]byte) (Bytes32, error) {
tree := merkle.NewTreeWithOpts(merkle.TreeOptions{DoubleOddNodes: true})
if err := tree.Generate(elements, sha256.New()); err != nil {
return Bytes32{}, err
}
root := tree.Root()
var bodyMR Bytes32
copy(bodyMR[:], root.Hash)
return bodyMR, nil
}
// ComputeEBlockBodyMR returns the merkle root of the tree created with
// elements as leaves, where the leaves are not hashed.
func ComputeEBlockBodyMR(elements [][]byte) (Bytes32, error) {
tree := merkle.NewTreeWithOpts(merkle.TreeOptions{
DoubleOddNodes: true,
DisableHashLeaves: true})
if err := tree.Generate(elements, sha256.New()); err != nil {
return Bytes32{}, err
}
root := tree.Root()
var bodyMR Bytes32
copy(bodyMR[:], root.Hash)
return bodyMR, nil
}
// ComputeFullHash returns sha256(data).
func ComputeFullHash(data []byte) Bytes32 {
return sha256.Sum256(data)
}
// ComputeKeyMR returns sha256(headerHash|bodyMR).
func ComputeKeyMR(headerHash, bodyMR *Bytes32) Bytes32 {
data := make([]byte, len(headerHash)+len(bodyMR))
i := copy(data, headerHash[:])
copy(data[i:], bodyMR[:])
return sha256.Sum256(data)
}
// ComputeChainID returns the chain ID for a set of NameIDs.
func ComputeChainID(nameIDs []Bytes) Bytes32 {
hash := sha256.New()
for _, id := range nameIDs {
idSum := sha256.Sum256(id)
hash.Write(idSum[:])
}
c := hash.Sum(nil)
var chainID Bytes32
copy(chainID[:], c)
return chainID
}
// ComputeEntryHash returns the Entry hash of data. Entry's are hashed via:
// sha256(sha512(data) + data).
func ComputeEntryHash(data []byte) Bytes32 {
sum := sha512.Sum512(data)
saltedSum := make([]byte, len(sum)+len(data))
i := copy(saltedSum, sum[:])
copy(saltedSum[i:], data)
return sha256.Sum256(saltedSum)
}