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.

perm.gno

2.57 Kb · 89 lines
 1package media
 2
 3import (
 4	"crypto/sha256"
 5	"strconv"
 6
 7	"gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia/safe/v1"
 8)
 9
10// Token → artwork assignment (design §7; frontend chain/perm.ts is the twin).
11//
12//	K         = sha256hex(PermDomain + collectionID + "|" + salt)
13//	slot(tid) = Permute(K, slots, tid-1)    (single-artwork store: tid-1)
14//
15// The salt exists only once the collection is Final (commit–reveal v2), so
16// nobody, the creator included, knows any token's slot before every token
17// was sold and both secrets were revealed.
18
19// PermKey is the permutation key of collection cid with its final salt.
20func PermKey(cid, salt string) string {
21	return safe.Sha256Hex(PermDomain + cid + "|" + salt)
22}
23
24// Permute is a bijection on [0, n): a 4-round Feistel network over b bits
25// (b = bitlen(n-1) rounded up to an even number ≥ 2, so 2^b < 4n) with cycle
26// walking. Round function: F = big-endian uint64 of the first 8 bytes of
27// sha256(keyHex + "|" + r + "|" + R), masked to h = b/2 bits, with r and R
28// in decimal. It panics unless 1 ≤ n and 0 ≤ x < n.
29func Permute(keyHex string, n, x int64) int64 {
30	if n < 1 || x < 0 || x >= n {
31		panic("media: permute: need 0 <= x < n")
32	}
33	if n == 1 {
34		return 0
35	}
36	b := bitLen(uint64(n - 1))
37	if b < 2 {
38		b = 2
39	}
40	if b%2 == 1 {
41		b++
42	}
43	h := uint(b / 2)
44	mask := uint64(1)<<h - 1
45	prefix := keyHex + "|"
46	v := uint64(x)
47	// The walk stays on x's cycle, which contains x itself (< n), so it
48	// ends; the bound only guards against a broken hash.
49	for walk := 0; walk < 1<<uint(b); walk++ {
50		l, r := v>>h, v&mask
51		for round := 0; round < 4; round++ {
52			f := roundF(prefix+strconv.Itoa(round)+"|", r) & mask
53			l, r = r, l^f
54		}
55		v = l<<h | r
56		if v < uint64(n) {
57			return int64(v)
58		}
59	}
60	panic("media: permute: cycle walk did not terminate")
61}
62
63func roundF(prefix string, r uint64) uint64 {
64	sum := sha256.Sum256([]byte(prefix + strconv.FormatUint(r, 10)))
65	return uint64(sum[0])<<56 | uint64(sum[1])<<48 | uint64(sum[2])<<40 | uint64(sum[3])<<32 |
66		uint64(sum[4])<<24 | uint64(sum[5])<<16 | uint64(sum[6])<<8 | uint64(sum[7])
67}
68
69func bitLen(v uint64) int {
70	n := 0
71	for v > 0 {
72		n++
73		v >>= 1
74	}
75	return n
76}
77
78// Slot is the slot of token tid (1-based) in a store of `artworks`
79// artworks and `slots` slots: the identity tid-1 for a single-artwork store
80// (1/1 and editions: "Edition 3 of 50"), Permute(K, slots, tid-1) otherwise.
81func Slot(keyHex string, artworks, slots, tid int64) int64 {
82	if tid < 1 || tid > slots {
83		panic("media: slot: token id out of range")
84	}
85	if artworks == 1 {
86		return tid - 1
87	}
88	return Permute(keyHex, slots, tid-1)
89}