github.com/koko1123/flow-go-1@v0.29.6/state/protocol/seed/seed.go (about)

     1  package seed
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/consensus/hotstuff/model"
     7  	"github.com/onflow/flow-go/crypto"
     8  	"github.com/onflow/flow-go/crypto/hash"
     9  	"github.com/onflow/flow-go/crypto/random"
    10  )
    11  
    12  // PRGFromRandomSource returns a PRG seeded by the source of randomness of the protocol.
    13  // The customizer is used to generate a task-specific PRG (customizer in this implementation
    14  // is up to 12-bytes long).
    15  //
    16  // The function hashes the input random source to obtain the PRG seed.
    17  // Hashing is required to uniformize the entropy over the output.
    18  func PRGFromRandomSource(randomSource []byte, customizer []byte) (random.Rand, error) {
    19  	// hash the source of randomness (signature) to uniformize the entropy
    20  	var seed [hash.HashLenSHA3_256]byte
    21  	hash.ComputeSHA3_256(&seed, randomSource)
    22  
    23  	// create random number generator from the seed and customizer
    24  	rng, err := random.NewChacha20PRG(seed[:], customizer)
    25  	if err != nil {
    26  		return nil, fmt.Errorf("could not create ChaCha20 PRG: %w", err)
    27  	}
    28  	return rng, nil
    29  }
    30  
    31  const RandomSourceLength = crypto.SignatureLenBLSBLS12381
    32  
    33  // FromParentQCSignature extracts the source of randomness from the given QC sigData.
    34  // The sigData is an RLP encoded structure that is part of QuorumCertificate.
    35  func FromParentQCSignature(sigData []byte) ([]byte, error) {
    36  	// unpack sig data to extract random beacon sig
    37  	randomBeaconSig, err := model.UnpackRandomBeaconSig(sigData)
    38  	if err != nil {
    39  		return nil, fmt.Errorf("could not unpack block signature: %w", err)
    40  	}
    41  
    42  	return randomBeaconSig, nil
    43  }