go-randomx/vm.go

241 lines
6.4 KiB
Go
Raw Normal View History

2019-10-15 18:16:56 +00:00
/*
Copyright (c) 2019 DERO Foundation. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2019-10-15 17:45:39 +00:00
package randomx
import (
"git.gammaspectra.live/P2Pool/go-randomx/v2/aes"
"math"
"runtime"
"unsafe"
)
2019-10-15 17:45:39 +00:00
import "golang.org/x/crypto/blake2b"
type REG struct {
Hi uint64
Lo uint64
}
type VM struct {
ScratchPad ScratchPad
2019-10-15 17:45:39 +00:00
2024-04-11 13:48:11 +00:00
Dataset Randomx_Dataset
2024-04-18 10:09:05 +00:00
JITProgram VMProgramFunc
2019-10-15 17:45:39 +00:00
}
// Run calculate hash based on input
// Warning: Underlying callers will run float64 SetRoundingMode directly
// It is the caller's responsibility to set and restore the mode to IEEE 754 roundTiesToEven between full executions
// Additionally, runtime.LockOSThread and defer runtime.UnlockOSThread is recommended to prevent other goroutines sharing these changes
func (vm *VM) Run(inputHash [64]byte, roundingMode uint8) (reg RegisterFile) {
2019-10-15 17:45:39 +00:00
reg.FPRC = roundingMode
2019-10-15 17:45:39 +00:00
// buffer first 128 bytes are entropy below rest are program bytes
var buffer [16*8 + RANDOMX_PROGRAM_SIZE*8]byte
aes.FillAes4Rx4(inputHash, buffer[:])
2019-10-15 17:45:39 +00:00
entropy := (*[16]uint64)(unsafe.Pointer(&buffer))
2019-10-15 17:45:39 +00:00
prog := buffer[len(entropy)*8:]
2019-10-15 17:45:39 +00:00
// do more initialization before we run
for i := range entropy[:8] {
reg.A[i/2][i%2] = SmallPositiveFloatBits(entropy[i])
}
var mem MemoryRegisters
mem.ma = entropy[8] & CacheLineAlignMask
mem.mx = entropy[10]
addressRegisters := entropy[12]
var readReg [4]uint64
for i := range readReg {
readReg[i] = uint64(i*2) + (addressRegisters & 1)
addressRegisters >>= 1
}
datasetOffset := (entropy[13] % (DATASETEXTRAITEMS + 1)) * CacheLineSize
eMask := [2]uint64{EMask(entropy[14]), EMask(entropy[15])}
2019-10-15 17:45:39 +00:00
byteCode := CompileProgramToByteCode(prog)
2019-10-15 17:45:39 +00:00
spAddr0 := mem.mx
spAddr1 := mem.ma
2019-10-15 17:45:39 +00:00
var rlCache RegisterLine
2024-04-18 10:09:05 +00:00
if vm.JITProgram != nil {
if vm.Dataset.Flags()&RANDOMX_FLAG_SECURE > 0 {
mapProgramRW(vm.JITProgram)
byteCode.generateCode(vm.JITProgram)
mapProgramRX(vm.JITProgram)
} else {
byteCode.generateCode(vm.JITProgram)
}
}
2019-10-15 17:45:39 +00:00
for ic := 0; ic < RANDOMX_PROGRAM_ITERATIONS; ic++ {
spMix := reg.R[readReg[0]] ^ reg.R[readReg[1]]
2019-10-15 17:45:39 +00:00
spAddr0 ^= spMix
spAddr0 &= ScratchpadL3Mask64
spAddr1 ^= spMix >> 32
spAddr1 &= ScratchpadL3Mask64
//TODO: optimize these loads!
2024-04-14 13:43:54 +00:00
for i := uint64(0); i < RegistersCount; i++ {
reg.R[i] ^= vm.ScratchPad.Load64(uint32(spAddr0 + 8*i))
2019-10-15 17:45:39 +00:00
}
2024-04-14 13:43:54 +00:00
for i := uint64(0); i < RegistersCountFloat; i++ {
reg.F[i] = vm.ScratchPad.Load32FA(uint32(spAddr1 + 8*i))
2019-10-15 17:45:39 +00:00
}
2024-04-14 13:43:54 +00:00
for i := uint64(0); i < RegistersCountFloat; i++ {
reg.E[i] = vm.ScratchPad.Load32FA(uint32(spAddr1 + 8*(i+RegistersCountFloat)))
2019-10-15 17:45:39 +00:00
reg.E[i][LOW] = MaskRegisterExponentMantissa(reg.E[i][LOW], eMask[LOW])
reg.E[i][HIGH] = MaskRegisterExponentMantissa(reg.E[i][HIGH], eMask[HIGH])
2019-10-15 17:45:39 +00:00
}
// Run the actual bytecode
2024-04-18 10:09:05 +00:00
if vm.JITProgram != nil {
vm.JITProgram.Execute(&reg, &vm.ScratchPad, eMask)
} else {
byteCode.Execute(&reg, &vm.ScratchPad, eMask)
}
2019-10-15 17:45:39 +00:00
mem.mx ^= reg.R[readReg[2]] ^ reg.R[readReg[3]]
mem.mx &= CacheLineAlignMask
2019-10-15 17:45:39 +00:00
vm.Dataset.PrefetchDataset(datasetOffset + mem.mx)
2019-10-15 17:45:39 +00:00
// execute diffuser superscalar program to get dataset 64 bytes
vm.Dataset.ReadDataset(datasetOffset+mem.ma, &reg.R, &rlCache)
2019-10-15 17:45:39 +00:00
2024-04-11 14:04:59 +00:00
// swap the elements
mem.mx, mem.ma = mem.ma, mem.mx
2019-10-15 17:45:39 +00:00
2024-04-14 13:43:54 +00:00
for i := uint64(0); i < RegistersCount; i++ {
vm.ScratchPad.Store64(uint32(spAddr1+8*i), reg.R[i])
2019-10-15 17:45:39 +00:00
}
2024-04-14 13:43:54 +00:00
for i := uint64(0); i < RegistersCountFloat; i++ {
reg.F[i][LOW] = Xor(reg.F[i][LOW], reg.E[i][LOW])
reg.F[i][HIGH] = Xor(reg.F[i][HIGH], reg.E[i][HIGH])
2019-10-15 17:45:39 +00:00
vm.ScratchPad.Store64(uint32(spAddr0+16*i), math.Float64bits(reg.F[i][LOW]))
vm.ScratchPad.Store64(uint32(spAddr0+16*i+8), math.Float64bits(reg.F[i][HIGH]))
2019-10-15 17:45:39 +00:00
}
spAddr0 = 0
spAddr1 = 0
}
return reg
2019-10-15 17:45:39 +00:00
}
func (vm *VM) InitScratchpad(seed *[64]byte) {
vm.ScratchPad.Init(seed)
}
func (vm *VM) RunLoops(tempHash [64]byte) RegisterFile {
hash512, _ := blake2b.New512(nil)
2019-10-15 17:45:39 +00:00
if lockThreadDueToRoundingMode {
// Lock thread due to rounding mode flags
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
roundingMode := uint8(0)
2019-10-15 17:45:39 +00:00
for chain := 0; chain < RANDOMX_PROGRAM_COUNT-1; chain++ {
reg := vm.Run(tempHash, roundingMode)
roundingMode = reg.FPRC
2019-10-15 17:45:39 +00:00
hash512.Reset()
// write R, F, E, A registers
hash512.Write(reg.Memory()[:])
runtime.KeepAlive(reg)
2019-10-15 17:45:39 +00:00
hash512.Sum(tempHash[:0])
2019-10-15 17:45:39 +00:00
}
// final loop executes here
reg := vm.Run(tempHash, roundingMode)
2024-04-18 10:09:05 +00:00
// always force a restore
reg.FPRC = 0xff
2024-04-18 10:09:05 +00:00
// restore rounding mode to 0
SetRoundingMode(&reg, 0)
return reg
}
func (vm *VM) CalculateHash(input []byte, output *[32]byte) {
tempHash := blake2b.Sum512(input)
vm.InitScratchpad(&tempHash)
reg := vm.RunLoops(tempHash)
2019-10-15 17:45:39 +00:00
// now hash the scratch pad as it will act as register A
aes.HashAes1Rx4(vm.ScratchPad[:], &tempHash)
2019-10-15 17:45:39 +00:00
hash256, _ := blake2b.New256(nil)
hash256.Reset()
// write R, F, E registers
hash256.Write(reg.Memory()[:RegisterFileSize-RegistersCountFloat*2*8])
runtime.KeepAlive(reg)
2019-10-15 17:45:39 +00:00
// write register A
hash256.Write(tempHash[:])
2019-10-15 17:45:39 +00:00
hash256.Sum(output[:0])
2019-10-15 17:45:39 +00:00
}
2024-04-18 10:09:05 +00:00
func (vm *VM) Close() error {
if vm.JITProgram != nil {
return vm.JITProgram.Close()
}
return nil
}