Compare commits

...

29 Commits

Author SHA1 Message Date
b8130ef527 Merge pull request 'infendro-patch-1' (#6) from infendro-patch-1 into develop
Reviewed-on: #6
2026-04-16 16:09:30 +00:00
996751c1c6 Update README.md 2026-04-16 16:07:58 +00:00
d9454fcdb2 Merge pull request '1.3.1 Release' (#5) from develop into main
All checks were successful
/ publish (push) Successful in 3m6s
Reviewed-on: Infendro/prng-kt#5
2026-03-12 10:34:32 +00:00
06d86a6679 Bump version to 1.3.1
All checks were successful
/ test (pull_request) Successful in 25s
2026-03-12 11:33:28 +01:00
a28cb7d2f9 Upgrade dependencies 2026-03-12 11:32:31 +01:00
8fab26a476 Fix algorithm name in README 2026-02-19 12:57:36 +01:00
3465a60cb1 Merge pull request '1.3.0 release' (#4) from develop into main
All checks were successful
/ publish (push) Successful in 1m50s
Reviewed-on: Infendro/prng-kt#4
2026-02-19 11:56:22 +00:00
2aed836e67 Upgrade dependencies
All checks were successful
/ test (pull_request) Successful in 32s
2026-02-19 12:46:41 +01:00
2ae6fda70e implement xoshiro256++ and xoshiro256**
All checks were successful
/ test (pull_request) Successful in 38s
2026-01-30 15:16:48 +01:00
2c6fe0a7d9 further optimize HMAC-DRBG 2026-01-30 15:10:04 +01:00
89e7569fa7 split ci
All checks were successful
/ test (pull_request) Successful in 37s
2026-01-30 13:43:33 +01:00
0f890c923c implement optimizations 2026-01-30 13:41:43 +01:00
7144c21074 improve ci
All checks were successful
/ test (push) Successful in 3m52s
/ publish (push) Has been skipped
2026-01-13 18:07:23 +01:00
517b58a3df update README
small rewording, link wikipedia article
2025-12-14 14:41:30 +01:00
a5c54ed16c replace IllegalArgumentException with require 2025-12-14 00:17:29 +01:00
f3a2e18d2e fix README 2025-12-13 23:52:51 +01:00
20d031fba6 update README 2025-12-13 23:51:23 +01:00
d4be1689e2 optimize nextBytes() 2025-12-13 23:51:08 +01:00
3effa09602 fix workflow
All checks were successful
/ publish (push) Successful in 2m59s
2025-12-13 01:29:54 +01:00
55c628def9 use common plugin
Some checks failed
/ publish (push) Failing after 1m17s
2025-12-13 01:19:11 +01:00
a8a8ea940c bump version to 1.2.0
All checks were successful
/ publish (push) Successful in 2m35s
2025-11-10 18:52:59 +01:00
feecfa49b9 refactor, implement LCG
LCG ... Linear Congruential Generator
2025-11-10 18:52:19 +01:00
7db8972e27 bump version to 1.1.0
All checks were successful
/ publish (push) Successful in 5m28s
2025-10-04 12:41:52 +02:00
55658f2b24 add README 2025-10-04 12:40:38 +02:00
08919cc9de upgrade gradle wrapper 2025-08-16 21:27:07 +02:00
150f37649d fix parameter name 2025-08-04 13:29:10 +02:00
02eaa9ec59 implement seed 2025-08-04 13:28:08 +02:00
9480d02021 small refactor 2025-08-03 00:22:16 +02:00
f6f9625cf2 enable passing hash function to HMAC PRNG 2025-08-03 00:12:34 +02:00
18 changed files with 318 additions and 132 deletions

View File

@@ -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/

View 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 }}

View 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

49
README.md Normal file
View File

@@ -0,0 +1,49 @@
# prng.kt
`prng.kt` is a Kotlin Multiplatform library that provides common [Pseudorandom Number Generators](https://en.wikipedia.org/wiki/Pseudorandom_number_generator).
Supported [PRNG](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) algorithms:
- LCG
- xoshiro256++
- xoshiro256**
Supported [CSPRNG](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) algorithms:
- HMAC-DRBG
## Installation
Add the following to your `build.gradle.kts`.
```kotlin
repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
}
dependencies {
implementation("com.infendro:prng:1.3.1")
}
```
## Usage
```kotlin
fun main() {
// create PRNG instance
val rng = CSPRNG.HMAC(`SHA-256`())
// seed PRNG to receive deterministic numbers
rng.seed(/* seed */)
val a = rng.nextInt()
val b = rng.nextInt()
val c = rng.nextInt()
// reseed PRNG to add additional entropy
rng.reseed(/* entropy */)
val d = rng.nextInt()
val e = rng.nextInt()
val f = rng.nextInt()
// a, b, and c will be deterministic
// d, e, and f will be random (assuming entropy is random)
}
```

View File

@@ -1,57 +1,43 @@
group = "com.infendro" @file:OptIn(ExperimentalWasmDsl::class)
version = "1.0.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)
} }
} }
} }
publishing { publishing.repositories {
repositories { infendro(token)
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(
key: String,
): String? {
val property = key
val environment = key
.uppercase()
.replace(".", "__")
val prop = properties[property] as String?
val env = System.getenv(environment)
return prop ?: env
} }

View File

@@ -1,13 +1,15 @@
[versions] [versions]
kotlin = "2.1.20" infendro = "1.1.0"
hash = "1.2.2" kotlin = "2.3.0"
mac = "1.1.2" bytes = "1.4.2"
bytearray = "1.1.1" 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" }

Binary file not shown.

View File

@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
networkTimeout=10000 networkTimeout=10000
validateDistributionUrl=true validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME

4
gradlew vendored
View File

@@ -114,7 +114,7 @@ case "$( uname )" in #(
NONSTOP* ) nonstop=true ;; NONSTOP* ) nonstop=true ;;
esac esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar CLASSPATH="\\\"\\\""
# Determine the Java command to use to start the JVM. # Determine the Java command to use to start the JVM.
@@ -213,7 +213,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
set -- \ set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \ "-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \ -classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \ -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
"$@" "$@"
# Stop when "xargs" is not available. # Stop when "xargs" is not available.

