Compare commits
20 Commits
a8a8ea940c
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
b8130ef527
|
|||
|
996751c1c6
|
|||
|
d9454fcdb2
|
|||
|
06d86a6679
|
|||
|
a28cb7d2f9
|
|||
|
8fab26a476
|
|||
|
3465a60cb1
|
|||
|
2aed836e67
|
|||
|
2ae6fda70e
|
|||
|
2c6fe0a7d9
|
|||
|
89e7569fa7
|
|||
|
0f890c923c
|
|||
|
7144c21074
|
|||
|
517b58a3df
|
|||
|
a5c54ed16c
|
|||
|
f3a2e18d2e
|
|||
|
20d031fba6
|
|||
|
d4be1689e2
|
|||
|
3effa09602
|
|||
|
55c628def9
|
@@ -1,36 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
env:
|
|
||||||
GIT__TOKEN: ${{ secrets.TOKEN }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
publish:
|
|
||||||
runs-on: linux
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- uses: actions/cache/restore@v4
|
|
||||||
with:
|
|
||||||
key: gradle
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches/
|
|
||||||
~/.gradle/wrapper/
|
|
||||||
~/.konan/
|
|
||||||
|
|
||||||
- uses: actions/setup-java@v4
|
|
||||||
with:
|
|
||||||
java-version: '24'
|
|
||||||
distribution: 'temurin'
|
|
||||||
|
|
||||||
- run: ./gradlew publish
|
|
||||||
|
|
||||||
- uses: actions/cache/save@v4
|
|
||||||
with:
|
|
||||||
key: gradle
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches/
|
|
||||||
~/.gradle/wrapper/
|
|
||||||
~/.konan/
|
|
||||||
47
.gitea/workflows/publish.yaml
Normal file
47
.gitea/workflows/publish.yaml
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: linux
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Java
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: '24'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Cache Gradle
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: gradle
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches/
|
||||||
|
~/.gradle/wrapper/
|
||||||
|
|
||||||
|
- name: Cache Konan
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: konan-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: konan
|
||||||
|
path: |
|
||||||
|
~/.konan/
|
||||||
|
|
||||||
|
- name: Cache Node
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: node-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: node
|
||||||
|
path: |
|
||||||
|
~/.gradle/nodejs
|
||||||
|
~/.gradle/yarn
|
||||||
|
|
||||||
|
- name: Publish
|
||||||
|
run: ./gradlew publish
|
||||||
|
env:
|
||||||
|
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
|
||||||
28
.gitea/workflows/test.yaml
Normal file
28
.gitea/workflows/test.yaml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: linux
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Java
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: '24'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: Cache Gradle
|
||||||
|
uses: actions/cache@v4
|
||||||
|
with:
|
||||||
|
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
|
||||||
|
restore-keys: gradle
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches/
|
||||||
|
~/.gradle/wrapper/
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
run: ./gradlew jvmTest
|
||||||
42
README.md
42
README.md
@@ -1,18 +1,14 @@
|
|||||||
# Kotlin OTP
|
# prng.kt
|
||||||
|
|
||||||
This library provides various [Pseudorandom Number Generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator)
|
`prng.kt` is a Kotlin Multiplatform library that provides common [Pseudorandom Number Generators](https://en.wikipedia.org/wiki/Pseudorandom_number_generator).
|
||||||
implementations in Kotlin Multiplatform.
|
|
||||||
|
|
||||||
## Features
|
Supported [PRNG](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) algorithms:
|
||||||
|
- LCG
|
||||||
|
- xoshiro256++
|
||||||
|
- xoshiro256**
|
||||||
|
|
||||||
* Generate random numbers using Kotlin's Random interface.
|
Supported [CSPRNG](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) algorithms:
|
||||||
* Seed the PRNGs to receive predictable results.
|
- HMAC-DRBG
|
||||||
* Reseed the PRNGs to add entropy to the following results.
|
|
||||||
* [HMAC DRBG](https://en.wikipedia.org/wiki/NIST_SP_800-90A#Hash_DRBG_and_HMAC_DRBG)
|
|
||||||
* Multiplatform support
|
|
||||||
* JVM
|
|
||||||
* JavaScript
|
|
||||||
* Native (Linux)
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -24,34 +20,30 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("com.infendro:random:1.1.0")
|
implementation("com.infendro:prng:1.3.1")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
import com.infendro.hash.SHA256
|
|
||||||
import com.infendro.random.PRNG
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
// create a PRNG instance
|
// create PRNG instance
|
||||||
val rng = PRNG.HMAC(SHA256)
|
val rng = CSPRNG.HMAC(`SHA-256`())
|
||||||
|
|
||||||
// seed the PRNG to receive predictable results
|
// seed PRNG to receive deterministic numbers
|
||||||
rng.seed(/* some seed */)
|
rng.seed(/* seed */)
|
||||||
val a = rng.nextInt()
|
val a = rng.nextInt()
|
||||||
val b = rng.nextInt()
|
val b = rng.nextInt()
|
||||||
val c = rng.nextInt()
|
val c = rng.nextInt()
|
||||||
|
|
||||||
// reseed the PRNG to add entropy to the following results
|
// reseed PRNG to add additional entropy
|
||||||
rng.reseed(/* some random entropy */)
|
rng.reseed(/* entropy */)
|
||||||
val d = rng.nextInt()
|
val d = rng.nextInt()
|
||||||
val e = rng.nextInt()
|
val e = rng.nextInt()
|
||||||
val f = rng.nextInt()
|
val f = rng.nextInt()
|
||||||
|
|
||||||
// each run:
|
// a, b, and c will be deterministic
|
||||||
// a, b, and c will be predictable
|
// d, e, and f will be random (assuming entropy is random)
|
||||||
// d, e, and f will be random (assuming the entropy is also random)
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,61 +1,43 @@
|
|||||||
group = "com.infendro"
|
@file:OptIn(ExperimentalWasmDsl::class)
|
||||||
version = "1.2.0"
|
|
||||||
|
|
||||||
repositories {
|
import com.infendro.plugin.infendro
|
||||||
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
import com.infendro.plugin.token
|
||||||
mavenCentral()
|
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||||
}
|
|
||||||
|
group = "com.infendro"
|
||||||
|
version = "1.3.1"
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
alias(libs.plugins.infendro)
|
||||||
alias(libs.plugins.multiplatform)
|
alias(libs.plugins.multiplatform)
|
||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm()
|
jvm()
|
||||||
js {
|
|
||||||
nodejs()
|
|
||||||
}
|
|
||||||
linuxX64()
|
linuxX64()
|
||||||
|
linuxArm64()
|
||||||
|
mingwX64()
|
||||||
|
macosX64()
|
||||||
|
macosArm64()
|
||||||
|
js { nodejs() }
|
||||||
|
wasmJs { nodejs() }
|
||||||
|
wasmWasi { nodejs() }
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
infendro()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
commonMain.dependencies {
|
commonMain.dependencies {
|
||||||
|
implementation(libs.bytes)
|
||||||
implementation(libs.hash)
|
implementation(libs.hash)
|
||||||
implementation(libs.mac)
|
implementation(libs.mac)
|
||||||
implementation(libs.bytearray)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
compilerOptions {
|
|
||||||
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
|
||||||
repositories {
|
|
||||||
maven {
|
|
||||||
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
|
|
||||||
authentication.create("header", HttpHeaderAuthentication::class)
|
|
||||||
credentials(HttpHeaderCredentials::class) {
|
|
||||||
val token = secret("git.token") ?: throw GradleException()
|
|
||||||
|
|
||||||
name = "Authorization"
|
|
||||||
value = "token $token"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun secret(
|
publishing.repositories {
|
||||||
key: String,
|
infendro(token)
|
||||||
): String? {
|
|
||||||
val property = key
|
|
||||||
val environment = key
|
|
||||||
.uppercase()
|
|
||||||
.replace(".", "__")
|
|
||||||
|
|
||||||
val prop = properties[property] as String?
|
|
||||||
val env = System.getenv(environment)
|
|
||||||
|
|
||||||
return prop ?: env
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.1.20"
|
infendro = "1.1.0"
|
||||||
hash = "1.3.2"
|
kotlin = "2.3.0"
|
||||||
mac = "1.2.0"
|
bytes = "1.4.2"
|
||||||
bytearray = "1.2.2"
|
hash = "1.7.0"
|
||||||
|
mac = "1.5.0"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
|
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }
|
||||||
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
hash = { module = "com.infendro:hash", version.ref = "hash" }
|
||||||
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
mac = { module = "com.infendro:mac", version.ref = "mac" }
|
||||||
bytearray = { module = "com.infendro:bytearray", version.ref = "bytearray" }
|
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
rootProject.name = "random"
|
rootProject.name = "prng"
|
||||||
|
|
||||||
|
pluginManagement.repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|||||||
5
src/commonMain/kotlin/com/infendro/random/CSPRNG.kt
Normal file
5
src/commonMain/kotlin/com/infendro/random/CSPRNG.kt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package com.infendro.random
|
||||||
|
|
||||||
|
abstract class CSPRNG : PRNG() {
|
||||||
|
abstract fun reseed(entropy: ByteArray)
|
||||||
|
}
|
||||||
16
src/commonMain/kotlin/com/infendro/random/PRNG.kt
Normal file
16
src/commonMain/kotlin/com/infendro/random/PRNG.kt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.infendro.random
|
||||||
|
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
|
abstract class PRNG : Random() {
|
||||||
|
abstract fun seed(seed: ByteArray)
|
||||||
|
|
||||||
|
protected abstract fun generate(): Int
|
||||||
|
|
||||||
|
override fun nextBits(bitCount: Int): Int {
|
||||||
|
require(bitCount in 0..32)
|
||||||
|
|
||||||
|
if (bitCount == 0) return 0
|
||||||
|
return generate() ushr (32 - bitCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package com.infendro.random.csprng
|
|
||||||
|
|
||||||
import com.infendro.hash.HashFunction
|
|
||||||
import com.infendro.random.prng.PRNG
|
|
||||||
|
|
||||||
abstract class CSPRNG : PRNG() {
|
|
||||||
abstract fun reseed(
|
|
||||||
entropy: UByteArray,
|
|
||||||
)
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun HMAC(
|
|
||||||
function: HashFunction,
|
|
||||||
) = com.infendro.random.csprng.HMAC(function)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.infendro.random.csprng
|
||||||
|
|
||||||
|
import com.infendro.bytes.bytearray.toInt
|
||||||
|
import com.infendro.hash.HashFunction
|
||||||
|
import com.infendro.mac.HMAC
|
||||||
|
import com.infendro.random.CSPRNG
|
||||||
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
|
class `HMAC-DRBG`(
|
||||||
|
private val function: HashFunction,
|
||||||
|
) : CSPRNG() {
|
||||||
|
private val hmac = HMAC(function)
|
||||||
|
|
||||||
|
private var k: ByteArray by notNull()
|
||||||
|
private var v: ByteArray by notNull()
|
||||||
|
|
||||||
|
private val buffer = ByteArray(function.bytes + 1)
|
||||||
|
|
||||||
|
override fun seed(seed: ByteArray) {
|
||||||
|
k = ByteArray(function.bytes) { 0x00 }
|
||||||
|
v = ByteArray(function.bytes) { 0x01 }
|
||||||
|
reseed(seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun reseed(entropy: ByteArray) {
|
||||||
|
val value = ByteArray(v.size + 1 + entropy.size)
|
||||||
|
|
||||||
|
v.copyInto(value)
|
||||||
|
value[v.size] = 0x00
|
||||||
|
entropy.copyInto(value, v.size + 1)
|
||||||
|
hmac.hashInto(k, value, k)
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
|
|
||||||
|
value[v.size] = 0x01
|
||||||
|
hmac.hashInto(k, value, k)
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generate(): Int {
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
|
val result = v.toInt()
|
||||||
|
|
||||||
|
v.copyInto(buffer)
|
||||||
|
buffer[v.size] = 0x00
|
||||||
|
hmac.hashInto(k, buffer, k)
|
||||||
|
hmac.hashInto(k, v, v)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package com.infendro.random.csprng
|
|
||||||
|
|
||||||
import com.infendro.hash.HashFunction
|
|
||||||
import kotlin.math.min
|
|
||||||
import kotlin.properties.Delegates.notNull
|
|
||||||
|
|
||||||
class HMAC internal constructor(
|
|
||||||
private val function: HashFunction,
|
|
||||||
) : CSPRNG() {
|
|
||||||
private val hmac = com.infendro.mac.HMAC(function)
|
|
||||||
|
|
||||||
private var k: UByteArray by notNull()
|
|
||||||
private var v: UByteArray by notNull()
|
|
||||||
|
|
||||||
override fun seed(
|
|
||||||
seed: UByteArray,
|
|
||||||
) {
|
|
||||||
k = UByteArray(function.bytes) { 0x00U }
|
|
||||||
v = UByteArray(function.bytes) { 0x01U }
|
|
||||||
reseed(seed)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun reseed(
|
|
||||||
entropy: UByteArray,
|
|
||||||
) {
|
|
||||||
k = hmac.hash(k, v + ubyteArrayOf(0x00U) + entropy)
|
|
||||||
v = hmac.hash(k, v)
|
|
||||||
k = hmac.hash(k, v + ubyteArrayOf(0x01U) + entropy)
|
|
||||||
v = hmac.hash(k, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun generate(
|
|
||||||
count: Int,
|
|
||||||
): UByteArray {
|
|
||||||
val bytes = buildList {
|
|
||||||
while (size < count) {
|
|
||||||
v = hmac.hash(k, v)
|
|
||||||
|
|
||||||
addAll(v.take(min(count - size, function.bytes)))
|
|
||||||
}
|
|
||||||
}.toUByteArray()
|
|
||||||
|
|
||||||
k = hmac.hash(k, v + ubyteArrayOf(0x00U))
|
|
||||||
v = hmac.hash(k, v)
|
|
||||||
|
|
||||||
return bytes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,34 +1,25 @@
|
|||||||
package com.infendro.random.prng
|
package com.infendro.random.prng
|
||||||
|
|
||||||
import com.infendro.bytearray.padStart
|
import com.infendro.bytes.bytearray.toLong
|
||||||
import com.infendro.bytearray.toUByteArray
|
import com.infendro.hash.sha2.`SHA-256`
|
||||||
import com.infendro.bytearray.toULong
|
import com.infendro.random.PRNG
|
||||||
import kotlin.math.min
|
import kotlin.properties.Delegates.notNull
|
||||||
import kotlin.properties.Delegates
|
|
||||||
|
|
||||||
class LCG internal constructor() : PRNG() {
|
class LCG : PRNG() {
|
||||||
private val a: ULong = 25214903917UL
|
private val function = `SHA-256`()
|
||||||
private val c: ULong = 11UL
|
|
||||||
private val m: ULong = 1UL shl 48
|
|
||||||
|
|
||||||
private var x: ULong by Delegates.notNull()
|
private val a: Long = 25214903917L
|
||||||
|
private val c: Long = 11L
|
||||||
|
private val mask: Long = (1L shl 48) - 1
|
||||||
|
|
||||||
override fun seed(
|
private var x: Long by notNull()
|
||||||
seed: UByteArray,
|
|
||||||
) {
|
override fun seed(seed: ByteArray) {
|
||||||
val s = seed.padStart(8, 0x00U).takeLast(8).toUByteArray()
|
x = function.hash(seed).toLong() and mask
|
||||||
x = s.toULong() % m
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generate(
|
override fun generate(): Int {
|
||||||
count: Int,
|
x = (a * x + c) and mask
|
||||||
): UByteArray {
|
return (x ushr 16).toInt()
|
||||||
return buildList {
|
|
||||||
while (size < count) {
|
|
||||||
x = (a * x + c) % m
|
|
||||||
|
|
||||||
addAll(x.toUByteArray().takeLast(min(count - size, 6)))
|
|
||||||
}
|
|
||||||
}.toUByteArray()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
package com.infendro.random.prng
|
|
||||||
|
|
||||||
import com.infendro.bytearray.toInt
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
abstract class PRNG : Random() {
|
|
||||||
abstract fun seed(
|
|
||||||
seed: UByteArray,
|
|
||||||
)
|
|
||||||
|
|
||||||
abstract fun generate(
|
|
||||||
count: Int,
|
|
||||||
): UByteArray
|
|
||||||
|
|
||||||
override fun nextBits(
|
|
||||||
bitCount: Int,
|
|
||||||
): Int {
|
|
||||||
if (bitCount !in 0..32) throw Exception()
|
|
||||||
if (bitCount == 0) return 0
|
|
||||||
|
|
||||||
return generate(4).toInt() ushr (32 - bitCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun LCG() = com.infendro.random.prng.LCG()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.infendro.random.prng
|
||||||
|
|
||||||
|
import com.infendro.bytes.bytearray.toLongArray
|
||||||
|
import com.infendro.hash.sha2.`SHA-256`
|
||||||
|
import com.infendro.random.PRNG
|
||||||
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
|
class Xoshiro256pp : PRNG() {
|
||||||
|
private val function = `SHA-256`()
|
||||||
|
|
||||||
|
private var s: LongArray by notNull()
|
||||||
|
|
||||||
|
override fun seed(seed: ByteArray) {
|
||||||
|
s = function.hash(seed).toLongArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generate(): Int {
|
||||||
|
val result = (s[0] + s[3]).rotateLeft(23) + s[0]
|
||||||
|
|
||||||
|
val t = s[1] shl 17
|
||||||
|
s[2] = s[2] xor s[0]
|
||||||
|
s[3] = s[3] xor s[1]
|
||||||
|
s[1] = s[1] xor s[2]
|
||||||
|
s[0] = s[0] xor s[3]
|
||||||
|
s[2] = s[2] xor t
|
||||||
|
s[3] = s[3].rotateLeft(45)
|
||||||
|
|
||||||
|
return (result ushr 32).toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.infendro.random.prng
|
||||||
|
|
||||||
|
import com.infendro.bytes.bytearray.toLongArray
|
||||||
|
import com.infendro.hash.sha2.`SHA-256`
|
||||||
|
import com.infendro.random.PRNG
|
||||||
|
import kotlin.properties.Delegates.notNull
|
||||||
|
|
||||||
|
class Xoshiro256ss : PRNG() {
|
||||||
|
private val function = `SHA-256`()
|
||||||
|
|
||||||
|
private var s: LongArray by notNull()
|
||||||
|
|
||||||
|
override fun seed(seed: ByteArray) {
|
||||||
|
s = function.hash(seed).toLongArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generate(): Int {
|
||||||
|
val result = (s[1] * 5).rotateLeft(7) * 9
|
||||||
|
|
||||||
|
val t = s[1] shl 17
|
||||||
|
s[2] = s[2] xor s[0]
|
||||||
|
s[3] = s[3] xor s[1]
|
||||||
|
s[1] = s[1] xor s[2]
|
||||||
|
s[0] = s[0] xor s[3]
|
||||||
|
s[2] = s[2] xor t
|
||||||
|
s[3] = s[3].rotateLeft(45)
|
||||||
|
|
||||||
|
return (result ushr 32).toInt()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user