diff options
Diffstat (limited to 'pow')
-rw-r--r-- | pow/block.go | 10 | ||||
-rw-r--r-- | pow/dagger/dagger.go | 160 | ||||
-rw-r--r-- | pow/dagger/dagger_test.go | 19 | ||||
-rw-r--r-- | pow/dash/crypto.c | 5 | ||||
-rw-r--r-- | pow/dash/crypto.go | 14 | ||||
-rw-r--r-- | pow/ezp/pow.go | 100 | ||||
-rw-r--r-- | pow/pow.go | 8 |
7 files changed, 316 insertions, 0 deletions
diff --git a/pow/block.go b/pow/block.go new file mode 100644 index 000000000..31e194d8d --- /dev/null +++ b/pow/block.go @@ -0,0 +1,10 @@ +package pow + +import "math/big" + +type Block interface { + Difficulty() *big.Int + HashNoNonce() []byte + N() []byte + Number() *big.Int +} diff --git a/pow/dagger/dagger.go b/pow/dagger/dagger.go new file mode 100644 index 000000000..310f8abdd --- /dev/null +++ b/pow/dagger/dagger.go @@ -0,0 +1,160 @@ +package dagger + +import ( + "hash" + "math/big" + "math/rand" + "time" + + "github.com/ethereum/go-ethereum/crypto/sha3" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" +) + +var powlogger = logger.NewLogger("POW") + +type Dagger struct { + hash *big.Int + xn *big.Int +} + +var Found bool + +func (dag *Dagger) Find(obj *big.Int, resChan chan int64) { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + + for i := 0; i < 1000; i++ { + rnd := r.Int63() + + res := dag.Eval(big.NewInt(rnd)) + powlogger.Infof("rnd %v\nres %v\nobj %v\n", rnd, res, obj) + if res.Cmp(obj) < 0 { + // Post back result on the channel + resChan <- rnd + // Notify other threads we've found a valid nonce + Found = true + } + + // Break out if found + if Found { + break + } + } + + resChan <- 0 +} + +func (dag *Dagger) Search(hash, diff *big.Int) *big.Int { + // TODO fix multi threading. Somehow it results in the wrong nonce + amountOfRoutines := 1 + + dag.hash = hash + + obj := ethutil.BigPow(2, 256) + obj = obj.Div(obj, diff) + + Found = false + resChan := make(chan int64, 3) + var res int64 + + for k := 0; k < amountOfRoutines; k++ { + go dag.Find(obj, resChan) + + // Wait for each go routine to finish + } + for k := 0; k < amountOfRoutines; k++ { + // Get the result from the channel. 0 = quit + if r := <-resChan; r != 0 { + res = r + } + } + + return big.NewInt(res) +} + +func (dag *Dagger) Verify(hash, diff, nonce *big.Int) bool { + dag.hash = hash + + obj := ethutil.BigPow(2, 256) + obj = obj.Div(obj, diff) + + return dag.Eval(nonce).Cmp(obj) < 0 +} + +func DaggerVerify(hash, diff, nonce *big.Int) bool { + dagger := &Dagger{} + dagger.hash = hash + + obj := ethutil.BigPow(2, 256) + obj = obj.Div(obj, diff) + + return dagger.Eval(nonce).Cmp(obj) < 0 +} + +func (dag *Dagger) Node(L uint64, i uint64) *big.Int { + if L == i { + return dag.hash + } + + var m *big.Int + if L == 9 { + m = big.NewInt(16) + } else { + m = big.NewInt(3) + } + + sha := sha3.NewKeccak256() + sha.Reset() + d := sha3.NewKeccak256() + b := new(big.Int) + ret := new(big.Int) + + for k := 0; k < int(m.Uint64()); k++ { + d.Reset() + d.Write(dag.hash.Bytes()) + d.Write(dag.xn.Bytes()) + d.Write(big.NewInt(int64(L)).Bytes()) + d.Write(big.NewInt(int64(i)).Bytes()) + d.Write(big.NewInt(int64(k)).Bytes()) + + b.SetBytes(Sum(d)) + pk := b.Uint64() & ((1 << ((L - 1) * 3)) - 1) + sha.Write(dag.Node(L-1, pk).Bytes()) + } + + ret.SetBytes(Sum(sha)) + + return ret +} + +func Sum(sha hash.Hash) []byte { + //in := make([]byte, 32) + return sha.Sum(nil) +} + +func (dag *Dagger) Eval(N *big.Int) *big.Int { + pow := ethutil.BigPow(2, 26) + dag.xn = pow.Div(N, pow) + + sha := sha3.NewKeccak256() + sha.Reset() + ret := new(big.Int) + + for k := 0; k < 4; k++ { + d := sha3.NewKeccak256() + b := new(big.Int) + + d.Reset() + d.Write(dag.hash.Bytes()) + d.Write(dag.xn.Bytes()) + d.Write(N.Bytes()) + d.Write(big.NewInt(int64(k)).Bytes()) + + b.SetBytes(Sum(d)) + pk := (b.Uint64() & 0x1ffffff) + + sha.Write(dag.Node(9, pk).Bytes()) + } + + return ret.SetBytes(Sum(sha)) +} diff --git a/pow/dagger/dagger_test.go b/pow/dagger/dagger_test.go new file mode 100644 index 000000000..f3a71d1eb --- /dev/null +++ b/pow/dagger/dagger_test.go @@ -0,0 +1,19 @@ +package dagger + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/ethutil" +) + +func BenchmarkDaggerSearch(b *testing.B) { + hash := big.NewInt(0) + diff := ethutil.BigPow(2, 36) + o := big.NewInt(0) // nonce doesn't matter. We're only testing against speed, not validity + + // Reset timer so the big generation isn't included in the benchmark + b.ResetTimer() + // Validate + DaggerVerify(hash, diff, o) +} diff --git a/pow/dash/crypto.c b/pow/dash/crypto.c new file mode 100644 index 000000000..9c5a62d16 --- /dev/null +++ b/pow/dash/crypto.c @@ -0,0 +1,5 @@ +extern char *Sha3(char *, int); +char *sha3_cgo(char *data, int l) +{ + return Sha3(data, l); +} diff --git a/pow/dash/crypto.go b/pow/dash/crypto.go new file mode 100644 index 000000000..0644a54ae --- /dev/null +++ b/pow/dash/crypto.go @@ -0,0 +1,14 @@ +package dash + +/* +char *sha3_cgo(char *, int); // Forward declaration +*/ +import "C" +import ( + "github.com/ethereum/go-ethereum/crypto" +) + +//export Sha3 +func Sha3(data []byte, l int) []byte { + return crypto.Sha3(data) +} diff --git a/pow/ezp/pow.go b/pow/ezp/pow.go new file mode 100644 index 000000000..540381243 --- /dev/null +++ b/pow/ezp/pow.go @@ -0,0 +1,100 @@ +package ezp + +import ( + "math/big" + "math/rand" + "time" + + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/crypto/sha3" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/pow" +) + +var powlogger = logger.NewLogger("POW") + +type EasyPow struct { + hash *big.Int + HashRate int64 + turbo bool +} + +func New() *EasyPow { + return &EasyPow{turbo: false} +} + +func (pow *EasyPow) GetHashrate() int64 { + return pow.HashRate +} + +func (pow *EasyPow) Turbo(on bool) { + pow.turbo = on +} + +func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}) []byte { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + hash := block.HashNoNonce() + diff := block.Difficulty() + //i := int64(0) + // TODO fix offset + i := rand.Int63() + starti := i + start := time.Now().UnixNano() + + defer func() { pow.HashRate = 0 }() + + // Make sure stop is empty +empty: + for { + select { + case <-stop: + default: + break empty + } + } + + for { + select { + case <-stop: + return nil + default: + i++ + + elapsed := time.Now().UnixNano() - start + hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000 + pow.HashRate = int64(hashes) + + sha := crypto.Sha3(big.NewInt(r.Int63()).Bytes()) + if verify(hash, diff, sha) { + return sha + } + } + + if !pow.turbo { + time.Sleep(20 * time.Microsecond) + } + } + + return nil +} + +func (pow *EasyPow) Verify(block pow.Block) bool { + return Verify(block) +} + +func verify(hash []byte, diff *big.Int, nonce []byte) bool { + sha := sha3.NewKeccak256() + + d := append(hash, nonce...) + sha.Write(d) + + verification := new(big.Int).Div(ethutil.BigPow(2, 256), diff) + res := ethutil.BigD(sha.Sum(nil)) + + return res.Cmp(verification) <= 0 +} + +func Verify(block pow.Block) bool { + return verify(block.HashNoNonce(), block.Difficulty(), block.N()) +} diff --git a/pow/pow.go b/pow/pow.go new file mode 100644 index 000000000..c94ee40ba --- /dev/null +++ b/pow/pow.go @@ -0,0 +1,8 @@ +package pow + +type PoW interface { + Search(block Block, stop <-chan struct{}) []byte + Verify(block Block) bool + GetHashrate() int64 + Turbo(bool) +} |