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.

challenge.gno

2.85 Kb · 117 lines
  1package poker
  2
  3import (
  4	"bytes"
  5
  6	"gno.land/p/g1lnkytfqcjwllws63gvf0mv9yt04aswy4y9amhm/poker/cards"
  7)
  8
  9// Challenge disputes one element of a shuffle or rekey pass during the
 10// challenge window (protocol.md section 5).
 11func Challenge(cur realm, tableID, handNo uint64, accused int, kind string, index int, inB64, inProofHex, outB64, outProofHex string) {
 12	if !challengesEnabled {
 13		panic("challenges are disabled on this deployment")
 14	}
 15	ctx := newCtx(cur)
 16	sent := sentUgnot(cur)
 17	t, h, c := handFor(cur, tableID, handNo)
 18	if h.Phase != phChallenge {
 19		panic("not in challenge window")
 20	}
 21	if ctx.height > h.Deadline {
 22		panic("challenge window has passed")
 23	}
 24	n := h.n()
 25	if accused < 0 || accused >= n {
 26		panic("invalid accused index")
 27	}
 28	if accused == c {
 29		panic("cannot challenge yourself")
 30	}
 31	if index < 0 || index >= cards.DeckSize {
 32		panic("invalid deck index")
 33	}
 34	bond := t.ChallengeBond
 35	challengerSeat := t.Seats[h.Players[c].Seat]
 36	if sent > 0 {
 37		if sent != bond {
 38			panic("attached ugnot must equal the challenge bond")
 39		}
 40	} else if challengerSeat.Stack < bond {
 41		panic("stack too small for the challenge bond")
 42	}
 43
 44	in, err := cards.DecodeElement(inB64)
 45	if err != nil {
 46		panic("in: " + err.Error())
 47	}
 48	out, err := cards.DecodeElement(outB64)
 49	if err != nil {
 50		panic("out: " + err.Error())
 51	}
 52	inProof, err := cards.DecodeHex(inProofHex, cards.ProofLen)
 53	if err != nil {
 54		panic("in proof: " + err.Error())
 55	}
 56	outProof, err := cards.DecodeHex(outProofHex, cards.ProofLen)
 57	if err != nil {
 58		panic("out proof: " + err.Error())
 59	}
 60	seed := seedBytes(h.Players[accused])
 61
 62	var inRoot, outRoot []byte
 63	inPos := index
 64	switch kind {
 65	case "shuffle":
 66		if accused == 0 {
 67			inRoot = cards.CanonRoot()
 68		} else {
 69			inRoot = rootAt(h, accused-1)
 70		}
 71		outRoot = rootAt(h, accused)
 72		inPos = cards.Permutation(seed)[index]
 73	case "rekey":
 74		inRoot = rootAt(h, n+accused-1)
 75		outRoot = rootAt(h, n+accused)
 76	default:
 77		panic("kind must be shuffle or rekey")
 78	}
 79	if !cards.VerifyProof(inRoot, in, inPos, inProof) {
 80		panic("invalid input proof")
 81	}
 82	if !cards.VerifyProof(outRoot, out, index, outProof) {
 83		panic("invalid output proof")
 84	}
 85
 86	e := cards.ShuffleKey(seed)
 87	var guilty bool
 88	if kind == "shuffle" {
 89		guilty = !bytes.Equal(powModP(in, e), out)
 90	} else {
 91		k := cards.CardKey(seed, index)
 92		guilty = !bytes.Equal(powModP(out, e), powModP(in, k))
 93	}
 94
 95	rec := ChallengeRecord{Challenger: c, Accused: accused, Kind: kind, Index: index, Guilty: guilty}
 96	h.Result.Challenges = append(h.Result.Challenges, rec)
 97	accusedSeat := t.Seats[h.Players[accused].Seat]
 98	if guilty {
 99		if sent > 0 {
100			ctx.pay(ctx.caller, sent)
101		}
102		t.abort(ctx, []int{accused}, true)
103		return
104	}
105	if sent == 0 {
106		challengerSeat.Stack -= bond
107	}
108	accusedSeat.Stack += bond
109}
110
111func rootAt(h *Hand, pass int) []byte {
112	b, err := cards.DecodeHex(h.Roots[pass], 32)
113	if err != nil {
114		panic("corrupt root")
115	}
116	return b
117}