This commit is contained in:
@@ -19,14 +19,14 @@ open class Keccak(
|
||||
override val block: Int
|
||||
get() = rate
|
||||
|
||||
private val rc = ulongArrayOf(
|
||||
0x0000000000000001UL, 0x0000000000008082UL, 0x800000000000808aUL, 0x8000000080008000UL,
|
||||
0x000000000000808bUL, 0x0000000080000001UL, 0x8000000080008081UL, 0x8000000000008009UL,
|
||||
0x000000000000008aUL, 0x0000000000000088UL, 0x0000000080008009UL, 0x000000008000000aUL,
|
||||
0x000000008000808bUL, 0x800000000000008bUL, 0x8000000000008089UL, 0x8000000000008003UL,
|
||||
0x8000000000008002UL, 0x8000000000000080UL, 0x000000000000800aUL, 0x800000008000000aUL,
|
||||
0x8000000080008081UL, 0x8000000000008080UL, 0x0000000080000001UL, 0x8000000080008008UL,
|
||||
).asLongArray()
|
||||
private val rc = longArrayOf(
|
||||
+0x0000000000000001L, +0x0000000000008082L, -0x7fffffffffff7f76L, -0x7fffffff7fff8000L,
|
||||
+0x000000000000808bL, +0x0000000080000001L, -0x7fffffff7fff7f7fL, -0x7fffffffffff7ff7L,
|
||||
+0x000000000000008aL, +0x0000000000000088L, +0x0000000080008009L, +0x000000008000000aL,
|
||||
+0x000000008000808bL, -0x7fffffffffffff75L, -0x7fffffffffff7f77L, -0x7fffffffffff7ffdL,
|
||||
-0x7fffffffffff7ffeL, -0x7fffffffffffff80L, +0x000000000000800aL, -0x7fffffff7ffffff6L,
|
||||
-0x7fffffff7fff7f7fL, -0x7fffffffffff7f80L, +0x0000000080000001L, -0x7fffffff7fff7ff8L,
|
||||
)
|
||||
|
||||
private val rotation = arrayOf(
|
||||
intArrayOf(0, 36, 3, 41, 18),
|
||||
@@ -39,16 +39,13 @@ open class Keccak(
|
||||
override fun hashInto(value: ByteArray, destination: ByteArray, destinationOffset: Int) {
|
||||
val input = pad(value)
|
||||
val state = LongArray(25)
|
||||
val w = LongArray(block / 8)
|
||||
val b = LongArray(25)
|
||||
val c = LongArray(5)
|
||||
val d = LongArray(5)
|
||||
|
||||
for (i in input.indices step block) {
|
||||
absorb(state, input, i, w)
|
||||
permute(state, b, c, d)
|
||||
val block = input.sliceArray(i..<(i + block))
|
||||
absorb(state, block)
|
||||
permute(state)
|
||||
}
|
||||
squeeze(state, b, c, d, destination, destinationOffset)
|
||||
squeeze(state, destination, destinationOffset)
|
||||
}
|
||||
|
||||
private fun pad(value: ByteArray): ByteArray {
|
||||
@@ -60,21 +57,15 @@ open class Keccak(
|
||||
}
|
||||
}
|
||||
|
||||
private fun absorb(state: LongArray, value: ByteArray, offset: Int, w: LongArray) {
|
||||
value.copyInto(w, startIndex = offset, endIndex = offset + block, order = LITTLE_ENDIAN)
|
||||
private fun absorb(state: LongArray, block: ByteArray) {
|
||||
val w = LongArray(this.block / 8)
|
||||
block.copyInto(w, order = LITTLE_ENDIAN)
|
||||
for (i in w.indices) {
|
||||
state[i] = state[i] xor w[i]
|
||||
}
|
||||
}
|
||||
|
||||
private fun squeeze(
|
||||
state: LongArray,
|
||||
b: LongArray,
|
||||
c: LongArray,
|
||||
d: LongArray,
|
||||
destination: ByteArray,
|
||||
destinationOffset: Int,
|
||||
) {
|
||||
private fun squeeze(state: LongArray, destination: ByteArray, destinationOffset: Int) {
|
||||
val buffer = ByteArray(8)
|
||||
val threshold = rate / 8
|
||||
|
||||
@@ -82,7 +73,7 @@ open class Keccak(
|
||||
var i = 0
|
||||
while (consumed < bytes) {
|
||||
if (i == threshold) {
|
||||
permute(state, b, c, d)
|
||||
permute(state)
|
||||
i = 0
|
||||
}
|
||||
|
||||
@@ -95,7 +86,11 @@ open class Keccak(
|
||||
}
|
||||
}
|
||||
|
||||
private fun permute(a: LongArray, b: LongArray, c: LongArray, d: LongArray) = repeat(24) { round ->
|
||||
private fun permute(a: LongArray) = repeat(24) { round ->
|
||||
val b = LongArray(25)
|
||||
val c = LongArray(5)
|
||||
val d = LongArray(5)
|
||||
|
||||
// θ
|
||||
for (x in 0..4) {
|
||||
c[x] = a[x] xor a[x + 5] xor a[x + 10] xor a[x + 15] xor a[x + 20]
|
||||
|
||||
@@ -2,6 +2,4 @@ package com.infendro.hash.keccak.shake
|
||||
|
||||
import com.infendro.hash.keccak.Keccak
|
||||
|
||||
class SHAKE128(
|
||||
bytes: Int,
|
||||
) : Keccak(bytes, 168, 0x1F)
|
||||
class SHAKE128(bytes: Int) : Keccak(bytes, 168, 0x1F)
|
||||
|
||||
@@ -2,6 +2,4 @@ package com.infendro.hash.keccak.shake
|
||||
|
||||
import com.infendro.hash.keccak.Keccak
|
||||
|
||||
class SHAKE256(
|
||||
bytes: Int,
|
||||
) : Keccak(bytes, 136, 0x1F)
|
||||
class SHAKE256(bytes: Int) : Keccak(bytes, 136, 0x1F)
|
||||
|
||||
Reference in New Issue
Block a user