|
| 1 | +package one.mixin.bot.util.keccak |
| 2 | + |
| 3 | +import com.ionspin.kotlin.bignum.integer.BigInteger |
| 4 | +import one.mixin.bot.util.keccak.extensions.fillWith |
| 5 | +import kotlin.math.min |
| 6 | + |
| 7 | +public object Keccak { |
| 8 | + private val BIT_65 = BigInteger.ONE shl (64) |
| 9 | + private val MAX_64_BITS = BIT_65 - BigInteger.ONE |
| 10 | + |
| 11 | + public fun digest( |
| 12 | + value: ByteArray, |
| 13 | + parameter: KeccakParameter, |
| 14 | + ): ByteArray { |
| 15 | + val uState = IntArray(200) |
| 16 | + val uMessage = convertToUInt(value) |
| 17 | + |
| 18 | + var blockSize = 0 |
| 19 | + var inputOffset = 0 |
| 20 | + |
| 21 | + // Absorbing phase |
| 22 | + while (inputOffset < uMessage.size) { |
| 23 | + blockSize = min(uMessage.size - inputOffset, parameter.rateInBytes) |
| 24 | + for (i in 0 until blockSize) { |
| 25 | + uState[i] = uState[i] xor uMessage[i + inputOffset] |
| 26 | + } |
| 27 | + |
| 28 | + inputOffset += blockSize |
| 29 | + |
| 30 | + if (blockSize == parameter.rateInBytes) { |
| 31 | + doF(uState) |
| 32 | + blockSize = 0 |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Padding phase |
| 37 | + uState[blockSize] = uState[blockSize] xor parameter.d |
| 38 | + if (parameter.d and 0x80 != 0 && blockSize == parameter.rateInBytes - 1) { |
| 39 | + doF(uState) |
| 40 | + } |
| 41 | + |
| 42 | + uState[parameter.rateInBytes - 1] = uState[parameter.rateInBytes - 1] xor 0x80 |
| 43 | + doF(uState) |
| 44 | + |
| 45 | + // Squeezing phase |
| 46 | + val byteResults = mutableListOf<Byte>() |
| 47 | + var tOutputLen = parameter.outputLengthInBytes |
| 48 | + while (tOutputLen > 0) { |
| 49 | + blockSize = min(tOutputLen, parameter.rateInBytes) |
| 50 | + for (i in 0 until blockSize) { |
| 51 | + byteResults.add(uState[i].toByte().toInt().toByte()) |
| 52 | + } |
| 53 | + |
| 54 | + tOutputLen -= blockSize |
| 55 | + if (tOutputLen > 0) { |
| 56 | + doF(uState) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return byteResults.toByteArray() |
| 61 | + } |
| 62 | + |
| 63 | + private fun doF(uState: IntArray) { |
| 64 | + val lState = Array(5) { Array(5) { BigInteger.ZERO } } |
| 65 | + |
| 66 | + for (i in 0..4) { |
| 67 | + for (j in 0..4) { |
| 68 | + val data = IntArray(8) |
| 69 | + val index = 8 * (i + 5 * j) |
| 70 | + uState.copyInto(data, 0, index, index + data.size) |
| 71 | + lState[i][j] = convertFromLittleEndianTo64(data) |
| 72 | + } |
| 73 | + } |
| 74 | + roundB(lState) |
| 75 | + |
| 76 | + uState.fillWith(0) |
| 77 | + for (i in 0..4) { |
| 78 | + for (j in 0..4) { |
| 79 | + val data = convertFrom64ToLittleEndian(lState[i][j]) |
| 80 | + data.copyInto(uState, 8 * (i + 5 * j)) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Permutation on the given state. |
| 87 | + */ |
| 88 | + private fun roundB(state: Array<Array<BigInteger>>) { |
| 89 | + var lfsrState = 1 |
| 90 | + for (round in 0..23) { |
| 91 | + val c = arrayOfNulls<BigInteger>(5) |
| 92 | + val d = arrayOfNulls<BigInteger>(5) |
| 93 | + |
| 94 | + // θ step |
| 95 | + for (i in 0..4) { |
| 96 | + c[i] = state[i][0].xor(state[i][1]).xor(state[i][2]).xor(state[i][3]).xor(state[i][4]) |
| 97 | + } |
| 98 | + |
| 99 | + for (i in 0..4) { |
| 100 | + d[i] = c[(i + 4) % 5]!!.xor(c[(i + 1) % 5]!!.leftRotate64(1)) |
| 101 | + } |
| 102 | + |
| 103 | + for (i in 0..4) { |
| 104 | + for (j in 0..4) { |
| 105 | + state[i][j] = state[i][j].xor(d[i]!!) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // ρ and π steps |
| 110 | + var x = 1 |
| 111 | + var y = 0 |
| 112 | + var current = state[x][y] |
| 113 | + for (i in 0..23) { |
| 114 | + val tX = x |
| 115 | + x = y |
| 116 | + y = (2 * tX + 3 * y) % 5 |
| 117 | + |
| 118 | + val shiftValue = current |
| 119 | + current = state[x][y] |
| 120 | + |
| 121 | + state[x][y] = shiftValue.leftRotate64Safely((i + 1) * (i + 2) / 2) |
| 122 | + } |
| 123 | + |
| 124 | + // χ step |
| 125 | + for (j in 0..4) { |
| 126 | + val t = arrayOfNulls<BigInteger>(5) |
| 127 | + for (i in 0..4) { |
| 128 | + t[i] = state[i][j] |
| 129 | + } |
| 130 | + |
| 131 | + for (i in 0..4) { |
| 132 | + // ~t[(i + 1) % 5] |
| 133 | + val invertVal = t[(i + 1) % 5]!!.xor(MAX_64_BITS) |
| 134 | + // t[i] ^ ((~t[(i + 1) % 5]) & t[(i + 2) % 5]) |
| 135 | + state[i][j] = t[i]!!.xor(invertVal.and(t[(i + 2) % 5]!!)) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // ι step |
| 140 | + for (i in 0..6) { |
| 141 | + lfsrState = (lfsrState shl 1 xor (lfsrState shr 7) * 0x71) % 256 |
| 142 | + // pow(2, i) - 1 |
| 143 | + val bitPosition = (1 shl i) - 1 |
| 144 | + if (lfsrState and 2 != 0) { |
| 145 | + state[0][0] = state[0][0].xor(BigInteger.ONE shl bitPosition) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Converts the given [data] array to an [IntArray] containing UInt values. |
| 153 | + */ |
| 154 | + private fun convertToUInt(data: ByteArray) = |
| 155 | + IntArray(data.size) { |
| 156 | + data[it].toInt() and 0xFF |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Converts the given [data] array containing the little endian representation of a number to a [BigInteger]. |
| 161 | + */ |
| 162 | + private fun convertFromLittleEndianTo64(data: IntArray): BigInteger { |
| 163 | + val value = |
| 164 | + data.map { it.toString(16) } |
| 165 | + .map { if (it.length == 2) it else "0$it" } |
| 166 | + .reversed() |
| 167 | + .joinToString("") |
| 168 | + return BigInteger.parseString(value, 16) |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Converts the given [BigInteger] to a little endian representation as an [IntArray]. |
| 173 | + */ |
| 174 | + private fun convertFrom64ToLittleEndian(uLong: BigInteger): IntArray { |
| 175 | + val asHex = uLong.toString(16) |
| 176 | + val asHexPadded = "0".repeat((8 * 2) - asHex.length) + asHex |
| 177 | + return IntArray(8) { |
| 178 | + ((7 - it) * 2).let { pos -> |
| 179 | + asHexPadded.substring(pos, pos + 2).toInt(16) |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private fun BigInteger.leftRotate64Safely(rotate: Int) = leftRotate64(rotate % 64) |
| 185 | + |
| 186 | + private fun BigInteger.leftRotate64(rotate: Int) = (this shr (64 - rotate)).add(this shl rotate).mod(BIT_65) |
| 187 | +} |
0 commit comments