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.

seal.gno

4.69 Kb · 132 lines
  1package fairness
  2
  3// FairDrop sealed-entry primitives: commit–reveal sealing and seed folding.
  4//
  5// A draw is fair when nobody — the organizer included — can steer it.
  6// FairDrop gets there in three steps:
  7//
  8//  1. While entries are open, an entrant publishes only a seal: the SHA-256
  9//     of a secret they generated locally, bound to the entrant id, the event
 10//     domain (chain + event id) and the entrant's ticket weight.
 11//  2. When entries close, entrants reveal their secrets. A reveal is accepted
 12//     only when it re-hashes to the seal already on chain, so nobody can swap
 13//     their value after seeing the others.
 14//  3. The seed is the SHA-256 fold of every accepted reveal in canonical
 15//     (ascending entrant id) order. Whoever folds the same on-chain reveals
 16//     gets the same seed, and Draw turns that seed into winners
 17//     deterministically.
 18//
 19// What this does not solve, stated plainly: an entrant who never reveals is
 20// dropped from the draw (their ticket does not count either way), and the
 21// choice to withhold shifts which leaves the seed folds over. A revealed
 22// secret cannot be changed, so withholding only ever removes the withholder's
 23// own chance — it cannot pick someone else's winner. Callers should treat a
 24// missed reveal as a forfeit and say so in their UI.
 25
 26import (
 27	"crypto/sha256"
 28	"encoding/hex"
 29	"strconv"
 30	"strings"
 31)
 32
 33const (
 34	// DigestHexLen is the length of a hex-encoded SHA-256 digest.
 35	DigestHexLen = 64
 36
 37	// MinSecretLen is the shortest secret a reveal may carry. Shorter secrets
 38	// are guessable from the public seal and would let a watcher brute-force
 39	// an entrant's reveal before it lands.
 40	MinSecretLen = 16
 41
 42	// MaxSecretLen bounds how much a single reveal may store on chain.
 43	MaxSecretLen = 256
 44
 45	// SealDomain is the domain-separation tag of the sealing format.
 46	SealDomain = "fairdrop/seal/v1"
 47
 48	// SeedDomain is the domain-separation tag of the seed fold.
 49	SeedDomain = "fairdrop/seed/v1"
 50)
 51
 52// sha256Sum returns the raw 32-byte digest of s.
 53func sha256Sum(s string) []byte {
 54	sum := sha256.Sum256([]byte(s))
 55	return sum[:]
 56}
 57
 58func hashHex(s string) string {
 59	return hex.EncodeToString(sha256Sum(s))
 60}
 61
 62// Canonical is the exact string a seal commits to. Binding the entrant id,
 63// the domain (chain + event) and the weight means a seal cannot be replayed
 64// in another event or on another chain, cannot be claimed by another entrant,
 65// and cannot be reused at a different weight.
 66func Canonical(domain string, entrant string, weight int64, secret string) string {
 67	return SealDomain + "|" + domain + "|" + entrant + "|" + strconv.FormatInt(weight, 10) + "|" + secret
 68}
 69
 70// Digest is the seal of one entry: hex SHA-256 over Canonical.
 71func Digest(domain string, entrant string, weight int64, secret string) string {
 72	return hashHex(Canonical(domain, entrant, weight, secret))
 73}
 74
 75// VerifySeal reports whether secret opens commit for this entrant, weight and
 76// domain. Input is normalised (trimmed, lowercased) so a wallet or a human
 77// pasting an upper-case digest is not turned away.
 78func VerifySeal(commit string, domain string, entrant string, weight int64, secret string) bool {
 79	return strings.ToLower(strings.TrimSpace(commit)) == Digest(domain, entrant, weight, secret)
 80}
 81
 82// IsDigest reports whether s is a well-formed lower-case hex SHA-256 digest.
 83func IsDigest(s string) bool {
 84	if len(s) != DigestHexLen {
 85		return false
 86	}
 87	for i := 0; i < len(s); i++ {
 88		c := s[i]
 89		if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') {
 90			continue
 91		}
 92		return false
 93	}
 94	return true
 95}
 96
 97// NormalizeDigest returns the canonical form of a seal as submitted.
 98func NormalizeDigest(s string) string {
 99	return strings.ToLower(strings.TrimSpace(s))
100}
101
102// ValidSecret reports whether s may be used as a reveal secret: printable
103// ASCII without spaces, within the accepted length bounds.
104func ValidSecret(s string) bool {
105	n := len(s)
106	if n < MinSecretLen || n > MaxSecretLen {
107		return false
108	}
109	for i := 0; i < n; i++ {
110		if s[i] < 0x21 || s[i] > 0x7e {
111			return false
112		}
113	}
114	return true
115}
116
117// Leaf is one input of the seed fold: the seal followed by the secret that
118// opened it. Leaves are folded in canonical entrant order.
119func Leaf(commit string, secret string) string {
120	return NormalizeDigest(commit) + ":" + secret
121}
122
123// Seed folds the revealed leaves into the draw seed. The domain, chain id and
124// event id are part of the fold, so identical reveals in two events give
125// different seeds and no seed can be lifted from one event to another.
126func Seed(domain string, chainID string, eventID uint64, leaves []string) string {
127	b := SeedDomain + "|" + domain + "|" + chainID + "|" + strconv.FormatUint(eventID, 10) + "|"
128	for i := 0; i < len(leaves); i++ {
129		b += leaves[i] + "|"
130	}
131	return hashHex(b)
132}