-
Notifications
You must be signed in to change notification settings - Fork 174
/
hasher.go
41 lines (33 loc) · 1.07 KB
/
hasher.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
package utils
import (
"fmt"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/crypto/random"
"github.com/onflow/flow-go/model/encoding"
"github.com/onflow/flow-go/model/flow"
)
// NewResultApprovalHasher generates and returns a hasher for signing
// and verification of result approvals
func NewResultApprovalHasher() hash.Hasher {
h := crypto.NewBLSKMAC(encoding.ResultApprovalTag)
return h
}
// NewChunkAssignmentRNG generates and returns a hasher for chunk
// assignment
func NewChunkAssignmentRNG(res *flow.ExecutionResult) (random.Rand, error) {
h := hash.NewSHA3_384()
// encodes result approval body to byte slice
b, err := encoding.DefaultEncoder.Encode(res.ExecutionResultBody)
if err != nil {
return nil, fmt.Errorf("could not encode execution result body: %w", err)
}
// takes hash of result approval body
hash := h.ComputeHash(b)
// creates a random generator
rng, err := random.NewRand(hash)
if err != nil {
return nil, fmt.Errorf("could not generate random generator: %w", err)
}
return rng, nil
}