forked from mining-pool/go-randomx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandomx.go
176 lines (138 loc) · 4.04 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
package randomx
//#cgo CFLAGS: -I./randomx
//#cgo LDFLAGS: -lrandomx -lstdc++
//#cgo linux,amd64 LDFLAGS:-L${SRCDIR}/build/linux-x86_64 -lm
//#cgo linux,arm64 LDFLAGS:-L${SRCDIR}/build/linux-aarch64 -lm
//#cgo darwin,amd64 LDFLAGS:-L${SRCDIR}/build/macos-x86_64 -lm
//#cgo windows,amd64 LDFLAGS:-L${SRCDIR}/build/windows-x86_64 -static -static-libgcc -static-libstdc++
//#include <stdlib.h>
//#include "randomx.h"
import "C"
import (
"errors"
"unsafe"
)
const RxHashSize = C.RANDOMX_HASH_SIZE
// All flags
const (
FlagDefault C.randomx_flags = 0 // for all default
FlagLargePages C.randomx_flags = 1 // for dataset & rxCache & vm
FlagHardAES C.randomx_flags = 2 // for vm
FlagFullMEM C.randomx_flags = 4 // for vm
FlagJIT C.randomx_flags = 8 // for vm & cache
FlagSecure C.randomx_flags = 16
FlagArgon2SSSE3 C.randomx_flags = 32 // for cache
FlagArgon2AVX2 C.randomx_flags = 64 // for cache
FlagArgon2 C.randomx_flags = 96 // = avx2 + sse3
)
type Cache *C.randomx_cache
type Dataset *C.randomx_dataset
type VM *C.randomx_vm
func AllocCache(flags ...C.randomx_flags) (Cache, error) {
var SumFlag = FlagDefault
var cache *C.randomx_cache
for _, flag := range flags {
SumFlag = SumFlag | flag
}
cache = C.randomx_alloc_cache(SumFlag)
if cache == nil {
return nil, errors.New("failed to alloc mem for rxCache")
}
return cache, nil
}
func InitCache(cache 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 Cache) {
C.randomx_release_cache(cache)
}
func AllocDataset(flags ...C.randomx_flags) (Dataset, error) {
var SumFlag = FlagDefault
for _, flag := range flags {
SumFlag = SumFlag | flag
}
var dataset *C.randomx_dataset
dataset = C.randomx_alloc_dataset(SumFlag)
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 Dataset, cache 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))
}
func GetDatasetMemory(dataset Dataset) unsafe.Pointer {
return C.randomx_get_dataset_memory(dataset)
}
func ReleaseDataset(dataset Dataset) {
C.randomx_release_dataset(dataset)
}
func CreateVM(cache Cache, dataset Dataset, flags ...C.randomx_flags) (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, cache, dataset)
if vm == nil {
return nil, errors.New("failed to create vm")
}
return vm, nil
}
func SetVMCache(vm VM, cache Cache) {
C.randomx_vm_set_cache(vm, cache)
}
func SetVMDataset(vm VM, dataset Dataset) {
C.randomx_vm_set_dataset(vm, dataset)
}
func DestroyVM(vm VM) {
C.randomx_destroy_vm(vm)
}
func CalculateHash(vm VM, in []byte) []byte {
if vm == nil {
panic("failed hashing: using empty vm")
}
input := C.CBytes(in)
output := C.CBytes(make([]byte, RxHashSize))
C.randomx_calculate_hash(vm, input, C.size_t(len(in)), output)
hash := C.GoBytes(output, RxHashSize)
C.free(unsafe.Pointer(input))
C.free(unsafe.Pointer(output))
return hash
}
func CalculateHashFirst(vm VM, in []byte) {
if vm == nil {
panic("failed hashing: using empty vm")
}
input := C.CBytes(in)
C.randomx_calculate_hash_first(vm, input, C.size_t(len(in)))
C.free(unsafe.Pointer(input))
}
func CalculateHashNext(vm VM, in []byte) []byte {
if vm == nil {
panic("failed hashing: using empty vm")
}
input := C.CBytes(in)
output := C.CBytes(make([]byte, RxHashSize))
C.randomx_calculate_hash_next(vm, input, C.size_t(len(in)), output)
hash := C.GoBytes(output, RxHashSize)
C.free(unsafe.Pointer(input))
C.free(unsafe.Pointer(output))
return hash
}