forked from iotaledger/iota.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pow_go.go
286 lines (238 loc) · 6.22 KB
/
pow_go.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
MIT License
Copyright (c) 2017 Shinya Yagyu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package giota
import (
"errors"
"runtime"
"sync"
)
// trytes
const (
hBits uint64 = 0xFFFFFFFFFFFFFFFF
lBits uint64 = 0x0000000000000000
low0 uint64 = 0xDB6DB6DB6DB6DB6D
high0 uint64 = 0xB6DB6DB6DB6DB6DB
low1 uint64 = 0xF1F8FC7E3F1F8FC7
high1 uint64 = 0x8FC7E3F1F8FC7E3F
low2 uint64 = 0x7FFFE00FFFFC01FF
high2 uint64 = 0xFFC01FFFF803FFFF
low3 uint64 = 0xFFC0000007FFFFFF
high3 uint64 = 0x003FFFFFFFFFFFFF
nonceOffset = HashSize - NonceTrinarySize
nonceInitStart = nonceOffset + 4
nonceIncrementStart = nonceInitStart + NonceTrinarySize/3
)
// PowFunc is the func type for PoW
type PowFunc func(Trytes, int) (Trytes, error)
var (
powFuncs = make(map[string]PowFunc)
// PowProcs is number of concurrent processes (default is NumCPU()-1)
PowProcs int
)
func init() {
powFuncs["PowGo"] = PowGo
PowProcs = runtime.NumCPU()
if PowProcs != 1 {
PowProcs--
}
}
// GetBestPoW returns most preferable PoW func.
func GetBestPoW() (string, PowFunc) {
// PowGo is the last and default return value
powOrderPreference := []string{"PowCL", "PowSSE", "PowC"}
for _, pow := range powOrderPreference {
if p, exist := powFuncs[pow]; exist {
return pow, p
}
}
return "PowGo", PowGo // defualt return PowGo if no others
}
func transform64(lmid *[stateSize]uint64, hmid *[stateSize]uint64) {
var ltmp, htmp [stateSize]uint64
lfrom := lmid
hfrom := hmid
lto := <mp
hto := &htmp
for r := 0; r < numberOfRounds-1; r++ {
for j := 0; j < stateSize; j++ {
t1 := indices[j]
t2 := indices[j+1]
alpha := lfrom[t1]
beta := hfrom[t1]
gamma := hfrom[t2]
delta := (alpha | (^gamma)) & (lfrom[t2] ^ beta)
lto[j] = ^delta
hto[j] = (alpha ^ gamma) | delta
}
lfrom, lto = lto, lfrom
hfrom, hto = hto, hfrom
}
for j := 0; j < stateSize; j++ {
t1, t2 := indices[j], indices[j+1]
alpha := lfrom[t1]
beta := hfrom[t1]
gamma := hfrom[t2]
delta := (alpha | (^gamma)) & (lfrom[t2] ^ beta)
lto[j] = ^delta
hto[j] = (alpha ^ gamma) | delta
}
copy(lmid[:], ltmp[:])
copy(hmid[:], htmp[:])
}
func incr(lmid *[stateSize]uint64, hmid *[stateSize]uint64) bool {
var carry uint64 = 1
var i int
//to avoid boundry check, I believe.
for i = nonceInitStart; i < HashSize && carry != 0; i++ {
low := lmid[i]
high := hmid[i]
lmid[i] = high ^ low
hmid[i] = low
carry = high & (^low)
}
return i == HashSize
}
func seri(l *[stateSize]uint64, h *[stateSize]uint64, n uint) Trits {
r := make(Trits, NonceTrinarySize)
for i := nonceOffset; i < HashSize; i++ {
ll := (l[i] >> n) & 1
hh := (h[i] >> n) & 1
switch {
case hh == 0 && ll == 1:
r[i-nonceOffset] = -1
case hh == 1 && ll == 1:
r[i-nonceOffset] = 0
case hh == 1 && ll == 0:
r[i-nonceOffset] = 1
}
}
return r
}
func check(l *[stateSize]uint64, h *[stateSize]uint64, m int) int {
nonceProbe := hBits
for i := HashSize - m; i < HashSize; i++ {
nonceProbe &= ^(l[i] ^ h[i])
if nonceProbe == 0 {
return -1
}
}
var i uint
for i = 0; i < 64; i++ {
if (nonceProbe>>i)&1 == 1 {
return int(i)
}
}
return -1
}
var stopGO = true
func loop(lmid *[stateSize]uint64, hmid *[stateSize]uint64, m int) (Trits, int64) {
var lcpy, hcpy [stateSize]uint64
var i int64
for i = 0; !incr(lmid, hmid) && !stopGO; i++ {
copy(lcpy[:], lmid[:])
copy(hcpy[:], hmid[:])
transform64(&lcpy, &hcpy)
if n := check(&lcpy, &hcpy, m); n >= 0 {
nonce := seri(lmid, hmid, uint(n))
return nonce, i * 64
}
}
return nil, i * 64
}
// 01:-1 11:0 10:1
func para(in Trits) (*[stateSize]uint64, *[stateSize]uint64) {
var l, h [stateSize]uint64
for i := 0; i < stateSize; i++ {
switch in[i] {
case 0:
l[i] = hBits
h[i] = hBits
case 1:
l[i] = lBits
h[i] = hBits
case -1:
l[i] = hBits
h[i] = lBits
}
}
return &l, &h
}
func incrN(n int, lmid *[stateSize]uint64, hmid *[stateSize]uint64) {
for j := 0; j < n; j++ {
var carry uint64 = 1
// to avoid boundry check, i believe.
for i := nonceInitStart; i < nonceIncrementStart && carry != 0; i++ {
low := lmid[i]
high := hmid[i]
lmid[i] = high ^ low
hmid[i] = low
carry = high & (^low)
}
}
}
var countGo int64 = 1
// PowGo is proof of work for iota in pure Go
func PowGo(trytes Trytes, mwm int) (Trytes, error) {
if !stopGO {
stopGO = true
return "", errors.New("pow is already running, stopped")
}
if trytes == "" {
return "", errors.New("invalid trytes")
}
countGo = 0
stopGO = false
c := NewCurl()
c.Absorb(trytes[:(transactionTrinarySize-HashSize)/3])
tr := trytes.Trits()
copy(c.state, tr[transactionTrinarySize-HashSize:])
var (
result Trytes
wg sync.WaitGroup
mutex sync.Mutex
)
for i := 0; i < PowProcs; i++ {
wg.Add(1)
go func(i int) {
lmid, hmid := para(c.state)
lmid[nonceOffset] = low0
hmid[nonceOffset] = high0
lmid[nonceOffset+1] = low1
hmid[nonceOffset+1] = high1
lmid[nonceOffset+2] = low2
hmid[nonceOffset+2] = high2
lmid[nonceOffset+3] = low3
hmid[nonceOffset+3] = high3
incrN(i, lmid, hmid)
nonce, cnt := loop(lmid, hmid, mwm)
mutex.Lock()
if nonce != nil {
result = nonce.Trytes()
stopGO = true
}
countGo += cnt
mutex.Unlock()
wg.Done()
}(i)
}
wg.Wait()
stopGO = true
return result, nil
}