Decoupling NIP01 methods from CryptoUtils

Decoupling Encryption and Decryptions from CryptoUtils
Decoupling live instances of JNI bindings for Secp and LibSodium from CryptoUtils
Decoupling key cache from CryptoUtils
Reorganizes NIP-04 to match new package structure
Adjusts test structures to match
This commit is contained in:
Vitor Pamplona
2025-02-17 19:13:05 -05:00
parent 7ad8b2ce46
commit 6d9964a1cb
89 changed files with 1048 additions and 1015 deletions
@@ -65,11 +65,20 @@ class EventBenchmark {
}
@Test
fun eventFactoryPerformanceTest() {
fun eventFactoryKind1PerformanceTest() {
val now = TimeUtils.now()
val tags = arrayOf(arrayOf(""))
benchmarkRule.measureRepeated {
EventFactory.create("id", "pubkey", now, 1, tags, "content", "sig")
}
}
@Test
fun eventFactoryKind30818PerformanceTest() {
val now = TimeUtils.now()
val tags = arrayOf(arrayOf(""))
benchmarkRule.measureRepeated {
EventFactory.create("id", "pubkey", now, 30818, tags, "content", "sig")
}
}
}
@@ -23,7 +23,6 @@ package com.vitorpamplona.quartz.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.CryptoUtils
import com.vitorpamplona.quartz.nip01Core.checkSignature
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
@@ -132,7 +131,6 @@ class GiftWrapBenchmark {
// Simulate Receiver
benchmarkRule.measureRepeated {
CryptoUtils.clearCache()
val counter = CountDownLatch(1)
val wrap = Event.fromJson(giftWrapJson) as GiftWrapEvent
@@ -23,7 +23,6 @@ package com.vitorpamplona.quartz.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.CryptoUtils
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.hexToByteArray
@@ -35,6 +34,7 @@ import com.vitorpamplona.quartz.nip01Core.verifySignature
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
import com.vitorpamplona.quartz.nip17Dm.messages.changeSubject
import com.vitorpamplona.quartz.nip36SensitiveContent.contentWarning
import com.vitorpamplona.quartz.nip44Encryption.Nip44
import com.vitorpamplona.quartz.nip57Zaps.zapraiser.zapraiser
import com.vitorpamplona.quartz.nip59Giftwrap.rumors.Rumor
import com.vitorpamplona.quartz.nip59Giftwrap.seals.SealedRumorEvent
@@ -171,7 +171,7 @@ class GiftWrapReceivingBenchmark {
benchmarkRule.measureRepeated {
assertNotNull(
CryptoUtils.decryptNIP44(
Nip44.decrypt(
wrap.content,
receiver.keyPair.privKey!!,
wrap.pubKey.hexToByteArray(),
@@ -188,7 +188,7 @@ class GiftWrapReceivingBenchmark {
val wrap = createWrap(sender, receiver)
val innerJson =
CryptoUtils.decryptNIP44(
Nip44.decrypt(
wrap.content,
receiver.keyPair.privKey!!,
wrap.pubKey.hexToByteArray(),
@@ -206,7 +206,7 @@ class GiftWrapReceivingBenchmark {
benchmarkRule.measureRepeated {
assertNotNull(
CryptoUtils.decryptNIP44(
Nip44.decrypt(
seal.content,
receiver.keyPair.privKey!!,
seal.pubKey.hexToByteArray(),
@@ -223,7 +223,7 @@ class GiftWrapReceivingBenchmark {
val seal = createSeal(sender, receiver)
val innerJson =
CryptoUtils.decryptNIP44(
Nip44.decrypt(
seal.content,
receiver.keyPair.privKey!!,
seal.pubKey.hexToByteArray(),
@@ -25,7 +25,7 @@ import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.nip01Core.EventHasher
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.utils.sha256Hash
import com.vitorpamplona.quartz.utils.sha256
import junit.framework.TestCase.assertNotNull
import org.junit.Rule
import org.junit.Test
@@ -49,7 +49,7 @@ class Sha256Benchmark {
benchmarkRule.measureRepeated {
// Should pass
assertNotNull(sha256Hash(byteArray))
assertNotNull(sha256(byteArray))
}
}
}
@@ -23,15 +23,17 @@ package com.vitorpamplona.quartz.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.CryptoUtils
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip04Dm.crypto.Encryption
import com.vitorpamplona.quartz.nip04Dm.crypto.Nip04
import com.vitorpamplona.quartz.nip44Encryption.Nip44v2
import junit.framework.TestCase.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class CryptoBenchmark {
class SharedKeyBenchmark {
@get:Rule val benchmarkRule = BenchmarkRule()
@Test
@@ -40,7 +42,7 @@ class CryptoBenchmark {
val keyPair2 = KeyPair()
benchmarkRule.measureRepeated {
assertNotNull(CryptoUtils.getSharedSecretNIP04(keyPair1.privKey!!, keyPair2.pubKey))
assertNotNull(Nip04.getSharedSecret(keyPair1.privKey!!, keyPair2.pubKey))
}
}
@@ -48,9 +50,10 @@ class CryptoBenchmark {
fun getSharedKeyNip44() {
val keyPair1 = KeyPair()
val keyPair2 = KeyPair()
val nip44v2 = Nip44v2()
benchmarkRule.measureRepeated {
assertNotNull(CryptoUtils.nip44.v1.getSharedSecret(keyPair1.privKey!!, keyPair2.pubKey))
assertNotNull(nip44v2.getConversationKey(keyPair1.privKey!!, keyPair2.pubKey))
}
}
@@ -58,9 +61,10 @@ class CryptoBenchmark {
fun computeSharedKeyNip04() {
val keyPair1 = KeyPair()
val keyPair2 = KeyPair()
val nip04 = Encryption()
benchmarkRule.measureRepeated {
assertNotNull(CryptoUtils.computeSharedSecretNIP04(keyPair1.privKey!!, keyPair2.pubKey))
assertNotNull(nip04.computeSharedSecret(keyPair1.privKey!!, keyPair2.pubKey))
}
}
@@ -68,40 +72,10 @@ class CryptoBenchmark {
fun computeSharedKeyNip44() {
val keyPair1 = KeyPair()
val keyPair2 = KeyPair()
val nip44v2 = Nip44v2()
benchmarkRule.measureRepeated {
assertNotNull(CryptoUtils.nip44.v1.computeSharedSecret(keyPair1.privKey!!, keyPair2.pubKey))
}
}
@Test
fun random() {
benchmarkRule.measureRepeated { assertNotNull(CryptoUtils.random(1000)) }
}
@Test
fun sha256() {
val keyPair = KeyPair()
benchmarkRule.measureRepeated { assertNotNull(CryptoUtils.sha256(keyPair.pubKey)) }
}
@Test
fun sign() {
val keyPair = KeyPair()
val msg = CryptoUtils.sha256(CryptoUtils.random(1000))
benchmarkRule.measureRepeated { assertNotNull(CryptoUtils.sign(msg, keyPair.privKey!!)) }
}
@Test
fun verify() {
val keyPair = KeyPair()
val msg = CryptoUtils.sha256(CryptoUtils.random(1000))
val signature = CryptoUtils.sign(msg, keyPair.privKey!!)
benchmarkRule.measureRepeated {
assertNotNull(CryptoUtils.verifySignature(signature, msg, keyPair.pubKey))
assertNotNull(nip44v2.computeConversationKey(keyPair1.privKey!!, keyPair2.pubKey))
}
}
}
@@ -0,0 +1,62 @@
/**
* Copyright (c) 2024 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.benchmark
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.quartz.nip01Core.crypto.Nip01
import com.vitorpamplona.quartz.utils.RandomInstance
import com.vitorpamplona.quartz.utils.sha256
import junit.framework.TestCase.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class SignVerifyBenchmark {
@get:Rule val benchmarkRule = BenchmarkRule()
@Test
fun random() {
benchmarkRule.measureRepeated { assertNotNull(RandomInstance.bytes(1000)) }
}
@Test
fun sign() {
val keyPair = KeyPair()
val msg = sha256(RandomInstance.bytes(1000))
benchmarkRule.measureRepeated { assertNotNull(Nip01.sign(msg, keyPair.privKey!!)) }
}
@Test
fun verify() {
val keyPair = KeyPair()
val msg = sha256(RandomInstance.bytes(1000))
val signature = Nip01.sign(msg, keyPair.privKey!!)
benchmarkRule.measureRepeated {
assertNotNull(Nip01.verify(signature, msg, keyPair.pubKey))
}
}
}