diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/FieldP.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/FieldP.kt index c4f2b3efc4..5b5afdc391 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/FieldP.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/FieldP.kt @@ -298,7 +298,7 @@ internal object FieldP { // ==================== Reduction ==================== - inline fun reduceSelf(a: LongArray) { + fun reduceSelf(a: LongArray) { // Exploit P's structure: P = [P0, -1, -1, -1] where P[1..3] = 0xFFFFFFFFFFFFFFFF. // a >= P only if a[3]==a[2]==a[1]==-1 AND a[0] >= P[0]. The first check (a[3]==-1) // fails >99.99% of the time for random field elements, making this a single branch. diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Glv.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Glv.kt index 8970036d93..314454cab3 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Glv.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Glv.kt @@ -49,7 +49,7 @@ internal object Glv { // ==================== GLV Scalar Decomposition ==================== - data class Split( + class Split( val k1: LongArray, val k2: LongArray, val negK1: Boolean, diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/KeyCodec.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/KeyCodec.kt index 31ab9875c4..8ada7efdd8 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/KeyCodec.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/KeyCodec.kt @@ -74,8 +74,8 @@ internal object KeyCodec { outX: LongArray, outY: LongArray, ): Boolean = - when { - pubkey.size == 33 && (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> { + when (pubkey.size) { + 33 if (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> { val x = U256.fromBytes(pubkey.copyOfRange(1, 33)) if (U256.cmp(x, FieldP.P) >= 0) return false val t = LongArray(4) @@ -89,7 +89,7 @@ internal object KeyCodec { true } - pubkey.size == 65 && pubkey[0] == 0x04.toByte() -> { + 65 if pubkey[0] == 0x04.toByte() -> { val x = U256.fromBytes(pubkey.copyOfRange(1, 33)) val y = U256.fromBytes(pubkey.copyOfRange(33, 65)) val y2 = LongArray(4) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Point.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Point.kt index 7d0070c434..427c390066 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Point.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Point.kt @@ -93,7 +93,7 @@ internal class MutablePoint( val y: LongArray = LongArray(4), val z: LongArray = LongArray(4), ) { - inline fun isInfinity(): Boolean = U256.isZero(z) + fun isInfinity(): Boolean = U256.isZero(z) fun setInfinity() { for (i in 0 until 4) { @@ -279,7 +279,7 @@ internal object ECPoint { val base = b * COMB_POINTS jac[base].setInfinity() for (m in 1 until COMB_POINTS) { - val changedBit = Integer.numberOfTrailingZeros(m) + val changedBit = m.countTrailingZeroBits() if (m and (m - 1) == 0) { jac[base + m].copyFrom(toothG[b * COMB_TEETH + changedBit]) } else { diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt index d08abf1ba5..a251fad0b5 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt @@ -122,15 +122,15 @@ object Secp256k1 { * For already-compressed keys: returns the input unchanged. */ fun pubKeyCompress(pubkey: ByteArray): ByteArray = - when { - pubkey.size == 65 && pubkey[0] == 0x04.toByte() -> { + when (pubkey.size) { + 65 if pubkey[0] == 0x04.toByte() -> { val result = ByteArray(33) result[0] = if (pubkey[64].toInt() and 1 == 0) 0x02 else 0x03 pubkey.copyInto(result, 1, 1, 33) result } - pubkey.size == 33 && (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> { + 33 if (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> { pubkey } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/U256.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/U256.kt index 76fa73420c..421afb4622 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/U256.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/U256.kt @@ -49,9 +49,7 @@ package com.vitorpamplona.quartz.utils.secp256k1 * Raw 256-bit unsigned integer arithmetic using 4×64-bit limbs. */ internal object U256 { - val ZERO = LongArray(4) - - inline fun isZero(a: LongArray): Boolean = (a[0] or a[1] or a[2] or a[3]) == 0L + fun isZero(a: LongArray): Boolean = (a[0] or a[1] or a[2] or a[3]) == 0L /** Unsigned comparison. Returns -1 if a < b, 0 if equal, 1 if a > b. */ fun cmp( @@ -254,7 +252,7 @@ internal object U256 { } /** Test if bit at position pos is set. Called ~2,800× per mulG (comb table lookup). */ - inline fun testBit( + fun testBit( a: LongArray, pos: Int, ): Boolean = (a[pos / 64] ushr (pos % 64)) and 1L == 1L