Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

drand.gno

3.83 Kb · 116 lines
  1// Package drand verifies drand "evmnet" beacons on gno.land and turns them
  2// into randomness.
  3//
  4// evmnet is the drand network that signs on the BN254 curve (scheme
  5// bls-bn254-unchained-on-g1, one beacon every 3 seconds). gno.land ships
  6// BN254 as native precompiles, so a beacon is checked fully on-chain: no
  7// trusted relayer, no oracle committee. Whoever submits a beacon can only
  8// submit the real one.
  9//
 10// Verification: e(sig, G2) == e(H(m), pk), with m = keccak256(round) and H
 11// the RFC 9380 SVDW hash-to-curve, checked as one native pairing product.
 12package drand
 13
 14import (
 15	"crypto/bn254"
 16	"crypto/keccak256"
 17	"crypto/sha256"
 18	"encoding/binary"
 19	"encoding/hex"
 20	"math/rand"
 21)
 22
 23// evmnet network parameters, from https://api.drand.sh/<ChainHash>/info.
 24const (
 25	ChainHash   = "04f1e9062b8a81f848fded9c12306733282b2727ecced50032187751166ec8c3"
 26	SchemeID    = "bls-bn254-unchained-on-g1"
 27	GenesisTime = int64(1727521075)
 28	Period      = int64(3)
 29	SigSize     = 64
 30)
 31
 32var (
 33	// Group public key on G2, encoded x_im|x_re|y_im|y_re (EIP-197 order).
 34	pubKey = mustHex("07e1d1d335df83fa98462005690372c643340060d205306a9aa8106b6bd0b3820557ec32c2ad488e4d4f6008f89a346f18492092ccc0d594610de2732c8b808f0095685ae3a85ba243747b1b2f426049010f6b73a0cf1d389351d5aaaa1047f6297d3a4f9749b33eb2d904c9d9ebf17224150ddd7abd7567a9bec6c74480ee0b")
 35	// Negated G2 generator, same encoding (y_im and y_re replaced by p - y).
 36	negG2Gen = mustHex("198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c21800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed275dc4a288d1afb3cbb1ac09187524c7db36395df7be3b99e673b13a075a65ec1d9befcd05a5323e6da4d435f3b617cdb3af83285c2df711ef39c01571827f9d")
 37)
 38
 39// RoundAt returns the latest round published at or before unix time t
 40// (0 before genesis).
 41func RoundAt(t int64) uint64 {
 42	if t < GenesisTime {
 43		return 0
 44	}
 45	return uint64((t-GenesisTime)/Period) + 1
 46}
 47
 48// TimeOf returns the unix time at which round is published.
 49func TimeOf(round uint64) int64 {
 50	if round == 0 {
 51		return GenesisTime
 52	}
 53	return GenesisTime + int64(round-1)*Period
 54}
 55
 56// Message returns the digest drand signs for round: keccak256(uint64_be(round)).
 57func Message(round uint64) [32]byte {
 58	var b [8]byte
 59	binary.BigEndian.PutUint64(b[:], round)
 60	return keccak256.Sum256(b[:])
 61}
 62
 63// Verify reports whether sig is the evmnet signature of round, checking
 64// e(sig, -G2) * e(H(m), pk) == 1. The pairing native rejects points off the
 65// curve and non-canonical coordinates.
 66func Verify(round uint64, sig []byte) bool {
 67	if round == 0 || len(sig) != SigSize {
 68		return false
 69	}
 70	msg := Message(round)
 71	in := make([]byte, 0, 2*bn254.PairingChunkSize)
 72	in = append(in, sig...)
 73	in = append(in, negG2Gen...)
 74	in = append(in, HashToG1(msg[:])...)
 75	in = append(in, pubKey...)
 76	out := bn254.PairingCheck(in)
 77	return len(out) == bn254.PairingOutputSize && out[31] == 1
 78}
 79
 80// Randomness returns the beacon randomness for sig, sha256(sig), the same
 81// value drand publishes as "randomness".
 82func Randomness(sig []byte) [32]byte {
 83	return sha256.Sum256(sig)
 84}
 85
 86// Derive returns sha256(beacon || salt). Use a distinct salt per consumer and
 87// per draw so that two users of the same round get independent values.
 88func Derive(beacon [32]byte, salt string) [32]byte {
 89	buf := make([]byte, 0, 32+len(salt))
 90	buf = append(buf, beacon[:]...)
 91	buf = append(buf, salt...)
 92	return sha256.Sum256(buf)
 93}
 94
 95// NewRand returns a deterministic PRNG seeded from 32 bytes of randomness,
 96// for drawing several values (IntN, Perm, Shuffle) from one beacon.
 97func NewRand(seed [32]byte) *rand.Rand {
 98	be := binary.BigEndian
 99	return rand.New(rand.NewPCG(be.Uint64(seed[0:8]), be.Uint64(seed[8:16])))
100}
101
102func mustHex(s string) []byte {
103	b, err := hex.DecodeString(s)
104	if err != nil {
105		panic(err)
106	}
107	return b
108}
109
110func mustFe(s string) fe {
111	e, ok := feFromCanonical(mustHex(s))
112	if !ok {
113		panic("drand: constant not reduced")
114	}
115	return e
116}