From 431d91b08014b1fb326d68aedfa7828041f28dab Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 16 Mar 2024 10:47:32 +0100 Subject: [PATCH 1/2] added more KMP platforms, reused protectedop from mp_bintools where it is implemented almost everywhere --- .idea/misc.xml | 1 - build.gradle.kts | 43 ++++++++----------- .../kotlin/net/sergeych/crypto2/contrail.kt | 6 ++- .../kotlin/net/sergeych/tools/AtomicValue.kt | 5 ++- .../kotlin/net/sergeych/tools/ProtectedOp.kt | 21 --------- .../net/sergeych/tools/ProtectedOp.js.kt | 6 --- .../net/sergeych/tools/ProtectedOp.jvm.kt | 8 ---- .../net/sergeych/tools/ProtectedOp.native.kt | 13 ------ 8 files changed, 27 insertions(+), 76 deletions(-) delete mode 100644 src/commonMain/kotlin/net/sergeych/tools/ProtectedOp.kt delete mode 100644 src/jsMain/kotlin/net/sergeych/tools/ProtectedOp.js.kt delete mode 100644 src/jvmMain/kotlin/net/sergeych/tools/ProtectedOp.jvm.kt delete mode 100644 src/nativeMain/kotlin/net/sergeych/tools/ProtectedOp.native.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index 61efd00..45281f7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/build.gradle.kts b/build.gradle.kts index 9f3f735..40db139 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,6 +11,7 @@ version = "0.1.1-SNAPSHOT" repositories { mavenCentral() + mavenLocal() maven("https://maven.universablockchain.com/") maven("https://gitea.sergeych.net/api/packages/SergeychWorks/maven") } @@ -26,23 +27,17 @@ kotlin { } } js(KotlinJsCompilerType.IR) { - browser { - } - } - val hostOs = System.getProperty("os.name") - val isArm64 = System.getProperty("os.arch") == "aarch64" - val isMingwX64 = hostOs.startsWith("Windows") - @Suppress("UNUSED_VARIABLE") - val nativeTarget = when { - hostOs == "Mac OS X" && isArm64 -> macosArm64("native") - hostOs == "Mac OS X" && !isArm64 -> macosX64("native") - hostOs == "Linux" && isArm64 -> linuxArm64("native") - hostOs == "Linux" && !isArm64 -> linuxX64("native") - isMingwX64 -> mingwX64("native") - else -> throw GradleException("Host OS is not supported in Kotlin/Native.") + browser() } - val ktor_version = "2.3.6" + macosX64() + iosX64() + iosArm64() + iosSimulatorArm64() + linuxX64() + mingwX64() + // wasmJs() no libsodimu bindings yet (strangely) +// val ktor_version = "2.3.6" sourceSets { all { @@ -53,14 +48,14 @@ kotlin { val commonMain by getting { dependencies { - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1") implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.9.0") - api("com.ionspin.kotlin:bignum:0.3.8") + api("com.ionspin.kotlin:bignum:0.3.9") - api("net.sergeych:mp_bintools:0.0.6") - api("net.sergeych:mp_stools:1.4.1") + api("net.sergeych:mp_bintools:0.1.1") + api("net.sergeych:mp_stools:1.4.7") } } val commonTest by getting { @@ -80,11 +75,11 @@ kotlin { } } val jsTest by getting - val nativeMain by getting { - dependencies { - } - } - val nativeTest by getting +// val nativeMain by getting { +// dependencies { +// } +// } +// val nativeTest by getting } } diff --git a/src/commonMain/kotlin/net/sergeych/crypto2/contrail.kt b/src/commonMain/kotlin/net/sergeych/crypto2/contrail.kt index abfd8d9..f4de5e3 100644 --- a/src/commonMain/kotlin/net/sergeych/crypto2/contrail.kt +++ b/src/commonMain/kotlin/net/sergeych/crypto2/contrail.kt @@ -2,6 +2,8 @@ package net.sergeych.crypto2 import net.sergeych.bintools.CRC -fun isValidContrail(data: UByteArray): Boolean = CRC.crc8(data.copyOfRange(1, data.size)) == data[0] +fun isValidContrail(data: UByteArray): Boolean = CRC.crc8( + data.copyOfRange(1, data.size).toByteArray() +) == data[0] -fun createContrail(data: UByteArray): UByteArray = ubyteArrayOf(CRC.crc8(data)) + data \ No newline at end of file +fun createContrail(data: UByteArray): UByteArray = ubyteArrayOf(CRC.crc8(data.toByteArray())) + data \ No newline at end of file diff --git a/src/commonMain/kotlin/net/sergeych/tools/AtomicValue.kt b/src/commonMain/kotlin/net/sergeych/tools/AtomicValue.kt index a4a5155..50d4f43 100644 --- a/src/commonMain/kotlin/net/sergeych/tools/AtomicValue.kt +++ b/src/commonMain/kotlin/net/sergeych/tools/AtomicValue.kt @@ -1,5 +1,8 @@ package net.sergeych.tools +import net.sergeych.synctools.ProtectedOp +import net.sergeych.synctools.invoke + /** * Multiplatform (JS and battery included) atomically mutable value. * Actual value can be either changed in a block of [mutuate] when @@ -18,7 +21,7 @@ open class AtomicValue(initialValue: T) { * @return result of the mutation. Note that immediate call to property [value] * could already return modified bu some other thread value! */ - fun mutate(mutator: (T) -> T): T = op { + fun mutate(mutator: (T) -> T): T = op.invoke { actualValue = mutator(actualValue) actualValue } diff --git a/src/commonMain/kotlin/net/sergeych/tools/ProtectedOp.kt b/src/commonMain/kotlin/net/sergeych/tools/ProtectedOp.kt deleted file mode 100644 index be9720c..0000000 --- a/src/commonMain/kotlin/net/sergeych/tools/ProtectedOp.kt +++ /dev/null @@ -1,21 +0,0 @@ -package net.sergeych.tools - -/** - * Multiplatform interface to perform a regular (not suspend) operation - * protected by a platform mutex (where necessary). Get real implementation - * with [ProtectedOp] - */ -interface ProtectedOpImplementation { - /** - * Call [f] iin mutually exclusive mode, it means that only one invocation - * can be active at a time, all the rest are waiting until the current operation - * will finish. - */ - operator fun invoke(f: ()->T): T -} - - -/** - * Get the platform-depended implementation of a mutex-protected operation. - */ -expect fun ProtectedOp(): ProtectedOpImplementation \ No newline at end of file diff --git a/src/jsMain/kotlin/net/sergeych/tools/ProtectedOp.js.kt b/src/jsMain/kotlin/net/sergeych/tools/ProtectedOp.js.kt deleted file mode 100644 index acd7d7d..0000000 --- a/src/jsMain/kotlin/net/sergeych/tools/ProtectedOp.js.kt +++ /dev/null @@ -1,6 +0,0 @@ -package net.sergeych.tools - -actual fun ProtectedOp(): ProtectedOpImplementation = object : ProtectedOpImplementation { - // JS targets are inherently single-threaded, so we do noting: - override fun invoke(f: () -> T): T = f() -} \ No newline at end of file diff --git a/src/jvmMain/kotlin/net/sergeych/tools/ProtectedOp.jvm.kt b/src/jvmMain/kotlin/net/sergeych/tools/ProtectedOp.jvm.kt deleted file mode 100644 index 1114558..0000000 --- a/src/jvmMain/kotlin/net/sergeych/tools/ProtectedOp.jvm.kt +++ /dev/null @@ -1,8 +0,0 @@ -package net.sergeych.tools - -actual fun ProtectedOp(): ProtectedOpImplementation = object : ProtectedOpImplementation { - private val lock = Object() - override fun invoke(f: () -> T): T { - synchronized(lock) { return f() } - } -} \ No newline at end of file diff --git a/src/nativeMain/kotlin/net/sergeych/tools/ProtectedOp.native.kt b/src/nativeMain/kotlin/net/sergeych/tools/ProtectedOp.native.kt deleted file mode 100644 index c772602..0000000 --- a/src/nativeMain/kotlin/net/sergeych/tools/ProtectedOp.native.kt +++ /dev/null @@ -1,13 +0,0 @@ -package net.sergeych.tools - -import kotlinx.atomicfu.locks.SynchronizedObject -import kotlinx.atomicfu.locks.synchronized - -actual fun ProtectedOp(): ProtectedOpImplementation = object : ProtectedOpImplementation { - private val lock = SynchronizedObject() - override fun invoke(f: () -> T): T { - synchronized(lock) { - return f() - } - } -} \ No newline at end of file From 193a0789b6db510535dd04068d49e457a97b301f Mon Sep 17 00:00:00 2001 From: sergeych Date: Sat, 16 Mar 2024 20:30:25 +0100 Subject: [PATCH 2/2] more macos targets again --- .idea/artifacts/crypto2_js_0_1_1_SNAPSHOT.xml | 8 ++++++++ .idea/artifacts/crypto2_jvm_0_1_1_SNAPSHOT.xml | 8 ++++++++ .idea/artifacts/crypto2_wasm_js_0_1_1_SNAPSHOT.xml | 8 ++++++++ build.gradle.kts | 1 + .../komputing/khash/keccak/extensions/PublicExtensions.kt | 4 ++++ 5 files changed, 29 insertions(+) create mode 100644 .idea/artifacts/crypto2_js_0_1_1_SNAPSHOT.xml create mode 100644 .idea/artifacts/crypto2_jvm_0_1_1_SNAPSHOT.xml create mode 100644 .idea/artifacts/crypto2_wasm_js_0_1_1_SNAPSHOT.xml diff --git a/.idea/artifacts/crypto2_js_0_1_1_SNAPSHOT.xml b/.idea/artifacts/crypto2_js_0_1_1_SNAPSHOT.xml new file mode 100644 index 0000000..552b3c5 --- /dev/null +++ b/.idea/artifacts/crypto2_js_0_1_1_SNAPSHOT.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/build/libs + + + + + \ No newline at end of file diff --git a/.idea/artifacts/crypto2_jvm_0_1_1_SNAPSHOT.xml b/.idea/artifacts/crypto2_jvm_0_1_1_SNAPSHOT.xml new file mode 100644 index 0000000..5ec80b1 --- /dev/null +++ b/.idea/artifacts/crypto2_jvm_0_1_1_SNAPSHOT.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/build/libs + + + + + \ No newline at end of file diff --git a/.idea/artifacts/crypto2_wasm_js_0_1_1_SNAPSHOT.xml b/.idea/artifacts/crypto2_wasm_js_0_1_1_SNAPSHOT.xml new file mode 100644 index 0000000..95644b9 --- /dev/null +++ b/.idea/artifacts/crypto2_wasm_js_0_1_1_SNAPSHOT.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/build/libs + + + + + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 40db139..569f410 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -31,6 +31,7 @@ kotlin { } macosX64() + macosArm64() iosX64() iosArm64() iosSimulatorArm64() diff --git a/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt b/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt index d68f972..563ac88 100644 --- a/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt +++ b/src/commonMain/kotlin/org/komputing/khash/keccak/extensions/PublicExtensions.kt @@ -11,6 +11,10 @@ fun ByteArray.digestKeccak(parameter: KeccakParameter): ByteArray { return Keccak.digest(this, parameter) } +fun UByteArray.digestKeccak(parameter: KeccakParameter): UByteArray { + return Keccak.digest(this.toByteArray(), parameter).toUByteArray() +} + /** * Computes the proper Keccak digest of [this] string based on the given [parameter] */