-
Notifications
You must be signed in to change notification settings - Fork 12
/
randomx.go
253 lines (202 loc) · 5.64 KB
/
randomx.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
package randomx
//#cgo CFLAGS: -I./randomx
//#cgo LDFLAGS: -L${SRCDIR}/lib -lrandomx
//#cgo LDFLAGS: -lstdc++
//#cgo LDFLAGS: -static -static-libgcc -static-libstdc++ -lpthread
/*
#include "randomx.h"
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <pthread.h>
struct arg_struct {
randomx_dataset *dataset;
randomx_cache *cache;
void *seed;
uint32_t a, b;
};
void *init_full_dataset_thread(void *arguments)
{
struct arg_struct *args = arguments;
randomx_init_dataset(args->dataset, args->cache, args->a, args->b - args->a);
free(arguments);
pthread_exit(NULL);
return NULL;
}
void init_full_dataset(randomx_dataset *dataset, randomx_cache *cache, uint32_t numThreads)
{
const uint64_t datasetItemCount = randomx_dataset_item_count();
if (numThreads > 1) {
pthread_t threads[numThreads];
for (uint64_t i = 0; i < numThreads; ++i) {
pthread_t thread;
threads[i] = thread;
uint32_t a = (datasetItemCount * i) / numThreads;
uint32_t b = (datasetItemCount * (i + 1)) / numThreads;
struct arg_struct *args = malloc(sizeof(struct arg_struct));
args->dataset = dataset;
args->cache = cache;
args->a = a;
args->b = b;
pthread_create(&thread, NULL, &init_full_dataset_thread, (void *)args);
}
for (uint32_t i = 0; i < numThreads; ++i) {
pthread_join(threads[i], NULL);
}
}
else {
randomx_init_dataset(dataset, cache, 0, datasetItemCount);
}
}
*/
import "C"
import (
"errors"
"sync"
"unsafe"
)
type Flag int
var (
FlagDefault Flag = 0 // for all default
FlagLargePages Flag = 1 // for dataset & rxCache & vm
FlagHardAES Flag = 2 // for vm
FlagFullMEM Flag = 4 // for vm
FlagJIT Flag = 8 // for vm & cache
FlagSecure Flag = 16
FlagArgon2SSSE3 Flag = 32 // for cache
FlagArgon2AVX2 Flag = 64 // for cache
FlagArgon2 Flag = 96 // = avx2 + sse3
)
func (f Flag) toC() C.randomx_flags {
return (C.randomx_flags)(f)
}
func AllocCache(flags ...Flag) (*C.randomx_cache, error) {
var SumFlag = FlagDefault
var cache *C.randomx_cache
for _, flag := range flags {
SumFlag = SumFlag | flag
}
cache = C.randomx_alloc_cache(SumFlag.toC())
if cache == nil {
return nil, errors.New("failed to alloc mem for rxCache")
}
return cache, nil
}
func InitCache(cache *C.randomx_cache, seed []byte) {
if len(seed) == 0 {
panic("seed cannot be NULL")
}
C.randomx_init_cache(cache, unsafe.Pointer(&seed[0]), C.size_t(len(seed)))
}
func ReleaseCache(cache *C.randomx_cache) {
C.randomx_release_cache(cache)
}
func AllocDataset(flags ...Flag) (*C.randomx_dataset, error) {
var SumFlag = FlagDefault
for _, flag := range flags {
SumFlag = SumFlag | flag
}
var dataset *C.randomx_dataset
dataset = C.randomx_alloc_dataset(SumFlag.toC())
if dataset == nil {
return nil, errors.New("failed to alloc mem for dataset")
}
return dataset, nil
}
func DatasetItemCount() uint32 {
var length C.ulong
length = C.randomx_dataset_item_count()
return uint32(length)
}
func InitDataset(dataset *C.randomx_dataset, cache *C.randomx_cache, startItem uint32, itemCount uint32) {
if dataset == nil {
panic("alloc dataset mem is required")
}
if cache == nil {
panic("alloc cache mem is required")
}
C.randomx_init_dataset(dataset, cache, C.ulong(startItem), C.ulong(itemCount))
}
// FastInitFullDataset using c's pthread to boost the dataset init. 472s -> 466s
func FastInitFullDataset(dataset *C.randomx_dataset, cache *C.randomx_cache, workerNum uint32) {
if dataset == nil {
panic("alloc dataset mem is required")
}
if cache == nil {
panic("alloc cache mem is required")
}
var wg sync.WaitGroup
go func() {
wg.Add(1)
C.init_full_dataset(dataset, cache, C.uint32_t(workerNum))
wg.Done()
}()
wg.Wait()
}
func GetDatasetMemory(dataset *C.randomx_dataset) unsafe.Pointer {
return C.randomx_get_dataset_memory(dataset)
}
func ReleaseDataset(dataset *C.randomx_dataset) {
C.randomx_release_dataset(dataset)
}
func CreateVM(cache *C.randomx_cache, dataset *C.randomx_dataset, flags ...Flag) (*C.randomx_vm, error) {
var SumFlag = FlagDefault
for _, flag := range flags {
SumFlag = SumFlag | flag
}
if dataset == nil {
panic("failed creating vm: using empty dataset")
}
vm := C.randomx_create_vm(SumFlag.toC(), cache, dataset)
if vm == nil {
return nil, errors.New("failed to create vm")
}
return vm, nil
}
func SetVMCache(vm *C.randomx_vm, cache *C.randomx_cache) {
C.randomx_vm_set_cache(vm, cache)
}
func SetVMDataset(vm *C.randomx_vm, dataset *C.randomx_dataset) {
C.randomx_vm_set_dataset(vm, dataset)
}
func DestroyVM(vm *C.randomx_vm) {
C.randomx_destroy_vm(vm)
}
func CalculateHash(vm *C.randomx_vm, in []byte) []byte {
out := make([]byte, C.RANDOMX_HASH_SIZE)
if vm == nil {
panic("failed hashing: using empty vm")
}
C.randomx_calculate_hash(vm, unsafe.Pointer(&in[0]), C.size_t(len(in)), unsafe.Pointer(&out[0]))
return out
}
func CalculateHashFirst(vm *C.randomx_vm, in []byte) {
if vm == nil {
panic("failed hashing: using empty vm")
}
C.randomx_calculate_hash_first(vm, unsafe.Pointer(&in[0]), C.size_t(len(in)))
}
func CalculateHashNext(vm *C.randomx_vm, in []byte) []byte {
out := make([]byte, C.RANDOMX_HASH_SIZE)
if vm == nil {
panic("failed hashing: using empty vm")
}
C.randomx_calculate_hash_next(vm, unsafe.Pointer(&in[0]), C.size_t(len(in)), unsafe.Pointer(&out[0]))
return out
}
//// Types
type RxCache struct {
seed []byte
cache *C.randomx_cache
initCount uint64
}
type RxDataset struct {
dataset *C.randomx_dataset
rxCache *RxCache
workerNum uint32
}
type RxVM struct {
vm *C.randomx_vm
rxDataset *RxDataset
}