Playground
commitrevealdemo.gno
// Package commitrevealdemo is a small gnoweb demo of the commit-reveal scheme
// provided by the [p/moul/x/daily/commitreveal](/p/nym-moultest123/x/daily/commitreveal/v0)
// library: the two phases, and what the salt is for.
//
// It contains no crypto of its own. Stateless, so Render is deterministic —
// which is precisely what the library is for.
package commitrevealdemo
import (
"strings"
"gno.land/p/nym-moultest123/kit/ui/v0"
"gno.land/p/nym-moultest123/x/daily/commitreveal/v0"
)
// Render renders the demo for gnoweb.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Commit–Reveal\n\n")
b.WriteString("Bind to a choice without disclosing it, demoing the ")
b.WriteString("[`p/moul/x/daily/commitreveal`](/p/nym-moultest123/x/daily/commitreveal/v0) library.\n\n")
b.WriteString("## The problem\n\n")
b.WriteString("A transaction is public before it executes. In a sealed-bid auction or ")
b.WriteString("a simultaneous-move game, whoever moves last reads everyone else's ")
b.WriteString("move and wins for free.\n\n")
aliceSalt := "alice-secret-salt-1"
bobSalt := "bob-secret-salt-0001"
aliceCommit := commitreveal.MustCommit("rock", aliceSalt)
bobCommit := commitreveal.MustCommit("paper", bobSalt)
b.WriteString("## Phase 1 — commit\n\n")
b.WriteString("Each player publishes only `H(value ‖ salt)`. Nothing about the move ")
b.WriteString("leaks, but neither can change it later.\n\n")
b.WriteString("| player | commitment |\n|---|---|\n")
b.WriteString("| alice | `" + ui.ShortN(aliceCommit, 16, 0) + "` |\n")
b.WriteString("| bob | `" + ui.ShortN(bobCommit, 16, 0) + "` |\n")
b.WriteString("\n## Phase 2 — reveal\n\n")
b.WriteString("Now the values and salts are published and checked against the ")
b.WriteString("commitments recorded earlier:\n\n")
b.WriteString("| check | result |\n|---|---|\n")
row(&b, "alice opens with `rock`", commitreveal.Open(aliceCommit, "rock", aliceSalt))
row(&b, "bob opens with `paper`", commitreveal.Open(bobCommit, "paper", bobSalt))
row(&b, "bob tries `scissors` instead", commitreveal.Open(bobCommit, "scissors", bobSalt))
row(&b, "bob claims alice's commitment", commitreveal.Open(aliceCommit, "rock", bobSalt))
b.WriteString("\nBob cannot switch his move after seeing Alice's, and cannot pass off ")
b.WriteString("her commitment as his own.\n\n")
b.WriteString("## Why the salt is mandatory\n\n")
b.WriteString("Rock-paper-scissors has three possible moves. Without a salt there are ")
b.WriteString("exactly three possible hashes, and hashing all three breaks the scheme ")
b.WriteString("outright. The library refuses a salt shorter than `")
b.WriteString(itoa(commitreveal.MinSaltLen) + "` bytes rather than leaving that as advice:\n\n")
_, err := commitreveal.Commit("rock", "tooshort")
b.WriteString("- `Commit(\"rock\", \"tooshort\")` → `")
if err != nil {
b.WriteString(err.Error())
}
b.WriteString("`\n\n")
b.WriteString("The salt also keeps two players who pick the *same* move from ")
b.WriteString("publishing the same commitment:\n\n")
one := commitreveal.MustCommit("rock", "salt-one-0123456789")
two := commitreveal.MustCommit("rock", "salt-two-0123456789")
b.WriteString("| same move, different salt | commitment |\n|---|---|\n")
b.WriteString("| player 1 | `" + ui.ShortN(one, 16, 0) + "` |\n")
b.WriteString("| player 2 | `" + ui.ShortN(two, 16, 0) + "` |\n")
b.WriteString("\n## Two details that are easy to get wrong\n\n")
b.WriteString("- **Length-prefixed hashing.** With plain concatenation `(\"ab\",\"cd…\")` ")
b.WriteString("and `(\"abc\",\"d…\")` hash identically, so one commitment could be opened ")
b.WriteString("two different ways.\n")
b.WriteString("- **Constant-time comparison.** A short-circuiting check leaks, through ")
b.WriteString("timing, how many leading bytes of a guess were right — enough to rebuild ")
b.WriteString("a commitment byte by byte.\n")
return b.String()
}
func row(b *strings.Builder, label string, err error) {
b.WriteString("| " + label + " | ")
if err == nil {
b.WriteString("✅ accepted")
} else {
b.WriteString("❌ `" + err.Error() + "`")
}
b.WriteString(" |\n")
}
func itoa(i int) string {
if i == 0 {
return "0"
}
s := ""
for i > 0 {
s = string(rune('0'+i%10)) + s
i /= 10
}
return s
}
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/commitrevealdemo/v1" gno = "0.9" private = true [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186413 max_deposit = "5000000ugnot"
render_example_test.gno
package commitrevealdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Commit–Reveal
//
// Bind to a choice without disclosing it, demoing the [`p/moul/x/daily/commitreveal`](/p/nym-moultest123/x/daily/commitreveal/v0) library.
//
// ## The problem
//
// A transaction is public before it executes. In a sealed-bid auction or a simultaneous-move game, whoever moves last reads everyone else's move and wins for free.
//
// ## Phase 1 — commit
//
// Each player publishes only `H(value ‖ salt)`. Nothing about the move leaks, but neither can change it later.
//
// | player | commitment |
// |---|---|
// | alice | `11811a2d4eede29f…` |
// | bob | `db066e8c917e07d5…` |
//
// ## Phase 2 — reveal
//
// Now the values and salts are published and checked against the commitments recorded earlier:
//
// | check | result |
// |---|---|
// | alice opens with `rock` | ✅ accepted |
// | bob opens with `paper` | ✅ accepted |
// | bob tries `scissors` instead | ❌ `commitreveal: reveal does not match the commitment` |
// | bob claims alice's commitment | ❌ `commitreveal: reveal does not match the commitment` |
//
// Bob cannot switch his move after seeing Alice's, and cannot pass off her commitment as his own.
//
// ## Why the salt is mandatory
//
// Rock-paper-scissors has three possible moves. Without a salt there are exactly three possible hashes, and hashing all three breaks the scheme outright. The library refuses a salt shorter than `16` bytes rather than leaving that as advice:
//
// - `Commit("rock", "tooshort")` → `commitreveal: salt is shorter than MinSaltLen`
//
// The salt also keeps two players who pick the *same* move from publishing the same commitment:
//
// | same move, different salt | commitment |
// |---|---|
// | player 1 | `a5d84fa94157acca…` |
// | player 2 | `ccbc5d0b7ab1e84d…` |
//
// ## Two details that are easy to get wrong
//
// - **Length-prefixed hashing.** With plain concatenation `("ab","cd…")` and `("abc","d…")` hash identically, so one commitment could be opened two different ways.
// - **Constant-time comparison.** A short-circuiting check leaks, through timing, how many leading bytes of a guess were right — enough to rebuild a commitment byte by byte.
}