Bump go-randomx to v2.0.0 with faster execution and JIT

This commit is contained in:
DataHoarder 2024-04-12 02:18:49 +02:00
parent d0c788b7c9
commit 249d97299e
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
4 changed files with 19 additions and 24 deletions

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.22
require (
git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20240405085108-e2f706cb5c00
git.gammaspectra.live/P2Pool/go-randomx v1.0.0
git.gammaspectra.live/P2Pool/go-randomx/v2 v2.0.0
git.gammaspectra.live/P2Pool/monero-base58 v1.0.0
git.gammaspectra.live/P2Pool/randomx-go-bindings v1.0.0
git.gammaspectra.live/P2Pool/sha3 v0.17.0

4
go.sum
View file

@ -1,7 +1,7 @@
git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20240405085108-e2f706cb5c00 h1:mDQY337iKB+kle5RYWL5CoAz+3DmnkAh/B2XD8B+PFk=
git.gammaspectra.live/P2Pool/edwards25519 v0.0.0-20240405085108-e2f706cb5c00/go.mod h1:FZsrMWGucMP3SZamzrd7m562geIs5zp1O/9MGoiAKH0=
git.gammaspectra.live/P2Pool/go-randomx v1.0.0 h1:3lE8UWl0509Q5TCtBECLQNnIyxEhPXnmROVMTngEnuM=
git.gammaspectra.live/P2Pool/go-randomx v1.0.0/go.mod h1:K3qOa7AMW0/5azfHraQXxEsc9HygHwlfoLOkHqnSGgE=
git.gammaspectra.live/P2Pool/go-randomx/v2 v2.0.0 h1:lHi2z8Nz3H1Xpu1p53TYccz072BDhbJGNHquiX9orCM=
git.gammaspectra.live/P2Pool/go-randomx/v2 v2.0.0/go.mod h1:vNmHlEIRAcU/bA85mxbUKEiBYtrtS4MVwozf29KmoHM=
git.gammaspectra.live/P2Pool/monero-base58 v1.0.0 h1:s8LZxVNc93YEs2NCCNWZ7CKr8RbEb031y6Wkvhn+TS4=
git.gammaspectra.live/P2Pool/monero-base58 v1.0.0/go.mod h1:WWEJy/AdWKxKAruvlKI82brw+DtVlePy0ct3ZiBlc68=
git.gammaspectra.live/P2Pool/randomx-go-bindings v1.0.0 h1:tajr4QFSPrb8NtHmU14JaXdhr+z+0RbOBLIgUDI5Tow=

View file

@ -55,6 +55,7 @@ func GetAuxiliarySlot(id types.Hash, nonce, numberAuxiliaryChains uint32) (auxil
return 0
}
// HashKeyMergeMineSlot HASH_KEY_MM_SLOT
const HashKeyMergeMineSlot = 'm'
var buf [types.HashSize + 4 + 1]byte

View file

@ -7,7 +7,7 @@ import (
"crypto/subtle"
"git.gammaspectra.live/P2Pool/consensus/v3/monero/crypto"
"git.gammaspectra.live/P2Pool/consensus/v3/types"
"git.gammaspectra.live/P2Pool/go-randomx"
"git.gammaspectra.live/P2Pool/go-randomx/v2"
"runtime"
"sync"
"unsafe"
@ -19,14 +19,13 @@ type hasher struct {
flags []Flag
key []byte
vm *randomx.VM
}
func ConsensusHash(buf []byte) types.Hash {
cache := randomx.Randomx_alloc_cache(0)
cache.Randomx_init_cache(buf)
cache.Init(buf)
scratchpad := unsafe.Slice((*byte)(unsafe.Pointer(&cache.Blocks[0])), len(cache.Blocks)*len(cache.Blocks[0])*int(unsafe.Sizeof(uint64(0))))
scratchpad := unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(cache.Blocks))), len(cache.Blocks)*len(cache.Blocks[0])*int(unsafe.Sizeof(uint64(0))))
defer runtime.KeepAlive(cache)
// Intentionally not a power of 2
@ -63,33 +62,28 @@ func (h *hasher) OptionNumberOfCachedStates(n int) error {
func NewRandomX(n int, flags ...Flag) (Hasher, error) {
return &hasher{
flags: flags,
cache: randomx.Randomx_alloc_cache(0),
cache: randomx.Randomx_alloc_cache(randomx.RANDOMX_FLAG_JIT),
}, nil
}
func (h *hasher) Hash(key []byte, input []byte) (output types.Hash, err error) {
h.lock.Lock()
defer h.lock.Unlock()
vm := func() *randomx.VM {
h.lock.Lock()
defer h.lock.Unlock()
if h.key == nil || bytes.Compare(h.key, key) != 0 {
h.key = make([]byte, len(key))
copy(h.key, key)
if h.key == nil || bytes.Compare(h.key, key) != 0 {
h.key = make([]byte, len(key))
copy(h.key, key)
h.cache.Randomx_init_cache(h.key)
gen := randomx.Init_Blake2Generator(h.key, 0)
for i := 0; i < 8; i++ {
h.cache.Programs[i] = randomx.Build_SuperScalar_Program(gen)
h.cache.Init(h.key)
}
h.vm = h.cache.VM_Initialize()
}
return h.cache.VM_Initialize()
}()
outputBuf := make([]byte, types.HashSize)
h.vm.CalculateHash(input, outputBuf)
copy(output[:], outputBuf)
vm.CalculateHash(input, (*[32]byte)(&output))
return
}
func (h *hasher) Close() {
h.cache.Close()
}