-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrandomx_test.go
193 lines (166 loc) · 4.47 KB
/
randomx_test.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
package randomx
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"log"
"math/rand"
//"math/rand"
"runtime"
"strconv"
//"strconv"
"sync"
"testing"
"time"
)
var testPairs = [][][]byte{
// randomX
{
[]byte("test key 000"),
[]byte("This is a test"),
[]byte("639183aae1bf4c9a35884cb46b09cad9175f04efd7684e7262a0ac1c2f0b4e3f"),
},
// randomXL
{
[]byte("test key 000"),
[]byte("This is a test"),
[]byte("b291ec8a532bc4f78bd75b43d211e1169bb65b1a8f66d4250376ba1d6fcff1bd"),
},
}
func TestAllocCache(t *testing.T) {
cache, _ := AllocCache(FlagDefault)
InitCache(cache, []byte("123"))
ReleaseCache(cache)
}
func TestAllocDataset(t *testing.T) {
ds, _ := AllocDataset(FlagDefault)
cache, _ := AllocCache(FlagDefault)
seed := make([]byte, 32)
InitCache(cache, seed)
log.Println("rxCache initialization finished")
count := DatasetItemCount()
log.Println("dataset count:", count/1024/1024, "mb")
InitDataset(ds, cache, 0, count)
log.Println(GetDatasetMemory(ds))
ReleaseDataset(ds)
ReleaseCache(cache)
}
func TestCreateVM(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
var tp = testPairs[1]
cache, _ := AllocCache(FlagDefault)
log.Println("alloc cache mem finished")
seed := tp[0]
InitCache(cache, seed)
log.Println("cache initialization finished")
ds, _ := AllocDataset(FlagDefault)
log.Println("alloc dataset mem finished")
count := DatasetItemCount()
log.Println("dataset count:", count)
var wg sync.WaitGroup
var workerNum = uint32(runtime.NumCPU())
for i := uint32(0); i < workerNum; i++ {
wg.Add(1)
a := (count * i) / workerNum
b := (count * (i + 1)) / workerNum
go func() {
defer wg.Done()
InitDataset(ds, cache, a, b-a)
}()
}
wg.Wait()
log.Println("dataset initialization finished") // too slow when one thread
vm, _ := CreateVM(cache, ds, FlagJIT, FlagHardAES, FlagFullMEM)
var hashCorrect = make([]byte, hex.DecodedLen(len(tp[2])))
_, err := hex.Decode(hashCorrect, tp[2])
if err != nil {
log.Println(err)
}
if bytes.Compare(CalculateHash(vm, tp[1]), hashCorrect) != 0 {
t.Fail()
}
}
func TestNewRxVM(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()
pair := testPairs[1]
workerNum := uint32(runtime.NumCPU())
seed := pair[0]
dataset, _ := NewRxDataset(FlagJIT)
if dataset.GoInit(seed, workerNum) == false {
log.Fatal("failed to init dataset")
}
//defer dataset.Close()
fmt.Println("Finished generating dataset in", time.Since(start).Seconds(), "sec")
vm, _ := NewRxVM(dataset, FlagFullMEM, FlagHardAES, FlagJIT, FlagSecure)
//defer vm.Close()
blob := pair[1]
hash := vm.CalcHash(blob)
var hashCorrect = make([]byte, hex.DecodedLen(len(pair[2])))
_, err := hex.Decode(hashCorrect, pair[2])
if err != nil {
log.Println(err)
}
if bytes.Compare(hash, hashCorrect) != 0 {
log.Println(hash)
t.Fail()
}
}
func TestCalculateHashFirst(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()
pair := testPairs[1]
workerNum := uint32(runtime.NumCPU())
seed := pair[0]
dataset, _ := NewRxDataset(FlagJIT)
if dataset.GoInit(seed, workerNum) == false {
log.Fatal("failed to init dataset")
}
//defer dataset.Close()
fmt.Println("Finished generating dataset in", time.Since(start).Seconds(), "sec")
vm, _ := NewRxVM(dataset, FlagFullMEM, FlagHardAES, FlagJIT, FlagSecure)
//defer vm.Close()
targetBlob := make([]byte, 76)
targetNonce := make([]byte, 4)
binary.LittleEndian.PutUint32(targetNonce, 2333)
copy(targetBlob[39:43], targetNonce)
targetResult := vm.CalcHash(targetBlob)
var wg sync.WaitGroup
for i := 0; i < runtime.NumCPU(); i++ {
vm, _ := NewRxVM(dataset, FlagFullMEM, FlagHardAES, FlagJIT, FlagSecure)
wg.Add(1)
blob := make([]byte, 76)
vm.CalcHashFirst(blob)
n := uint32(0)
go func() {
for {
n++
nonce := make([]byte, 4)
binary.LittleEndian.PutUint32(nonce, n)
copy(blob[39:43], nonce)
result := vm.CalcHashNext(blob)
if bytes.Compare(result, targetResult) == 0 {
fmt.Println(n, "found")
wg.Done()
} else {
//fmt.Println(n, "failed")
}
}
}()
}
wg.Wait()
}
// go test -v -bench "." -benchtime=30m
func BenchmarkCalculateHash(b *testing.B) {
cache, _ := AllocCache(FlagDefault)
ds, _ := AllocDataset(FlagDefault)
InitCache(cache, []byte("123"))
FastInitFullDataset(ds, cache, uint32(runtime.NumCPU()))
vm, _ := CreateVM(cache, ds, FlagDefault)
for i := 0; i < b.N; i++ {
nonce := strconv.FormatInt(rand.Int63(), 10) // just test
CalculateHash(vm, []byte("123"+nonce))
}
DestroyVM(vm)
}