Moves the hex operations to our own functions

This commit is contained in:
Vitor Pamplona
2026-04-06 17:55:54 -04:00
parent 094586ff41
commit fe5f07da3f
7 changed files with 27 additions and 34 deletions
@@ -20,12 +20,13 @@
*/
package com.vitorpamplona.quartz.nip44Encryption.crypto
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import kotlin.test.Test
import kotlin.test.assertContentEquals
/** Test vectors from libsodium xchacha20.c */
class XChaCha20Test {
private fun hex(s: String): ByteArray = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun hex(s: String): ByteArray = s.hexToByteArray()
// HChaCha20 test vectors from libsodium
data class HChaCha20TV(
@@ -20,6 +20,8 @@
*/
package com.vitorpamplona.quartz.utils.secp256k1
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@@ -27,12 +29,9 @@ import kotlin.test.assertTrue
/** Tests for field arithmetic modulo p. */
class FieldPTest {
private fun hex(s: String) =
U256.fromBytes(
s.chunked(2).map { it.toInt(16).toByte() }.toByteArray(),
)
private fun hex(s: String) = U256.fromBytes(s.hexToByteArray())
private fun toHex(a: LongArray) = U256.toBytes(a).joinToString("") { "%02x".format(it) }
private fun toHex(a: LongArray) = U256.toBytes(a).toHexKey()
// ==================== Basic identities ====================
@@ -20,18 +20,17 @@
*/
package com.vitorpamplona.quartz.utils.secp256k1
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
/** Comprehensive tests for GLV endomorphism and wNAF encoding. */
class GlvTest {
private fun toHex(a: LongArray) = U256.toBytes(a).joinToString("") { "%02x".format(it) }
private fun toHex(a: LongArray) = U256.toBytes(a).toHexKey()
private fun hex(s: String) =
U256.fromBytes(
s.chunked(2).map { it.toInt(16).toByte() }.toByteArray(),
)
private fun hex(s: String) = U256.fromBytes(s.hexToByteArray())
@Suppress("ktlint:standard:property-naming")
private val LAMBDA = hex("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72")
@@ -20,6 +20,7 @@
*/
package com.vitorpamplona.quartz.utils.secp256k1
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@@ -27,7 +28,7 @@ import kotlin.test.assertTrue
/** Tests for KeyCodec: key parsing, serialization, liftX, hasEvenY. */
class KeyCodecTest {
private fun toHex(a: LongArray) = U256.toBytes(a).joinToString("") { "%02x".format(it) }
private fun toHex(a: LongArray) = U256.toBytes(a).toHexKey()
// ==================== liftX ====================
@@ -20,6 +20,8 @@
*/
package com.vitorpamplona.quartz.utils.secp256k1
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@@ -27,12 +29,9 @@ import kotlin.test.assertTrue
/** Tests for elliptic curve point operations. */
class PointTest {
private fun hex(s: String) =
U256.fromBytes(
s.chunked(2).map { it.toInt(16).toByte() }.toByteArray(),
)
private fun hex(s: String) = U256.fromBytes(s.hexToByteArray())
private fun toHex(a: LongArray) = U256.toBytes(a).joinToString("") { "%02x".format(it) }
private fun toHex(a: LongArray) = U256.toBytes(a).toHexKey()
// ==================== Generator point ====================
@@ -361,11 +360,7 @@ class PointTest {
@Test
fun parseCompressedOddY() {
val privKeyBytes =
"65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a"
.chunked(2)
.map { it.toInt(16).toByte() }
.toByteArray()
val privKeyBytes = "65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a".hexToByteArray()
val pubkey = Secp256k1.pubkeyCreate(privKeyBytes)
val compressed = Secp256k1.pubKeyCompress(pubkey)
assertEquals(0x03.toByte(), compressed[0]) // Odd y → 03 prefix
@@ -20,6 +20,8 @@
*/
package com.vitorpamplona.quartz.utils.secp256k1
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@@ -27,12 +29,9 @@ import kotlin.test.assertTrue
/** Tests for scalar arithmetic modulo n. */
class ScalarNTest {
private fun hex(s: String) =
U256.fromBytes(
s.chunked(2).map { it.toInt(16).toByte() }.toByteArray(),
)
private fun hex(s: String) = U256.fromBytes(s.hexToByteArray())
private fun toHex(a: LongArray) = U256.toBytes(a).joinToString("") { "%02x".format(it) }
private fun toHex(a: LongArray) = U256.toBytes(a).toHexKey()
// ==================== isValid ====================
@@ -20,6 +20,8 @@
*/
package com.vitorpamplona.quartz.utils.secp256k1
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@@ -27,12 +29,9 @@ import kotlin.test.assertTrue
/** Tests for 256-bit unsigned integer arithmetic. */
class U256Test {
private fun hex(s: String) =
U256.fromBytes(
s.chunked(2).map { it.toInt(16).toByte() }.toByteArray(),
)
private fun hex(s: String) = U256.fromBytes(s.hexToByteArray())
private fun toHex(a: LongArray) = U256.toBytes(a).joinToString("") { "%02x".format(it) }
private fun toHex(a: LongArray) = U256.toBytes(a).toHexKey()
// ==================== isZero / cmp ====================
@@ -150,10 +149,10 @@ class U256Test {
@Test
fun bytesRoundTrip() {
val hex = "67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530"
val bytes = hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
val bytes = hex.hexToByteArray()
val limbs = U256.fromBytes(bytes)
val back = U256.toBytes(limbs)
assertEquals(hex.lowercase(), back.joinToString("") { "%02x".format(it) })
assertEquals(hex.lowercase(), back.toHexKey())
}
@Test