murmur3demo.gno
3.58 Kb · 96 lines
1// Package murmur3demo is a small gnoweb demo of the MurmurHash3 x86_32 hash
2// provided by the [p/moul/x/daily/murmur3](/p/moul/x/daily/murmur3/v0)
3// library: it hashes a few strings, runs the SMHasher self-test on chain, and
4// shows a thousand keys spreading evenly over eight shards.
5//
6// It contains no hashing logic of its own. Stateless, so Render is
7// deterministic.
8package murmur3demo
9
10import (
11 "strconv"
12 "strings"
13
14 "gno.land/p/moul/kit/ui/v0"
15 "gno.land/p/moul/md/v0"
16 "gno.land/p/moul/x/daily/murmur3/v0"
17)
18
19var samples = []string{"", "a", "abc", "123456789", "hello world", "hello worle"}
20
21// shards is the bucket count the spread table demonstrates.
22const shards = 8
23
24// keys is how many synthetic keys the spread table distributes.
25const keys = 1000
26
27// Render renders the demo for gnoweb.
28//
29// Render("") / Render("/") -> the samples, the self-test, the spread
30// Render("/<text>") -> hash and shard that text
31func Render(path string) string {
32 var b strings.Builder
33 b.WriteString("# MurmurHash3 x86_32\n\n")
34 b.WriteString("Austin Appleby's non-cryptographic hash, demoing the ")
35 b.WriteString("[`p/moul/x/daily/murmur3`](/p/moul/x/daily/murmur3/v0) library.\n\n")
36
37 if in := parseArg(path); in != "" {
38 b.WriteString("## " + md.InlineCode(in) + "\n\n")
39 t := ui.NewTable("seed", "hash", "bucket of 8")
40 t.Row("0", md.InlineCode(murmur3.SumHex(in)), strconv.Itoa(murmur3.Bucket(in, shards)))
41 t.Row("1", md.InlineCode(murmur3.Hex(murmur3.Sum32Seed(in, 1))), "n/a")
42 b.WriteString(t.String())
43 b.WriteString("\nTwo seeds, two unrelated results: that is how one function ")
44 b.WriteString("becomes a family of them.\n")
45 return b.String()
46 }
47
48 b.WriteString("## Vectors\n\n")
49 st := ui.NewTable("input", "seed 0", "seed 1")
50 for _, s := range samples {
51 label := "_(empty)_"
52 if s != "" {
53 label = md.InlineCode(s)
54 }
55 st.Row(label, md.InlineCode(murmur3.SumHex(s)), md.InlineCode(murmur3.Hex(murmur3.Sum32Seed(s, 1))))
56 }
57 b.WriteString(st.String())
58 b.WriteString("\nThe last two inputs differ by one letter, and their seed-0 hashes ")
59 b.WriteString("differ in 18 of 32 bits, close to the half a good mix aims for. ")
60 b.WriteString("That avalanche is the finalizer doing its job.\n\n")
61
62 b.WriteString("## Self-test\n\n")
63 b.WriteString("SMHasher hashes 256 growing keys under 256 seeds and hashes the ")
64 b.WriteString("results. For MurmurHash3 x86_32 the answer is `b0f57ee3`, and this ")
65 b.WriteString("realm computes it on chain every time you load the page:\n\n")
66 b.WriteString("`" + murmur3.Hex(murmur3.Verification()) + "`\n\n")
67
68 b.WriteString("## Spread\n\n")
69 b.WriteString("`key-0` through `key-" + strconv.Itoa(keys-1) + "` over " +
70 strconv.Itoa(shards) + " buckets. A hash worth using fills all of them:\n\n")
71 counts := make([]int, shards)
72 for i := 0; i < keys; i++ {
73 counts[murmur3.Bucket("key-"+strconv.Itoa(i), shards)]++
74 }
75 bt := ui.NewTable("bucket", "keys", "")
76 for i, c := range counts {
77 bt.Row(strconv.Itoa(i), strconv.Itoa(c), strings.Repeat("#", c/10))
78 }
79 b.WriteString(bt.String())
80
81 b.WriteString("\nAppend a path to hash your own text: ")
82 b.WriteString("[`/gno.land`](/r/moul/x/daily/murmur3demo/v0:gno.land)\n\n")
83 b.WriteString("> ⚠️ **Not a cryptographic hash.** MurmurHash3 is invertible and ")
84 b.WriteString("collisions are cheap to generate, so anyone who picks the keys can ")
85 b.WriteString("pick the bucket. Use it to spread, never to commit or authenticate.\n")
86 return b.String()
87}
88
89func parseArg(path string) string {
90 s := strings.TrimSpace(path)
91 s = strings.TrimPrefix(s, "/")
92 if i := strings.IndexByte(s, '/'); i >= 0 {
93 s = s[:i]
94 }
95 return s
96}