drng

package
v0.7.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2021 License: Apache-2.0, BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SignatureSize defines the BLS Signature size in bytes.
	SignatureSize = 96

	// PublicKeySize defines the BLS Public Key size in bytes.
	PublicKeySize = 48
)
View Source
const HeaderLength = 5

HeaderLength defines the length of a DRNG header

View Source
const (
	// ObjectName defines the name of the dRNG object.
	ObjectName = "dRNG"
)

Variables

View Source
var (
	// ErrDistributedPubKeyMismatch is returned if the distributed public key does not match.
	ErrDistributedPubKeyMismatch = errors.New("Distributed Public Key does not match")
	// ErrInvalidRound is returned if the round is invalid.
	ErrInvalidRound = errors.New("Invalid Round")
	// ErrInstanceIDMismatch is returned if the instanceID does not match.
	ErrInstanceIDMismatch = errors.New("InstanceID does not match")
	// ErrInvalidIssuer is returned if the issuer is invalid.
	ErrInvalidIssuer = errors.New("Invalid Issuer")
	// ErrNilState is returned on nil state.
	ErrNilState = errors.New("Nil state")
	// ErrNilData is returned on nil data.
	ErrNilData = errors.New("Nil data")
)
View Source
var PayloadType = payload.NewType(111, ObjectName, func(data []byte) (payload payload.Payload, err error) {
	var consumedBytes int
	payload, consumedBytes, err = FromBytes(data)
	if err != nil {
		return nil, err
	}
	if consumedBytes != len(data) {
		return nil, errors.New("not all payload bytes were consumed")
	}

	return
})

PayloadType defines the type of the drng payload.

Functions

func CollectiveBeaconReceived

func CollectiveBeaconReceived(handler interface{}, params ...interface{})

CollectiveBeaconReceived returns the data of a collective beacon event.

func ExtractRandomness

func ExtractRandomness(signature []byte) ([]byte, error)

ExtractRandomness returns the randomness from a given signature.

func ProcessBeacon

func ProcessBeacon(state *State, cb *CollectiveBeaconEvent) error

ProcessBeacon performs the following tasks: - verify that we have a valid random - update drng state

func ResolveNextTimePoint added in v0.5.8

func ResolveNextTimePoint(nowTime, interval time.Duration) time.Duration

ResolveNextTimePoint returns the next time point.

func VerifyCollectiveBeacon

func VerifyCollectiveBeacon(state *State, cb *CollectiveBeaconEvent) error

VerifyCollectiveBeacon verifies against a given state that the given CollectiveBeaconEvent contains a valid beacon.

Types

type CollectiveBeaconEvent

type CollectiveBeaconEvent struct {
	// Public key of the issuer.
	IssuerPublicKey ed25519.PublicKey
	// Timestamp when the beacon was issued.
	Timestamp time.Time
	// InstanceID of the beacon.
	InstanceID uint32
	// Round of the current beacon.
	Round uint64
	// Collective signature of the previous beacon.
	PrevSignature []byte
	// Collective signature of the current beacon.
	Signature []byte
	// The distributed public key.
	Dpk []byte
}

CollectiveBeaconEvent holds data about a collective beacon event.

type CollectiveBeaconPayload

type CollectiveBeaconPayload struct {
	Header

	// Round of the current beacon
	Round uint64
	// Collective signature of the previous beacon
	PrevSignature []byte
	// Collective signature of the current beacon
	Signature []byte
	// The distributed public key
	Dpk []byte
	// contains filtered or unexported fields
}

CollectiveBeaconPayload is a collective beacon payload.

func CollectiveBeaconPayloadFromBytes

func CollectiveBeaconPayloadFromBytes(bytes []byte) (result *CollectiveBeaconPayload, consumedBytes int, err error)

CollectiveBeaconPayloadFromBytes parses the marshaled version of a Payload into an object. It either returns a new Payload or fills an optionally provided Payload with the parsed information.

func CollectiveBeaconPayloadFromMarshalUtil

func CollectiveBeaconPayloadFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (*CollectiveBeaconPayload, error)

CollectiveBeaconPayloadFromMarshalUtil is a wrapper for simplified unmarshaling in a byte stream using the marshalUtil package.

func NewCollectiveBeaconPayload

func NewCollectiveBeaconPayload(instanceID uint32, round uint64, prevSignature, signature, dpk []byte) *CollectiveBeaconPayload

NewCollectiveBeaconPayload creates a new collective beacon payload.

func (*CollectiveBeaconPayload) Bytes

func (p *CollectiveBeaconPayload) Bytes() (bytes []byte)

Bytes returns the collective beacon payload bytes.

func (*CollectiveBeaconPayload) Marshal

func (p *CollectiveBeaconPayload) Marshal() (bytes []byte, err error)

Marshal marshals the collective beacon payload into bytes.

func (*CollectiveBeaconPayload) String

func (p *CollectiveBeaconPayload) String() string

func (*CollectiveBeaconPayload) Type

Type returns the collective beacon payload type.

type Committee

type Committee struct {
	// InstanceID holds the identifier of the dRAND instance.
	InstanceID uint32
	// Threshold holds the threshold of the secret sharing protocol.
	Threshold uint8
	// Identities holds the nodes' identities of the committee members.
	Identities []ed25519.PublicKey
	// DistributedPK holds the drand distributed public key.
	DistributedPK []byte
}

Committee defines the current committee state of a DRNG instance.

type DRNG

type DRNG struct {
	State  map[uint32]*State // The state of the DRNG.
	Events *Event            // The events fired on the DRNG.
}

DRNG holds the state and events of a drng instance.

func New

func New(config map[uint32][]Option) *DRNG

New creates a new DRNG instance.

