diff options
author | Felix Lange <fjl@twurst.com> | 2014-10-23 21:01:27 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2014-10-23 21:01:27 +0800 |
commit | 69baa465ea69ae60eed802445cf0132b9eb69934 (patch) | |
tree | b09da7582b5c4850d4db13aee808f2fef2f97de0 /pow/ar/rnd.go | |
parent | 50fd46924900869e7210217c6a07979b544991c8 (diff) | |
parent | feef194829b07570e91873ed5d1e8cc51e8fa430 (diff) | |
download | dexon-69baa465ea69ae60eed802445cf0132b9eb69934.tar.gz dexon-69baa465ea69ae60eed802445cf0132b9eb69934.tar.zst dexon-69baa465ea69ae60eed802445cf0132b9eb69934.zip |
Merge eth-go repository into go-ethereum
mist, etheruem have been moved to cmd/
Diffstat (limited to 'pow/ar/rnd.go')
-rw-r--r-- | pow/ar/rnd.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/pow/ar/rnd.go b/pow/ar/rnd.go new file mode 100644 index 000000000..7dbd99373 --- /dev/null +++ b/pow/ar/rnd.go @@ -0,0 +1,66 @@ +package ar + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/ethcrypto" + "github.com/ethereum/go-ethereum/ethutil" +) + +var b = big.NewInt + +type Node interface { + Big() *big.Int +} + +type ByteNode []byte + +func (self ByteNode) Big() *big.Int { + return ethutil.BigD(ethutil.Encode([]byte(self))) +} + +func Sha3(v interface{}) *big.Int { + if b, ok := v.(*big.Int); ok { + return ethutil.BigD(ethcrypto.Sha3(b.Bytes())) + } else if b, ok := v.([]interface{}); ok { + return ethutil.BigD(ethcrypto.Sha3(ethutil.Encode(b))) + } else if s, ok := v.([]*big.Int); ok { + v := make([]interface{}, len(s)) + for i, b := range s { + v[i] = b + } + + return ethutil.BigD(ethcrypto.Sha3(ethutil.Encode(v))) + } + + return nil +} + +type NumberGenerator interface { + rand(r *big.Int) *big.Int + rand64(r int64) *big.Int +} + +type rnd struct { + seed *big.Int +} + +func Rnd(s *big.Int) rnd { + return rnd{s} +} + +func (self rnd) rand(r *big.Int) *big.Int { + o := b(0).Mod(self.seed, r) + + self.seed.Div(self.seed, r) + + if self.seed.Cmp(ethutil.BigPow(2, 64)) < 0 { + self.seed = Sha3(self.seed) + } + + return o +} + +func (self rnd) rand64(r int64) *big.Int { + return self.rand(b(r)) +} |