Add a xoShiRo256PlusPlus implementation

This commit is contained in:
Elichai Turkel 2021-11-06 21:43:41 +02:00
parent c26b4deefe
commit 0d09d1df66
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package pow
import (
"encoding/binary"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"math/bits"
)
type xoShiRo256PlusPlus struct {
s0 uint64
s1 uint64
s2 uint64
s3 uint64
}
func newxoShiRo256PlusPlus(hash *externalapi.DomainHash) *xoShiRo256PlusPlus {
hashArray := hash.ByteArray()
return &xoShiRo256PlusPlus{
s0: binary.LittleEndian.Uint64(hashArray[:8]),
s1: binary.LittleEndian.Uint64(hashArray[8:16]),
s2: binary.LittleEndian.Uint64(hashArray[16:24]),
s3: binary.LittleEndian.Uint64(hashArray[24:32]),
}
}
func (x *xoShiRo256PlusPlus) Uint64() uint64 {
res := bits.RotateLeft64(x.s0+x.s3, 23) + x.s0
t := x.s1 << 17
x.s2 ^= x.s0
x.s3 ^= x.s1
x.s1 ^= x.s2
x.s0 ^= x.s3
x.s2 ^= t
x.s3 = bits.RotateLeft64(x.s3, 45)
return res
}

View File

@ -0,0 +1,17 @@
package pow
import "testing"
// Test vectors are from here: https://github.com/rust-random/rngs/blob/17aa826cc38d3e8408c9489ac859fa9397acd479/rand_xoshiro/src/xoshiro256plusplus.rs#L121
func TestXoShiRo256PlusPlus_Uint64(t *testing.T) {
state := xoShiRo256PlusPlus{1, 2, 3, 4}
expected := []uint64{41943041, 58720359, 3588806011781223, 3591011842654386,
9228616714210784205, 9973669472204895162, 14011001112246962877,
12406186145184390807, 15849039046786891736, 10450023813501588000}
for _, ex := range expected {
val := state.Uint64()
if val != ex {
t.Errorf("expected: %d, found: %d", ex, val)
}
}
}