dagger.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package dagger
  17. import (
  18. "hash"
  19. "math/big"
  20. "math/rand"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto/sha3"
  24. "github.com/ethereum/go-ethereum/logger"
  25. )
  26. var powlogger = logger.NewLogger("POW")
  27. type Dagger struct {
  28. hash *big.Int
  29. xn *big.Int
  30. }
  31. var Found bool
  32. func (dag *Dagger) Find(obj *big.Int, resChan chan int64) {
  33. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  34. for i := 0; i < 1000; i++ {
  35. rnd := r.Int63()
  36. res := dag.Eval(big.NewInt(rnd))
  37. powlogger.Infof("rnd %v\nres %v\nobj %v\n", rnd, res, obj)
  38. if res.Cmp(obj) < 0 {
  39. // Post back result on the channel
  40. resChan <- rnd
  41. // Notify other threads we've found a valid nonce
  42. Found = true
  43. }
  44. // Break out if found
  45. if Found {
  46. break
  47. }
  48. }
  49. resChan <- 0
  50. }
  51. func (dag *Dagger) Search(hash, diff *big.Int) (uint64, []byte) {
  52. // TODO fix multi threading. Somehow it results in the wrong nonce
  53. amountOfRoutines := 1
  54. dag.hash = hash
  55. obj := common.BigPow(2, 256)
  56. obj = obj.Div(obj, diff)
  57. Found = false
  58. resChan := make(chan int64, 3)
  59. var res int64
  60. for k := 0; k < amountOfRoutines; k++ {
  61. go dag.Find(obj, resChan)
  62. // Wait for each go routine to finish
  63. }
  64. for k := 0; k < amountOfRoutines; k++ {
  65. // Get the result from the channel. 0 = quit
  66. if r := <-resChan; r != 0 {
  67. res = r
  68. }
  69. }
  70. return uint64(res), nil
  71. }
  72. func (dag *Dagger) Verify(hash, diff, nonce *big.Int) bool {
  73. dag.hash = hash
  74. obj := common.BigPow(2, 256)
  75. obj = obj.Div(obj, diff)
  76. return dag.Eval(nonce).Cmp(obj) < 0
  77. }
  78. func DaggerVerify(hash, diff, nonce *big.Int) bool {
  79. dagger := &Dagger{}
  80. dagger.hash = hash
  81. obj := common.BigPow(2, 256)
  82. obj = obj.Div(obj, diff)
  83. return dagger.Eval(nonce).Cmp(obj) < 0
  84. }
  85. func (dag *Dagger) Node(L uint64, i uint64) *big.Int {
  86. if L == i {
  87. return dag.hash
  88. }
  89. var m *big.Int
  90. if L == 9 {
  91. m = big.NewInt(16)
  92. } else {
  93. m = big.NewInt(3)
  94. }
  95. sha := sha3.NewKeccak256()
  96. sha.Reset()
  97. d := sha3.NewKeccak256()
  98. b := new(big.Int)
  99. ret := new(big.Int)
  100. for k := 0; k < int(m.Uint64()); k++ {
  101. d.Reset()
  102. d.Write(dag.hash.Bytes())
  103. d.Write(dag.xn.Bytes())
  104. d.Write(big.NewInt(int64(L)).Bytes())
  105. d.Write(big.NewInt(int64(i)).Bytes())
  106. d.Write(big.NewInt(int64(k)).Bytes())
  107. b.SetBytes(Sum(d))
  108. pk := b.Uint64() & ((1 << ((L - 1) * 3)) - 1)
  109. sha.Write(dag.Node(L-1, pk).Bytes())
  110. }
  111. ret.SetBytes(Sum(sha))
  112. return ret
  113. }
  114. func Sum(sha hash.Hash) []byte {
  115. //in := make([]byte, 32)
  116. return sha.Sum(nil)
  117. }
  118. func (dag *Dagger) Eval(N *big.Int) *big.Int {
  119. pow := common.BigPow(2, 26)
  120. dag.xn = pow.Div(N, pow)
  121. sha := sha3.NewKeccak256()
  122. sha.Reset()
  123. ret := new(big.Int)
  124. for k := 0; k < 4; k++ {
  125. d := sha3.NewKeccak256()
  126. b := new(big.Int)
  127. d.Reset()
  128. d.Write(dag.hash.Bytes())
  129. d.Write(dag.xn.Bytes())
  130. d.Write(N.Bytes())
  131. d.Write(big.NewInt(int64(k)).Bytes())
  132. b.SetBytes(Sum(d))
  133. pk := (b.Uint64() & 0x1ffffff)
  134. sha.Write(dag.Node(9, pk).Bytes())
  135. }
  136. return ret.SetBytes(Sum(sha))
  137. }