From 617db3613fa262f78422672d71989daf1242f94a Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 25 Mar 2026 11:17:17 -0400 Subject: [PATCH] Re-enables benchmark with native android ChaCha functions --- .../quartz/benchmark/ChaCha20Benchmark.kt | 33 ++++++++++++++----- .../quartz/nip44Encryption/crypto/ChaCha20.kt | 22 ------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/ChaCha20Benchmark.kt b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/ChaCha20Benchmark.kt index 296a8927f..407e8292f 100644 --- a/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/ChaCha20Benchmark.kt +++ b/benchmark/src/androidTest/java/com/vitorpamplona/quartz/benchmark/ChaCha20Benchmark.kt @@ -27,9 +27,12 @@ import com.vitorpamplona.quartz.nip01Core.crypto.Nip01Crypto import com.vitorpamplona.quartz.nip44Encryption.Nip44v2 import com.vitorpamplona.quartz.nip44Encryption.crypto.ChaCha20 import com.vitorpamplona.quartz.utils.RandomInstance +import com.vitorpamplona.quartz.utils.mac.FixedKey import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import javax.crypto.Cipher +import javax.crypto.spec.IvParameterSpec @RunWith(AndroidJUnit4::class) class ChaCha20Benchmark { @@ -58,15 +61,12 @@ class ChaCha20Benchmark { } } - /* - Removed in the conversion to KMP @Test fun encryptNative() { benchmarkRule.measureRepeated { - chaCha.encryptNative(padded, messageKeys.chachaNonce, messageKeys.chachaKey) + encryptNative(padded, messageKeys.chachaNonce, messageKeys.chachaKey) } } - */ @Test fun decryptLibSodium() { @@ -75,13 +75,30 @@ class ChaCha20Benchmark { } } - /* - Removed in the conversion to KMP @Test fun decryptNative() { benchmarkRule.measureRepeated { - chaCha.decryptNative(padded, messageKeys.chachaNonce, messageKeys.chachaKey) + decryptNative(padded, messageKeys.chachaNonce, messageKeys.chachaKey) } } - */ + + fun encryptNative( + message: ByteArray, + nonce: ByteArray, + key: ByteArray, + ): ByteArray { + val cipher = Cipher.getInstance("ChaCha20") + cipher.init(Cipher.ENCRYPT_MODE, FixedKey(key, "ChaCha20"), IvParameterSpec(nonce)) + return cipher.doFinal(message) + } + + fun decryptNative( + message: ByteArray, + nonce: ByteArray, + key: ByteArray, + ): ByteArray { + val cipher = Cipher.getInstance("ChaCha20") + cipher.init(Cipher.DECRYPT_MODE, FixedKey(key, "ChaCha20"), IvParameterSpec(nonce)) + return cipher.doFinal(message) + } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip44Encryption/crypto/ChaCha20.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip44Encryption/crypto/ChaCha20.kt index 3fe570a86..7dd451ed5 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip44Encryption/crypto/ChaCha20.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip44Encryption/crypto/ChaCha20.kt @@ -38,28 +38,6 @@ class ChaCha20 { key: ByteArray, ) = decryptLibSodium(message, nonce, key) - /* - fun encryptNative( - message: ByteArray, - nonce: ByteArray, - key: ByteArray, - ): ByteArray { - val cipher = Cipher.getInstance("ChaCha20") - cipher.init(Cipher.ENCRYPT_MODE, FixedKey(key, "ChaCha20"), IvParameterSpec(nonce)) - return cipher.doFinal(message) - } - - fun decryptNative( - message: ByteArray, - nonce: ByteArray, - key: ByteArray, - ): ByteArray { - val cipher = Cipher.getInstance("ChaCha20") - cipher.init(Cipher.DECRYPT_MODE, FixedKey(key, "ChaCha20"), IvParameterSpec(nonce)) - return cipher.doFinal(message) - } - */ - fun encryptLibSodium( message: ByteArray, nonce: ByteArray,