49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package randomx
|
|
|
|
/*
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "randomx.h"
|
|
|
|
bool search(randomx_vm* vm, void* in, const uint64_t target, const uint64_t max_times, const uint32_t jump, void* nonce, void* out, void* sol)
|
|
{
|
|
//randomx_calculate_hash_first(vm, in, 76);
|
|
|
|
for (uint64_t i=0; i < max_times; i++)
|
|
{
|
|
*(uint32_t*)(in+39) = *(uint32_t*)(nonce) + jump;
|
|
randomx_calculate_hash_next(vm, in, 76, out);
|
|
|
|
*(uint32_t*)(sol) = *(uint32_t*)(nonce);
|
|
*(uint32_t*)(nonce) = *(uint32_t*)(in+39);
|
|
|
|
if (*(uint64_t*)(out+24) < target) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
*/
|
|
import "C"
|
|
import (
|
|
"runtime"
|
|
"unsafe"
|
|
)
|
|
|
|
func Search(vm *C.randomx_vm, in []byte, target uint64, maxTimes uint64, jump uint32, nonce []byte) (hash RxHash, found bool, sol []byte) {
|
|
if vm == nil {
|
|
panic("failed hashing: using empty vm")
|
|
}
|
|
sol = make([]byte, 4)
|
|
|
|
var cFound C.bool
|
|
cFound = C.search(vm, unsafe.Pointer(unsafe.SliceData(in)), C.uint64_t(target), C.uint64_t(maxTimes), C.uint32_t(jump), unsafe.Pointer(unsafe.SliceData(nonce)), unsafe.Pointer(&hash), unsafe.Pointer(unsafe.SliceData(sol)))
|
|
runtime.KeepAlive(in)
|
|
runtime.KeepAlive(nonce)
|
|
return hash, bool(cFound), sol
|
|
}
|
|
|
|
func (vm *RxVM) Search(in []byte, target uint64, maxTimes uint64, jump uint32, nonce []byte) (hash RxHash, found bool, sol []byte) {
|
|
return Search(vm.vm, in, target, maxTimes, jump, nonce)
|
|
}
|