implement optimizations

This commit is contained in:
2026-01-15 22:06:28 +01:00
parent ab036f4f9f
commit d40df04e2e
26 changed files with 463 additions and 855 deletions

View File

@@ -1,8 +1,9 @@
package com.infendro.hash.blake
import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray
import com.infendro.bytes.intarray.copyInto
import com.infendro.bytes.ulong.copyInto
import com.infendro.hash.HashFunction
import com.infendro.hash.util.ceilPow2
object `Blake-224` : HashFunction {
override val bytes: Int
@@ -16,108 +17,31 @@ object `Blake-224` : HashFunction {
0x3070dd17U, 0xf70e5939U,
0xffc00b31U, 0x68581511U,
0x64f98fa7U, 0xbefa4fa4U,
)
).asIntArray()
private val c = uintArrayOf(
0x243f6a88U, 0x85a308d3U,
0x13198a2eU, 0x03707344U,
0xa4093822U, 0x299f31d0U,
0x082efa98U, 0xec4e6c89U,
0x452821e6U, 0x38d01377U,
0xbe5466cfU, 0x34e90c6cU,
0xc0ac29b7U, 0xc97c50ddU,
0x3f84d5b5U, 0xb5470917U,
)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
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 = IntArray(16)
val v = IntArray(16)
var t = 0U
val blocks = pad(value)
.chunked(64) { it.toUByteArray() }
for (block in blocks) {
t += minOf(l - t, 512UL).toUInt()
compress(h, block, t)
for (i in input.indices step block) {
t += minOf(length - t, 512UL).toUInt()
`Blake-256`.compress(h, input, i, m, v, t)
}
return h.sliceArray(0..6).toUByteArray()
h.copyInto(destination, endIndex = 7)
}
private fun pad(
value: UByteArray,
): UByteArray {
val l = value.size.toULong() * 8UL
return buildList {
addAll(value)
add(0x80U)
while (size % 64 != 56) {
add(0x00U)
}
addAll(l.toUByteArray())
}.toUByteArray()
}
private fun pad(value: ByteArray): ByteArray {
val length = value.size.toULong() * 8UL
private fun compress(
h: UIntArray,
block: UByteArray,
t: UInt,
) {
val m = block.toUIntArray()
val v = UIntArray(16)
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 + 14] = c[i + 6]
repeat(14) { r ->
val s = sigma[r % 10]
g(v, 0, 4, 8, 12, m[s[0]] xor c[s[1]], m[s[1]] xor c[s[0]])
g(v, 1, 5, 9, 13, m[s[2]] xor c[s[3]], m[s[3]] xor c[s[2]])
g(v, 2, 6, 10, 14, m[s[4]] xor c[s[5]], m[s[5]] xor c[s[4]])
g(v, 3, 7, 11, 15, m[s[6]] xor c[s[7]], m[s[7]] xor c[s[6]])
g(v, 0, 5, 10, 15, m[s[8]] xor c[s[9]], m[s[9]] xor c[s[8]])
g(v, 1, 6, 11, 12, m[s[10]] xor c[s[11]], m[s[11]] xor c[s[10]])
g(v, 2, 7, 8, 13, m[s[12]] xor c[s[13]], m[s[13]] xor c[s[12]])
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) {
h[i] = h[i] xor v[i] xor v[i + 8]
}
}
private fun g(
v: UIntArray,
a: Int,
b: Int,
c: Int,
d: Int,
x: UInt,
y: UInt,
) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(12)
v[a] += v[b] + y
v[d] = (v[d] xor v[a]).rotateRight(8)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(7)
val size = ceilPow2(value.size + 9, block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
}
}

View File

