Playground
commit.gno
// Package commit provides small, deterministic commitment helpers shared by
// the agent demos. On-chain realms store *commitments* to off-chain data
// (reports, patches, verdicts) rather than the data itself; this package is
// the one place that defines how those commitments are computed, so several
// realms agree on the scheme.
package commit
import (
"crypto/sha256"
"encoding/hex"
)
// Hash returns the hex-encoded SHA-256 over its parts, length-prefixed so
// that Hash("ab","c") != Hash("a","bc"). Use it to commit to structured
// off-chain material without revealing it.
func Hash(parts ...string) string {
var buf []byte
for _, p := range parts {
buf = appendUvarint(buf, uint64(len(p)))
buf = append(buf, p...)
}
sum := sha256.Sum256(buf)
return hex.EncodeToString(sum[:])
}
// Verdict is the canonical commitment used by commit-reveal voting: a hidden
// boolean plus a salt. Reveal the same (verdict, salt) later and any observer
// can recompute this and confirm the vote was not changed after the fact.
func Verdict(verdict bool, salt string) string {
v := "no"
if verdict {
v = "yes"
}
return Hash("verdict", v, salt)
}
// Short abbreviates a hex commitment for display.
func Short(h string) string {
if len(h) <= 12 {
return h
}
return h[:6] + "…" + h[len(h)-4:]
}
func appendUvarint(buf []byte, x uint64) []byte {
for x >= 0x80 {
buf = append(buf, byte(x)|0x80)
x >>= 7
}
return append(buf, byte(x))
}
commit_test.gno
package commit
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestHashDeterministicAndPrefixed(t *testing.T) {
uassert.Equal(t, Hash("a", "bc"), Hash("a", "bc"))
// length-prefixing prevents boundary collisions
uassert.NotEqual(t, Hash("ab", "c"), Hash("a", "bc"))
uassert.Equal(t, 64, len(Hash("x"))) // sha256 hex is 64 chars
}
func TestVerdictCommitReveal(t *testing.T) {
c := Verdict(true, "salt-123")
// reproducible from the revealed values
uassert.Equal(t, c, Verdict(true, "salt-123"))
// flipping the vote or the salt changes the commitment
uassert.NotEqual(t, c, Verdict(false, "salt-123"))
uassert.NotEqual(t, c, Verdict(true, "salt-999"))
}
func TestShort(t *testing.T) {
uassert.Equal(t, "abc", Short("abc"))
uassert.True(t, len(Short(Hash("x"))) < 20)
}
gnomod.toml
module = "gno.land/p/nym-moultest123/agents/commit/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186404 max_deposit = "5000000ugnot"