randomx

package
v0.0.0-...-decf009 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2023 License: ISC, BSD-3-Clause Imports: 6 Imported by: 0

README

go-randomx

WARNING: this is not a lib, but a binding

Do NOT use go mod import this

NOTICE: For better go.mod experience, like direcly import go-randomx dep through go get or go build, check the https://github.com/ngchain/RandomX and https://github.com/ngchain/go-randomx and their github actions.

Algorithms

  • random-x
  • random-xl
  • random-wow
  • random-arq
  • random-yada
  • ...

Build

Windows

Firstly download and install the msys2, then open and install the following components:

Take msys2's pacman for example

pacman -Syu
pacman -S git mingw64/mingw-w64-x86_64-go mingw64/mingw-w64-x86_64-gcc mingw64/mingw-w64-x86_64-cmake mingw64/mingw-w64-x86_64-make

Secondly clone this repo to your project folder

cd MyProject
git clone https://github.com/maoxs2/go-randomx

And then run ./build.sh to auto compile official random-x code

# clone and compile RandomX source code into librandomx
./build random-x # random-x can be replaced with random-xl random-arq random-wow

Finally you can using the package as your internal one.

Directly using it with import "github.com/MyProject/go-randomx" and then randomx.AllocCache() etc.

Linux

Take Ubuntu for example

Download the latest go from here and then install it following this instruction

sudo apt update && sudo apt upgrade 
sudo apt install git cmake make gcc build-essential

Secondly clone this repo to your project folder

cd MyProject
git clone https://github.com/maoxs2/go-randomx

And then run go generate to auto compile official random-x code

# clone and compile RandomX source code into librandomx
./build random-x # random-x can be replaced with random-xl random-arq random-wow

Finally you can using the package as your internal one.

Directly using it with import "github.com/myname/my-project/go-randomx" and then start the functions like randomx.AllocCache() etc.

More

If you have any better solution, tell me please.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllocCache

func AllocCache(flags ...Flag) (*C.randomx_cache, error)

AllocCache allocates and initializes a new RandomX cache with given flags.

func AllocDataset

func AllocDataset(flags ...Flag) (*C.randomx_dataset, error)

AllocDataset allocates and initializes a new RandomX dataset with given flags.

func CalculateHash

func CalculateHash(vm *C.randomx_vm, in []byte) []byte

CalculateHash calculates the hash for the given input using the specified RandomX VM.

func CalculateHashFirst

func CalculateHashFirst(vm *C.randomx_vm, in []byte)

CalculateHashFirst prepares the calculation of a hash for the given input using the specified RandomX VM.

func CalculateHashNext

func CalculateHashNext(vm *C.randomx_vm, in []byte) []byte

CalculateHashNext calculates the hash for the next input using the specified RandomX VM.

func CreateVM

func CreateVM(cache *C.randomx_cache, dataset *C.randomx_dataset, flags ...Flag) (*C.randomx_vm, error)

CreateVM creates a new RandomX VM with the given cache, dataset, and flags.

func DatasetItemCount

func DatasetItemCount() uint32

DatasetItemCount returns the count of items in the RandomX dataset.

func DestroyVM

func DestroyVM(vm *C.randomx_vm)

DestroyVM destroys the given RandomX VM.

func FastInitFullDataset

func FastInitFullDataset(dataset *C.randomx_dataset, cache *C.randomx_cache, workerNum uint32)

FastInitFullDataset using c's pthread to boost the dataset init. 472s -> 466s

func GetDatasetMemory

func GetDatasetMemory(dataset *C.randomx_dataset) unsafe.Pointer

GetDatasetMemory returns a pointer to the memory of the given RandomX dataset.

func InitCache

func InitCache(cache *C.randomx_cache, seed []byte)

InitCache initializes the given RandomX cache with the specified seed.

func InitDataset

func InitDataset(dataset *C.randomx_dataset, cache *C.randomx_cache, startItem uint32, itemCount uint32)

InitDataset initializes the given RandomX dataset with specified parameters.

func ReleaseCache

func ReleaseCache(cache *C.randomx_cache)

ReleaseCache releases the allocated RandomX cache.

func ReleaseDataset

func ReleaseDataset(dataset *C.randomx_dataset)

ReleaseDataset releases the allocated RandomX dataset.

func Search(vm *C.randomx_vm, in []byte, target uint64, maxTimes uint64, jump uint32, nonce []byte) (Hash []byte, Found bool, Sol []byte)

Search performs a hash search operation using the provided RandomX VM, input bytes, target, maximum iterations, jump, and nonce. It returns the hash, a boolean indicating if a solution was found, and the solution bytes.