@@ -1,8 +1,10 @@
package com.infendro.hash.blake
import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray
import com.infendro.bytes.bytearray.copyInto
import com.infendro.bytes.intarray.copyInto
import com.infendro.bytes.ulong.copyInto
import com.infendro.hash.HashFunction
import com.infendro.hash.util.ceilPow2
object `Blake-256` : HashFunction {
override val bytes: Int
@@ -16,7 +18,7 @@ object `Blake-256` : HashFunction {
0x3c6ef372U, 0xa54ff53aU,
0x510e527fU, 0x9b05688cU,
0x1f83d9abU, 0x5be0cd19U,
)
).asIntArray()
private val c = uintArrayOf(
0x243f6a88U, 0x85a308d3U,
@@ -27,7 +29,7 @@ object `Blake-256` : HashFunction {
0xbe5466cfU, 0x34e90c6cU,
0xc0ac29b7U, 0xc97c50ddU,
0x3f84d5b5U, 0xb5470917U,
)
).asIntArray()
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
@@ -42,46 +44,38 @@ object `Blake-256` : 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 = IntArray(16)
val v = IntArray(16)
var t = 0U
val blocks = pad(value)
.chunked(64) { it.toUByteArray() }
for (block in blocks) {
t += minOf(l - t, 512UL).toUInt()
compress(h, block, t)
for (i in input.indices step block) {
t += minOf(length - t, 512UL).toUInt()
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 % 64 != 55) {
add(0x00U)
}
add(0x01U)
addAll(l.toUByteArray())
}.toUByteArray()
private fun pad(value: ByteArray): ByteArray {
val length = value.size.toULong() * 8UL
val size = ceilPow2(value.size + 10, block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
result[result.size - 9] = 0x01
length.copyInto(result, result.size - 8)
return result
}
private fun compress(
h: UIntArray,
block: UByteArray,
t: UInt,
) {
val m = block.toUIntArray()
val v = UIntArray(16)
internal fun compress(h: IntArray, value: ByteArray, offset: Int, m: IntArray, v: IntArray, t: UInt) {
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.toInt()
for (i in 0..<2) v[i + 14] = c[i + 6]
repeat(14) { r ->
@@ -98,20 +92,12 @@ object `Blake-256` : 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: UIntArray,
a: Int,
b: Int,
c: Int,
d: Int,
x: UInt,
y: UInt,
) {
private fun g(v: IntArray, a: Int, b: Int, c: Int, d: Int, x: Int, y: Int) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]

View File

@@ -1,8 +1,9 @@
package com.infendro.hash.blake
import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toULongArray
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-384` : HashFunction {
override val bytes: Int
@@ -16,108 +17,31 @@ object `Blake-384` : HashFunction {
0x9159015a3070dd17U, 0x152fecd8f70e5939U,
0x67332667ffc00b31U, 0x8eb44a8768581511U,
0xdb0c2e0d64f98fa7U, 0x47b5481dbefa4fa4U,
)
).asLongArray()
private val c = ulongArrayOf(
0x243f6a8885a308d3UL, 0x13198a2e03707344UL,
0xa4093822299f31d0UL, 0x082efa98ec4e6c89UL,
0x452821e638d01377UL, 0xbe5466cf34e90c6cUL,
0xc0ac29b7c97c50ddUL, 0x3f84d5b5b5470917UL,
0x9216d5d98979fb1bUL, 0xd1310ba698dfb5acUL,
0x2ffd72dbd01adfb7UL, 0xb8e1afed6a267e96UL,
0xba7c9045f12c7f99UL, 0x24a19947b3916cf7UL,
0x0801f2e2858efc16UL, 0x636920d871574e69UL,
)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
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)
`Blake-512`.compress(h, input, i, m, v, t)
}
return h.sliceArray(0..5).toUByteArray()
h.copyInto(destination, endIndex = 6)
}
private fun pad(
value: UByteArray,
): UByteArray {
val l = value.size.toULong() * 8UL
return buildList {
addAll(value)
add(0x80U)
while (size % 128 != 120) {
add(0x00U)
}
addAll(l.toUByteArray())
}.toUByteArray()
}
private fun pad(value: ByteArray): ByteArray {
val length = value.size.toULong() * 8UL
private fun compress(
h: ULongArray,
block: UByteArray,
t: ULong,
) {
val m = block.toULongArray()
val v = ULongArray(16)
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 + 14] = c[i + 6]
repeat(16) { r ->
val s = sigma[r % 10]
g(v, 0, 4, 8, 12, m[s[0]] xor c[s[1]], m[s[1]] xor c[s[0]])
g(v, 1, 5, 9, 13, m[s[2]] xor c[s[3]], m[s[3]] xor c[s[2]])
g(v, 2, 6, 10, 14, m[s[4]] xor c[s[5]], m[s[5]] xor c[s[4]])
g(v, 3, 7, 11, 15, m[s[6]] xor c[s[7]], m[s[7]] xor c[s[6]])
g(v, 0, 5, 10, 15, m[s[8]] xor c[s[9]], m[s[9]] xor c[s[8]])
g(v, 1, 6, 11, 12, m[s[10]] xor c[s[11]], m[s[11]] xor c[s[10]])
g(v, 2, 7, 8, 13, m[s[12]] xor c[s[13]], m[s[13]] xor c[s[12]])
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) {
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,
) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(32)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(25)
v[a] += v[b] + y
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(11)
val size = ceilPow2(value.size + 17, block)
val result = ByteArray(size)
value.copyInto(result)
result[value.size] = 0x80U.toByte()
length.copyInto(result, result.size - 8)
return result
}
}

View File

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