From 8dbded0185c5ec6257d5a1cec09e323c9840e14d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 18 Aug 2023 11:52:54 -0400 Subject: [PATCH] Single method to test performance of receiving a GiftWrap. --- .../amethyst/benchmark/GiftWrapBenchmark.kt | 54 +++++++++++++++++++ .../benchmark/GiftWrapReceivingBenchmark.kt | 44 +++++++++++++++ .../quartz/crypto/CryptoUtils.kt | 5 ++ 3 files changed, 103 insertions(+) diff --git a/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapBenchmark.kt b/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapBenchmark.kt index c63d2c58e..e9f6ffe7a 100644 --- a/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapBenchmark.kt +++ b/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapBenchmark.kt @@ -3,8 +3,11 @@ package com.vitorpamplona.amethyst.benchmark import androidx.benchmark.junit4.BenchmarkRule import androidx.benchmark.junit4.measureRepeated import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.vitorpamplona.quartz.crypto.CryptoUtils import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.crypto.KeyPair +import com.vitorpamplona.quartz.events.Event +import com.vitorpamplona.quartz.events.GiftWrapEvent import com.vitorpamplona.quartz.events.NIP24Factory import com.vitorpamplona.quartz.events.SealedGossipEvent import org.junit.Assert @@ -53,6 +56,38 @@ class GiftWrapBenchmark { } } + fun receivePerformanceTest(message: String) { + val sender = KeyPair() + val receiver = KeyPair() + + val giftWrap = NIP24Factory().createMsgNIP24( + message, + listOf(receiver.pubKey.toHexKey()), + sender.privKey!! + ).first() + + val keyToUse = if (giftWrap.recipientPubKey() == sender.pubKey.toHexKey()) sender.privKey!! else receiver.privKey!! + val giftWrapJson = giftWrap.toJson() + + // Simulate Receiver + benchmarkRule.measureRepeated { + CryptoUtils.clearCache() + + val wrap = Event.fromJson(giftWrapJson) as GiftWrapEvent + wrap.checkSignature() + + val seal = wrap.unwrap(keyToUse) + seal?.checkSignature() + + if (seal is SealedGossipEvent) { + val innerData = seal.unseal(keyToUse) + Assert.assertEquals(message, innerData?.content) + } else { + Assert.fail("Wrong Event") + } + } + } + @Test fun tinyMessageHardCoded() { @@ -77,6 +112,25 @@ class GiftWrapBenchmark { ) } } + + @Test + fun receivesTinyMessage() { + receivePerformanceTest("Hola, que tal?") + } + + @Test + fun receivesRegularMessage() { + receivePerformanceTest("Hi, honey, can you drop by the market and get some bread?") + } + + @Test + fun receivesLongMessageHardCoded() { + receivePerformanceTest( + "My queen, you are nothing short of royalty to me. You possess more beauty in the nail of your pinkie toe than everything else in this world combined. I am astounded by your grace, generosity, and graciousness. I am so lucky to know you. " + ) + } + + /* @Test fun tinyMessageHardCodedCompressed() { diff --git a/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapReceivingBenchmark.kt b/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapReceivingBenchmark.kt index 10cd3307c..f4f634346 100644 --- a/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapReceivingBenchmark.kt +++ b/benchmark/src/androidTest/java/com/vitorpamplona/amethyst/benchmark/GiftWrapReceivingBenchmark.kt @@ -74,6 +74,50 @@ class GiftWrapReceivingBenchmark { } } + @Test + fun checkId() { + val sender = KeyPair() + val receiver = KeyPair() + + val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey() + val senderMessage = createMessage(sender, receiver) + + val wrap = GiftWrapEvent.create( + event = SealedGossipEvent.create( + event = senderMessage, + encryptTo = senderPublicKey, + privateKey = sender.privKey!! + ), + recipientPubKey = senderPublicKey + ) + + benchmarkRule.measureRepeated { + wrap.hasCorrectIDHash() + } + } + + @Test + fun checkSignature() { + val sender = KeyPair() + val receiver = KeyPair() + + val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey() + val senderMessage = createMessage(sender, receiver) + + val wrap = GiftWrapEvent.create( + event = SealedGossipEvent.create( + event = senderMessage, + encryptTo = senderPublicKey, + privateKey = sender.privKey!! + ), + recipientPubKey = senderPublicKey + ) + + benchmarkRule.measureRepeated { + wrap.hasVerifedSignature() + } + } + @Test fun decodeWrapEvent() { val sender = KeyPair() diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt index 9d479675b..b2b3a89ac 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/crypto/CryptoUtils.kt @@ -24,6 +24,11 @@ object CryptoUtils { private val random = SecureRandom() private val h02 = Hex.decode("02") + fun clearCache() { + sharedKeyCache04.evictAll() + sharedKeyCache24.evictAll() + } + fun randomInt(bound: Int): Int { return random.nextInt(bound) }