Playground
draw.gno
package fairness
// Deterministic draw: a seed-driven byte stream and a weighted, multi-winner
// sample taken from it without replacement.
//
// Everything here is a pure function of its inputs, so the same seed over the
// same roster gives the same winners wherever it is recomputed — in a realm,
// in a test, or in a verifier script off chain.
import (
"errors"
"strconv"
)
// maxUint64 is 2^64-1.
const maxUint64 uint64 = 1<<64 - 1
// Ticket is one entry in a draw.
//
// ID is the entrant's stable identity — FairDrop uses the bech32 address
// string, so the roster is ordered and deduplicated by address. Commit is the
// seal, Secret the revealed preimage (empty until reveal) and Rank 0 until the
// draw, then 1..k for winners. The package only reads ID, Weight and Commit;
// Secret and Rank are carried so a realm can keep one record per ticket.
type Ticket struct {
ID string
Weight uint64
Commit string
Secret string
Rank int
}
// Insert adds t to a roster kept in canonical (ascending ID) order and refuses
// an ID that is already present. One call therefore enforces both rules a
// draw depends on: a single ticket per entrant, and the fixed ordering the
// seed fold and the draw walk over.
//
// The returned slice is a new slice; the caller must keep it.
func Insert(roster []Ticket, t Ticket) ([]Ticket, error) {
if t.ID == "" {
return roster, errors.New("fairness: empty entrant id")
}
if t.Weight == 0 {
return roster, errors.New("fairness: weight must be positive")
}
n := len(roster)
lo, hi := 0, n
for lo < hi {
mid := (lo + hi) / 2
if roster[mid].ID < t.ID {
lo = mid + 1
} else {
hi = mid
}
}
if lo < n && roster[lo].ID == t.ID {
return roster, errors.New("fairness: entrant already holds a ticket")
}
out := make([]Ticket, n+1)
for i := 0; i < lo; i++ {
out[i] = roster[i]
}
out[lo] = t
for i := lo; i < n; i++ {
out[i+1] = roster[i]
}
return out, nil
}
// Find returns the index of the ticket held by id, or -1.
func Find(roster []Ticket, id string) int {
lo, hi := 0, len(roster)
for lo < hi {
mid := (lo + hi) / 2
if roster[mid].ID < id {
lo = mid + 1
} else {
hi = mid
}
}
if lo < len(roster) && roster[lo].ID == id {
return lo
}
return -1
}
// Stream is a deterministic byte stream: SHA-256 in counter mode over the
// seed. It exists so a draw needs no randomness from the chain, which is what
// makes the result reproducible by anyone holding the seed.
type Stream struct {
seed string
ctr uint64
pool []byte
pos int
}
// NewStream starts a stream at seed.
func NewStream(seed string) *Stream {
return &Stream{seed: seed}
}
func (s *Stream) refill() {
sum := sha256Sum(s.seed + "|" + strconv.FormatUint(s.ctr, 10))
s.ctr++
s.pool = sum
s.pos = 0
}
// Uint64 returns the next 8 stream bytes as a big-endian unsigned integer.
func (s *Stream) Uint64() uint64 {
if s.pool == nil || s.pos+8 > len(s.pool) {
s.refill()
}
var v uint64
for i := 0; i < 8; i++ {
v = (v << 8) | uint64(s.pool[s.pos+i])
}
s.pos += 8
return v
}
// Below returns a uniform integer in [0, n) by rejection sampling, so no
// residue is favoured by the modulo. It panics on n == 0.
func (s *Stream) Below(n uint64) uint64 {
if n == 0 {
panic("fairness: Below(0)")
}
limit := (maxUint64 / n) * n
for {
v := s.Uint64()
if v < limit {
return v % n
}
}
}
// Draw samples up to k distinct tickets from roster, without replacement,
// with probability proportional to Weight, and returns their IDs in the order
// they were drawn (index 0 is rank 1). Zero-weight tickets are never drawn.
//
// The order of roster must be canonical (as Insert maintains it), because the
// stream walks the roster in that order.
func Draw(seed string, roster []Ticket, k int) []string {
if k <= 0 || len(roster) == 0 {
return []string{}
}
if k > len(roster) {
k = len(roster)
}
// Work on a copy so the caller's roster (realm state) is untouched.
pool := make([]Ticket, len(roster))
for i := 0; i < len(roster); i++ {
pool[i] = roster[i]
}
s := NewStream(seed)
winners := make([]string, 0, k)
for len(winners) < k {
var total uint64
for i := 0; i < len(pool); i++ {
w := pool[i].Weight
if w > 0 && total > maxUint64-w {
panic("fairness: total weight overflows uint64")
}
total += w
}
if total == 0 {
break
}
pick := s.Below(total)
at := -1
var acc uint64
for i := 0; i < len(pool); i++ {
if pool[i].Weight == 0 {
continue
}
acc += pool[i].Weight
if pick < acc {
at = i
break
}
}
if at < 0 {
// Unreachable while total > 0, kept as a hard stop rather than a
// silent wrong pick.
panic("fairness: weighted pick fell through")
}
winners = append(winners, pool[at].ID)
kept := make([]Ticket, 0, len(pool)-1)
for i := 0; i < len(pool); i++ {
if i != at {
kept = append(kept, pool[i])
}
}
pool = kept
}
return winners
}
// TotalWeight sums the weights of a roster, panicking rather than wrapping.
func TotalWeight(roster []Ticket) uint64 {
var total uint64
for i := 0; i < len(roster); i++ {
w := roster[i].Weight
if w > 0 && total > maxUint64-w {
panic("fairness: total weight overflows uint64")
}
total += w
}
return total
}
gnomod.toml
module = "gno.land/p/g1k6cl2huxpps9fxls8tqyxknvl7fgq9hua3f2yt/fairness" gno = "0.9" [addpkg] creator = "g1k6cl2huxpps9fxls8tqyxknvl7fgq9hua3f2yt" height = 1375256 max_deposit = "50000000ugnot"
seal.gno
package fairness
// FairDrop sealed-entry primitives: commit–reveal sealing and seed folding.
//
// A draw is fair when nobody — the organizer included — can steer it.
// FairDrop gets there in three steps:
//
// 1. While entries are open, an entrant publishes only a seal: the SHA-256
// of a secret they generated locally, bound to the entrant id, the event
// domain (chain + event id) and the entrant's ticket weight.
// 2. When entries close, entrants reveal their secrets. A reveal is accepted
// only when it re-hashes to the seal already on chain, so nobody can swap
// their value after seeing the others.
// 3. The seed is the SHA-256 fold of every accepted reveal in canonical
// (ascending entrant id) order. Whoever folds the same on-chain reveals
// gets the same seed, and Draw turns that seed into winners
// deterministically.
//
// What this does not solve, stated plainly: an entrant who never reveals is
// dropped from the draw (their ticket does not count either way), and the
// choice to withhold shifts which leaves the seed folds over. A revealed
// secret cannot be changed, so withholding only ever removes the withholder's
// own chance — it cannot pick someone else's winner. Callers should treat a
// missed reveal as a forfeit and say so in their UI.
import (
"crypto/sha256"
"encoding/hex"
"strconv"
"strings"
)
const (
// DigestHexLen is the length of a hex-encoded SHA-256 digest.
DigestHexLen = 64
// MinSecretLen is the shortest secret a reveal may carry. Shorter secrets
// are guessable from the public seal and would let a watcher brute-force
// an entrant's reveal before it lands.
MinSecretLen = 16
// MaxSecretLen bounds how much a single reveal may store on chain.
MaxSecretLen = 256
// SealDomain is the domain-separation tag of the sealing format.
SealDomain = "fairdrop/seal/v1"
// SeedDomain is the domain-separation tag of the seed fold.
SeedDomain = "fairdrop/seed/v1"
)
// sha256Sum returns the raw 32-byte digest of s.
func sha256Sum(s string) []byte {
sum := sha256.Sum256([]byte(s))
return sum[:]
}
func hashHex(s string) string {
return hex.EncodeToString(sha256Sum(s))
}
// Canonical is the exact string a seal commits to. Binding the entrant id,
// the domain (chain + event) and the weight means a seal cannot be replayed
// in another event or on another chain, cannot be claimed by another entrant,
// and cannot be reused at a different weight.
func Canonical(domain string, entrant string, weight int64, secret string) string {
return SealDomain + "|" + domain + "|" + entrant + "|" + strconv.FormatInt(weight, 10) + "|" + secret
}
// Digest is the seal of one entry: hex SHA-256 over Canonical.
func Digest(domain string, entrant string, weight int64, secret string) string {
return hashHex(Canonical(domain, entrant, weight, secret))
}
// VerifySeal reports whether secret opens commit for this entrant, weight and
// domain. Input is normalised (trimmed, lowercased) so a wallet or a human
// pasting an upper-case digest is not turned away.
func VerifySeal(commit string, domain string, entrant string, weight int64, secret string) bool {
return strings.ToLower(strings.TrimSpace(commit)) == Digest(domain, entrant, weight, secret)
}
// IsDigest reports whether s is a well-formed lower-case hex SHA-256 digest.
func IsDigest(s string) bool {
if len(s) != DigestHexLen {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') {
continue
}
return false
}
return true
}
// NormalizeDigest returns the canonical form of a seal as submitted.
func NormalizeDigest(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
// ValidSecret reports whether s may be used as a reveal secret: printable
// ASCII without spaces, within the accepted length bounds.
func ValidSecret(s string) bool {
n := len(s)
if n < MinSecretLen || n > MaxSecretLen {
return false
}
for i := 0; i < n; i++ {
if s[i] < 0x21 || s[i] > 0x7e {
return false
}
}
return true
}
// Leaf is one input of the seed fold: the seal followed by the secret that
// opened it. Leaves are folded in canonical entrant order.
func Leaf(commit string, secret string) string {
return NormalizeDigest(commit) + ":" + secret
}
// Seed folds the revealed leaves into the draw seed. The domain, chain id and
// event id are part of the fold, so identical reveals in two events give
// different seeds and no seed can be lifted from one event to another.
func Seed(domain string, chainID string, eventID uint64, leaves []string) string {
b := SeedDomain + "|" + domain + "|" + chainID + "|" + strconv.FormatUint(eventID, 10) + "|"
for i := 0; i < len(leaves); i++ {
b += leaves[i] + "|"
}
return hashHex(b)
}