mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-03-17 21:31:57 +01:00
Moves Sha256 to a pool of digests to double the hashing performance
This commit is contained in:
parent
fad08830a1
commit
9c35008fc4
@ -35,7 +35,7 @@ import coil3.request.ImageRequest
|
||||
import coil3.request.Options
|
||||
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser.Companion.base64contentPattern
|
||||
import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.util.Base64
|
||||
|
||||
@Stable
|
||||
|
@ -25,7 +25,7 @@ import android.net.Uri
|
||||
import androidx.core.net.toUri
|
||||
import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.nip17Dm.files.encryption.NostrCipher
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.io.File
|
||||
|
||||
class EncryptFilesResult(
|
||||
|
@ -32,7 +32,7 @@ import com.vitorpamplona.amethyst.commons.blurhash.toBlurhash
|
||||
import com.vitorpamplona.amethyst.service.Blurhash
|
||||
import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.nip94FileMetadata.tags.DimensionTag
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import java.io.IOException
|
||||
|
||||
|
@ -38,7 +38,7 @@ import com.vitorpamplona.amethyst.ui.stringRes
|
||||
import com.vitorpamplona.quartz.blossom.BlossomAuthorizationEvent
|
||||
import com.vitorpamplona.quartz.nip01Core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
|
@ -97,7 +97,7 @@ import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.nip19Bech32.Nip19Parser
|
||||
import com.vitorpamplona.quartz.nip19Bech32.entities.NEvent
|
||||
import com.vitorpamplona.quartz.nip94FileMetadata.tags.DimensionTag
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.persistentListOf
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
@ -26,6 +26,8 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.vitorpamplona.quartz.EventFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
import com.vitorpamplona.quartz.nip01Core.verify
|
||||
import com.vitorpamplona.quartz.nip01Core.verifyId
|
||||
import com.vitorpamplona.quartz.nip01Core.verifySignature
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import junit.framework.TestCase.assertTrue
|
||||
import org.junit.Rule
|
||||
@ -42,6 +44,15 @@ import org.junit.runner.RunWith
|
||||
class EventBenchmark {
|
||||
@get:Rule val benchmarkRule = BenchmarkRule()
|
||||
|
||||
@Test
|
||||
fun parseComplete() {
|
||||
benchmarkRule.measureRepeated {
|
||||
val tree = EventMapper.mapper.readTree(reqResponseEvent)
|
||||
val event = EventMapper.fromJson(tree[2])
|
||||
assertTrue(event.verify())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun parseREQString() {
|
||||
benchmarkRule.measureRepeated { EventMapper.mapper.readTree(reqResponseEvent) }
|
||||
@ -54,13 +65,23 @@ class EventBenchmark {
|
||||
benchmarkRule.measureRepeated { EventMapper.fromJson(msg[2]) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkId() {
|
||||
val msg = EventMapper.mapper.readTree(reqResponseEvent)
|
||||
val event = EventMapper.fromJson(msg[2])
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue(event.verifyId())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkSignature() {
|
||||
val msg = EventMapper.mapper.readTree(reqResponseEvent)
|
||||
val event = EventMapper.fromJson(msg[2])
|
||||
benchmarkRule.measureRepeated {
|
||||
// Should pass
|
||||
assertTrue(event.verify())
|
||||
assertTrue(event.verifySignature())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -25,11 +25,12 @@ 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.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.security.MessageDigest
|
||||
|
||||
/**
|
||||
* Benchmark, which will execute on an Android device.
|
||||
@ -43,7 +44,7 @@ class Sha256Benchmark {
|
||||
val benchmarkRule = BenchmarkRule()
|
||||
|
||||
@Test
|
||||
fun sha256() {
|
||||
fun sha256Pool() {
|
||||
val event = Event.fromJson(largeKind1Event)
|
||||
val byteArray = EventHasher.makeJsonForId(event.pubKey, event.createdAt, event.kind, event.tags, event.content).toByteArray()
|
||||
|
||||
@ -52,4 +53,28 @@ class Sha256Benchmark {
|
||||
assertNotNull(sha256(byteArray))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256NewEachTime() {
|
||||
val event = Event.fromJson(largeKind1Event)
|
||||
val byteArray = EventHasher.makeJsonForId(event.pubKey, event.createdAt, event.kind, event.tags, event.content).toByteArray()
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
assertNotNull(digest.digest(byteArray))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sha256Reuse() {
|
||||
val event = Event.fromJson(largeKind1Event)
|
||||
val byteArray = EventHasher.makeJsonForId(event.pubKey, event.createdAt, event.kind, event.tags, event.content).toByteArray()
|
||||
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
|
||||
benchmarkRule.measureRepeated {
|
||||
assertNotNull(digest.digest(byteArray))
|
||||
digest.reset()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ 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 com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import junit.framework.TestCase.assertNotNull
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
|
@ -86,7 +86,7 @@ import com.vitorpamplona.amethyst.commons.robohash.parts.mouth7Happy
|
||||
import com.vitorpamplona.amethyst.commons.robohash.parts.mouth8Buttons
|
||||
import com.vitorpamplona.amethyst.commons.robohash.parts.mouth9Closed
|
||||
import com.vitorpamplona.quartz.utils.Hex
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
val Black = SolidColor(Color.Black)
|
||||
val Gray = SolidColor(Color(0xFF6d6e70))
|
||||
|
@ -20,40 +20,46 @@
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip01Core
|
||||
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.EventMapper
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
class EventHasher {
|
||||
companion object {
|
||||
fun makeJsonObjectForId(
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
kind: Int,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
): ArrayNode {
|
||||
val factory = JsonNodeFactory.instance
|
||||
return factory.arrayNode(6).apply {
|
||||
add(0)
|
||||
add(pubKey)
|
||||
add(createdAt)
|
||||
add(kind)
|
||||
add(
|
||||
factory.arrayNode(tags.size).apply {
|
||||
tags.forEach { tag ->
|
||||
add(
|
||||
factory.arrayNode(tag.size).apply { tag.forEach { add(it) } },
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
add(content)
|
||||
}
|
||||
}
|
||||
|
||||
fun makeJsonForId(
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
kind: Int,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
): String {
|
||||
val factory = JsonNodeFactory.instance
|
||||
val rawEvent =
|
||||
factory.arrayNode(6).apply {
|
||||
add(0)
|
||||
add(pubKey)
|
||||
add(createdAt)
|
||||
add(kind)
|
||||
add(
|
||||
factory.arrayNode(tags.size).apply {
|
||||
tags.forEach { tag ->
|
||||
add(
|
||||
factory.arrayNode(tag.size).apply { tag.forEach { add(it) } },
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
add(content)
|
||||
}
|
||||
|
||||
return EventMapper.toJson(rawEvent)
|
||||
}
|
||||
): String = EventMapper.toJson(makeJsonObjectForId(pubKey, createdAt, kind, tags, content))
|
||||
|
||||
fun hashIdBytes(
|
||||
pubKey: HexKey,
|
||||
|
@ -54,7 +54,6 @@ data class Address(
|
||||
Address(parts[0].toInt(), parts[1], parts[2])
|
||||
} else {
|
||||
Log.w("AddressableId", "Error parsing. Pubkey is not hex: $addressId")
|
||||
throw RuntimeException("It shouldn't get here.")
|
||||
null
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
|
@ -21,7 +21,7 @@
|
||||
package com.vitorpamplona.quartz.nip06KeyDerivation
|
||||
|
||||
import com.vitorpamplona.quartz.nip49PrivKeyEnc.PBKDF
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
// CODE FROM: https://github.com/ACINQ/bitcoin-kmp/
|
||||
|
||||
|
@ -24,7 +24,7 @@ import android.util.Log
|
||||
import com.vitorpamplona.quartz.utils.LibSodiumInstance
|
||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||
import com.vitorpamplona.quartz.utils.Secp256k1Instance
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.util.Base64
|
||||
|
||||
class Nip44v1 {
|
||||
|
@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip55AndroidSigner
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.crypto.Nip01
|
||||
import com.vitorpamplona.quartz.utils.RandomInstance
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
fun signString(
|
||||
message: String,
|
||||
|
@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip57Zaps
|
||||
|
||||
import com.vitorpamplona.quartz.nip04Dm.crypto.Nip04
|
||||
import com.vitorpamplona.quartz.nip19Bech32.bech32.Bech32
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.nio.charset.Charset
|
||||
import java.security.SecureRandom
|
||||
import javax.crypto.BadPaddingException
|
||||
|
@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip98HttpAuth.tags
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.HexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.toHexKey
|
||||
import com.vitorpamplona.quartz.utils.sha256
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
|
||||
class PayloadHashTag {
|
||||
companion object {
|
||||
|
@ -18,11 +18,8 @@
|
||||
* 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.utils
|
||||
package com.vitorpamplona.quartz.utils.sha256
|
||||
|
||||
import java.security.MessageDigest
|
||||
val pool = Sha256Pool(5) // max parallel operations
|
||||
|
||||
fun sha256(data: ByteArray): ByteArray {
|
||||
// Creates a new buffer every time
|
||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
||||
}
|
||||
fun sha256(data: ByteArray) = pool.hash(data)
|
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* 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.utils.sha256
|
||||
|
||||
import android.util.Log
|
||||
import java.security.MessageDigest
|
||||
import java.util.concurrent.ArrayBlockingQueue
|
||||
|
||||
class Sha256Pool(
|
||||
size: Int,
|
||||
) {
|
||||
private val pool = ArrayBlockingQueue<MessageDigest>(size)
|
||||
|
||||
private fun digest() = MessageDigest.getInstance("SHA-256")
|
||||
|
||||
init {
|
||||
repeat(size) {
|
||||
pool.add(digest())
|
||||
}
|
||||
}
|
||||
|
||||
private fun acquire(): MessageDigest {
|
||||
if (pool.size < 1) {
|
||||
Log.w("SHA256Pool", "Pool running low in available digests")
|
||||
}
|
||||
return pool.take()
|
||||
}
|
||||
|
||||
private fun release(digest: MessageDigest) {
|
||||
digest.reset()
|
||||
pool.put(digest)
|
||||
}
|
||||
|
||||
fun hash(byteArray: ByteArray): ByteArray {
|
||||
val digest = acquire()
|
||||
try {
|
||||
return digest.digest(byteArray)
|
||||
} finally {
|
||||
release(digest)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user