diff --git a/build.gradle.kts b/build.gradle.kts index 4c441fd..464b3c0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,7 +51,7 @@ kotlin { implementation(kotlin("stdlib-jdk8")) } compilations["test"].dependencies { - implementation(project(":jni")) + implementation(project(":jni:jvm")) implementation(kotlin("stdlib-jdk8")) implementation(kotlin("test-junit")) } diff --git a/jni/android/build.gradle.kts b/jni/android/build.gradle.kts new file mode 100644 index 0000000..ee82311 --- /dev/null +++ b/jni/android/build.gradle.kts @@ -0,0 +1,48 @@ +plugins { + id("com.android.library") + kotlin("android") +} + +kotlin { + explicitApi() +} + +dependencies { + api(project(":jni")) + implementation(kotlin("stdlib-jdk8")) +} + +android { + defaultConfig { + compileSdkVersion(30) + minSdkVersion(21) + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + externalNativeBuild { + cmake {} + } + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 + } + + externalNativeBuild { + cmake { + setPath("src/main/CMakeLists.txt") + } + } + ndkVersion = "21.3.6528147" + + afterEvaluate { + tasks.withType().all { + enabled = false + } + } +} + +afterEvaluate { + configure(listOf("Debug", "Release").map { tasks["externalNativeBuild$it"] }) { + dependsOn(":native:buildSecp256k1Android") + } +} diff --git a/jni/src/androidMain/AndroidManifest.xml b/jni/android/src/main/AndroidManifest.xml similarity index 53% rename from jni/src/androidMain/AndroidManifest.xml rename to jni/android/src/main/AndroidManifest.xml index 2696859..f5cf839 100644 --- a/jni/src/androidMain/AndroidManifest.xml +++ b/jni/android/src/main/AndroidManifest.xml @@ -1,3 +1,3 @@ - + diff --git a/jni/android/src/main/CMakeLists.txt b/jni/android/src/main/CMakeLists.txt new file mode 100644 index 0000000..b044a0e --- /dev/null +++ b/jni/android/src/main/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.10.0) + +add_library( secp256k1-jni SHARED + ${CMAKE_CURRENT_LIST_DIR}/../../../../native/jni/src/org_bitcoin_Secp256k1CFunctions.c +) + +target_include_directories( secp256k1-jni + PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../../../../native/secp256k1 + PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../../../../native/jni/headers/java +) + +target_link_libraries( secp256k1-jni + ${CMAKE_CURRENT_LIST_DIR}/../../../../native/build/android/${ANDROID_ABI}/libsecp256k1.a +) diff --git a/jni/src/androidMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt b/jni/android/src/main/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt similarity index 75% rename from jni/src/androidMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt rename to jni/android/src/main/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt index 511b840..e5ecac1 100644 --- a/jni/src/androidMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt +++ b/jni/android/src/main/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt @@ -3,12 +3,12 @@ package fr.acinq.secp256k1.jni import fr.acinq.secp256k1.Secp256k1 import org.bitcoin.NativeSecp256k1 -public actual object NativeSecp256k1Loader { +public object NativeSecp256k1Loader { @JvmStatic @Synchronized @Throws(Exception::class) - actual fun load(): Secp256k1 { + fun load(): Secp256k1 { System.loadLibrary("secp256k1-jni") return NativeSecp256k1 } diff --git a/jni/build.gradle.kts b/jni/build.gradle.kts index f2244d9..da1bf8c 100644 --- a/jni/build.gradle.kts +++ b/jni/build.gradle.kts @@ -1,104 +1,27 @@ plugins { - kotlin("multiplatform") // version "1.4-M2-mt" - id("com.android.library") - `maven-publish` + kotlin("jvm") } -val currentOs = org.gradle.internal.os.OperatingSystem.current() - kotlin { explicitApi() - - val commonMain by sourceSets.getting { - dependencies { - api(rootProject) - implementation(kotlin("stdlib-common")) - } - } - val commonTest by sourceSets.getting { - dependencies { - implementation(kotlin("test-common")) - implementation(kotlin("test-annotations-common")) - } - } - - jvm { - compilations.all { - kotlinOptions.jvmTarget = "1.8" - } - (tasks[compilations["main"].processResourcesTaskName] as ProcessResources).apply { - dependsOn("copyJni") - from(buildDir.resolve("jniResources")) - } - compilations["main"].dependencies { - implementation(kotlin("stdlib-jdk8")) - } - compilations["test"].dependencies { - implementation(kotlin("test-junit")) - } - } - - android { - publishLibraryVariants("release", "debug") - compilations.all { - kotlinOptions.jvmTarget = "1.8" - } - sourceSets["androidMain"].dependencies { - implementation(kotlin("stdlib-jdk8")) - } - sourceSets["androidTest"].dependencies { - implementation(kotlin("test-junit")) - implementation("androidx.test.ext:junit:1.1.1") - implementation("androidx.test.espresso:espresso-core:3.2.0") - } - } - - sourceSets.all { - languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn") - } } -android { - defaultConfig { - compileSdkVersion(30) - minSdkVersion(21) - testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" - externalNativeBuild { - cmake {} - } - } - - compileOptions { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 - } - - externalNativeBuild { - cmake { - setPath("src/androidMain/CMakeLists.txt") - } - } - ndkVersion = "21.3.6528147" - - sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml") - - afterEvaluate { - tasks.withType().all { - enabled = false - } - } +dependencies { + api(rootProject) + implementation(kotlin("stdlib-jdk8")) } -val copyJni by tasks.creating(Sync::class) { - dependsOn(":native:buildSecp256k1Jvm") - from(rootDir.resolve("native/build/linux/libsecp256k1-jni.so")) { rename { "libsecp256k1-jni-linux-x86_64.so" } } - from(rootDir.resolve("native/build/darwin/libsecp256k1-jni.dylib")) { rename { "libsecp256k1-jni-darwin-x86_64.dylib" } } - from(rootDir.resolve("native/build/mingw/secp256k1-jni.dll")) { rename { "secp256k1-jni-mingw-x86_64.dll" } } - into(buildDir.resolve("jniResources/fr/acinq/secp256k1/jni/native")) -} - -afterEvaluate { - configure(listOf("Debug", "Release").map { tasks["externalNativeBuild$it"] }) { - dependsOn(":native:buildSecp256k1Android") +val generateJniHeaders by tasks.creating(JavaCompile::class) { + group = "build" + classpath = sourceSets["main"].compileClasspath + destinationDir = file("${buildDir}/generated/jni") + source = sourceSets["main"].java + options.compilerArgs = listOf( + "-h", file("${buildDir}/generated/jni").absolutePath, + "-d", file("${buildDir}/generated/jni-tmp").absolutePath + ) + // options.verbose = true + doLast { + delete(file("${buildDir}/generated/jni-tmp")) } } diff --git a/jni/jvm/build.gradle.kts b/jni/jvm/build.gradle.kts new file mode 100644 index 0000000..0684a1a --- /dev/null +++ b/jni/jvm/build.gradle.kts @@ -0,0 +1,25 @@ +plugins { + kotlin("jvm") +} + +kotlin { + explicitApi() +} + +dependencies { + api(project(":jni")) + implementation(kotlin("stdlib-jdk8")) +} + +val copyJni by tasks.creating(Sync::class) { + dependsOn(":native:buildSecp256k1Jvm") + from(rootDir.resolve("native/build/linux/libsecp256k1-jni.so")) { rename { "libsecp256k1-jni-linux-x86_64.so" } } + from(rootDir.resolve("native/build/darwin/libsecp256k1-jni.dylib")) { rename { "libsecp256k1-jni-darwin-x86_64.dylib" } } + from(rootDir.resolve("native/build/mingw/secp256k1-jni.dll")) { rename { "secp256k1-jni-mingw-x86_64.dll" } } + into(buildDir.resolve("jniResources/fr/acinq/secp256k1/jni/native")) +} + +(tasks["processResources"] as ProcessResources).apply { + dependsOn("copyJni") + from(buildDir.resolve("jniResources")) +} diff --git a/jni/src/jvmMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt b/jni/jvm/src/main/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt similarity index 98% rename from jni/src/jvmMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt rename to jni/jvm/src/main/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt index 10f5710..d40b199 100644 --- a/jni/src/jvmMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt +++ b/jni/jvm/src/main/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt @@ -14,7 +14,7 @@ import java.util.* * * @author leo */ -public actual object NativeSecp256k1Loader { +public object NativeSecp256k1Loader { private var extracted = false /** @@ -26,7 +26,7 @@ public actual object NativeSecp256k1Loader { @JvmStatic @Synchronized @Throws(Exception::class) - public actual fun load(): Secp256k1 { + public fun load(): Secp256k1 { // only cleanup before the first extract if (!extracted) { cleanup() diff --git a/jni/src/jvmMain/kotlin/fr/acinq/secp256k1/jni/OSInfo.kt b/jni/jvm/src/main/kotlin/fr/acinq/secp256k1/jni/OSInfo.kt similarity index 100% rename from jni/src/jvmMain/kotlin/fr/acinq/secp256k1/jni/OSInfo.kt rename to jni/jvm/src/main/kotlin/fr/acinq/secp256k1/jni/OSInfo.kt diff --git a/jni/src/androidMain/CMakeLists.txt b/jni/src/androidMain/CMakeLists.txt deleted file mode 100644 index 553d148..0000000 --- a/jni/src/androidMain/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -cmake_minimum_required(VERSION 3.10.0) - -add_library( secp256k1-jni SHARED - ${CMAKE_CURRENT_LIST_DIR}/../../../native/jni/src/org_bitcoin_NativeSecp256k1.c - ${CMAKE_CURRENT_LIST_DIR}/../../../native/jni/src/org_bitcoin_Secp256k1Context.c -) - -target_include_directories( secp256k1-jni - PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../../../native/secp256k1 -) - -target_link_libraries( secp256k1-jni - ${CMAKE_CURRENT_LIST_DIR}/../../../native/build/android/${ANDROID_ABI}/libsecp256k1.a -) diff --git a/jni/src/commonMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt b/jni/src/commonMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt deleted file mode 100644 index 3403b02..0000000 --- a/jni/src/commonMain/kotlin/fr/acinq/secp256k1/jni/NativeSecp256k1Loader.kt +++ /dev/null @@ -1,10 +0,0 @@ -package fr.acinq.secp256k1.jni - -import fr.acinq.secp256k1.Secp256k1 - - -public expect object NativeSecp256k1Loader { - - public fun load(): Secp256k1 - -} diff --git a/jni/src/main/java/org/bitcoin/Secp256k1CFunctions.java b/jni/src/main/java/org/bitcoin/Secp256k1CFunctions.java new file mode 100644 index 0000000..0a0de79 --- /dev/null +++ b/jni/src/main/java/org/bitcoin/Secp256k1CFunctions.java @@ -0,0 +1,24 @@ +package org.bitcoin; + +import java.nio.ByteBuffer; + +public class Secp256k1CFunctions { + static native long secp256k1_init_context(); + static native int secp256k1_context_randomize(ByteBuffer byteBuff, long context); + static native byte[][] secp256k1_privkey_negate(ByteBuffer byteBuff, long context); + static native byte[][] secp256k1_privkey_tweak_add(ByteBuffer byteBuff, long context); + static native byte[][] secp256k1_privkey_tweak_mul(ByteBuffer byteBuff, long context); + static native byte[][] secp256k1_pubkey_negate(ByteBuffer byteBuff, long context, int pubLen); + static native byte[][] secp256k1_pubkey_tweak_add(ByteBuffer byteBuff, long context, int pubLen); + static native byte[][] secp256k1_pubkey_tweak_mul(ByteBuffer byteBuff, long context, int pubLen); + static native void secp256k1_destroy_context(long context); + static native int secp256k1_ecdsa_verify(ByteBuffer byteBuff, long context, int sigLen, int pubLen); + static native byte[][] secp256k1_ecdsa_sign(ByteBuffer byteBuff, boolean compact, long context); + static native byte[][] secp256k1_ecdsa_normalize(ByteBuffer byteBuff, int sigLen, boolean compact, long context); + static native int secp256k1_ec_seckey_verify(ByteBuffer byteBuff, long context); + static native byte[][] secp256k1_ec_pubkey_create(ByteBuffer byteBuff, boolean compressed, long context); + static native byte[][] secp256k1_ec_pubkey_parse(ByteBuffer byteBuff, long context, int inputLen, boolean compressed); + static native byte[][] secp256k1_ec_pubkey_add(ByteBuffer byteBuff, long context, int lent1, int len2); + static native byte[][] secp256k1_ecdh(ByteBuffer byteBuff, long context, int inputLen); + static native byte[][] secp256k1_ecdsa_recover(ByteBuffer byteBuff, long context, int recid, boolean compressed); +} diff --git a/jni/src/commonMain/kotlin/org/bitcoin/NativeSecp256k1.kt b/jni/src/main/kotlin/org/bitcoin/NativeSecp256k1.kt similarity index 86% rename from jni/src/commonMain/kotlin/org/bitcoin/NativeSecp256k1.kt rename to jni/src/main/kotlin/org/bitcoin/NativeSecp256k1.kt index c4f6217..c595706 100644 --- a/jni/src/commonMain/kotlin/org/bitcoin/NativeSecp256k1.kt +++ b/jni/src/main/kotlin/org/bitcoin/NativeSecp256k1.kt @@ -40,7 +40,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock * or point the JVM to the folder containing it with -Djava.library.path * */ -internal object NativeSecp256k1 : Secp256k1 { +public object NativeSecp256k1 : Secp256k1 { private val rwl = ReentrantReadWriteLock() private val r: Lock = rwl.readLock() private val w: Lock = rwl.writeLock() @@ -80,7 +80,7 @@ internal object NativeSecp256k1 : Secp256k1 { val byteBuff = pack(data, signature, pub) r.lock() return try { - secp256k1_ecdsa_verify( + Secp256k1CFunctions.secp256k1_ecdsa_verify( byteBuff, Secp256k1Context.getContext(), signature.size, @@ -107,7 +107,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ecdsa_sign( + Secp256k1CFunctions.secp256k1_ecdsa_sign( byteBuff, format == SigFormat.COMPACT, Secp256k1Context.getContext() @@ -133,7 +133,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ecdsa_normalize( + Secp256k1CFunctions.secp256k1_ecdsa_normalize( byteBuff, sig.size, format == SigFormat.COMPACT, @@ -165,7 +165,7 @@ internal object NativeSecp256k1 : Secp256k1 { val byteBuff = pack(seckey) r.lock() return try { - secp256k1_ec_seckey_verify( + Secp256k1CFunctions.secp256k1_ec_seckey_verify( byteBuff, Secp256k1Context.getContext() ) == 1 @@ -189,7 +189,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ec_pubkey_create( + Secp256k1CFunctions.secp256k1_ec_pubkey_create( byteBuff, format == PubKeyFormat.COMPRESSED, Secp256k1Context.getContext() @@ -216,7 +216,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ec_pubkey_parse( + Secp256k1CFunctions.secp256k1_ec_pubkey_parse( byteBuff, Secp256k1Context.getContext(), pubkey.size, @@ -244,7 +244,7 @@ internal object NativeSecp256k1 : Secp256k1 { override fun cleanup() { w.lock() try { - secp256k1_destroy_context(Secp256k1Context.getContext()) + Secp256k1CFunctions.secp256k1_destroy_context(Secp256k1Context.getContext()) } finally { w.unlock() } @@ -257,7 +257,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_privkey_negate( + Secp256k1CFunctions.secp256k1_privkey_negate( byteBuff, Secp256k1Context.getContext() ) @@ -291,7 +291,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_privkey_tweak_mul( + Secp256k1CFunctions.secp256k1_privkey_tweak_mul( byteBuff, Secp256k1Context.getContext() ) @@ -325,7 +325,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_privkey_tweak_add( + Secp256k1CFunctions.secp256k1_privkey_tweak_add( byteBuff, Secp256k1Context.getContext() ) @@ -347,7 +347,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_pubkey_negate( + Secp256k1CFunctions.secp256k1_pubkey_negate( byteBuff, Secp256k1Context.getContext(), pubkey.size @@ -378,7 +378,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_pubkey_tweak_add( + Secp256k1CFunctions.secp256k1_pubkey_tweak_add( byteBuff, Secp256k1Context.getContext(), pubkey.size @@ -409,7 +409,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_pubkey_tweak_mul( + Secp256k1CFunctions.secp256k1_pubkey_tweak_mul( byteBuff, Secp256k1Context.getContext(), pubkey.size @@ -433,7 +433,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ec_pubkey_add( + Secp256k1CFunctions.secp256k1_ec_pubkey_add( byteBuff, Secp256k1Context.getContext(), pubkey1.size, @@ -465,7 +465,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ecdh( + Secp256k1CFunctions.secp256k1_ecdh( byteBuff, Secp256k1Context.getContext(), pubkey.size @@ -488,7 +488,7 @@ internal object NativeSecp256k1 : Secp256k1 { val retByteArray: Array r.lock() retByteArray = try { - secp256k1_ecdsa_recover( + Secp256k1CFunctions.secp256k1_ecdsa_recover( byteBuff, Secp256k1Context.getContext(), recid, @@ -522,7 +522,7 @@ internal object NativeSecp256k1 : Secp256k1 { val byteBuff = pack(seed) w.lock() return try { - secp256k1_context_randomize( + Secp256k1CFunctions.secp256k1_context_randomize( byteBuff, Secp256k1Context.getContext() ) == 1 @@ -530,22 +530,4 @@ internal object NativeSecp256k1 : Secp256k1 { w.unlock() } } - - @JvmStatic private external fun secp256k1_context_randomize(byteBuff: ByteBuffer, context: Long): Int - @JvmStatic private external fun secp256k1_privkey_negate(byteBuff: ByteBuffer, context: Long): Array - @JvmStatic private external fun secp256k1_privkey_tweak_add(byteBuff: ByteBuffer, context: Long): Array - @JvmStatic private external fun secp256k1_privkey_tweak_mul(byteBuff: ByteBuffer, context: Long): Array - @JvmStatic private external fun secp256k1_pubkey_negate(byteBuff: ByteBuffer, context: Long, pubLen: Int): Array - @JvmStatic private external fun secp256k1_pubkey_tweak_add(byteBuff: ByteBuffer, context: Long, pubLen: Int): Array - @JvmStatic private external fun secp256k1_pubkey_tweak_mul(byteBuff: ByteBuffer, context: Long, pubLen: Int): Array - @JvmStatic private external fun secp256k1_destroy_context(context: Long) - @JvmStatic private external fun secp256k1_ecdsa_verify(byteBuff: ByteBuffer, context: Long, sigLen: Int, pubLen: Int): Int - @JvmStatic private external fun secp256k1_ecdsa_sign(byteBuff: ByteBuffer, compact: Boolean, context: Long): Array - @JvmStatic private external fun secp256k1_ecdsa_normalize(byteBuff: ByteBuffer, sigLen: Int, compact: Boolean, context: Long): Array - @JvmStatic private external fun secp256k1_ec_seckey_verify(byteBuff: ByteBuffer, context: Long): Int - @JvmStatic private external fun secp256k1_ec_pubkey_create(byteBuff: ByteBuffer, compressed: Boolean, context: Long): Array - @JvmStatic private external fun secp256k1_ec_pubkey_parse(byteBuff: ByteBuffer, context: Long, inputLen: Int, compressed: Boolean): Array - @JvmStatic private external fun secp256k1_ec_pubkey_add(byteBuff: ByteBuffer, context: Long, lent1: Int, len2: Int): Array - @JvmStatic private external fun secp256k1_ecdh(byteBuff: ByteBuffer, context: Long, inputLen: Int): Array - @JvmStatic private external fun secp256k1_ecdsa_recover(byteBuff: ByteBuffer, context: Long, recid: Int, compressed: Boolean): Array -} \ No newline at end of file +} diff --git a/jni/src/commonMain/kotlin/org/bitcoin/NativeSecp256k1Util.kt b/jni/src/main/kotlin/org/bitcoin/NativeSecp256k1Util.kt similarity index 100% rename from jni/src/commonMain/kotlin/org/bitcoin/NativeSecp256k1Util.kt rename to jni/src/main/kotlin/org/bitcoin/NativeSecp256k1Util.kt diff --git a/jni/src/commonMain/kotlin/org/bitcoin/Secp256k1Context.kt b/jni/src/main/kotlin/org/bitcoin/Secp256k1Context.kt similarity index 89% rename from jni/src/commonMain/kotlin/org/bitcoin/Secp256k1Context.kt rename to jni/src/main/kotlin/org/bitcoin/Secp256k1Context.kt index 13deb5b..832b788 100644 --- a/jni/src/commonMain/kotlin/org/bitcoin/Secp256k1Context.kt +++ b/jni/src/main/kotlin/org/bitcoin/Secp256k1Context.kt @@ -29,11 +29,8 @@ public object Secp256k1Context { return if (!isEnabled) -1 else context //sanity check } - @JvmStatic private external fun secp256k1_init_context(): Long - init { //static initializer isEnabled = true - context = - secp256k1_init_context() + context = Secp256k1CFunctions.secp256k1_init_context() } } diff --git a/native/build.gradle.kts b/native/build.gradle.kts index e65e1e9..83bb75e 100644 --- a/native/build.gradle.kts +++ b/native/build.gradle.kts @@ -1,9 +1,16 @@ -evaluationDependsOn(":jni") +evaluationDependsOn(":jni:android") val currentOs = org.gradle.internal.os.OperatingSystem.current() val buildSecp256k1 by tasks.creating { group = "build" } +val generateJniHeaders by tasks.creating(Sync::class) { + group = "build" + dependsOn(":jni:generateJniHeaders") + from(rootDir.resolve("jni/build/generated/jni")) + into(projectDir.resolve("jni/headers/java")) +} + sealed class Cross { abstract fun cmd(target: String, nativeDir: File): List class DockCross(val cross: String) : Cross() { @@ -73,7 +80,7 @@ fun creatingBuildSecp256k1Android(arch: String) = tasks.creating(Exec::class) { } environment("TOOLCHAIN", toolchain) environment("ARCH", arch) - environment("ANDROID_NDK", (project(":jni").extensions["android"] as com.android.build.gradle.LibraryExtension).ndkDirectory) + environment("ANDROID_NDK", (project(":jni:android").extensions["android"] as com.android.build.gradle.LibraryExtension).ndkDirectory) commandLine("./build-android.sh") } val buildSecp256k1AndroidX86_64 by creatingBuildSecp256k1Android("x86_64") @@ -82,6 +89,7 @@ val buildSecp256k1AndroidArm64v8a by creatingBuildSecp256k1Android("arm64-v8a") val buildSecp256k1AndroidArmeabiv7a by creatingBuildSecp256k1Android("armeabi-v7a") val clean by tasks.creating { + group = "build" doLast { delete(projectDir.resolve("build")) } diff --git a/native/build.sh b/native/build.sh index c788b2d..af1c7c5 100755 --- a/native/build.sh +++ b/native/build.sh @@ -50,7 +50,7 @@ elif [ "$TARGET" == "mingw" ]; then CC_OPTS="-fpic" fi -$CC -shared $CC_OPTS -o build/$TARGET/$OUTFILE jni/src/org_bitcoin_NativeSecp256k1.c jni/src/org_bitcoin_Secp256k1Context.c -Ijni/headers/ -Ijni/headers/$JNI_HEADERS/ -Isecp256k1/ -lsecp256k1 -Lbuild/$TARGET/ $ADD_LIB +$CC -shared $CC_OPTS -o build/$TARGET/$OUTFILE jni/src/org_bitcoin_Secp256k1CFunctions.c -Ijni/headers/ -Ijni/headers/java -Ijni/headers/$JNI_HEADERS/ -Isecp256k1/ -lsecp256k1 -Lbuild/$TARGET/ $ADD_LIB [[ ! -z "$TO_UID" ]] && chown -R $TO_UID:$TO_UID build diff --git a/native/jni/headers/java/org_bitcoin_Secp256k1CFunctions.h b/native/jni/headers/java/org_bitcoin_Secp256k1CFunctions.h new file mode 100644 index 0000000..73dfd07 --- /dev/null +++ b/native/jni/headers/java/org_bitcoin_Secp256k1CFunctions.h @@ -0,0 +1,157 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_bitcoin_Secp256k1CFunctions */ + +#ifndef _Included_org_bitcoin_Secp256k1CFunctions +#define _Included_org_bitcoin_Secp256k1CFunctions +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_init_context + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1init_1context + (JNIEnv *, jclass); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_context_randomize + * Signature: (Ljava/nio/ByteBuffer;J)I + */ +JNIEXPORT jint JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1context_1randomize + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_privkey_negate + * Signature: (Ljava/nio/ByteBuffer;J)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1privkey_1negate + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_privkey_tweak_add + * Signature: (Ljava/nio/ByteBuffer;J)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1privkey_1tweak_1add + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_privkey_tweak_mul + * Signature: (Ljava/nio/ByteBuffer;J)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1privkey_1tweak_1mul + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_pubkey_negate + * Signature: (Ljava/nio/ByteBuffer;JI)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1pubkey_1negate + (JNIEnv *, jclass, jobject, jlong, jint); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_pubkey_tweak_add + * Signature: (Ljava/nio/ByteBuffer;JI)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1pubkey_1tweak_1add + (JNIEnv *, jclass, jobject, jlong, jint); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_pubkey_tweak_mul + * Signature: (Ljava/nio/ByteBuffer;JI)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1pubkey_1tweak_1mul + (JNIEnv *, jclass, jobject, jlong, jint); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_destroy_context + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1destroy_1context + (JNIEnv *, jclass, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ecdsa_verify + * Signature: (Ljava/nio/ByteBuffer;JII)I + */ +JNIEXPORT jint JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1verify + (JNIEnv *, jclass, jobject, jlong, jint, jint); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ecdsa_sign + * Signature: (Ljava/nio/ByteBuffer;ZJ)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1sign + (JNIEnv *, jclass, jobject, jboolean, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ecdsa_normalize + * Signature: (Ljava/nio/ByteBuffer;IZJ)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1normalize + (JNIEnv *, jclass, jobject, jint, jboolean, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ec_seckey_verify + * Signature: (Ljava/nio/ByteBuffer;J)I + */ +JNIEXPORT jint JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1seckey_1verify + (JNIEnv *, jclass, jobject, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ec_pubkey_create + * Signature: (Ljava/nio/ByteBuffer;ZJ)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1pubkey_1create + (JNIEnv *, jclass, jobject, jboolean, jlong); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ec_pubkey_parse + * Signature: (Ljava/nio/ByteBuffer;JIZ)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1pubkey_1parse + (JNIEnv *, jclass, jobject, jlong, jint, jboolean); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ec_pubkey_add + * Signature: (Ljava/nio/ByteBuffer;JII)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1pubkey_1add + (JNIEnv *, jclass, jobject, jlong, jint, jint); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ecdh + * Signature: (Ljava/nio/ByteBuffer;JI)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdh + (JNIEnv *, jclass, jobject, jlong, jint); + +/* + * Class: org_bitcoin_Secp256k1CFunctions + * Method: secp256k1_ecdsa_recover + * Signature: (Ljava/nio/ByteBuffer;JIZ)[[B + */ +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1recover + (JNIEnv *, jclass, jobject, jlong, jint, jboolean); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/native/jni/src/org_bitcoin_NativeSecp256k1.h b/native/jni/src/org_bitcoin_NativeSecp256k1.h deleted file mode 100644 index 8e1c2e0..0000000 --- a/native/jni/src/org_bitcoin_NativeSecp256k1.h +++ /dev/null @@ -1,157 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_bitcoin_NativeSecp256k1 */ - -#ifndef _Included_org_bitcoin_NativeSecp256k1 -#define _Included_org_bitcoin_NativeSecp256k1 -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ctx_clone - * Signature: (J)J - */ -JNIEXPORT jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ctx_1clone - (JNIEnv *, jclass, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_context_randomize - * Signature: (Ljava/nio/ByteBuffer;J)I - */ -JNIEXPORT jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1context_1randomize - (JNIEnv *, jclass, jobject, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_privkey_negate - * Signature: (Ljava/nio/ByteBuffer;J)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1negate - (JNIEnv *, jclass, jobject, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_privkey_tweak_add - * Signature: (Ljava/nio/ByteBuffer;J)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1add - (JNIEnv *, jclass, jobject, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_privkey_tweak_mul - * Signature: (Ljava/nio/ByteBuffer;J)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1mul - (JNIEnv *, jclass, jobject, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_pubkey_negate - * Signature: (Ljava/nio/ByteBuffer;JI)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1negate - (JNIEnv *, jclass, jobject, jlong, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_pubkey_tweak_add - * Signature: (Ljava/nio/ByteBuffer;JI)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1add - (JNIEnv *, jclass, jobject, jlong, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_pubkey_tweak_mul - * Signature: (Ljava/nio/ByteBuffer;JI)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1mul - (JNIEnv *, jclass, jobject, jlong, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_destroy_context - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1destroy_1context - (JNIEnv *, jclass, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_verify - * Signature: (Ljava/nio/ByteBuffer;JII)I - */ -JNIEXPORT jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1verify - (JNIEnv *, jclass, jobject, jlong, jint, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_sign - * Signature: (Ljava/nio/ByteBuffer;ZJ)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1sign - (JNIEnv *, jclass, jobject, jboolean, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_normalize - * Signature: (Ljava/nio/ByteBuffer;I114:1ZJ)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1normalize - (JNIEnv *, jclass, jobject, jint, jboolean, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ec_seckey_verify - * Signature: (Ljava/nio/ByteBuffer;J)I - */ -JNIEXPORT jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1seckey_1verify - (JNIEnv *, jclass, jobject, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ec_pubkey_create - * Signature: (Ljava/nio/ByteBuffer;ZJ)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1create - (JNIEnv *, jclass, jobject, jboolean, jlong); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ec_pubkey_parse - * Signature: (Ljava/nio/ByteBuffer;JIZ)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1parse - (JNIEnv *, jclass, jobject, jlong, jint, jboolean); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ec_pubkey_add - * Signature: (Ljava/nio/ByteBuffer;JII)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1add - (JNIEnv *, jclass, jobject, jlong, jint, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdh - * Signature: (Ljava/nio/ByteBuffer;JI)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdh - (JNIEnv *, jclass, jobject, jlong, jint); - -/* - * Class: org_bitcoin_NativeSecp256k1 - * Method: secp256k1_ecdsa_recover - * Signature: (Ljava/nio/ByteBuffer;JIZ)[[B - */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1recover - (JNIEnv *, jclass, jobject, jlong, jint, jboolean); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/native/jni/src/org_bitcoin_NativeSecp256k1.c b/native/jni/src/org_bitcoin_Secp256k1CFunctions.c similarity index 90% rename from native/jni/src/org_bitcoin_NativeSecp256k1.c rename to native/jni/src/org_bitcoin_Secp256k1CFunctions.c index ebaf366..e7d54ec 100644 --- a/native/jni/src/org_bitcoin_NativeSecp256k1.c +++ b/native/jni/src/org_bitcoin_Secp256k1CFunctions.c @@ -1,13 +1,23 @@ #include #include #include -#include "org_bitcoin_NativeSecp256k1.h" +#include "org_bitcoin_Secp256k1CFunctions.h" #include "include/secp256k1.h" #include "include/secp256k1_ecdh.h" #include "include/secp256k1_recovery.h" -SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ctx_1clone +SECP256K1_API jlong JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1init_1context + (JNIEnv* env, jclass classObject) +{ + secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); + + (void)classObject;(void)env; + + return (uintptr_t)ctx; +} + +SECP256K1_API jlong JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ctx_1clone (JNIEnv* env, jclass classObject, jlong ctx_l) { const secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -20,7 +30,7 @@ SECP256K1_API jlong JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ctx_1clo } -SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1context_1randomize +SECP256K1_API jint JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1context_1randomize (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -33,7 +43,7 @@ SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1context_1 } -SECP256K1_API void JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1destroy_1context +SECP256K1_API void JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1destroy_1context (JNIEnv* env, jclass classObject, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -43,7 +53,7 @@ SECP256K1_API void JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1destroy_1 (void)classObject;(void)env; } -SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1verify +SECP256K1_API jint JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1verify (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint siglen, jint publen) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -75,7 +85,7 @@ SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1ve return ret; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1sign +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1sign (JNIEnv* env, jclass classObject, jobject byteBufferObject, jboolean compact, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -121,7 +131,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1normalize +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1normalize (JNIEnv* env, jclass classObject, jobject byteBufferObject, jint siglen, jboolean compact, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -178,7 +188,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e } -SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1seckey_1verify +SECP256K1_API jint JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1seckey_1verify (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -189,7 +199,7 @@ SECP256K1_API jint JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1secke return secp256k1_ec_seckey_verify(ctx, secKey); } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1create +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1pubkey_1create (JNIEnv* env, jclass classObject, jobject byteBufferObject, jboolean compressed, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -230,7 +240,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1parse +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1pubkey_1parse (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint inputlen, jboolean compressed) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -271,7 +281,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1negate +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1privkey_1negate (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -305,7 +315,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1p return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1add +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1privkey_1tweak_1add (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -340,7 +350,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1p return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1privkey_1tweak_1mul +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1privkey_1tweak_1mul (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -375,7 +385,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1p return retArray; } -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1negate +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1pubkey_1negate (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -418,7 +428,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubke return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1add +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1pubkey_1tweak_1add (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -463,7 +473,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1p return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1pubkey_1tweak_1mul +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1pubkey_1tweak_1mul (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -507,7 +517,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1p return retArray; } -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1pubkey_1add +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ec_1pubkey_1add (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen1, jint publen2) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -554,7 +564,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ec_1p return retArray; } -SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdh +SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdh (JNIEnv* env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint publen) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; @@ -605,7 +615,7 @@ SECP256K1_API jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1e * Method: secp256k1_ecdsa_recover * Signature: (Ljava/nio/ByteBuffer;JI)[[B */ -JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_NativeSecp256k1_secp256k1_1ecdsa_1recover +JNIEXPORT jobjectArray JNICALL Java_org_bitcoin_Secp256k1CFunctions_secp256k1_1ecdsa_1recover (JNIEnv *env, jclass classObject, jobject byteBufferObject, jlong ctx_l, jint recid, jboolean compressed) { secp256k1_context *ctx = (secp256k1_context*)(uintptr_t)ctx_l; diff --git a/native/jni/src/org_bitcoin_Secp256k1Context.c b/native/jni/src/org_bitcoin_Secp256k1Context.c deleted file mode 100644 index a52939e..0000000 --- a/native/jni/src/org_bitcoin_Secp256k1Context.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include "org_bitcoin_Secp256k1Context.h" -#include "include/secp256k1.h" - -SECP256K1_API jlong JNICALL Java_org_bitcoin_Secp256k1Context_secp256k1_1init_1context - (JNIEnv* env, jclass classObject) -{ - secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); - - (void)classObject;(void)env; - - return (uintptr_t)ctx; -} - diff --git a/native/jni/src/org_bitcoin_Secp256k1Context.h b/native/jni/src/org_bitcoin_Secp256k1Context.h deleted file mode 100644 index 0d2bc84..0000000 --- a/native/jni/src/org_bitcoin_Secp256k1Context.h +++ /dev/null @@ -1,22 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -#include "include/secp256k1.h" -/* Header for class org_bitcoin_Secp256k1Context */ - -#ifndef _Included_org_bitcoin_Secp256k1Context -#define _Included_org_bitcoin_Secp256k1Context -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: org_bitcoin_Secp256k1Context - * Method: secp256k1_init_context - * Signature: ()J - */ -SECP256K1_API jlong JNICALL Java_org_bitcoin_Secp256k1Context_secp256k1_1init_1context - (JNIEnv *, jclass); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/settings.gradle.kts b/settings.gradle.kts index 3caba25..b48c3ed 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -10,5 +10,8 @@ rootProject.name = "secp256k1-kmp" include( ":native", - ":jni" + ":jni", + ":jni:android", + ":jni:jvm" +// ":jni" ) \ No newline at end of file diff --git a/src/commonMain/kotlin/fr/acinq/secp256k1/utils.kt b/src/commonMain/kotlin/fr/acinq/secp256k1/utils.kt index d7c2733..db7ed5a 100644 --- a/src/commonMain/kotlin/fr/acinq/secp256k1/utils.kt +++ b/src/commonMain/kotlin/fr/acinq/secp256k1/utils.kt @@ -2,11 +2,11 @@ package fr.acinq.secp256k1 import kotlin.jvm.JvmStatic -internal object Hex { +public object Hex { private val hexCode = arrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f') @JvmStatic - fun decode(hex: String): ByteArray { + public fun decode(hex: String): ByteArray { val input = hex.filterNot { it.isWhitespace() } val offset = when { input.length >= 2 && input[0] == '0' && input[1] == 'x' -> 2 @@ -32,7 +32,7 @@ internal object Hex { } @JvmStatic - fun encode(input: ByteArray, offset: Int, len: Int): String { + public fun encode(input: ByteArray, offset: Int, len: Int): String { val r = StringBuilder(len * 2) for (i in 0 until len) { val b = input[offset + i] @@ -43,5 +43,5 @@ internal object Hex { } @JvmStatic - fun encode(input: ByteArray): String = encode(input, 0, input.size) + public fun encode(input: ByteArray): String = encode(input, 0, input.size) } \ No newline at end of file