Transaction

7E03E202F74A85…D2C0C446A9FB

Block 25,654 · index 0 · indexed

Summary

Hash
7E03E202F74A85D64DE997BBC1BE5D6668877196F6C89693860DD2C0C446A9FB
Block
25,654
Size
3131 bytes
Gas used
8,345,205 / 10,014,306
Fee
1000000ugnot
Memo
gnopublish
Status
success

Messages

#1AddPackagegno.land/p/moul/entropy/v09 arguments

Arguments · 9

  1. #1entropy
  2. #2README.md
  3. #3# `gno.land/p/moul/entropy/v0` A cheap, deterministic pseudo-entropy source for gno realms (seeded from block height / time / caller), useful for demos and non-security-critical randomness. <!-- BEGIN GNOCONTRACTS FOOTER (generated by `make readmes`; do not edit below) --> --- Part of **[moul/gno-contracts](https://github.com/moul/gno-contracts)** — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage. > ⚠️ **Disclaimer:** provided as-is, without warranty; not security-audited. Full disclaimer: [DISCLAIMER](https://github.com/moul/gno-contracts/blob/main/DISCLAIMER.md). <!-- END GNOCONTRACTS FOOTER -->
  4. #4entropy.gno
  5. #5// 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) }
  6. #6gnomod.toml
  7. #7module = "gno.land/p/moul/entropy/v0" gno = "0.9"
  8. #8/gno.MemPackageType
  9. #9 MPUserProd

Result log

msg:0,success:true,log:,events:[]

← Back to block 25,654