Playground
commitreveal.gno
// Package commitreveal implements a minimal commit-reveal scheme.
//
// A commitment is the hex-encoded SHA-256 of the exact byte string
//
// salt + ":" + payload
//
// The payload stays secret until the revealer publishes payload and salt;
// anyone can then recompute the commitment and check it matches the value
// that was committed earlier. The salt keeps short or guessable payloads
// from being brute-forced against a bare hash of the payload alone.
//
// The package is pure and stateless: it is safe to import from any realm.
package commitreveal
import (
"crypto/sha256"
"encoding/hex"
)
// Commit returns the commitment for payload under salt: the lowercase
// hex encoding of SHA-256(salt + ":" + payload).
func Commit(payload string, salt string) string {
sum := sha256.Sum256([]byte(salt + ":" + payload))
return hex.EncodeToString(sum[:])
}
// IsCommitment reports whether s has the canonical commitment format:
// exactly 64 lowercase hexadecimal characters.
func IsCommitment(s string) bool {
if len(s) != 64 {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
return false
}
}
return true
}
// Verify reports whether commitment is well-formed and equals the
// commitment recomputed from payload and salt.
func Verify(commitment string, payload string, salt string) bool {
if !IsCommitment(commitment) {
return false
}
return Commit(payload, salt) == commitment
}
commitreveal_test.gno
package commitreveal
import "testing"
// Known vector, computed off-chain with:
//
// printf 's3cret:hello world' | sha256sum
const (
testPayload = "hello world"
testSalt = "s3cret"
testCommitment = "b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409a"
)
func TestCommitKnownVector(t *testing.T) {
got := Commit(testPayload, testSalt)
if got != testCommitment {
t.Fatalf("Commit(%q, %q) = %q, want %q", testPayload, testSalt, got, testCommitment)
}
}
func TestCommitFormat(t *testing.T) {
if !IsCommitment(Commit("anything", "else")) {
t.Fatal("Commit output must satisfy IsCommitment")
}
if !IsCommitment(testCommitment) {
t.Fatal("known vector must satisfy IsCommitment")
}
}
func TestVerifyHappyPath(t *testing.T) {
if !Verify(testCommitment, testPayload, testSalt) {
t.Fatal("Verify must accept the correct payload and salt")
}
c := Commit("a secret message", "random-salt-123")
if !Verify(c, "a secret message", "random-salt-123") {
t.Fatal("Verify must accept a freshly computed commitment")
}
}
func TestVerifyWrongPayload(t *testing.T) {
if Verify(testCommitment, "hello worle", testSalt) {
t.Fatal("Verify must reject a wrong payload")
}
if Verify(testCommitment, "", testSalt) {
t.Fatal("Verify must reject an empty payload")
}
}
func TestVerifyWrongSalt(t *testing.T) {
if Verify(testCommitment, testPayload, "s3creT") {
t.Fatal("Verify must reject a wrong salt")
}
if Verify(testCommitment, testPayload, "") {
t.Fatal("Verify must reject an empty salt")
}
}
func TestMalformedCommitments(t *testing.T) {
bad := []string{
"", // empty
"b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409", // 63 chars
"b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409aa", // 65 chars
"B5165D20616E37FF3B4C84C2BA2914CED11BF2DF7755A8876F2B22D30FB2409A", // uppercase
"g5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409a", // non-hex char
" b5165d20616e37ff3b4c84c2ba2914ced11bf2df7755a8876f2b22d30fb2409a", // leading space
}
for _, s := range bad {
if IsCommitment(s) {
t.Fatalf("IsCommitment(%q) = true, want false", s)
}
if Verify(s, testPayload, testSalt) {
t.Fatalf("Verify(%q, ...) = true, want false", s)
}
}
}
gnomod.toml
module = "gno.land/p/g1gu8mdvjg8l5wj2ytknhs7pmyu0rmeclyvvlsd8/commitreveal" gno = "0.9" [addpkg] creator = "g1gu8mdvjg8l5wj2ytknhs7pmyu0rmeclyvvlsd8" height = 1348154 max_deposit = "100000000ugnot"