Skip to content

Commit

Permalink
perf: improve ProteusClientCoreCrypto performance [WPB-10000] (#2974)
Browse files Browse the repository at this point in the history
* perf: improve ProteusClientCoreCrypto performance

* docs: fix typos

* chore: move notebooks to docs/notebooks
  • Loading branch information
vitorhugods authored and github-actions[bot] committed Sep 2, 2024
1 parent 27532f3 commit a35eff0
Show file tree
Hide file tree
Showing 7 changed files with 5,700 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ class ProteusClientCoreCryptoImpl private constructor(private val coreCrypto: Co

override suspend fun encryptBatched(message: ByteArray, sessionIds: List<CryptoSessionId>): Map<CryptoSessionId, ByteArray> {
return wrapException {
coreCrypto.proteusEncryptBatched(sessionIds.map { it.value }, toUByteList((message))).mapNotNull { entry ->
coreCrypto.proteusEncryptBatched(
sessionId = sessionIds.map { it.value },
plaintext = toUByteList((message))
).mapNotNull { entry ->
CryptoSessionId.fromEncodedString(entry.key)?.let { sessionId ->
sessionId to toByteArray(entry.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ import com.wire.crypto.client.toByteArray
import com.wire.kalium.cryptography.exceptions.ProteusException
import io.ktor.util.decodeBase64Bytes
import io.ktor.util.encodeBase64
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.io.File

@Suppress("TooManyFunctions")
class ProteusClientCoreCryptoImpl private constructor(
private val coreCrypto: CoreCrypto,
) : ProteusClient {

private val mutex = Mutex()
private val existingSessionsCache = mutableSetOf<CryptoSessionId>()

override suspend fun close() {
coreCrypto.close()
}
Expand All @@ -46,6 +51,7 @@ class ProteusClientCoreCryptoImpl private constructor(
override suspend fun remoteFingerPrint(sessionId: CryptoSessionId): ByteArray = wrapException {
coreCrypto.proteusFingerprintRemote(sessionId.value).toByteArray()
}

override suspend fun getFingerprintFromPreKey(preKey: PreKeyCrypto): ByteArray = wrapException {
coreCrypto.proteusFingerprintPrekeybundle(preKey.encodedData.decodeBase64Bytes()).toByteArray()
}
Expand All @@ -62,9 +68,16 @@ class ProteusClientCoreCryptoImpl private constructor(
return wrapException { toPreKey(coreCrypto.proteusLastResortPrekeyId().toInt(), coreCrypto.proteusLastResortPrekey()) }
}

override suspend fun doesSessionExist(sessionId: CryptoSessionId): Boolean {
return wrapException {
override suspend fun doesSessionExist(sessionId: CryptoSessionId): Boolean = mutex.withLock {
if (existingSessionsCache.contains(sessionId)) {
return@withLock true
}
wrapException {
coreCrypto.proteusSessionExists(sessionId.value)
}.also { exists ->
if (exists) {
existingSessionsCache.add(sessionId)
}
}
}

Expand All @@ -77,13 +90,9 @@ class ProteusClientCoreCryptoImpl private constructor(

return wrapException {
if (sessionExists) {
val decryptedMessage = coreCrypto.proteusDecrypt(sessionId.value, message)
coreCrypto.proteusSessionSave(sessionId.value)
decryptedMessage
coreCrypto.proteusDecrypt(sessionId.value, message)
} else {
val decryptedMessage = coreCrypto.proteusSessionFromMessage(sessionId.value, message)
coreCrypto.proteusSessionSave(sessionId.value)
decryptedMessage
coreCrypto.proteusSessionFromMessage(sessionId.value, message)
}
}
}
Expand Down Expand Up @@ -119,7 +128,8 @@ class ProteusClientCoreCryptoImpl private constructor(
}
}

override suspend fun deleteSession(sessionId: CryptoSessionId) {
override suspend fun deleteSession(sessionId: CryptoSessionId) = mutex.withLock {
existingSessionsCache.remove(sessionId)
wrapException {
coreCrypto.proteusSessionDelete(sessionId.value)
}
Expand Down
3 changes: 3 additions & 0 deletions docs/notebooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
A place to store [Jupyter Notebook](https://jupyter.org/) files of ongoing studies.

You can use Kotlin and run it directly in the IDE, thanks to [Kotlin Notebook](https://kotlinlang.org/docs/kotlin-notebook-overview.html).
Loading

0 comments on commit a35eff0

Please sign in to comment.