mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.1 KiB
67 lines
1.1 KiB
10 years ago
|
package ar
|
||
|
|
||
|
import (
|
||
|
"math/big"
|
||
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ethcrypto"
|
||
|
"github.com/ethereum/go-ethereum/ethutil"
|
||
10 years ago
|
)
|
||
|
|
||
|
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))
|
||
|
}
|