refactor hash functions

This commit is contained in:
2025-12-01 19:13:58 +01:00
parent 10ba03dedc
commit 8a2848e6e3
7 changed files with 341 additions and 392 deletions

View File

@@ -19,7 +19,7 @@ abstract class Keccak internal constructor(
0x000000000000008aUL, 0x0000000000000088UL, 0x0000000080008009UL, 0x000000008000000aUL,
0x000000008000808bUL, 0x800000000000008bUL, 0x8000000000008089UL, 0x8000000000008003UL,
0x8000000000008002UL, 0x8000000000000080UL, 0x000000000000800aUL, 0x800000008000000aUL,
0x8000000080008081UL, 0x8000000000008080UL, 0x0000000080000001UL, 0x8000000080008008UL
0x8000000080008081UL, 0x8000000000008080UL, 0x0000000080000001UL, 0x8000000080008008UL,
)
private val rotation = arrayOf(
@@ -33,6 +33,9 @@ abstract class Keccak internal constructor(
override fun hash(
value: UByteArray,
): UByteArray {
val state = ULongArray(25)
// padding
val paddedValue = buildList {
addAll(value)
add(domain)
@@ -42,13 +45,11 @@ abstract class Keccak internal constructor(
add(0x80U)
}
val state = ULongArray(25)
// absorb
val chunks = paddedValue
val blocks = paddedValue
.chunked(rate) { it.toUByteArray() }
for (chunk in chunks) {
val numbers = chunk.toULongArray(LITTLE_ENDIAN)
for (block in blocks) {
val numbers = block.toULongArray(LITTLE_ENDIAN)
for ((i, number) in numbers.withIndex()) {
state[i] = state[i] xor number
}