implement blake
This commit is contained in:
127
src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt
Normal file
127
src/commonMain/kotlin/com/infendro/hash/blake/Blake-224.kt
Normal file
@@ -0,0 +1,127 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.toUIntArray
|
||||
import com.infendro.hash.HashFunction
|
||||
|
||||
object `Blake-224` : HashFunction() {
|
||||
override val bytes: Int
|
||||
get() = 28
|
||||
|
||||
override val block: Int
|
||||
get() = 64
|
||||
|
||||
private val initial = uintArrayOf(
|
||||
0xc1059ed8U, 0x367cd507U,
|
||||
0x3070dd17U, 0xf70e5939U,
|
||||
0xffc00b31U, 0x68581511U,
|
||||
0x64f98fa7U, 0xbefa4fa4U,
|
||||
)
|
||||
|
||||
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 h = initial.copyOf()
|
||||
var t = 0U
|
||||
val l = value.size.toULong() * 8UL
|
||||
|
||||
// padding
|
||||
val paddedValue = buildList {
|
||||
addAll(value)
|
||||
add(0x80U)
|
||||
while (size % 64 != 56) {
|
||||
add(0x00U)
|
||||
}
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
|
||||
// compress
|
||||
val blocks = paddedValue
|
||||
.chunked(64) { it.toUByteArray() }
|
||||
for (block in blocks) {
|
||||
t += minOf(l - t, 512UL).toUInt()
|
||||
|
||||
val m = block.toUIntArray()
|
||||
compress(h, m, t)
|
||||
}
|
||||
|
||||
return h.sliceArray(0..6).toUByteArray()
|
||||
}
|
||||
|
||||
private fun compress(
|
||||
h: UIntArray,
|
||||
m: UIntArray,
|
||||
t: UInt,
|
||||
) {
|
||||
// initialization
|
||||
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]
|
||||
|
||||
// rounds
|
||||
for (r in 0..<14) {
|
||||
val s = sigma[r % 10]
|
||||
|
||||
// column
|
||||
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]])
|
||||
|
||||
// diagonal
|
||||
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]])
|
||||
}
|
||||
|
||||
// finalization
|
||||
for (i in 0..7) {
|
||||
h[i] = h[i] xor v[i] xor v[i + 8]
|
||||
}
|
||||
}
|
||||
|
||||
// round function
|
||||
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)
|
||||
}
|
||||
}
|
||||
128
src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt
Normal file
128
src/commonMain/kotlin/com/infendro/hash/blake/Blake-256.kt
Normal file
@@ -0,0 +1,128 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.toUIntArray
|
||||
import com.infendro.hash.HashFunction
|
||||
|
||||
object `Blake-256` : HashFunction() {
|
||||
override val bytes: Int
|
||||
get() = 32
|
||||
|
||||
override val block: Int
|
||||
get() = 64
|
||||
|
||||
private val initial = uintArrayOf(
|
||||
0x6a09e667U, 0xbb67ae85U,
|
||||
0x3c6ef372U, 0xa54ff53aU,
|
||||
0x510e527fU, 0x9b05688cU,
|
||||
0x1f83d9abU, 0x5be0cd19U,
|
||||
)
|
||||
|
||||
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 h = initial.copyOf()
|
||||
var t = 0U
|
||||
val l = value.size.toULong() * 8UL
|
||||
|
||||
// padding
|
||||
val paddedValue = buildList {
|
||||
addAll(value)
|
||||
add(0x80U)
|
||||
while (size % 64 != 55) {
|
||||
add(0x00U)
|
||||
}
|
||||
add(0x01U)
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
|
||||
// compress
|
||||
val blocks = paddedValue
|
||||
.chunked(64) { it.toUByteArray() }
|
||||
for (block in blocks) {
|
||||
t += minOf(l - t, 512UL).toUInt()
|
||||
|
||||
val m = block.toUIntArray()
|
||||
compress(h, m, t)
|
||||
}
|
||||
|
||||
return h.toUByteArray()
|
||||
}
|
||||
|
||||
private fun compress(
|
||||
h: UIntArray,
|
||||
m: UIntArray,
|
||||
t: UInt,
|
||||
) {
|
||||
// initialization
|
||||
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]
|
||||
|
||||
// rounds
|
||||
for (r in 0..<14) {
|
||||
val s = sigma[r % 10]
|
||||
|
||||
// column
|
||||
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]])
|
||||
|
||||
// diagonal
|
||||
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]])
|
||||
}
|
||||
|
||||
// finalization
|
||||
for (i in 0..7) {
|
||||
h[i] = h[i] xor v[i] xor v[i + 8]
|
||||
}
|
||||
}
|
||||
|
||||
// round function
|
||||
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)
|
||||
}
|
||||
}
|
||||
127
src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt
Normal file
127
src/commonMain/kotlin/com/infendro/hash/blake/Blake-384.kt
Normal file
@@ -0,0 +1,127 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.toULongArray
|
||||
import com.infendro.hash.HashFunction
|
||||
|
||||
object `Blake-384` : HashFunction() {
|
||||
override val bytes: Int
|
||||
get() = 48
|
||||
|
||||
override val block: Int
|
||||
get() = 128
|
||||
|
||||
private val initial = ulongArrayOf(
|
||||
0xcbbb9d5dc1059ed8U, 0x629a292a367cd507U,
|
||||
0x9159015a3070dd17U, 0x152fecd8f70e5939U,
|
||||
0x67332667ffc00b31U, 0x8eb44a8768581511U,
|
||||
0xdb0c2e0d64f98fa7U, 0x47b5481dbefa4fa4U,
|
||||
)
|
||||
|
||||
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 h = initial.copyOf()
|
||||
var t = 0UL
|
||||
val l = value.size.toULong() * 8UL
|
||||
|
||||
// padding
|
||||
val paddedValue = buildList {
|
||||
addAll(value)
|
||||
add(0x80U)
|
||||
while (size % 128 != 120) {
|
||||
add(0x00U)
|
||||
}
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
|
||||
// compress
|
||||
val blocks = paddedValue
|
||||
.chunked(128) { it.toUByteArray() }
|
||||
for (block in blocks) {
|
||||
t += minOf(l - t, 1024UL)
|
||||
|
||||
val m = block.toULongArray()
|
||||
compress(h, m, t)
|
||||
}
|
||||
|
||||
return h.sliceArray(0..5).toUByteArray()
|
||||
}
|
||||
|
||||
private fun compress(
|
||||
h: ULongArray,
|
||||
m: ULongArray,
|
||||
t: ULong,
|
||||
) {
|
||||
// initialization
|
||||
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]
|
||||
|
||||
// rounds
|
||||
for (r in 0..<16) {
|
||||
val s = sigma[r % 10]
|
||||
|
||||
// column
|
||||
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]])
|
||||
|
||||
// diagonal
|
||||
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]])
|
||||
}
|
||||
|
||||
// finalization
|
||||
for (i in 0..7) {
|
||||
h[i] = h[i] xor v[i] xor v[i + 8]
|
||||
}
|
||||
}
|
||||
|
||||
// round function
|
||||
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)
|
||||
}
|
||||
}
|
||||
129
src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt
Normal file
129
src/commonMain/kotlin/com/infendro/hash/blake/Blake-512.kt
Normal file
@@ -0,0 +1,129 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import com.infendro.bytearray.toUByteArray
|
||||
import com.infendro.bytearray.toULongArray
|
||||
import com.infendro.hash.HashFunction
|
||||
|
||||
object `Blake-512` : HashFunction() {
|
||||
override val bytes: Int
|
||||
get() = 64
|
||||
|
||||
override val block: Int
|
||||
get() = 128
|
||||
|
||||
private val initial = ulongArrayOf(
|
||||
0x6a09e667f3bcc908UL, 0xbb67ae8584caa73bUL,
|
||||
0x3c6ef372fe94f82bUL, 0xa54ff53a5f1d36f1UL,
|
||||
0x510e527fade682d1UL, 0x9b05688c2b3e6c1fUL,
|
||||
0x1f83d9abfb41bd6bUL, 0x5be0cd19137e2179UL,
|
||||
)
|
||||
|
||||
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 h = initial.copyOf()
|
||||
var t = 0UL
|
||||
val l = value.size.toULong() * 8UL
|
||||
|
||||
// padding
|
||||
val paddedValue = buildList {
|
||||
addAll(value)
|
||||
add(0x80U)
|
||||
while (size % 128 != 111) {
|
||||
add(0x00U)
|
||||
}
|
||||
add(0x01U)
|
||||
repeat(8) { add(0x00U) }
|
||||
addAll(l.toUByteArray())
|
||||
}.toUByteArray()
|
||||
|
||||
// compress
|
||||
val blocks = paddedValue
|
||||
.chunked(128) { it.toUByteArray() }
|
||||
for (block in blocks) {
|
||||
t += minOf(l - t, 1024UL)
|
||||
|
||||
val m = block.toULongArray()
|
||||
compress(h, m, t)
|
||||
}
|
||||
|
||||
return h.toUByteArray()
|
||||
}
|
||||
|
||||
private fun compress(
|
||||
h: ULongArray,
|
||||
m: ULongArray,
|
||||
t: ULong,
|
||||
) {
|
||||
// initialization
|
||||
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]
|
||||
|
||||
// rounds
|
||||
for (r in 0..<16) {
|
||||
val s = sigma[r % 10]
|
||||
|
||||
// column
|
||||
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]])
|
||||
|
||||
// diagonal
|
||||
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]])
|
||||
}
|
||||
|
||||
// finalization
|
||||
for (i in 0..7) {
|
||||
h[i] = h[i] xor v[i] xor v[i + 8]
|
||||
}
|
||||
}
|
||||
|
||||
// round function
|
||||
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)
|
||||
}
|
||||
}
|
||||
67
src/commonTest/kotlin/com/infendro/hash/blake/Blake-224.kt
Normal file
67
src/commonTest/kotlin/com/infendro/hash/blake/Blake-224.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `Blake-224 Test` {
|
||||
val function = `Blake-224`
|
||||
|
||||
@Test
|
||||
fun `bytes and block`() {
|
||||
assertEquals(28, function.bytes)
|
||||
assertEquals(224, function.bits)
|
||||
assertEquals(64, function.block)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `empty bytearray`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x7dU, 0xc5U, 0x31U, 0x3bU,
|
||||
0x1cU, 0x04U, 0x51U, 0x2aU,
|
||||
0x17U, 0x4bU, 0xd6U, 0x50U,
|
||||
0x3bU, 0x89U, 0x60U, 0x7aU,
|
||||
0xecU, 0xbeU, 0xe0U, 0x90U,
|
||||
0x3dU, 0x40U, 0xa8U, 0xa5U,
|
||||
0x69U, 0xc9U, 0x4eU, 0xedU,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(0)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one block`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x45U, 0x04U, 0xcbU, 0x03U,
|
||||
0x14U, 0xfbU, 0x2aU, 0x4fU,
|
||||
0x7aU, 0x69U, 0x2eU, 0x69U,
|
||||
0x6eU, 0x48U, 0x79U, 0x12U,
|
||||
0xfeU, 0x3fU, 0x24U, 0x68U,
|
||||
0xfeU, 0x31U, 0x2cU, 0x73U,
|
||||
0xa5U, 0x27U, 0x8eU, 0xc5U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(1)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two blocks`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0xf5U, 0xaaU, 0x00U, 0xddU,
|
||||
0x1cU, 0xb8U, 0x47U, 0xe3U,
|
||||
0x14U, 0x03U, 0x72U, 0xafU,
|
||||
0x7bU, 0x5cU, 0x46U, 0xb4U,
|
||||
0x88U, 0x8dU, 0x82U, 0xc8U,
|
||||
0xc0U, 0xa9U, 0x17U, 0x91U,
|
||||
0x3cU, 0xfbU, 0x5dU, 0x04U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(72)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
70
src/commonTest/kotlin/com/infendro/hash/blake/Blake-256.kt
Normal file
70
src/commonTest/kotlin/com/infendro/hash/blake/Blake-256.kt
Normal file
@@ -0,0 +1,70 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `Blake-256 Test` {
|
||||
val function = `Blake-256`
|
||||
|
||||
@Test
|
||||
fun `bytes and block`() {
|
||||
assertEquals(32, function.bytes)
|
||||
assertEquals(256, function.bits)
|
||||
assertEquals(64, function.block)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `empty bytearray`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x71U, 0x6fU, 0x6eU, 0x86U,
|
||||
0x3fU, 0x74U, 0x4bU, 0x9aU,
|
||||
0xc2U, 0x2cU, 0x97U, 0xecU,
|
||||
0x7bU, 0x76U, 0xeaU, 0x5fU,
|
||||
0x59U, 0x08U, 0xbcU, 0x5bU,
|
||||
0x2fU, 0x67U, 0xc6U, 0x15U,
|
||||
0x10U, 0xbfU, 0xc4U, 0x75U,
|
||||
0x13U, 0x84U, 0xeaU, 0x7aU,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(0)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one block`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x0cU, 0xe8U, 0xd4U, 0xefU,
|
||||
0x4dU, 0xd7U, 0xcdU, 0x8dU,
|
||||
0x62U, 0xdfU, 0xdeU, 0xd9U,
|
||||
0xd4U, 0xedU, 0xb0U, 0xa7U,
|
||||
0x74U, 0xaeU, 0x6aU, 0x41U,
|
||||
0x92U, 0x9aU, 0x74U, 0xdaU,
|
||||
0x23U, 0x10U, 0x9eU, 0x8fU,
|
||||
0x11U, 0x13U, 0x9cU, 0x87U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(1)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two blocks`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0xd4U, 0x19U, 0xbaU, 0xd3U,
|
||||
0x2dU, 0x50U, 0x4fU, 0xb7U,
|
||||
0xd4U, 0x4dU, 0x46U, 0x0cU,
|
||||
0x42U, 0xc5U, 0x59U, 0x3fU,
|
||||
0xe5U, 0x44U, 0xfaU, 0x4cU,
|
||||
0x13U, 0x5dU, 0xecU, 0x31U,
|
||||
0xe2U, 0x1bU, 0xd9U, 0xabU,
|
||||
0xdcU, 0xc2U, 0x2dU, 0x41U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(72)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
82
src/commonTest/kotlin/com/infendro/hash/blake/Blake-384.kt
Normal file
82
src/commonTest/kotlin/com/infendro/hash/blake/Blake-384.kt
Normal file
@@ -0,0 +1,82 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `Blake-384 Test` {
|
||||
val function = `Blake-384`
|
||||
|
||||
@Test
|
||||
fun `bytes and block`() {
|
||||
assertEquals(48, function.bytes)
|
||||
assertEquals(384, function.bits)
|
||||
assertEquals(128, function.block)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `empty bytearray`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0xc6U, 0xcbU, 0xd8U, 0x9cU,
|
||||
0x92U, 0x6aU, 0xb5U, 0x25U,
|
||||
0xc2U, 0x42U, 0xe6U, 0x62U,
|
||||
0x1fU, 0x2fU, 0x5fU, 0xa7U,
|
||||
0x3aU, 0xa4U, 0xafU, 0xe3U,
|
||||
0xd9U, 0xe2U, 0x4aU, 0xedU,
|
||||
0x72U, 0x7fU, 0xaaU, 0xadU,
|
||||
0xd6U, 0xafU, 0x38U, 0xb6U,
|
||||
0x20U, 0xbdU, 0xb6U, 0x23U,
|
||||
0xddU, 0x2bU, 0x47U, 0x88U,
|
||||
0xb1U, 0xc8U, 0x08U, 0x69U,
|
||||
0x84U, 0xafU, 0x87U, 0x06U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(0)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one block`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x10U, 0x28U, 0x1fU, 0x67U,
|
||||
0xe1U, 0x35U, 0xe9U, 0x0aU,
|
||||
0xe8U, 0xe8U, 0x82U, 0x25U,
|
||||
0x1aU, 0x35U, 0x55U, 0x10U,
|
||||
0xa7U, 0x19U, 0x36U, 0x7aU,
|
||||
0xd7U, 0x02U, 0x27U, 0xb1U,
|
||||
0x37U, 0x34U, 0x3eU, 0x1bU,
|
||||
0xc1U, 0x22U, 0x01U, 0x5cU,
|
||||
0x29U, 0x39U, 0x1eU, 0x85U,
|
||||
0x45U, 0xb5U, 0x27U, 0x2dU,
|
||||
0x13U, 0xa7U, 0xc2U, 0x87U,
|
||||
0x9dU, 0xa3U, 0xd8U, 0x07U
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(1)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two blocks`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x0bU, 0x98U, 0x45U, 0xddU,
|
||||
0x42U, 0x95U, 0x66U, 0xcdU,
|
||||
0xabU, 0x77U, 0x2bU, 0xa1U,
|
||||
0x95U, 0xd2U, 0x71U, 0xefU,
|
||||
0xfeU, 0x2dU, 0x02U, 0x11U,
|
||||
0xf1U, 0x69U, 0x91U, 0xd7U,
|
||||
0x66U, 0xbaU, 0x74U, 0x94U,
|
||||
0x47U, 0xc5U, 0xcdU, 0xe5U,
|
||||
0x69U, 0x78U, 0x0bU, 0x2dU,
|
||||
0xaaU, 0x66U, 0xc4U, 0xb2U,
|
||||
0x24U, 0xa2U, 0xecU, 0x2eU,
|
||||
0x5dU, 0x09U, 0x17U, 0x4cU
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(144)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
94
src/commonTest/kotlin/com/infendro/hash/blake/Blake-512.kt
Normal file
94
src/commonTest/kotlin/com/infendro/hash/blake/Blake-512.kt
Normal file
@@ -0,0 +1,94 @@
|
||||
package com.infendro.hash.blake
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertContentEquals
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class `Blake-512 Test` {
|
||||
val function = `Blake-512`
|
||||
|
||||
@Test
|
||||
fun `bytes and block`() {
|
||||
assertEquals(64, function.bytes)
|
||||
assertEquals(512, function.bits)
|
||||
assertEquals(128, function.block)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `empty bytearray`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0xa8U, 0xcfU, 0xbbU, 0xd7U,
|
||||
0x37U, 0x26U, 0x06U, 0x2dU,
|
||||
0xf0U, 0xc6U, 0x86U, 0x4dU,
|
||||
0xdaU, 0x65U, 0xdeU, 0xfeU,
|
||||
0x58U, 0xefU, 0x0cU, 0xc5U,
|
||||
0x2aU, 0x56U, 0x25U, 0x09U,
|
||||
0x0fU, 0xa1U, 0x76U, 0x01U,
|
||||
0xe1U, 0xeeU, 0xcdU, 0x1bU,
|
||||
0x62U, 0x8eU, 0x94U, 0xf3U,
|
||||
0x96U, 0xaeU, 0x40U, 0x2aU,
|
||||
0x00U, 0xacU, 0xc9U, 0xeaU,
|
||||
0xb7U, 0x7bU, 0x4dU, 0x4cU,
|
||||
0x2eU, 0x85U, 0x2aU, 0xaaU,
|
||||
0xa2U, 0x5aU, 0x63U, 0x6dU,
|
||||
0x80U, 0xafU, 0x3fU, 0xc7U,
|
||||
0x91U, 0x3eU, 0xf5U, 0xb8U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(0)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one block`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x97U, 0x96U, 0x15U, 0x87U,
|
||||
0xf6U, 0xd9U, 0x70U, 0xfaU,
|
||||
0xbaU, 0x6dU, 0x24U, 0x78U,
|
||||
0x04U, 0x5dU, 0xe6U, 0xd1U,
|
||||
0xfaU, 0xbdU, 0x09U, 0xb6U,
|
||||
0x1aU, 0xe5U, 0x09U, 0x32U,
|
||||
0x05U, 0x4dU, 0x52U, 0xbcU,
|
||||
0x29U, 0xd3U, 0x1bU, 0xe4U,
|
||||
0xffU, 0x91U, 0x02U, 0xb9U,
|
||||
0xf6U, 0x9eU, 0x2bU, 0xbdU,
|
||||
0xb8U, 0x3bU, 0xe1U, 0x3dU,
|
||||
0x4bU, 0x9cU, 0x06U, 0x09U,
|
||||
0x1eU, 0x5fU, 0xa0U, 0xb4U,
|
||||
0x8bU, 0xd0U, 0x81U, 0xb6U,
|
||||
0x34U, 0x05U, 0x8bU, 0xe0U,
|
||||
0xecU, 0x49U, 0xbeU, 0xb3U,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(1)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `two blocks`() {
|
||||
val expected = ubyteArrayOf(
|
||||
0x31U, 0x37U, 0x17U, 0xd6U,
|
||||
0x08U, 0xe9U, 0xcfU, 0x75U,
|
||||
0x8dU, 0xcbU, 0x1eU, 0xb0U,
|
||||
0xf0U, 0xc3U, 0xcfU, 0x9fU,
|
||||
0xc1U, 0x50U, 0xb2U, 0xd5U,
|
||||
0x00U, 0xfbU, 0x33U, 0xf5U,
|
||||
0x1cU, 0x52U, 0xafU, 0xc9U,
|
||||
0x9dU, 0x35U, 0x8aU, 0x2fU,
|
||||
0x13U, 0x74U, 0xb8U, 0xa3U,
|
||||
0x8bU, 0xbaU, 0x79U, 0x74U,
|
||||
0xe7U, 0xf6U, 0xefU, 0x79U,
|
||||
0xcaU, 0xb1U, 0x6fU, 0x22U,
|
||||
0xceU, 0x1eU, 0x64U, 0x9dU,
|
||||
0x6eU, 0x01U, 0xadU, 0x95U,
|
||||
0x89U, 0xc2U, 0x13U, 0x04U,
|
||||
0x5dU, 0x54U, 0x5dU, 0xdeU,
|
||||
)
|
||||
val actual = function.hash(
|
||||
UByteArray(144)
|
||||
)
|
||||
assertContentEquals(expected, actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user