removes some warnings

This commit is contained in:
Vitor Pamplona
2026-04-06 17:12:00 -04:00
parent 034c1ab2d1
commit 8a914d3130
6 changed files with 12 additions and 14 deletions
@@ -298,7 +298,7 @@ internal object FieldP {
// ==================== Reduction ==================== // ==================== Reduction ====================
inline fun reduceSelf(a: LongArray) { fun reduceSelf(a: LongArray) {
// Exploit P's structure: P = [P0, -1, -1, -1] where P[1..3] = 0xFFFFFFFFFFFFFFFF. // 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) // 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. // fails >99.99% of the time for random field elements, making this a single branch.
@@ -49,7 +49,7 @@ internal object Glv {
// ==================== GLV Scalar Decomposition ==================== // ==================== GLV Scalar Decomposition ====================
data class Split( class Split(
val k1: LongArray, val k1: LongArray,
val k2: LongArray, val k2: LongArray,
val negK1: Boolean, val negK1: Boolean,
@@ -74,8 +74,8 @@ internal object KeyCodec {
outX: LongArray, outX: LongArray,
outY: LongArray, outY: LongArray,
): Boolean = ): Boolean =
when { when (pubkey.size) {
pubkey.size == 33 && (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> { 33 if (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> {
val x = U256.fromBytes(pubkey.copyOfRange(1, 33)) val x = U256.fromBytes(pubkey.copyOfRange(1, 33))
if (U256.cmp(x, FieldP.P) >= 0) return false if (U256.cmp(x, FieldP.P) >= 0) return false
val t = LongArray(4) val t = LongArray(4)
@@ -89,7 +89,7 @@ internal object KeyCodec {
true true
} }
pubkey.size == 65 && pubkey[0] == 0x04.toByte() -> { 65 if pubkey[0] == 0x04.toByte() -> {
val x = U256.fromBytes(pubkey.copyOfRange(1, 33)) val x = U256.fromBytes(pubkey.copyOfRange(1, 33))
val y = U256.fromBytes(pubkey.copyOfRange(33, 65)) val y = U256.fromBytes(pubkey.copyOfRange(33, 65))
val y2 = LongArray(4) val y2 = LongArray(4)
@@ -93,7 +93,7 @@ internal class MutablePoint(
val y: LongArray = LongArray(4), val y: LongArray = LongArray(4),
val z: LongArray = LongArray(4), val z: LongArray = LongArray(4),
) { ) {
inline fun isInfinity(): Boolean = U256.isZero(z) fun isInfinity(): Boolean = U256.isZero(z)
fun setInfinity() { fun setInfinity() {
for (i in 0 until 4) { for (i in 0 until 4) {
@@ -279,7 +279,7 @@ internal object ECPoint {
val base = b * COMB_POINTS val base = b * COMB_POINTS
jac[base].setInfinity() jac[base].setInfinity()
for (m in 1 until COMB_POINTS) { for (m in 1 until COMB_POINTS) {
val changedBit = Integer.numberOfTrailingZeros(m) val changedBit = m.countTrailingZeroBits()
if (m and (m - 1) == 0) { if (m and (m - 1) == 0) {
jac[base + m].copyFrom(toothG[b * COMB_TEETH + changedBit]) jac[base + m].copyFrom(toothG[b * COMB_TEETH + changedBit])
} else { } else {
@@ -122,15 +122,15 @@ object Secp256k1 {
* For already-compressed keys: returns the input unchanged. * For already-compressed keys: returns the input unchanged.
*/ */
fun pubKeyCompress(pubkey: ByteArray): ByteArray = fun pubKeyCompress(pubkey: ByteArray): ByteArray =
when { when (pubkey.size) {
pubkey.size == 65 && pubkey[0] == 0x04.toByte() -> { 65 if pubkey[0] == 0x04.toByte() -> {
val result = ByteArray(33) val result = ByteArray(33)
result[0] = if (pubkey[64].toInt() and 1 == 0) 0x02 else 0x03 result[0] = if (pubkey[64].toInt() and 1 == 0) 0x02 else 0x03
pubkey.copyInto(result, 1, 1, 33) pubkey.copyInto(result, 1, 1, 33)
result result
} }
pubkey.size == 33 && (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> { 33 if (pubkey[0] == 0x02.toByte() || pubkey[0] == 0x03.toByte()) -> {
pubkey pubkey
} }
@@ -49,9 +49,7 @@ package com.vitorpamplona.quartz.utils.secp256k1
* Raw 256-bit unsigned integer arithmetic using 4×64-bit limbs. * Raw 256-bit unsigned integer arithmetic using 4×64-bit limbs.
*/ */
internal object U256 { internal object U256 {
val ZERO = LongArray(4) fun isZero(a: LongArray): Boolean = (a[0] or a[1] or a[2] or a[3]) == 0L
inline 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. */ /** Unsigned comparison. Returns -1 if a < b, 0 if equal, 1 if a > b. */
fun cmp( 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). */ /** Test if bit at position pos is set. Called ~2,800× per mulG (comb table lookup). */
inline fun testBit( fun testBit(
a: LongArray, a: LongArray,
pos: Int, pos: Int,
): Boolean = (a[pos / 64] ushr (pos % 64)) and 1L == 1L ): Boolean = (a[pos / 64] ushr (pos % 64)) and 1L == 1L