-
Notifications
You must be signed in to change notification settings - Fork 179
/
seed.go
44 lines (36 loc) · 1.36 KB
/
seed.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
package seed
import (
"encoding/binary"
"fmt"
"github.com/onflow/flow-go/consensus/hotstuff/packer"
"github.com/onflow/flow-go/crypto/hash"
)
// FromParentSignature reads the raw random seed from a main consensus QC sigData.
// The sigData is an RLP encoded structure that is part of QuorumCertificate.
// The indices can be used to generate task-specific seeds from the same signature.
func FromParentSignature(indices []uint32, sigData []byte) ([]byte, error) {
// unpack sig data to extract random beacon sig
randomBeaconSig, err := packer.UnpackRandomBeaconSig(sigData)
if err != nil {
return nil, fmt.Errorf("could not unpack block signature: %w", err)
}
return FromRandomSource(indices, randomBeaconSig)
}
// FromRandomSource generates a task-specific seed (task is determined by indices).
func FromRandomSource(indices []uint32, sor []byte) ([]byte, error) {
// create the key used for the KMAC by concatenating all indices
keyLen := 4 * len(indices)
if keyLen < hash.KmacMinKeyLen {
keyLen = hash.KmacMinKeyLen
}
key := make([]byte, keyLen)
for i, index := range indices {
binary.LittleEndian.PutUint32(key[4*i:4*i+4], index)
}
// create a KMAC instance with our key and 32 bytes output size
kmac, err := hash.NewKMAC_128(key, nil, 32)
if err != nil {
return nil, fmt.Errorf("could not create kmac: %w", err)
}
return kmac.ComputeHash(sor), nil
}