func (*DRNG) Dispatch

func (d *DRNG) Dispatch(issuer ed25519.PublicKey, timestamp time.Time, payload *Payload) error

Dispatch parses a DRNG message and processes it based on its subtype

func (*DRNG) LoadState added in v0.5.8

func (d *DRNG) LoadState(instanceID uint32) *State

LoadState returns the pointer to the state associated to the given instanceID.

type Event

type Event struct {
	// Collective Beacon is triggered each time we receive a new CollectiveBeacon message.
	CollectiveBeacon *events.Event
	// Randomness is triggered each time we receive a new and valid CollectiveBeacon message.
	Randomness *events.Event
}

Event holds the different events triggered by a DRNG instance.

type Header struct {
	PayloadType Type   // message type
	InstanceID  uint32 // identifier of the DRNG instance
}

Header defines defines a DRNG payload header

func HeaderFromBytes

func HeaderFromBytes(bytes []byte, optionalTargetObject ...*Header) (result Header, consumedBytes int, err error)

HeaderFromBytes unmarshals a header from a sequence of bytes. It either creates a new header or fills the optionally provided object with the parsed information.

func HeaderFromMarshalUtil

func HeaderFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (Header, error)

HeaderFromMarshalUtil is a wrapper for simplified unmarshaling in a byte stream using the marshalUtil package.

func NewHeader

func NewHeader(payloadType Type, instanceID uint32) Header

NewHeader creates a new DRNG payload header for the given type and instance id.

func (*Header) Bytes

func (h *Header) Bytes() (bytes []byte)

Bytes returns the header in serialized bytes form.

type Option

type Option func(*Options)

Option is a function which sets the given option.

func SetCommittee

func SetCommittee(c *Committee) Option

SetCommittee sets the initial committee

func SetRandomness

func SetRandomness(r *Randomness) Option

SetRandomness sets the initial randomness

type Options

type Options struct {
	// The initial committee of the DRNG.
	Committee *Committee
	// The initial randomness of the DRNG.
	Randomness *Randomness
}

Options define state options of a DRNG.

type Payload

type Payload struct {
	Header
	Data []byte
	// contains filtered or unexported fields
}

Payload defines a DRNG payload.

func FromBytes

func FromBytes(bytes []byte) (result *Payload, consumedBytes int, err error)

FromBytes parses the marshaled version of a Payload into an object. It either returns a new Payload or fills an optionally provided Payload with the parsed information.

func NewPayload

func NewPayload(header Header, data []byte) *Payload

NewPayload creates a new DRNG payload.

func PayloadFromMarshalUtil

func PayloadFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (*Payload, error)

PayloadFromMarshalUtil is a wrapper for simplified unmarshaling in a byte stream using the marshalUtil package.

func (*Payload) Bytes

func (p *Payload) Bytes() (bytes []byte)

Bytes returns the drng payload bytes.

func (*Payload) Marshal

func (p *Payload) Marshal() (bytes []byte, err error)

Marshal marshals the drng payload into bytes.

func (*Payload) String

func (p *Payload) String() string

func (*Payload) Type

func (p *Payload) Type() payload.Type

Type returns the type of the drng payload.

type Randomness

type Randomness struct {
	// Round holds the current DRNG round.
	Round uint64
	// Randomness holds the current randomness as a slice of bytes.
	Randomness []byte
	// Timestamp holds the timestamp of the current randomness message
	Timestamp time.Time
}

Randomness defines the current randomness state of a DRNG instance.

func (Randomness) Float64

func (r Randomness) Float64() float64

Float64 returns a float64 [0.0,1.0) representation of the randomness byte slice.

type State

type State struct {
	// contains filtered or unexported fields
}

State represents the state of the DRNG.

func NewState

func NewState(setters ...Option) *State

NewState creates a new State with the given optional options

func (*State) Committee

func (s *State) Committee() Committee

Committee returns the committee of the DRNG state

func (*State) Randomness

func (s *State) Randomness() Randomness

Randomness returns the randomness of the DRNG state

func (*State) UpdateCommittee

func (s *State) UpdateCommittee(c *Committee)

UpdateCommittee updates the committee of the DRNG state

func (*State) UpdateDPK

func (s *State) UpdateDPK(dpk []byte)

UpdateDPK updates the distributed public key of the DRNG state

func (*State) UpdateRandomness

func (s *State) UpdateRandomness(r *Randomness)

UpdateRandomness updates the randomness of the DRNG state

type Ticker added in v0.5.8

type Ticker struct {
	// contains filtered or unexported fields
}

Ticker holds a channel that delivers randomness at intervals.

func NewTicker added in v0.5.8

func NewTicker(dRNGState func() *State, interval time.Duration, defaultValue float64, awaitOffset time.Duration) *Ticker

NewTicker returns a pointer to a new Ticker.

func (*Ticker) C added in v0.5.8

func (t *Ticker) C() <-chan float64

C returns the channel from which random generated numbers can be consumed from.

func (*Ticker) DelayedRoundStart added in v0.5.8

func (t *Ticker) DelayedRoundStart() time.Duration

DelayedRoundStart returns how much the current Round is delayed already.

func (*Ticker) Start added in v0.5.8

func (t *Ticker) Start()

Start starts the Ticker.

func (*Ticker) Stop added in v0.5.8

func (t *Ticker) Stop()

Stop stops the Ticker.

func (*Ticker) UpdateRandomness added in v0.5.8

func (t *Ticker) UpdateRandomness(r Randomness)

UpdateRandomness updates the randomness of the ticker.

type Type

type Type = byte

Type defines a drng payload type

const (
	// TypeCollectiveBeacon defines a CollectiveBeacon payload type
	TypeCollectiveBeacon Type = 1
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL