implement optimizations
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.infendro.hash.sha2
|
||||
|
||||
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 `SHA-256` : HashFunction {
|
||||
override val bytes: Int
|
||||
@@ -16,7 +18,7 @@ object `SHA-256` : HashFunction {
|
||||
0x3c6ef372U, 0xa54ff53aU,
|
||||
0x510e527fU, 0x9b05688cU,
|
||||
0x1f83d9abU, 0x5be0cd19U,
|
||||
)
|
||||
).asIntArray()
|
||||
|
||||
private val c = uintArrayOf(
|
||||
0x428a2f98U, 0x71374491U, 0xb5c0fbcfU, 0xe9b5dba5U, 0x3956c25bU, 0x59f111f1U, 0x923f82a4U, 0xab1c5ed5U,
|
||||
@@ -27,49 +29,39 @@ object `SHA-256` : HashFunction {
|
||||
0xa2bfe8a1U, 0xa81a664bU, 0xc24b8b70U, 0xc76c51a3U, 0xd192e819U, 0xd6990624U, 0xf40e3585U, 0x106aa070U,
|
||||
0x19a4c116U, 0x1e376c08U, 0x2748774cU, 0x34b0bcb5U, 0x391c0cb3U, 0x4ed8aa4aU, 0x5b9cca4fU, 0x682e6ff3U,
|
||||
0x748f82eeU, 0x78a5636fU, 0x84c87814U, 0x8cc70208U, 0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U,
|
||||
)
|
||||
).asIntArray()
|
||||
|
||||
override fun hash(
|
||||
value: UByteArray,
|
||||
): UByteArray {
|
||||
override fun hashInto(value: ByteArray, destination: ByteArray) {
|
||||
val input = pad(value)
|
||||
val h = initial.copyOf()
|
||||
val w = IntArray(64)
|
||||
val v = IntArray(8)
|
||||
|
||||
val blocks = pad(value)
|
||||
.chunked(64) { it.toUByteArray() }
|
||||
for (block in blocks) {
|
||||
process(h, block)
|
||||
for (i in input.indices step block) {
|
||||
process(h, input, i, w, v)
|
||||
}
|
||||
|
||||
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 != 56) {
|
||||
add(0x00U)
|
||||
}
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
internal fun pad(value: ByteArray): ByteArray {
|
||||
val length = value.size.toULong() * 8UL
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
private fun process(
|
||||
h: UIntArray,
|
||||
block: UByteArray,
|
||||
) {
|
||||
val w = buildList {
|
||||
addAll(block.toUIntArray())
|
||||
for (i in 16..63) {
|
||||
val s0 = get(i - 15).rotateRight(7) xor get(i - 15).rotateRight(18) xor (get(i - 15) shr 3)
|
||||
val s1 = get(i - 2).rotateRight(17) xor get(i - 2).rotateRight(19) xor (get(i - 2) shr 10)
|
||||
add(get(i - 16) + s0 + get(i - 7) + s1)
|
||||
}
|
||||
}.toUIntArray()
|
||||
val v = h.copyOf()
|
||||
internal fun process(h: IntArray, value: ByteArray, offset: Int, w: IntArray, v: IntArray) {
|
||||
value.copyInto(w, startIndex = offset, endIndex = offset + block)
|
||||
for (i in 16..<64) {
|
||||
val s0 = w[i - 15].rotateRight(7) xor w[i - 15].rotateRight(18) xor (w[i - 15] ushr 3)
|
||||
val s1 = w[i - 2].rotateRight(17) xor w[i - 2].rotateRight(19) xor (w[i - 2] ushr 10)
|
||||
w[i] = (w[i - 16] + s0 + w[i - 7] + s1)
|
||||
}
|
||||
h.copyInto(v)
|
||||
|
||||
repeat(64) { i ->
|
||||
val s0 = v[0].rotateRight(2) xor v[0].rotateRight(13) xor v[0].rotateRight(22)
|
||||
@@ -89,7 +81,7 @@ object `SHA-256` : HashFunction {
|
||||
v[0] = temp1 + temp2
|
||||
}
|
||||
|
||||
for (i in 0..7) {
|
||||
for (i in 0..<8) {
|
||||
h[i] += v[i]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user