implement optimizations
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.toULongArray
|
||||
import com.infendro.bytes.bytearray.copyInto
|
||||
import com.infendro.bytes.longarray.copyInto
|
||||
import com.infendro.bytes.ulong.copyInto
|
||||
import com.infendro.hash.HashFunction
|
||||
import com.infendro.hash.util.ceilPow2
|
||||
|
||||
object `Blake-512` : HashFunction {
|
||||
override val bytes: Int
|
||||
@@ -16,7 +18,7 @@ object `Blake-512` : HashFunction {
|
||||
0x3c6ef372fe94f82bUL, 0xa54ff53a5f1d36f1UL,
|
||||
0x510e527fade682d1UL, 0x9b05688c2b3e6c1fUL,
|
||||
0x1f83d9abfb41bd6bUL, 0x5be0cd19137e2179UL,
|
||||
)
|
||||
).asLongArray()
|
||||
|
||||
private val c = ulongArrayOf(
|
||||
0x243f6a8885a308d3UL, 0x13198a2e03707344UL,
|
||||
@@ -27,7 +29,7 @@ object `Blake-512` : HashFunction {
|
||||
0x2ffd72dbd01adfb7UL, 0xb8e1afed6a267e96UL,
|
||||
0xba7c9045f12c7f99UL, 0x24a19947b3916cf7UL,
|
||||
0x0801f2e2858efc16UL, 0x636920d871574e69UL,
|
||||
)
|
||||
).asLongArray()
|
||||
|
||||
private val sigma = arrayOf(
|
||||
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
|
||||
@@ -42,47 +44,38 @@ object `Blake-512` : HashFunction {
|
||||
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
|
||||
)
|
||||
|
||||
override fun hash(value: UByteArray): UByteArray {
|
||||
val l = value.size.toULong() * 8UL
|
||||
override fun hashInto(value: ByteArray, destination: ByteArray) {
|
||||
val length = value.size.toULong() * 8UL
|
||||
val input = pad(value)
|
||||
val h = initial.copyOf()
|
||||
val m = LongArray(16)
|
||||
val v = LongArray(16)
|
||||
var t = 0UL
|
||||
|
||||
val blocks = pad(value)
|
||||
.chunked(128) { it.toUByteArray() }
|
||||
for (block in blocks) {
|
||||
t += minOf(l - t, 1024UL)
|
||||
compress(h, block, t)
|
||||
for (i in input.indices step block) {
|
||||
t += minOf(length - t, 1024UL)
|
||||
compress(h, input, i, m, v, t)
|
||||
}
|
||||
|
||||
return h.toUByteArray()
|
||||
h.copyInto(destination)
|
||||
}
|
||||
|
||||
private fun pad(
|
||||
value: UByteArray,
|
||||
): UByteArray {
|
||||
val l = value.size.toULong() * 8UL
|
||||
return buildList {
|
||||
addAll(value)
|
||||
add(0x80U)
|
||||
while (size % 128 != 111) {
|
||||
add(0x00U)
|
||||
}
|
||||
add(0x01U)
|
||||
repeat(8) { add(0x00U) }
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
private fun pad(value: ByteArray): ByteArray {
|
||||
val length = value.size.toULong() * 8UL
|
||||
|
||||
val size = ceilPow2(value.size + 18, block)
|
||||
val result = ByteArray(size)
|
||||
value.copyInto(result)
|
||||
result[value.size] = 0x80U.toByte()
|
||||
result[result.size - 17] = 0x01
|
||||
length.copyInto(result, result.size - 8)
|
||||
return result
|
||||
}
|
||||
|
||||
private fun compress(
|
||||
h: ULongArray,
|
||||
block: UByteArray,
|
||||
t: ULong,
|
||||
) {
|
||||
val m = block.toULongArray()
|
||||
val v = ULongArray(16)
|
||||
internal fun compress(h: LongArray, value: ByteArray, offset: Int, m: LongArray, v: LongArray, t: ULong) {
|
||||
value.copyInto(m, startIndex = offset, endIndex = offset + block)
|
||||
for (i in 0..<8) v[i] = h[i]
|
||||
for (i in 0..<4) v[i + 8] = c[i]
|
||||
for (i in 0..<2) v[i + 12] = c[i + 4] xor t
|
||||
for (i in 0..<2) v[i + 12] = c[i + 4] xor t.toLong()
|
||||
for (i in 0..<2) v[i + 14] = c[i + 6]
|
||||
|
||||
repeat(16) { r ->
|
||||
@@ -99,20 +92,12 @@ object `Blake-512` : HashFunction {
|
||||
g(v, 3, 4, 9, 14, m[s[14]] xor c[s[15]], m[s[15]] xor c[s[14]])
|
||||
}
|
||||
|
||||
for (i in 0..7) {
|
||||
for (i in 0..<8) {
|
||||
h[i] = h[i] xor v[i] xor v[i + 8]
|
||||
}
|
||||
}
|
||||
|
||||
private fun g(
|
||||
v: ULongArray,
|
||||
a: Int,
|
||||
b: Int,
|
||||
c: Int,
|
||||
d: Int,
|
||||
x: ULong,
|
||||
y: ULong,
|
||||
) {
|
||||
private fun g(v: LongArray, a: Int, b: Int, c: Int, d: Int, x: Long, y: Long) {
|
||||
v[a] += v[b] + x
|
||||
v[d] = (v[d] xor v[a]).rotateRight(32)
|
||||
v[c] += v[d]
|
||||
|
||||
Reference in New Issue
Block a user