4
gradlew.bat vendored
View File

@@ -70,11 +70,11 @@ goto fail
:execute :execute
@rem Setup the command line @rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar set CLASSPATH=
@rem Execute Gradle @rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end :end
@rem End local scope for the variables with windows NT shell @rem End local scope for the variables with windows NT shell

View File

@@ -1 +1,6 @@
rootProject.name = "random" rootProject.name = "prng"
pluginManagement.repositories {
maven("https://git.infendro.com/api/packages/Infendro/maven")
mavenCentral()
}

View File

@@ -0,0 +1,5 @@
package com.infendro.random
abstract class CSPRNG : PRNG() {
abstract fun reseed(entropy: ByteArray)
}

View File

@@ -1,46 +0,0 @@
package com.infendro.random
import com.infendro.bytearray.toInt
import com.infendro.hash.SHA256
import com.infendro.mac.HMAC
import kotlin.random.Random
class HMAC internal constructor() : Random() {
private val random = Default
private val hmac = HMAC(SHA256)
private var K: ByteArray
private var V: ByteArray
init {
K = ByteArray(32).also {
random.nextBytes(it)
}
V = ByteArray(32).also {
random.nextBytes(it)
}
}
fun reseed(
entropy: ByteArray,
) {
K = hmac.hash(K, V + byteArrayOf(0x00) + entropy)
V = hmac.hash(K, V)
K = hmac.hash(K, V + byteArrayOf(0x01) + entropy)
V = hmac.hash(K, V)
}
override fun nextBits(
count: Int,
): Int {
if (count !in 0..32) throw Exception()
if (count == 0) return 0
val bytes = hmac.hash(K, V)
val bits = bytes.take(4).toInt() ushr (32 - count)
K = hmac.hash(K, V + byteArrayOf(0x00))
V = hmac.hash(K, V)
return bits
}
}

View File

@@ -1,5 +1,16 @@
package com.infendro.random package com.infendro.random
object PRNG { import kotlin.random.Random
val HMAC = HMAC()
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)
}
} }

View File

@@ -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
}
}

View File

@@ -0,0 +1,25 @@
package com.infendro.random.prng
import com.infendro.bytes.bytearray.toLong
import com.infendro.hash.sha2.`SHA-256`
import com.infendro.random.PRNG
import kotlin.properties.Delegates.notNull
class LCG : PRNG() {
private val function = `SHA-256`()
private val a: Long = 25214903917L
private val c: Long = 11L
private val mask: Long = (1L shl 48) - 1
private var x: Long by notNull()
override fun seed(seed: ByteArray) {
x = function.hash(seed).toLong() and mask
}
override fun generate(): Int {
x = (a * x + c) and mask
return (x ushr 16).toInt()
}
}

View File

@@ -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()
}
}

View File

@@ -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()
}
}