go-ethereum/dagger.go

150 lines
2.6 KiB
Go
Raw Normal View History

2014-01-08 16:41:03 -06:00
package main
import (
2014-01-11 08:27:08 -06:00
"github.com/ethereum/ethutil-go"
"github.com/obscuren/sha3"
"hash"
"math/big"
"math/rand"
"time"
2014-01-12 18:22:33 -06:00
"log"
2014-01-08 16:41:03 -06:00
)
type Dagger struct {
2014-01-11 08:27:08 -06:00
hash *big.Int
xn *big.Int
2014-01-08 16:41:03 -06:00
}
2014-01-09 09:19:16 -06:00
var Found bool
2014-01-11 08:27:08 -06:00
2014-01-09 09:19:16 -06:00
func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
2014-01-11 08:27:08 -06:00
r := rand.New(rand.NewSource(time.Now().UnixNano()))
2014-01-09 09:19:16 -06:00
2014-01-11 08:27:08 -06:00
for i := 0; i < 1000; i++ {
rnd := r.Int63()
2014-01-09 09:19:16 -06:00
2014-01-12 18:22:33 -06:00
res := dag.Eval(big.NewInt(rnd))
log.Printf("rnd %v\nres %v\nobj %v\n", rnd, res, obj)
if res.Cmp(obj) < 0 {
2014-01-11 08:27:08 -06:00
// Post back result on the channel
resChan <- rnd
// Notify other threads we've found a valid nonce
Found = true
}
2014-01-09 09:19:16 -06:00
2014-01-11 08:27:08 -06:00
// Break out if found
if Found {
break
}
}
2014-01-09 09:19:16 -06:00
2014-01-11 08:27:08 -06:00
resChan <- 0
2014-01-09 09:19:16 -06:00
}
func (dag *Dagger) Search(hash, diff *big.Int) *big.Int {
2014-01-11 08:27:08 -06:00
// TODO fix multi threading. Somehow it results in the wrong nonce
amountOfRoutines := 1
2014-01-09 09:19:16 -06:00
2014-01-11 08:27:08 -06:00
dag.hash = hash
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
obj := ethutil.BigPow(2, 256)
obj = obj.Div(obj, diff)
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
Found = false
resChan := make(chan int64, 3)
var res int64
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
for k := 0; k < amountOfRoutines; k++ {
go dag.Find(obj, resChan)
}
2014-01-09 04:57:35 -06:00
2014-01-11 08:27:08 -06:00
// 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
}
}
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
return big.NewInt(res)
2014-01-08 16:41:03 -06:00
}
2014-01-09 04:57:35 -06:00
func DaggerVerify(hash, diff, nonce *big.Int) bool {
2014-01-11 08:27:08 -06:00
dagger := &Dagger{}
dagger.hash = hash
2014-01-09 04:57:35 -06:00
2014-01-11 08:27:08 -06:00
obj := ethutil.BigPow(2, 256)
obj = obj.Div(obj, diff)
2014-01-09 04:57:35 -06:00
2014-01-11 08:27:08 -06:00
return dagger.Eval(nonce).Cmp(obj) < 0
2014-01-09 04:57:35 -06:00
}
2014-01-08 16:41:03 -06:00
func (dag *Dagger) Node(L uint64, i uint64) *big.Int {
2014-01-11 08:27:08 -06:00
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
2014-01-08 16:41:03 -06:00
}
2014-01-09 04:57:35 -06:00
func Sum(sha hash.Hash) []byte {
2014-01-14 14:48:16 -06:00
//in := make([]byte, 32)
return sha.Sum(nil)
2014-01-09 04:57:35 -06:00
}
2014-01-08 16:41:03 -06:00
func (dag *Dagger) Eval(N *big.Int) *big.Int {
2014-01-11 08:27:08 -06:00
pow := ethutil.BigPow(2, 26)
2014-01-12 18:22:33 -06:00
dag.xn = pow.Div(N, pow)
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
sha := sha3.NewKeccak256()
sha.Reset()
ret := new(big.Int)
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
for k := 0; k < 4; k++ {
d := sha3.NewKeccak256()
b := new(big.Int)
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
d.Reset()
d.Write(dag.hash.Bytes())
d.Write(dag.xn.Bytes())
d.Write(N.Bytes())
d.Write(big.NewInt(int64(k)).Bytes())
2014-01-09 04:57:35 -06:00
2014-01-11 08:27:08 -06:00
b.SetBytes(Sum(d))
pk := (b.Uint64() & 0x1ffffff)
2014-01-11 08:27:08 -06:00
sha.Write(dag.Node(9, pk).Bytes())
}
2014-01-08 16:41:03 -06:00
2014-01-11 08:27:08 -06:00
return ret.SetBytes(Sum(sha))
2014-01-08 16:41:03 -06:00
}
2014-01-14 14:48:16 -06:00