mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-11-10 11:57:29 +01:00
- Creates a Quartz Module for Nostr Events
- Creates a Benchmark Module to test Performance - Migrates from GSon to Jackson for Performance Gains, adapts all serializers accordingly - Recreates Hex encoding/decoding classes for Performance. - Migrates NIP24 to the new ByteArray Concat encoding. - Removes support for Lenient choices in the. events. - Reorganizes Nostr Events dependencies. - Refactors TLV's and NIP-19 dependencies. - Adds a Large DB for signature checks.
This commit is contained in:
15
benchmark/src/androidTest/AndroidManifest.xml
Normal file
15
benchmark/src/androidTest/AndroidManifest.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<!--
|
||||
Important: disable debugging for accurate performance results
|
||||
|
||||
In a com.android.library project, this flag must be disabled from this
|
||||
manifest, as it is not possible to override this flag from Gradle.
|
||||
-->
|
||||
<application
|
||||
android:debuggable="false"
|
||||
tools:ignore="HardcodedDebugMode"
|
||||
tools:replace="android:debuggable" />
|
||||
</manifest>
|
||||
@@ -0,0 +1,122 @@
|
||||
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.events.Event
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import junit.framework.TestCase.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
*
|
||||
* The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
|
||||
* output the result. Modify your code to see how it affects performance.
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class EventBenchmark {
|
||||
|
||||
val payload1 = "[\"EVENT\",\"40b9\",{\"id\":\"48a72b485d38338627ec9d427583551f9af4f016c739b8ec0d6313540a8b12cf\",\"kind\":1,\"pubkey\":\"3d842afecd5e293f28b6627933704a3fb8ce153aa91d790ab11f6a752d44a42d\",\"created_at\":1677940007,\"content\":\"I got asked about follower count again today. Why does my follower count go down when I delete public relays (in our list) and replace them with filter.nostr.wine? \\n\\nI’ll give you one final explanation to rule them all. First, let’s go over how clients calculate your follower count.\\n\\n1. Your client sends a request to all your connected relays asking for accounts who follow you\\n2. Relays answer back with the events requested\\n3. The client aggregates the event total and displays it\\n\\nEach relay has a set limit on how many stored events it will return per request. For some relays it’s 500, others 1000, some as high as 5000. Let’s say for simplicity that all your public relays use 500 as their limit. If you ask 10 relays for your followers the max possible answer you can get is 5000. That won’t change if you have 20,000 followers or 100,000. You may get back a “different” 5000 each time, but you’ll still cap out at 5000 because that is the most events your client will receive.\u2028\u2028Our limit on filter.nostr.wine is 2000 events. If you replace 10 public relays with only filter.nostr.wine, the MOST followers you will ever get back from our filter relay is 2000. That doesn’t mean you only have 2000 followers or that your reach is reduced in any way.\\n\\nAs long as you are writing to and reading from the same public relays, neither your reach nor any content was lost. That concludes my TED talk. I hope you all have a fantastic day and weekend.\",\"tags\":[],\"sig\":\"dcaf8ab98bb9179017b35bd814092850d1062b26c263dff89fb1ae8c019a324139d1729012d9d05ff0a517f76b1117d869b2cc7d36bea8aa5f4b94c5e2548aa8\"}]"
|
||||
|
||||
val payload2 = """
|
||||
{
|
||||
"content": "Astral:\n\nhttps://void.cat/d/A5Fba5B1bcxwEmeyoD9nBs.webp\n\nIris:\n\nhttps://void.cat/d/44hTcVvhRps6xYYs99QsqA.webp\n\nSnort:\n\nhttps://void.cat/d/4nJD5TRePuQChM5tzteYbU.webp\n\nAmethyst agrees with Astral which I suspect are both wrong. nostr:npub13sx6fp3pxq5rl70x0kyfmunyzaa9pzt5utltjm0p8xqyafndv95q3saapa nostr:npub1v0lxxxxutpvrelsksy8cdhgfux9l6a42hsj2qzquu2zk7vc9qnkszrqj49 nostr:npub1g53mukxnjkcmr94fhryzkqutdz2ukq4ks0gvy5af25rgmwsl4ngq43drvk nostr:npub1gcxzte5zlkncx26j68ez60fzkvtkm9e0vrwdcvsjakxf9mu9qewqlfnj5z ",
|
||||
"created_at": 1683596206,
|
||||
"id": "98b574c3527f0ffb30b7271084e3f07480733c7289f8de424d29eae82e36c758",
|
||||
"kind": 1,
|
||||
"pubkey": "46fcbe3065eaf1ae7811465924e48923363ff3f526bd6f73d7c184b16bd8ce4d",
|
||||
"sig": "4aa5264965018fa12a326686ad3d3bd8beae3218dcc83689b19ca1e6baeb791531943c15363aa6707c7c0c8b2d601deca1f20c32078b2872d356cdca03b04cce",
|
||||
"tags": [
|
||||
["e","27ac621d7dc4a932e1a79f984308e7d20656dd6fddb2ce9cdfcb6a67b9a7bcc3","","root"],
|
||||
["e","be7245af96210a0dd048cab4ad38e52dbd6c09a53ea21a7edb6be8898e5727cc","","reply"],
|
||||
["p","22aa81510ee63fe2b16cae16e0921f78e9ba9882e2868e7e63ad6d08ae9b5954"],
|
||||
["p","22aa81510ee63fe2b16cae16e0921f78e9ba9882e2868e7e63ad6d08ae9b5954"],
|
||||
["p","3f770d65d3a764a9c5cb503ae123e62ec7598ad035d836e2a810f3877a745b24"],
|
||||
["p","ec4d241c334311b3a304433ee3442be29d0e88e7ec19b85edf2bba29b93565e2"],
|
||||
["p","0fe0b18b4dbf0e0aa40fcd47209b2a49b3431fc453b460efcf45ca0bd16bd6ac"],
|
||||
["p","8c0da4862130283ff9e67d889df264177a508974e2feb96de139804ea66d6168"],
|
||||
["p","63fe6318dc58583cfe16810f86dd09e18bfd76aabc24a0081ce2856f330504ed"],
|
||||
["p","4523be58d395b1b196a9b8c82b038b6895cb02b683d0c253a955068dba1facd0"],
|
||||
["p","460c25e682fda7832b52d1f22d3d22b3176d972f60dcdc3212ed8c92ef85065c"]
|
||||
],
|
||||
"seenOn": [
|
||||
"wss://nostr.wine/"
|
||||
]
|
||||
}
|
||||
"""
|
||||
|
||||
@get:Rule
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
@Test
|
||||
fun parseREQString() {
|
||||
benchmarkRule.measureRepeated {
|
||||
Event.mapper.readTree(payload1)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseEvent() {
|
||||
val msg = Event.mapper.readTree(payload1)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
Event.fromJson(msg[2])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkSignature() {
|
||||
val msg = Event.mapper.readTree(payload1)
|
||||
val event = Event.fromJson(msg[2])
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue( event.hasVerifedSignature() )
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkIDHashPayload1() {
|
||||
val msg = Event.mapper.readTree(payload1)
|
||||
val event = Event.fromJson(msg[2])
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue( event.hasCorrectIDHash() )
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkIDHashPayload2() {
|
||||
val event = Event.fromJson(payload2)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue( event.hasCorrectIDHash() )
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun toMakeJsonForID() {
|
||||
val event = Event.fromJson(payload2)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(event.makeJsonForId())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256() {
|
||||
val event = Event.fromJson(payload2)
|
||||
val byteArray = event.makeJsonForId().toByteArray()
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertNotNull(CryptoUtils.sha256(byteArray))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
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.encoders.toHexKey
|
||||
import com.vitorpamplona.quartz.crypto.KeyPair
|
||||
import com.vitorpamplona.quartz.events.NIP24Factory
|
||||
import com.vitorpamplona.quartz.events.SealedGossipEvent
|
||||
import org.junit.Assert
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
*
|
||||
* The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
|
||||
* output the result. Modify your code to see how it affects performance.
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class GiftWrapBenchmark {
|
||||
|
||||
@get:Rule
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
fun basePerformanceTest(message: String, expectedLength: Int) {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val events = NIP24Factory().createMsgNIP24(
|
||||
message,
|
||||
listOf(receiver.pubKey.toHexKey()),
|
||||
sender.privKey!!
|
||||
)
|
||||
|
||||
Assert.assertEquals(expectedLength, events.map { it.toJson() }.joinToString("").length)
|
||||
|
||||
// Simulate Receiver
|
||||
events.forEach {
|
||||
it.checkSignature()
|
||||
|
||||
val keyToUse = if (it.recipientPubKey() == sender.pubKey.toHexKey()) sender.privKey!! else receiver.privKey!!
|
||||
val event = it.unwrap(keyToUse)
|
||||
event?.checkSignature()
|
||||
|
||||
if (event is SealedGossipEvent) {
|
||||
val innerData = event.unseal(keyToUse)
|
||||
Assert.assertEquals(message, innerData?.content)
|
||||
} else {
|
||||
Assert.fail("Wrong Event")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun tinyMessageHardCoded() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 2946)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageHardCoded() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 3098)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageHardCoded() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
3738
|
||||
)
|
||||
}
|
||||
}
|
||||
/*
|
||||
@Test
|
||||
fun tinyMessageHardCodedCompressed() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 2318)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageHardCodedCompressed() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 2406)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageHardCodedCompressed() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
2722
|
||||
)
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
@Test
|
||||
fun tinyMessageJSONCompressed() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 2318)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageJSONCompressed() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 2394)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageJSONCompressed() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
2714
|
||||
)
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
@Test
|
||||
fun tinyMessageJSON() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 3154)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageJSON() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 3298)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageJSON() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
3938
|
||||
)
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
@Test
|
||||
fun tinyMessageJackson() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 3154)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageJackson() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 3298)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageJackson() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
3938
|
||||
)
|
||||
}
|
||||
} */
|
||||
/*
|
||||
@Test
|
||||
fun tinyMessageKotlin() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 3154)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageKotlin() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 3298)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageKotlin() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
3938
|
||||
)
|
||||
}
|
||||
}*/
|
||||
/*
|
||||
@Test
|
||||
fun tinyMessageCSV() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hola, que tal?", 2960)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun regularMessageCSV() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest("Hi, honey, can you drop by the market and get some bread?", 3112)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun longMessageCSV() {
|
||||
benchmarkRule.measureRepeated {
|
||||
basePerformanceTest(
|
||||
"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. ",
|
||||
3752
|
||||
)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
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.crypto.decodeNIP44
|
||||
import com.vitorpamplona.quartz.encoders.hexToByteArray
|
||||
import com.vitorpamplona.quartz.events.ChatMessageEvent
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.events.GiftWrapEvent
|
||||
import com.vitorpamplona.quartz.events.Gossip
|
||||
import com.vitorpamplona.quartz.events.SealedGossipEvent
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
*
|
||||
* The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
|
||||
* output the result. Modify your code to see how it affects performance.
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class GiftWrapReceivingBenchmark {
|
||||
|
||||
@get:Rule
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
fun createMessage(sender: KeyPair, receiver: KeyPair): ChatMessageEvent {
|
||||
val to = listOf(receiver.pubKey.toHexKey())
|
||||
|
||||
return ChatMessageEvent.create(
|
||||
msg = "Hi there! This is a test message",
|
||||
to = to,
|
||||
privateKey = sender.privKey!!,
|
||||
subject = "Party Tonight",
|
||||
replyTos = emptyList(),
|
||||
mentions = emptyList(),
|
||||
zapReceiver = null,
|
||||
markAsSensitive = true,
|
||||
zapRaiserAmount = 10000,
|
||||
geohash = null
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseWrapFromString() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val wrap = GiftWrapEvent.create(
|
||||
event = seal,
|
||||
recipientPubKey = senderPublicKey
|
||||
)
|
||||
|
||||
val str = wrap.toJson()
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
Event.fromJson(str)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun decodeWrapEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val wrappedEvent = GiftWrapEvent.create(
|
||||
event = seal,
|
||||
recipientPubKey = senderPublicKey
|
||||
)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(decodeNIP44(wrappedEvent.content))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun decryptWrapEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val wrappedEvent = GiftWrapEvent.create(
|
||||
event = seal,
|
||||
recipientPubKey = senderPublicKey
|
||||
)
|
||||
|
||||
val toDecrypt = decodeNIP44(wrappedEvent.content) ?: return
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(CryptoUtils.decryptNIP24(toDecrypt, sender.privKey!!, wrappedEvent.pubKey.hexToByteArray()))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseWrappedEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val wrappedEvent = GiftWrapEvent.create(
|
||||
event = seal,
|
||||
recipientPubKey = senderPublicKey
|
||||
)
|
||||
|
||||
val toDecrypt = decodeNIP44(wrappedEvent.content) ?: return
|
||||
val innerJson = CryptoUtils.decryptNIP24(toDecrypt, sender.privKey!!, wrappedEvent.pubKey.hexToByteArray())
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(innerJson?.let { Event.fromJson(it) })
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun decodeSealEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(decodeNIP44(seal.content))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun decryptSealedEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val toDecrypt = decodeNIP44(seal.content) ?: return
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(CryptoUtils.decryptNIP24(toDecrypt, sender.privKey!!, seal.pubKey.hexToByteArray()))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseSealedEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val toDecrypt = decodeNIP44(seal.content) ?: return
|
||||
val innerJson = CryptoUtils.decryptNIP24(toDecrypt, sender.privKey!!, seal.pubKey.hexToByteArray())
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(innerJson?.let { Gossip.fromJson(it) })
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
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.ChatMessageEvent
|
||||
import com.vitorpamplona.quartz.events.Event
|
||||
import com.vitorpamplona.quartz.events.GiftWrapEvent
|
||||
import com.vitorpamplona.quartz.events.Gossip
|
||||
import com.vitorpamplona.quartz.events.SealedGossipEvent
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import junit.framework.TestCase.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
*
|
||||
* The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
|
||||
* output the result. Modify your code to see how it affects performance.
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class GiftWrapSigningBenchmark {
|
||||
|
||||
@get:Rule
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
fun createMessage(sender: KeyPair, receiver: KeyPair): ChatMessageEvent {
|
||||
val to = listOf(receiver.pubKey.toHexKey())
|
||||
|
||||
return ChatMessageEvent.create(
|
||||
msg = "Hi there! This is a test message",
|
||||
to = to,
|
||||
privateKey = sender.privKey!!,
|
||||
subject = "Party Tonight",
|
||||
replyTos = emptyList(),
|
||||
mentions = emptyList(),
|
||||
zapReceiver = null,
|
||||
markAsSensitive = true,
|
||||
zapRaiserAmount = 10000,
|
||||
geohash = null
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun createMessageEvent() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
createMessage(sender, receiver)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sealMessage() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wrapSeal() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
GiftWrapEvent.create(
|
||||
event = seal,
|
||||
recipientPubKey = senderPublicKey
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wrapToString() {
|
||||
val sender = KeyPair()
|
||||
val receiver = KeyPair()
|
||||
|
||||
val senderPublicKey = CryptoUtils.pubkeyCreate(sender.privKey!!).toHexKey()
|
||||
|
||||
val senderMessage = createMessage(sender, receiver)
|
||||
|
||||
val seal = SealedGossipEvent.create(
|
||||
event = senderMessage,
|
||||
encryptTo = senderPublicKey,
|
||||
privateKey = sender.privKey!!
|
||||
)
|
||||
|
||||
val wrap = GiftWrapEvent.create(
|
||||
event = seal,
|
||||
recipientPubKey = senderPublicKey
|
||||
)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
wrap.toJson()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.vitorpamplona.amethyst.benchmark
|
||||
|
||||
import androidx.benchmark.junit4.BenchmarkRule
|
||||
import androidx.benchmark.junit4.measureRepeated
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import junit.framework.TestCase.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
*
|
||||
* The body of [BenchmarkRule.measureRepeated] is measured in a loop, and Studio will
|
||||
* output the result. Modify your code to see how it affects performance.
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class HexBenchmark {
|
||||
|
||||
@get:Rule
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
val TestHex = "48a72b485d38338627ec9d427583551f9af4f016c739b8ec0d6313540a8b12cf"
|
||||
|
||||
@Test
|
||||
fun hexDecodeOurs() {
|
||||
benchmarkRule.measureRepeated {
|
||||
com.vitorpamplona.quartz.encoders.Hex.decode(TestHex)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hexEncodeOurs() {
|
||||
val bytes = com.vitorpamplona.quartz.encoders.Hex.decode(TestHex)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertEquals(TestHex, com.vitorpamplona.quartz.encoders.Hex.encode(bytes))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hexDecodeBaseSecp() {
|
||||
benchmarkRule.measureRepeated {
|
||||
fr.acinq.secp256k1.Hex.decode(TestHex)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hexEncodeBaseSecp() {
|
||||
val bytes = fr.acinq.secp256k1.Hex.decode(TestHex)
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertEquals(TestHex, fr.acinq.secp256k1.Hex.encode(bytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
2
benchmark/src/main/AndroidManifest.xml
Normal file
2
benchmark/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest />
|
||||
Reference in New Issue
Block a user