-
Notifications
You must be signed in to change notification settings - Fork 174
/
seed.go
47 lines (39 loc) · 1.51 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
45
46
47
package seed
import (
"encoding/binary"
"fmt"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/module/signature"
)
// FromParentSignature reads the raw random seed from a combined signature.
// the combinedSig must be from a QuorumCertificate. The indices can be used to
// generate task-specific seeds from the same signature.
func FromParentSignature(indices []uint32, combinedSig crypto.Signature) ([]byte, error) {
// split the parent voter sig into staking & beacon parts
combiner := signature.NewCombiner(encodable.ConsensusVoteSigLen, encodable.RandomBeaconSigLen)
_, randomBeaconSig, err := combiner.Split(combinedSig)
if err != nil {
return nil, fmt.Errorf("could not split 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
}