-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/airdrop' of https://github.com/irreverentsimplicit…
…y/gno into feat/airdrop
- Loading branch information
Showing
43 changed files
with
1,439 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 ( | ||
"math" | ||
"std" | ||
"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 | ||
{ | ||
caller1 := std.GetCallerAt(1).String() | ||
i.djb2String(caller1) | ||
caller2 := std.GetCallerAt(2).String() | ||
i.djb2String(caller2) | ||
} | ||
|
||
// height | ||
{ | ||
height := std.GetHeight() | ||
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 | ||
} |
Oops, something went wrong.