-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcryptonight.go
More file actions
244 lines (204 loc) · 6.52 KB
/
cryptonight.go
File metadata and controls
244 lines (204 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2015 The go-ethereum Authors
// Copyright 2015 Lefteris Karapetsas <lefteris@refu.co>
// Copyright 2015 Matthew Wampler-Doty <matthew.wampler.doty@gmail.com>
// Copyright 2018-2019 Webchain project
// This file is part of Webchain.
//
// Webchain is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Webchain is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Webchain. If not, see <http://www.gnu.org/licenses/>.
package cryptonight
// TODO: Refactor this package. "cryptonight" isn't right place for
// LYRA2 implementation.
/*
#cgo CFLAGS: -std=gnu99
#cgo amd64 CFLAGS: -maes
#include "cryptonight.h"
#include "Lyra2.h"
#include <stdlib.h>
*/
import "C"
import (
"encoding/binary"
"math/big"
"math/rand"
"sync/atomic"
"time"
"unsafe"
"github.com/webchain-network/webchaind/common"
"github.com/webchain-network/webchaind/core/types"
"github.com/webchain-network/webchaind/logger"
"github.com/webchain-network/webchaind/logger/glog"
"github.com/webchain-network/webchaind/pow"
"github.com/mintme-com/cpuid"
)
var (
maxUint256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
)
type Cryptonight struct {
lyra2_height uint64
lyra2v2_height uint64
hashRate int32
turbo bool
}
func (pow *Cryptonight) GetHashrate() int64 {
return int64(atomic.LoadInt32(&pow.hashRate))
}
func (pow *Cryptonight) Search(block pow.Block, stop <-chan struct{}, index int) (nonce uint64) {
is_lyra2 := block.NumberU64() >= pow.lyra2_height
is_lyra2v2 := block.NumberU64() >= pow.lyra2v2_height
r := rand.New(rand.NewSource(time.Now().UnixNano()))
diff := block.Difficulty()
i := int64(0)
start := time.Now().UnixNano()
previousHashrate := int32(0)
nonce = uint64(r.Int63())
target := new(big.Int).Div(maxUint256, diff)
var ctx unsafe.Pointer
if is_lyra2 {
ctx = C.LYRA2_create()
} else {
ctx = C.cryptonight_create()
}
headerBytes := types.HeaderToBytes(block.Header())
for {
select {
case <-stop:
atomic.AddInt32(&pow.hashRate, -previousHashrate)
if is_lyra2 {
C.LYRA2_destroy(ctx)
} else {
C.cryptonight_destroy(ctx)
}
return 0
default:
i++
// we don't have to update hash rate on every nonce, so update after
// first nonce check and then after 2^X nonces
if i == 2 || ((i % (1 << 7)) == 0) {
elapsed := time.Now().UnixNano() - start
hashes := (float64(1e9) / float64(elapsed)) * float64(i)
hashrateDiff := int32(hashes) - previousHashrate
previousHashrate = int32(hashes)
atomic.AddInt32(&pow.hashRate, hashrateDiff)
}
var result *big.Int
if is_lyra2 {
tcost := 4
if is_lyra2v2 {
tcost = 1
}
result = pow.computeLYRA2(ctx, headerBytes, nonce, tcost).Big()
} else {
result = pow.compute(ctx, headerBytes, nonce).Big()
}
// TODO: disagrees with the spec https://github.com/ethereum/wiki/wiki/Ethash#mining
if result.Cmp(target) <= 0 {
atomic.AddInt32(&pow.hashRate, -previousHashrate)
if is_lyra2 {
C.LYRA2_destroy(ctx)
} else {
C.cryptonight_destroy(ctx)
}
return nonce
}
nonce += 1
}
if !pow.turbo {
time.Sleep(20 * time.Microsecond)
}
}
}
func (pow *Cryptonight) Turbo(on bool) {
pow.turbo = on
}
func bytesToHash(in unsafe.Pointer) common.Hash {
return *(*common.Hash)(in)
}
func (pow *Cryptonight) compute(ctx unsafe.Pointer, blockBytes []byte, nonce uint64) common.Hash {
binary.BigEndian.PutUint64(blockBytes[len(blockBytes)-8:], nonce)
var in unsafe.Pointer = C.CBytes(blockBytes)
var out unsafe.Pointer = C.malloc(common.HashLength)
if cpuid.CPU.AesNi() {
C.cryptonight_hash_aesni(ctx, (*C.char)(in), (*C.char)(out), C.uint32_t(len(blockBytes)))
} else {
C.cryptonight_hash(ctx, (*C.char)(in), (*C.char)(out), C.uint32_t(len(blockBytes)))
}
var hash common.Hash = bytesToHash(unsafe.Pointer(out))
C.free(in)
C.free(out)
return hash
}
func (pow *Cryptonight) computeLYRA2(lyra2_ctx unsafe.Pointer, blockBytes []byte, nonce uint64, tcost int) common.Hash {
binary.BigEndian.PutUint64(blockBytes[len(blockBytes)-8:], nonce)
var in unsafe.Pointer = C.CBytes(blockBytes)
var out unsafe.Pointer = C.malloc(common.HashLength)
C.LYRA2(lyra2_ctx, unsafe.Pointer(out), common.HashLength, unsafe.Pointer(in), C.int32_t(len(blockBytes)), C.int32_t(tcost))
var hash common.Hash = bytesToHash(unsafe.Pointer(out))
C.free(in)
C.free(out)
return hash
}
func (pow *Cryptonight) CalcHash(headerBytes []byte, nonce uint64) *big.Int {
var cn_ctx unsafe.Pointer = C.cryptonight_create()
result := pow.compute(cn_ctx, headerBytes, nonce)
C.cryptonight_destroy(cn_ctx)
return result.Big()
}
func (pow *Cryptonight) CalcHashLYRA2(headerBytes []byte, nonce uint64, tcost int) *big.Int {
var lyra2_ctx unsafe.Pointer = C.LYRA2_create()
result := pow.computeLYRA2(lyra2_ctx, headerBytes, nonce, tcost)
C.LYRA2_destroy(lyra2_ctx)
return result.Big()
}
func (pow *Cryptonight) Verify(block pow.Block) bool {
difficulty := block.Difficulty()
headerBytes := types.HeaderToBytes(block.Header())
/* Cannot happen if block header diff is validated prior to PoW, but can
happen if PoW is checked first due to parallel PoW checking.
*/
if difficulty.Cmp(common.Big0) == 0 {
glog.V(logger.Debug).Infof("invalid block difficulty")
return false
}
var result *big.Int
if block.NumberU64() >= pow.lyra2_height {
tcost := 4
if block.NumberU64() >= pow.lyra2v2_height {
tcost = 1
}
result = pow.CalcHashLYRA2(headerBytes, block.Nonce(), tcost)
} else {
result = pow.CalcHash(headerBytes, block.Nonce())
}
// The actual check.
target := new(big.Int).Div(maxUint256, difficulty)
return result.Cmp(target) <= 0
}
func New(lyra2_height uint64, lyra2v2_height uint64) *Cryptonight {
return &Cryptonight{
lyra2_height: lyra2_height,
lyra2v2_height: lyra2v2_height,
}
}
func NewShared(lyra2_height uint64, lyra2v2_height uint64) *Cryptonight {
return &Cryptonight{
lyra2_height: lyra2_height,
lyra2v2_height: lyra2v2_height,
}
}
func NewForTesting(lyra2_height uint64, lyra2v2_height uint64) (*Cryptonight, error) {
return &Cryptonight{
lyra2_height: lyra2_height,
lyra2v2_height: lyra2v2_height,
}, nil
}