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.

cards source pure

Package cards implements the byte-level primitives of the gno poker protocol (docs/protocol.md sections 1 to 4): the ...

Overview

Package cards implements the byte-level primitives of the gno poker protocol (docs/protocol.md sections 1 to 4): the group prime, seed derivations, the seed permutation, deck parsing and validation, deck Merkle trees and the derivation of official cards from revealed seeds. It does no modular exponentiation; callers that need it use crypto/modexp.

Constants 2

const CardLen, DeckSize, DeckLen, ProofLen, PHex, CanonRootHex, tagCard, tagCommit, tagShufKey, tagCardKey, tagPerm

 1const (
 2	CardLen  = 256
 3	DeckSize = 52
 4	DeckLen  = CardLen * DeckSize
 5	ProofLen = 6 * 32
 6
 7	// PHex is the RFC 3526 2048-bit MODP group (group 14) prime.
 8	PHex = "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74" +
 9		"020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f1437" +
10		"4fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7ed" +
11		"ee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf05" +
12		"98da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb" +
13		"9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3b" +
14		"e39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf695581718" +
15		"3995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff"
16
17	// CanonRootHex is the Merkle root of the canonical deck [m_0 .. m_51].
18	// TestCanonRoot recomputes it with crypto/modexp.
19	CanonRootHex = "cbbd0f99d1b6d995ea84edbcf80a62c245ea59240ccee3fde85b87bd35077030"
20
21	tagCard    = "gno-poker/card/v1"
22	tagCommit  = "gno-poker/commit/v1"
23	tagShufKey = "gno-poker/shufkey/v1"
24	tagCardKey = "gno-poker/cardkey/v1"
25	tagPerm    = "gno-poker/perm/v1"
26)
source

const MaxBase64Len

1const MaxBase64Len = (DeckLen + 2) / 3 * 4
source

MaxBase64Len is the longest input DecodeBase64 accepts: one full deck.

Functions 20

func CanonRoot

1func CanonRoot() []byte
source

CanonRoot returns the hardcoded canonical deck root.

func CardKey

1func CardKey(seed []byte, i int) []byte
source

CardKey returns the card key k_i derived from seed for deck position i.

func CardPreimage

1func CardPreimage(c int) []byte
source

CardPreimage returns the 256-byte value x of card c; the plaintext card is m_c = x^2 mod P.

func CardString

1func CardString(c int) string
source

CardString renders card index c as e.g. "As", "Td", "2c".

func Commit

1func Commit(seed []byte) []byte
source

Commit returns H("gno-poker/commit/v1" || seed).

func DecodeBase64

1func DecodeBase64(s string) ([]byte, error)
source

DecodeBase64 decodes padded standard base64 and rejects non-canonical input and anything longer than MaxBase64Len.

func DecodeElement

1func DecodeElement(b64 string) ([]byte, error)
source

DecodeElement decodes one base64 deck element and checks its range.

func DecodeHex

1func DecodeHex(s string, n int) ([]byte, error)
source

DecodeHex decodes lowercase hex of exactly n bytes.

func EncodeDeck

1func EncodeDeck(deck []byte) string
source

EncodeDeck returns the standard base64 encoding of raw deck bytes.

func EncodeHex

1func EncodeHex(b []byte) string
source

EncodeHex returns lowercase hex.

func IsValidElement

1func IsValidElement(v []byte) bool
source

IsValidElement reports whether 1 < v < P for a 256-byte big-endian v.

func LeafHash

1func LeafHash(el []byte) []byte
source

LeafHash returns H(0x00 || element).

func MerkleProof

1func MerkleProof(deck []byte, i int) []byte
source

MerkleProof returns the 192-byte proof for position i of a raw deck.

func MerkleRoot

1func MerkleRoot(deck []byte) []byte
source

MerkleRoot returns the root of a raw 13312-byte deck.

func OfficialCards

1func OfficialCards(perms [][]int) []int
source

OfficialCards maps every deck position to its card index given the permutations of hand players 0..n-1.

func P

1func P() []byte
source

P returns a copy of the prime as 256 big-endian bytes.

func ParseDeck

1func ParseDeck(b64 string) ([]byte, []byte, error)
source

ParseDeck decodes a base64 deck, validates every element (1 < v < P, no duplicates) and returns the raw deck bytes and its Merkle root.

func Permutation

1func Permutation(seed []byte) []int
source

Permutation returns the Fisher-Yates permutation of 0..51 determined by seed.

func ShuffleKey

1func ShuffleKey(seed []byte) []byte
source

ShuffleKey returns the shuffle exponent e derived from seed.

func VerifyProof

1func VerifyProof(root, el []byte, i int, proof []byte) bool
source

VerifyProof checks that el sits at position i under root.

Dependencies

Source Files 3