Compare commits

..

2 Commits

Author SHA1 Message Date
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
13 changed files with 174 additions and 260 deletions

View File

@@ -1,92 +0,0 @@
on:
push:
branches: [ develop, main ]
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: 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: Test
run: ./gradlew allTests
publish:
if: ${{ github.event_name == 'push' && github.ref_name == 'main' }}
needs: 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: 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,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

View File

@@ -6,16 +6,7 @@ Supported [PRNG](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) al
- LCG - LCG
Supported [CSPRNG](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) algorithms: Supported [CSPRNG](https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) algorithms:
- HMAC DRBG - HMAC-DRBG
## Targets
| Target | Status |
|------------------|---------------|
| JVM | Supported |
| JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
## Installation ## Installation
@@ -27,34 +18,30 @@ repositories {
} }
dependencies { dependencies {
implementation("com.infendro:prng:1.2.0") implementation("com.infendro:prng:1.3.0")
} }
``` ```
## Usage ## Usage
```kotlin ```kotlin
import com.infendro.hash.sha2.`SHA-256`
import com.infendro.random.csprng.CSPRNG
fun main() { fun main() {
// create a PRNG instance // create PRNG instance
val rng = CSPRNG.HMAC(`SHA-256`) 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)
} }
``` ```

View File

@@ -1,8 +1,11 @@
@file:OptIn(ExperimentalWasmDsl::class)
import com.infendro.plugin.infendro import com.infendro.plugin.infendro
import com.infendro.plugin.token import com.infendro.plugin.token
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
group = "com.infendro" group = "com.infendro"
version = "1.2.0" version = "1.3.0"
plugins { plugins {
alias(libs.plugins.infendro) alias(libs.plugins.infendro)
@@ -13,9 +16,13 @@ plugins {
kotlin { kotlin {
jvm() jvm()
linuxX64() linuxX64()
js { linuxArm64()
nodejs() mingwX64()
} macosX64()
macosArm64()
js { nodejs() }
wasmJs { nodejs() }
wasmWasi { nodejs() }
repositories { repositories {
infendro() infendro()
@@ -26,13 +33,9 @@ kotlin {
commonMain.dependencies { commonMain.dependencies {
implementation(libs.hash) implementation(libs.hash)
implementation(libs.mac) implementation(libs.mac)
implementation(libs.bytearray) implementation(libs.bytes)
} }
} }
compilerOptions {
freeCompilerArgs.add("-opt-in=kotlin.ExperimentalUnsignedTypes")
}
} }
publishing.repositories { publishing.repositories {

View File

@@ -1,15 +1,15 @@
[versions] [versions]
infendro = "1.0.0" infendro = "1.1.0"
kotlin = "2.2.21" kotlin = "2.3.0"
hash = "1.3.2" mac = "1.3.0"
mac = "1.2.0" hash = "1.5.1"
bytearray = "1.2.4" bytes = "1.3.3"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" } 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]
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" } hash = { module = "com.infendro:hash", version.ref = "hash" }
bytes = { module = "com.infendro:bytes", version.ref = "bytes" }

View File

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

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

View File

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

View File

@@ -0,0 +1,37 @@
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` internal constructor(
private val function: HashFunction,
) : CSPRNG() {
private val hmac = HMAC(function)
private var k: ByteArray by notNull()
private var v: ByteArray by notNull()
override fun seed(seed: ByteArray) {
k = ByteArray(function.bytes) { 0x00 }
v = ByteArray(function.bytes) { 0x01 }
reseed(seed)
}
override 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 generate(): Int {
v = hmac.hash(k, v)
val result = v.toInt()
k = hmac.hash(k, v + byteArrayOf(0x00))
v = hmac.hash(k, v)
return result
}
}

View File

@@ -1,47 +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
}
}

View File

@@ -1,33 +1,23 @@
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 internal constructor() : PRNG() {
private val a: ULong = 25214903917UL private val a: Long = 25214903917L
private val c: ULong = 11UL private val c: Long = 11L
private val m: ULong = 1UL shl 48 private val mask: Long = (1L shl 48) - 1
private var x: ULong by Delegates.notNull() private var x: Long by notNull()
override fun seed( override fun seed(seed: ByteArray) {
seed: UByteArray, x = `SHA-256`.hash(seed).toLong() and mask
) {
val s = seed.padStart(8, 0x00U).takeLast(8).toUByteArray()
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()
} }
} }

View File

@@ -1,44 +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 {
require(bitCount in 0..32)
if (bitCount == 0) return 0
return generate(4).toInt() ushr (32 - bitCount)
}
override fun nextBytes(
array: ByteArray,
fromIndex: Int,
toIndex: Int,
): ByteArray {
require(fromIndex in 0..array.size && toIndex in 0..array.size)
require(fromIndex <= toIndex)
val size = toIndex - fromIndex
val bytes = generate(size).asByteArray()
for (i in 0..<size) {
array[i + fromIndex] = bytes[i]
}
return array
}
companion object {
fun LCG() = com.infendro.random.prng.LCG()
}
}