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.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 `SHA-512` : HashFunction {
|
||||
override val bytes: Int
|
||||
@@ -16,7 +18,7 @@ object `SHA-512` : HashFunction {
|
||||
0x3c6ef372fe94f82bUL, 0xa54ff53a5f1d36f1UL,
|
||||
0x510e527fade682d1UL, 0x9b05688c2b3e6c1fUL,
|
||||
0x1f83d9abfb41bd6bUL, 0x5be0cd19137e2179UL,
|
||||
)
|
||||
).asLongArray()
|
||||
|
||||
private val c = ulongArrayOf(
|
||||
0x428a2f98d728ae22UL, 0x7137449123ef65cdUL, 0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL,
|
||||
@@ -39,55 +41,45 @@ object `SHA-512` : HashFunction {
|
||||
0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL, 0x113f9804bef90daeUL, 0x1b710b35131c471bUL,
|
||||
0x28db77f523047d84UL, 0x32caab7b40c72493UL, 0x3c9ebe0a15c9bebcUL, 0x431d67c49c100d4cUL,
|
||||
0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL, 0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL,
|
||||
)
|
||||
).asLongArray()
|
||||
|
||||
override fun hash(
|
||||
value: UByteArray,
|
||||
): UByteArray {
|
||||
override fun hashInto(value: ByteArray, destination: ByteArray) {
|
||||
val input = pad(value)
|
||||
val h = initial.copyOf()
|
||||
val w = LongArray(80)
|
||||
val v = LongArray(8)
|
||||
|
||||
val blocks = pad(value)
|
||||
.chunked(128) { 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 % 128 != 120) {
|
||||
add(0x00U)
|
||||
}
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
internal fun pad(value: ByteArray): ByteArray {
|
||||
val length = value.size.toULong() * 8UL
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
private fun process(
|
||||
h: ULongArray,
|
||||
block: UByteArray,
|
||||
) {
|
||||
val w = buildList {
|
||||
addAll(block.toULongArray())
|
||||
for (i in 16..79) {
|
||||
val s0 = get(i - 15).rotateRight(1) xor get(i - 15).rotateRight(8) xor (get(i - 15) shr 7)
|
||||
val s1 = get(i - 2).rotateRight(19) xor get(i - 2).rotateRight(61) xor (get(i - 2) shr 6)
|
||||
add(get(i - 16) + s0 + get(i - 7) + s1)
|
||||
}
|
||||
}.toULongArray()
|
||||
val v = h.copyOf()
|
||||
internal fun process(h: LongArray, value: ByteArray, offset: Int, w: LongArray, v: LongArray) {
|
||||
value.copyInto(w, startIndex = offset, endIndex = offset + block)
|
||||
for (i in 16..<80) {
|
||||
val s0 = w[i - 15].rotateRight(1) xor w[i - 15].rotateRight(8) xor (w[i - 15] ushr 7)
|
||||
val s1 = w[i - 2].rotateRight(19) xor w[i - 2].rotateRight(61) xor (w[i - 2] ushr 6)
|
||||
w[i] = (w[i - 16] + s0 + w[i - 7] + s1)
|
||||
}
|
||||
h.copyInto(v)
|
||||
|
||||
repeat(80) { i ->
|
||||
val s0 = v[0].rotateRight(28) xor v[0].rotateRight(34) xor v[0].rotateRight(39)
|
||||
val s1 = v[4].rotateRight(14) xor v[4].rotateRight(18) xor v[4].rotateRight(41)
|
||||
val ch = (v[4] and v[5]) xor (v[4].inv() and v[6])
|
||||
val temp1 = v[7] + s1 + ch + this.c[i] + w[i]
|
||||
val temp1 = v[7] + s1 + ch + c[i] + w[i]
|
||||
val maj = (v[0] and v[1]) xor (v[0] and v[2]) xor (v[1] and v[2])
|
||||
val temp2 = s0 + maj
|
||||
|
||||
@@ -101,7 +93,7 @@ object `SHA-512` : 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