func (kc *keychain) Sample() (ecid.ID, error) {
	if len(kc.pubs) == 0 {
		return nil, ErrEmptyKeychain
	}
	i := kc.rng.Int31n(int32(len(kc.pubs)))
	return kc.privs[kc.pubs[i]], nil
}
Sample returns a uniformly random key from the keychain.
package keychain
import (
	"errors"
	"fmt"
	"io/ioutil"
	"math/rand"
	"sort"
	"github.com/drausin/libri/libri/common/ecid"
	"github.com/golang/protobuf/proto"
)
const (
	StandardScryptN = 1 << 18
	StandardScryptP = 1
	LightScryptN = 1 << 12
	LightScryptP = 6
)
var (
	ErrEmptyKeychain = errors.New("empty keychain")
	ErrUnexpectedMissingKey = errors.New("missing key")
)
type Getter interface {
	Get(publicKey []byte) (ecid.ID, bool)
}
type Sampler interface {
	Sample() (ecid.ID, error)
}
type GetterSampler interface {
	Getter
	Sampler
}
type keychain struct {
	privs map[string]ecid.ID
	pubs []string
	rng *rand.Rand
}
func New(n int) GetterSampler {
	ecids := make([]ecid.ID, n)
	for i := 0; i < n; i++ {
		ecids[i] = ecid.NewRandom()
	}
	return FromECIDs(ecids)
}
func FromECIDs(ecids []ecid.ID) GetterSampler {
	pubs := make([]string, len(ecids))
	privs := make(map[string]ecid.ID)
	for i, priv := range ecids {
		pubs[i] = pubKeyString(priv.PublicKeyBytes())
		privs[pubs[i]] = priv
	}
	sort.Strings(pubs)
	return &keychain{
		privs: privs,
		pubs:  pubs,
		rng:   rand.New(rand.NewSource(int64(len(privs)))),
	}
}