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