func SetVMCache

func SetVMCache(vm *C.randomx_vm, cache *C.randomx_cache)

SetVMCache sets the cache for the given RandomX VM.

func SetVMDataset

func SetVMDataset(vm *C.randomx_vm, dataset *C.randomx_dataset)

SetVMDataset sets the dataset for the given RandomX VM.

Types

type Flag

type Flag int

Flag represents the type for various configuration flags in RandomX.

var (
	FlagDefault     Flag = 0  // FlagDefault represents the default configuration.
	FlagLargePages  Flag = 1  // FlagLargePages enables large pages for dataset, cache, and VM.
	FlagHardAES     Flag = 2  // FlagHardAES enables the use of hardware AES for VM.
	FlagFullMEM     Flag = 4  // FlagFullMEM uses full memory for VM.
	FlagJIT         Flag = 8  // FlagJIT enables Just-In-Time compilation for VM and cache.
	FlagSecure      Flag = 16 // FlagSecure represents a secure configuration setting.
	FlagArgon2SSSE3 Flag = 32 // FlagArgon2SSSE3 uses SSSE3 instructions for cache.
	FlagArgon2AVX2  Flag = 64 // FlagArgon2AVX2 uses AVX2 instructions for cache.
	FlagArgon2      Flag = 96 // FlagArgon2 combines AVX2 and SSSE3 for cache.
)

Various flags used to configure RandomX.

type RxCache

type RxCache struct {
	// contains filtered or unexported fields
}

RxCache represents a RandomX cache with associated seed and initialization count.

func NewRxCache

func NewRxCache(flags ...Flag) (*RxCache, error)

NewRxCache creates a new RxCache with the specified flags.

func (*RxCache) Close

func (c *RxCache) Close()

Close releases the resources associated with the RxCache.

func (*RxCache) Init

func (c *RxCache) Init(seed []byte) bool

Init initializes the RxCache with the given seed. Returns true if initialization was successful.

func (*RxCache) IsReady

func (c *RxCache) IsReady(seed []byte) bool

IsReady checks if the RxCache is ready and matches the given seed.

type RxDataset

type RxDataset struct {
	// contains filtered or unexported fields
}

RxDataset represents a RandomX dataset with associated cache and worker number.

func NewRxDataset

func NewRxDataset(flags ...Flag) (*RxDataset, error)

NewRxDataset creates a new RxDataset with the specified flags.

func (*RxDataset) CInit

func (ds *RxDataset) CInit(seed []byte, workerNum uint32) bool

CInit initializes the RxDataset using C's pthreads for a faster setup. Returns true if initialization was successful.

func (*RxDataset) Close

func (ds *RxDataset) Close()

Close releases the resources associated with the RxDataset.

func (*RxDataset) GoInit

func (ds *RxDataset) GoInit(seed []byte, workerNum uint32) bool

GoInit initializes the RxDataset using Go routines. Returns true if initialization was successful.

type RxVM

type RxVM struct {
	// contains filtered or unexported fields
}

RxVM represents a RandomX Virtual Machine with associated dataset.

func NewRxVM

func NewRxVM(rxDataset *RxDataset, flags ...Flag) (*RxVM, error)

NewRxVM creates a new RxVM (RandomX Virtual Machine) using the provided RxDataset and flags.

func (*RxVM) CalcHash

func (vm *RxVM) CalcHash(in []byte) []byte

CalcHash calculates and returns the hash of the given input using this RxVM.

func (*RxVM) CalcHashFirst

func (vm *RxVM) CalcHashFirst(in []byte)

CalcHashFirst prepares the RxVM to calculate the hash of the given input.

func (*RxVM) CalcHashNext

func (vm *RxVM) CalcHashNext(in []byte) []byte

CalcHashNext calculates and returns the hash of the next input using this RxVM.

func (*RxVM) Close

func (vm *RxVM) Close()

Close releases the resources associated with the RxVM.

func (*RxVM) Search

func (vm *RxVM) Search(in []byte, target uint64, maxTimes uint64, jump uint32, nonce []byte) (hash []byte, found bool, sol []byte)

Search performs a hash search operation using this RxVM with the given input bytes, target, maximum iterations, jump, and nonce. It returns the hash, a boolean indicating if a solution was found, and the solution bytes.

func (*RxVM) UpdateDataset

func (vm *RxVM) UpdateDataset(rxDataset *RxDataset)

UpdateDataset updates the RxVM with a new dataset and cache from the provided RxDataset.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL