Skip to content

Commit

Permalink
implemented inverse operation for xoshiro because why not
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Jun 10, 2019
1 parent 39265ec commit 17fab01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions xoshiro.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,19 @@ func (xoshi *Xoshiro) Jump() {
var xoshiroJump = [4]uint64{
0x76e15d3efefdcbbf, 0xc5004e441c522fb3, 0x77710069854ee241, 0x39109bb02acbe635,
}

// Reverse moves the generator backward one step.
func (xoshi *Xoshiro) Reverse() {
xa := xoshi.x
xa ^= xa << 17
xa ^= xa << 34
ya := xoshi.y
ya ^= ya << 17
ya ^= ya << 34
xoshi.z = bits.RotateLeft64(xoshi.z, 19)
xoshi.y = xoshi.w ^ xoshi.x
xoshi.w ^= xoshi.z
xoshi.x = xa ^ ya
xoshi.z ^= xoshi.x
xoshi.y ^= xoshi.z
}
13 changes: 13 additions & 0 deletions xoshiro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ func TestXoshiCopy(t *testing.T) {
}
}

func TestXoshiReverse(t *testing.T) {
xoshi := CryptoSeeded(NewXoshiro(), 32).(*Xoshiro)
for i := 0; i < 1024; i++ {
a := xoshi.Uint64()
xoshi.Reverse()
b := xoshi.Uint64()
if a != b {
t.Fail()
}
xoshi.Uint64()
}
}

func BenchmarkXoshiro(b *testing.B) {
xoshi := CryptoSeeded(NewXoshiro(), 32).(*Xoshiro)
f := func(p []byte) func(b *testing.B) {
Expand Down

0 comments on commit 17fab01

Please sign in to comment.