Skip to content

Commit

Permalink
Testing that both 02 and 03 compressed keys are valid Nostr keys and …
Browse files Browse the repository at this point in the history
…can encrypt and decrypt to each other.
  • Loading branch information
vitorpamplona committed Jul 29, 2024
1 parent b440661 commit 143c3a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ class Nip01Test {
)
}

@Test
fun testGetPublicCompressedWith2() {
val key = "e6159851715b4aa6190c22b899b0c792847de0a4435ac5b678f35738351c43b0".hexToByteArray()
assertEquals(
"029fa4ce8c87ca546b196e6518db80a6780e1bd5552b61f9f17bafee5d4e34e09b",
nip01.compressedPubkeyCreate(key).toHexKey(),
)
}

@Test
fun testGetPublicCompressedWith3() {
val key = "65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a".hexToByteArray()
assertEquals(
"033dcef7585efbdb68747d919152bd481e21f5e952aaaef5a19604fbd096a93dd5",
nip01.compressedPubkeyCreate(key).toHexKey(),
)
}

@Test
fun testDeterministicSign() {
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.crypto.nip01.Nip01
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import fr.acinq.secp256k1.Secp256k1
Expand All @@ -48,6 +49,7 @@ class NIP44v2Test {

private val random = SecureRandom()
private val nip44v2 = Nip44v2(Secp256k1.get(), random)
private val nip01 = Nip01(Secp256k1.get(), random)

@Test
fun conversationKeyTest() {
Expand All @@ -67,6 +69,34 @@ class NIP44v2Test {
}
}

@Test
fun testCompressedWith02Keys() {
val privateKeyA = "f410f88bcec6cbfda04d6a273c7b1dd8bba144cd45b71e87109cfa11dd7ed561".hexToByteArray()
val privateKeyB = "65f039136f8da8d3e87b4818746b53318d5481e24b2673f162815144223a0b5a".hexToByteArray()

val publicKeyA = nip01.pubkeyCreate(privateKeyA)
val publicKeyB = nip01.pubkeyCreate(privateKeyB)

assertEquals(
nip44v2.getConversationKey(privateKeyA, publicKeyB).toHexKey(),
nip44v2.getConversationKey(privateKeyB, publicKeyA).toHexKey(),
)
}

@Test
fun testCompressedWith03Keys() {
val privateKeyA = "f410f88bcec6cbfda04d6a273c7b1dd8bba144cd45b71e87109cfa11dd7ed561".hexToByteArray()
val privateKeyB = "e6159851715b4aa6190c22b899b0c792847de0a4435ac5b678f35738351c43b0".hexToByteArray()

val publicKeyA = nip01.pubkeyCreate(privateKeyA)
val publicKeyB = nip01.pubkeyCreate(privateKeyB)

assertEquals(
nip44v2.getConversationKey(privateKeyA, publicKeyB).toHexKey(),
nip44v2.getConversationKey(privateKeyB, publicKeyA).toHexKey(),
)
}

@Test
fun encryptDecryptTest() {
for (v in vectors.v2?.valid?.encryptDecrypt!!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class Nip01(
/** Provides a 32B "private key" aka random number */
fun privkeyCreate() = random(32)

fun pubkeyCreate(privKey: ByteArray) = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(privKey)).copyOfRange(1, 33)
fun compressedPubkeyCreate(privKey: ByteArray) = secp256k1.pubKeyCompress(secp256k1.pubkeyCreate(privKey))

fun pubkeyCreate(privKey: ByteArray) = compressedPubkeyCreate(privKey).copyOfRange(1, 33)

fun sign(
data: ByteArray,
Expand Down

0 comments on commit 143c3a1

Please sign in to comment.