Version v2.0.0 with asm, jit, performance and allocation improvements
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
DataHoarder 2024-04-12 02:08:40 +02:00
parent 0a681cd2da
commit c232f60979
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
5 changed files with 40 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package randomx
import (
"slices"
"unsafe"
)
@ -53,10 +54,9 @@ func (cache *Randomx_Cache) Close() error {
}
func (cache *Randomx_Cache) Init(key []byte) {
//fmt.Printf("appending null byte is not necessary but only done for testing")
kkey := append([]byte{}, key...)
//kkey = append(kkey,0)
//cache->initialize(cache, key, keySize);
kkey := slices.Clone(key)
argonBlocks := argon2_buildBlocks(kkey, []byte(RANDOMX_ARGON_SALT), []byte{}, []byte{}, RANDOMX_ARGON_ITERATIONS, RANDOMX_ARGON_MEMORY, RANDOMX_ARGON_LANES, 0)
memoryBlocks := unsafe.Slice((*MemoryBlock)(unsafe.Pointer(unsafe.SliceData(argonBlocks))), int(unsafe.Sizeof(argonBlock{}))/int(unsafe.Sizeof(MemoryBlock{}))*len(argonBlocks))

2
go.mod
View file

@ -1,4 +1,4 @@
module git.gammaspectra.live/P2Pool/go-randomx
module git.gammaspectra.live/P2Pool/go-randomx/v2
go 1.21

View file

@ -92,7 +92,6 @@ func Benchmark_RandomX(b *testing.B) {
}()
vm := c.VM_Initialize()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var output_hash [32]byte
@ -100,3 +99,30 @@ func Benchmark_RandomX(b *testing.B) {
runtime.KeepAlive(output_hash)
}
}
func Benchmark_RandomXParallel(b *testing.B) {
b.ReportAllocs()
tt := Tests[0]
c := Randomx_alloc_cache(0)
c.Init(tt.key)
defer func() {
err := c.Close()
if err != nil {
b.Error(err)
}
}()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var output_hash [32]byte
vm := c.VM_Initialize()
for pb.Next() {
vm.CalculateHash(tt.input, &output_hash)
runtime.KeepAlive(output_hash)
}
})
}

15
vm.go
View file

@ -30,7 +30,7 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package randomx
import (
"git.gammaspectra.live/P2Pool/go-randomx/asm"
"git.gammaspectra.live/P2Pool/go-randomx/v2/asm"
"math"
"runtime"
)
@ -47,7 +47,7 @@ type VM struct {
State_start [64]byte
buffer [RANDOMX_PROGRAM_SIZE*8 + 16*8]byte // first 128 bytes are entropy below rest are program bytes
Prog []byte
ScratchPad []byte
ScratchPad [ScratchpadSize]byte
ByteCode [RANDOMX_PROGRAM_SIZE]InstructionByteCode
@ -102,9 +102,7 @@ func (vm *VM) Run(input_hash []byte) {
vm.Prog = vm.buffer[len(vm.entropy)*8:]
for i := range vm.reg.r {
vm.reg.r[i] = 0
}
clear(vm.reg.r[:])
// do more initialization before we run
@ -209,8 +207,9 @@ func (vm *VM) CalculateHash(input []byte, output *[32]byte) {
input_hash := blake2b.Sum512(input)
vm.ScratchPad = make([]byte, ScratchpadSize, ScratchpadSize) // calculate and fill scratchpad
fillAes1Rx4(input_hash[:], vm.ScratchPad)
// calculate and fill scratchpad
clear(vm.ScratchPad[:])
fillAes1Rx4(input_hash[:], vm.ScratchPad[:])
hash512, _ := blake2b.New512(nil)
@ -254,7 +253,7 @@ func (vm *VM) CalculateHash(input []byte, output *[32]byte) {
vm.Run(temp_hash)
// now hash the scratch pad and place into register a
hashAes1Rx4(vm.ScratchPad, temp_hash)
hashAes1Rx4(vm.ScratchPad[:], temp_hash)
hash256, _ := blake2b.New256(nil)

View file

@ -31,7 +31,7 @@ package randomx
import (
"fmt"
"git.gammaspectra.live/P2Pool/go-randomx/asm"
"git.gammaspectra.live/P2Pool/go-randomx/v2/asm"
)
import "math"
import "math/bits"