2014-01-08 16:41:03 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
"github.com/obscuren/sha3"
|
2014-01-09 04:57:35 -06:00
|
|
|
"hash"
|
2014-01-08 16:41:03 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Dagger struct {
|
|
|
|
hash *big.Int
|
|
|
|
xn *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dag *Dagger) Search(diff *big.Int) *big.Int {
|
|
|
|
dag.hash = big.NewInt(0)
|
|
|
|
|
|
|
|
obj := BigPow(2, 256)
|
|
|
|
obj = obj.Div(obj, diff)
|
|
|
|
|
|
|
|
fmt.Println("diff", diff, "< objective", obj)
|
|
|
|
|
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
rnd := big.NewInt(r.Int63())
|
|
|
|
fmt.Println("init rnd =", rnd)
|
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
if dag.Eval(rnd).Cmp(obj) < 0 {
|
2014-01-09 04:57:35 -06:00
|
|
|
fmt.Println("Found result! nonce = ", rnd)
|
|
|
|
|
2014-01-08 16:41:03 -06:00
|
|
|
return rnd
|
2014-01-09 04:57:35 -06:00
|
|
|
} else {
|
|
|
|
fmt.Println("Not found :( nonce = ", rnd)
|
2014-01-08 16:41:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
rnd = rnd.Add(rnd, big.NewInt(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return big.NewInt(0)
|
|
|
|
}
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
func DaggerVerify(hash, diff, nonce *big.Int) bool {
|
|
|
|
dagger := &Dagger{}
|
|
|
|
dagger.hash = hash
|
|
|
|
|
|
|
|
obj := BigPow(2, 256)
|
|
|
|
obj = obj.Div(obj, diff)
|
|
|
|
|
|
|
|
return dagger.Eval(nonce).Cmp(obj) < 0
|
|
|
|
}
|
|
|
|
|
2014-01-08 16:41:03 -06:00
|
|
|
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.NewKeccak224()
|
|
|
|
sha.Reset()
|
|
|
|
d := sha3.NewKeccak224()
|
|
|
|
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())
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
b.SetBytes(Sum(d))
|
2014-01-08 16:41:03 -06:00
|
|
|
pk := b.Uint64() & ((1 << ((L - 1) * 3)) - 1)
|
|
|
|
sha.Write(dag.Node(L - 1, pk).Bytes())
|
|
|
|
}
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
ret.SetBytes(Sum(sha))
|
2014-01-08 16:41:03 -06:00
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
func Sum(sha hash.Hash) []byte {
|
|
|
|
in := make([]byte, 32)
|
|
|
|
return sha.Sum(in)
|
|
|
|
}
|
|
|
|
|
2014-01-08 16:41:03 -06:00
|
|
|
func (dag *Dagger) Eval(N *big.Int) *big.Int {
|
|
|
|
pow := BigPow(2, 26)
|
|
|
|
dag.xn = N.Div(N, pow)
|
|
|
|
|
|
|
|
sha := sha3.NewKeccak224()
|
|
|
|
sha.Reset()
|
|
|
|
ret := new(big.Int)
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
//doneChan := make(chan bool, 3)
|
2014-01-08 16:41:03 -06:00
|
|
|
|
|
|
|
for k := 0; k < 4; k++ {
|
2014-01-09 04:57:35 -06:00
|
|
|
//go func(_k int) {
|
|
|
|
_k := k
|
2014-01-08 16:41:03 -06:00
|
|
|
d := sha3.NewKeccak224()
|
|
|
|
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())
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
b.SetBytes(Sum(d))
|
2014-01-08 16:41:03 -06:00
|
|
|
pk := (b.Uint64() & 0x1ffffff)
|
|
|
|
|
|
|
|
sha.Write(dag.Node(9, pk).Bytes())
|
2014-01-09 04:57:35 -06:00
|
|
|
//doneChan <- true
|
|
|
|
//}(k)
|
2014-01-08 16:41:03 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
//for k := 0; k < 4; k++ {
|
|
|
|
// <- doneChan
|
|
|
|
//}
|
|
|
|
|
2014-01-08 16:41:03 -06:00
|
|
|
|
2014-01-09 04:57:35 -06:00
|
|
|
return ret.SetBytes(Sum(sha))
|
2014-01-08 16:41:03 -06:00
|
|
|
}
|