feat: add custom C secp256k1 implementation for maximum platform performance

Add a complete C implementation of secp256k1 elliptic curve operations
alongside the existing Kotlin implementation, enabling direct comparison
and extraction of maximum performance from each platform (ARM64, x86_64).

C Implementation (quartz/src/main/c/secp256k1/):
- field.h/c: 5x52-bit limb field arithmetic with __int128 support and
  lazy reduction (12-bit headroom per limb vs Kotlin's fully-packed 4x64)
- scalar.h/c: Scalar mod n arithmetic, GLV decomposition, wNAF encoding
- point.h/c: Jacobian point operations (3M+4S double, 8M+3S mixed add),
  GLV+wNAF scalar multiplication, Strauss/Shamir dual scalar multiply,
  Montgomery batch-to-affine, precomputed G tables (wNAF-12)
- schnorr.c: BIP-340 Schnorr sign/verify/verifyFast/verifyBatch with
  pubkey decompression cache and precomputed tag hash prefixes
- sha256.c: Self-contained SHA-256 for BIP-340 tagged hashes
- secp256k1_c.h: Public API matching the Kotlin Secp256k1 object
- jni_bridge.c: JNI bridge for JVM/Android integration
- benchmark.c: Standalone C benchmark (cmake build)
- CMakeLists.txt: Build system with ARM64/x86_64 optimization flags

Kotlin Integration:
- Secp256k1InstanceC: expect/actual wrapper (commonMain/jvmMain/androidMain/nativeMain)
- Secp256k1C: JVM JNI binding class
- Secp256k1TripleBenchmark: Three-way JVM benchmark (ACINQ vs Kotlin vs Custom C)
- Secp256k1CBenchmark: Android benchmark for the C implementation

Current status: sign works correctly (verified against BIP-340 test vectors),
verify path needs ecmult_double_g debugging (GLV wNAF-12 table issue). The
comb table for ecmult_gen also needs fixing (currently falls back to GLV+wNAF).
Field arithmetic is fully verified: 5x52 limbs with R=0x1000003D10 fold.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
This commit is contained in:
Claude
2026-04-11 02:36:58 +00:00
parent 9bf628b823
commit 29b678ab14
21 changed files with 4337 additions and 0 deletions
@@ -0,0 +1,178 @@
/*
* Copyright (c) 2025 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
actual object Secp256k1InstanceC {
private var loaded = false
private fun ensureLoaded() {
if (!loaded) {
System.loadLibrary("secp256k1_amethyst_jni")
nativeInit()
loaded = true
}
}
private external fun nativeInit()
private external fun nativePubkeyCreate(seckey: ByteArray): ByteArray?
private external fun nativePubkeyCompress(pubkey: ByteArray): ByteArray?
private external fun nativeSecKeyVerify(seckey: ByteArray): Boolean
private external fun nativeSchnorrSign(
msg: ByteArray,
seckey: ByteArray,
auxrand: ByteArray?,
): ByteArray?
private external fun nativeSchnorrSignXOnly(
msg: ByteArray,
seckey: ByteArray,
xonlyPub: ByteArray,
auxrand: ByteArray?,
): ByteArray?
private external fun nativeSchnorrVerify(
sig: ByteArray,
msg: ByteArray,
pub: ByteArray,
): Boolean
private external fun nativeSchnorrVerifyFast(
sig: ByteArray,
msg: ByteArray,
pub: ByteArray,
): Boolean
private external fun nativeSchnorrVerifyBatch(
pub: ByteArray,
sigs: Array<ByteArray>,
msgs: Array<ByteArray>,
): Boolean
private external fun nativePrivKeyTweakAdd(
seckey: ByteArray,
tweak: ByteArray,
): ByteArray?
private external fun nativePubKeyTweakMul(
pubkey: ByteArray,
tweak: ByteArray,
): ByteArray?
private external fun nativeEcdhXOnly(
xonlyPub: ByteArray,
scalar: ByteArray,
): ByteArray?
actual fun init() = ensureLoaded()
actual fun compressedPubKeyFor(privKey: ByteArray): ByteArray {
ensureLoaded()
val pub65 = nativePubkeyCreate(privKey) ?: error("Invalid private key")
return nativePubkeyCompress(pub65) ?: error("Compression failed")
}
actual fun isPrivateKeyValid(il: ByteArray): Boolean {
ensureLoaded()
return nativeSecKeyVerify(il)
}
actual fun signSchnorr(
data: ByteArray,
privKey: ByteArray,
nonce: ByteArray?,
): ByteArray {
ensureLoaded()
return nativeSchnorrSign(data, privKey, nonce) ?: error("Sign failed")
}
actual fun signSchnorrWithXOnlyPubKey(
data: ByteArray,
privKey: ByteArray,
xOnlyPubKey: ByteArray,
nonce: ByteArray?,
): ByteArray {
ensureLoaded()
return nativeSchnorrSignXOnly(data, privKey, xOnlyPubKey, nonce) ?: error("Sign failed")
}
actual fun verifySchnorr(
signature: ByteArray,
hash: ByteArray,
pubKey: ByteArray,
): Boolean {
ensureLoaded()
return nativeSchnorrVerify(signature, hash, pubKey)
}
actual fun verifySchnorrFast(
signature: ByteArray,
hash: ByteArray,
pubKey: ByteArray,
): Boolean {
ensureLoaded()
return nativeSchnorrVerifyFast(signature, hash, pubKey)
}
actual fun verifySchnorrBatch(
pubKey: ByteArray,
signatures: List<ByteArray>,
messages: List<ByteArray>,
): Boolean {
ensureLoaded()
return nativeSchnorrVerifyBatch(
pubKey,
signatures.toTypedArray(),
messages.toTypedArray(),
)
}
actual fun privateKeyAdd(
first: ByteArray,
second: ByteArray,
): ByteArray {
ensureLoaded()
return nativePrivKeyTweakAdd(first, second) ?: error("Tweak add failed")
}
actual fun pubKeyTweakMulCompact(
pubKey: ByteArray,
privateKey: ByteArray,
): ByteArray {
ensureLoaded()
val compressedPub = ByteArray(33)
compressedPub[0] = 0x02
pubKey.copyInto(compressedPub, 1, 0, 32)
val result = nativePubKeyTweakMul(compressedPub, privateKey) ?: error("Tweak mul failed")
return result.copyOfRange(1, 33)
}
actual fun ecdhXOnly(
xOnlyPub: ByteArray,
scalar: ByteArray,
): ByteArray {
ensureLoaded()
return nativeEcdhXOnly(xOnlyPub, scalar) ?: error("ECDH failed")
}
}