Playground
entropy.gno
// Entropy generates fully deterministic, cost-effective, and hard to guess
// numbers.
//
// It is designed both for single-usage, like seeding math/rand or for being
// reused which increases the entropy and its cost effectiveness.
//
// Disclaimer: this package is unsafe and won't prevent others to guess values
// in advance.
//
// It uses the Bernstein's hash djb2 to be CPU-cycle efficient.
package entropy
import (
"chain/runtime"
"chain/runtime/unsafe"
"math"
"time"
)
type Instance struct {
value uint32
}
func New() *Instance {
r := Instance{value: 5381}
r.addEntropy()
return &r
}
func FromSeed(seed uint32) *Instance {
r := Instance{value: seed}
r.addEntropy()
return &r
}
func (i *Instance) Seed() uint32 {
return i.value
}
func (i *Instance) djb2String(input string) {
for _, c := range input {
i.djb2Uint32(uint32(c))
}
}
// super fast random algorithm.
// http://www.cse.yorku.ca/~oz/hash.html
func (i *Instance) djb2Uint32(input uint32) {
i.value = (i.value << 5) + i.value + input
}
// AddEntropy uses various runtime variables to add entropy to the existing seed.
func (i *Instance) addEntropy() {
// FIXME: reapply the 5381 initial value?
// inherit previous entropy
// nothing to do
// handle callers
{
currentRealm := unsafe.CurrentRealm().Address().String()
i.djb2String(currentRealm)
originCaller := unsafe.OriginCaller().String()
i.djb2String(originCaller)
}
// height
{
height := runtime.ChainHeight()
if height >= math.MaxUint32 {
height -= math.MaxUint32
}
i.djb2Uint32(uint32(height))
}
// time
{
secs := time.Now().Second()
i.djb2Uint32(uint32(secs))
nsecs := time.Now().Nanosecond()
i.djb2Uint32(uint32(nsecs))
}
// FIXME: compute other hard-to-guess but deterministic variables, like real gas?
}
func (i *Instance) Value() uint32 {
i.addEntropy()
return i.value
}
func (i *Instance) Value64() uint64 {
i.addEntropy()
high := i.value
i.addEntropy()
return (uint64(high) << 32) | uint64(i.value)
}
entropy_test.gno
package entropy
import (
"strconv"
"testing"
)
func TestInstance(t *testing.T) {
instance := New()
if instance == nil {
t.Errorf("instance should not be nil")
}
}
func TestInstanceValue(t *testing.T) {
baseEntropy := New()
baseResult := computeValue(t, baseEntropy)
sameHeightEntropy := New()
sameHeightResult := computeValue(t, sameHeightEntropy)
if baseResult != sameHeightResult {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}
testing.SkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue(t, differentHeightEntropy)
if baseResult == differentHeightResult {
t.Errorf("should have different result: new=%s, base=%s", differentHeightResult, baseResult)
}
}
func TestInstanceValue64(t *testing.T) {
baseEntropy := New()
baseResult := computeValue64(t, baseEntropy)
sameHeightEntropy := New()
sameHeightResult := computeValue64(t, sameHeightEntropy)
if baseResult != sameHeightResult {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}
testing.SkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue64(t, differentHeightEntropy)
if baseResult == differentHeightResult {
t.Errorf("should have different result: new=%s, base=%s", differentHeightResult, baseResult)
}
}
func computeValue(t *testing.T, r *Instance) string {
t.Helper()
out := ""
for i := 0; i < 10; i++ {
val := int(r.Value())
out += strconv.Itoa(val) + " "
}
return out
}
func computeValue64(t *testing.T, r *Instance) string {
t.Helper()
out := ""
for i := 0; i < 10; i++ {
val := int(r.Value64())
out += strconv.Itoa(val) + " "
}
return out
}
gnomod.toml
module = "gno.land/p/nym-moultest123/entropy/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186404 max_deposit = "5000000ugnot"