implement optimizations
This commit is contained in:
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,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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,23 @@
|
||||
package com.infendro.random.prng
|
||||
|
||||
import com.infendro.bytearray.padStart
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.toULong
|
||||
import kotlin.math.min
|
||||
import kotlin.properties.Delegates
|
||||
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 internal constructor() : PRNG() {
|
||||
private val a: ULong = 25214903917UL
|
||||
private val c: ULong = 11UL
|
||||
private val m: ULong = 1UL shl 48
|
||||
private val a: Long = 25214903917L
|
||||
private val c: Long = 11L
|
||||
private val mask: Long = (1L shl 48) - 1
|
||||
|
||||
private var x: ULong by Delegates.notNull()
|
||||
private var x: Long by notNull()
|
||||
|
||||
override fun seed(
|
||||
seed: UByteArray,
|
||||
) {
|
||||
val s = seed.padStart(8, 0x00U).takeLast(8).toUByteArray()
|
||||
x = s.toULong() % m
|
||||
override fun seed(seed: ByteArray) {
|
||||
x = `SHA-256`.hash(seed).toLong() and mask
|
||||
}
|
||||
|
||||
override fun generate(
|
||||
count: Int,
|
||||
): UByteArray {
|
||||
return buildList {
|
||||
while (size < count) {
|
||||
x = (a * x + c) % m
|
||||
addAll(x.toUByteArray().takeLast(min(count - size, 6)))
|
||||
}
|
||||
}.toUByteArray()
|
||||
override fun generate(): Int {
|
||||
x = (a * x + c) and mask
|
||||
return (x ushr 16).toInt()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user