fix: align all 3 secp256k1 benchmarks for fairness and comparability

JVM benchmark fixes:
- signSchnorr(cached pk): document that native baseline still derives
  pubkey (apples-to-oranges by design — shows what caching enables)
- privKeyTweakAdd: document .copyOf() penalty on native side (ACINQ
  wrapper mutates input, requiring defensive copy)
- Remove duplicate tweakMulCompact test (redundant with ecdhXOnly)
- Add cache-warming note to benchmark KDoc
- Change output "slower" → neutral "x" (0.7x isn't "slower")

Android benchmark fixes:
- Remove misnamed pubKeyCompressOurs (was identical to compressedPubKeyForOurs)
- Remove duplicate pubKeyCompress (was also doing create+compress)
- Clean up structure: matched Native/Ours pairs with clear section headers
- Add cache-warming note to benchmark KDoc
- Standardize test data comment for cross-platform comparability

K/Native benchmark fixes:
- Fix DCE risk in micro-benchmarks: use result-dependent chains
  (out→out for field ops, xor hiSink for multiplyHigh) so LLVM can't
  hoist constant computations out of the loop
- Add batch verify (was missing — JVM had it, Android/K/N didn't)
- Add -opt documentation warning in KDoc
- Remove unused pubkeyCreate/pubKeyCompress standalone benchmarks
  (compressedPubKeyFor already covers create+compress)
- Extract printResults helper to reduce duplication

All 3 benchmarks now:
- Use the same test data (same hex keys across JVM/Android/K/N)
- Measure the same core operations (verify, sign, compressedPubKeyFor,
  secKeyVerify, privKeyTweakAdd, ecdhXOnly/pubKeyTweakMulCompact)
- Document cache-warming effects in their KDoc
- Include batch verify (JVM + K/N; Android uses framework-managed tests)

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
This commit is contained in:
Claude
2026-04-09 01:58:54 +00:00
parent 715e6e598c
commit 0c27ed89bc
3 changed files with 160 additions and 172 deletions
@@ -26,13 +26,19 @@ import fr.acinq.secp256k1.Secp256k1 as NativeSecp256k1
/**
* Benchmark comparing the pure-Kotlin secp256k1 implementation against
* the native ACINQ/secp256k1-kmp JNI bindings.
* the native ACINQ/secp256k1-kmp JNI bindings on JVM (HotSpot C2).
*
* Each benchmark runs warmup iterations, then measures the timed iterations.
* Results are printed as ops/sec and relative speed.
* Each benchmark runs warmup iterations to trigger JIT compilation, then
* measures timed iterations. Results are printed as ops/sec and relative speed.
*
* NOTE: verifySchnorr and signSchnorr use the same pubkey repeatedly, so they
* benefit from the pubkey decompression cache and P-table cache. This is realistic
* for Nostr (feed from one author) but not representative of first-time verification.
*
* Run with: ./gradlew :quartz:jvmTest --tests "*.Secp256k1Benchmark"
*/
class Secp256k1Benchmark {
// Test data
// Test data (same across all 3 platform benchmarks for comparability)
private val privKey = hexToBytes("67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530")
private val msg32 = hexToBytes("243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89")
private val auxRand = hexToBytes("0000000000000000000000000000000000000000000000000000000000000001")
@@ -55,11 +61,7 @@ class Secp256k1Benchmark {
// ECDH test data
private val privKey2 = hexToBytes("3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3")
private val pubKey2Uncompressed =
hexToBytes(
"040A629506E1B65CD9D2E0BA9C75DF9C4FED0DB16DC9625ED14397F0AFC836FAE5" +
"95DC53F8B0EFE61E703075BD9B143BAC75EC0E19F82A2208CAEB32BE53414C40",
)
private val pub2xOnly = hexToBytes("c2f9d9948dc8c7c38321e4b85c8558872eafa0641cd269db76848a6073e69133")
private val h02 = byteArrayOf(0x02)
// ============================================================
@@ -78,7 +80,7 @@ class Secp256k1Benchmark {
override fun toString(): String =
String.format(
"%-25s Native: %,8d ops/s Kotlin: %,8d ops/s Ratio: %.1fx slower",
"%-25s Native: %,8d ops/s Kotlin: %,8d ops/s Ratio: %.1fx",
name,
nativeOpsPerSec,
kotlinOpsPerSec,
@@ -130,7 +132,7 @@ class Secp256k1Benchmark {
val results = mutableListOf<BenchResult>()
// --- verifySchnorr ---
// --- verifySchnorr (same pubkey = cache-warm, typical Nostr pattern) ---
results +=
bench(
name = "verifySchnorr",
@@ -146,7 +148,7 @@ class Secp256k1Benchmark {
},
)
// --- signSchnorr (derives pubkey from seckey each time) ---
// --- signSchnorr (derives pubkey from seckey each time — both sides do the same work) ---
results +=
bench(
name = "signSchnorr",
@@ -162,7 +164,11 @@ class Secp256k1Benchmark {
},
)
// --- signSchnorrWithPubKey (pre-computed pubkey, no self-verify — matches C) ---
// --- signSchnorr (cached pk) ---
// NOTE: The C library's signSchnorr always derives the pubkey internally.
// Our signSchnorrWithPubKey skips that derivation. This is NOT an apples-to-apples
// comparison — it shows what's possible when the caller caches the compressed pubkey.
// The native baseline here is the same signSchnorr (with pubkey derivation).
results +=
bench(
name = "signSchnorr (cached pk)",
@@ -209,32 +215,17 @@ class Secp256k1Benchmark {
},
)
// --- pubKeyTweakMul (ECDH) ---
// --- compressedPubKeyFor (create + compress combined) ---
results +=
bench(
name = "pubKeyTweakMul (ECDH)",
name = "compressedPubKeyFor",
warmup = 1000,
iterations = 3000,
nativeOp = { native.pubKeyTweakMul(pubKey2Uncompressed.copyOf(), privKey) },
iterations = 5000,
nativeOp = { native.pubKeyCompress(native.pubkeyCreate(privKey)) },
kotlinOp = {
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1.pubKeyTweakMul(
pubKey2Uncompressed,
privKey,
)
},
)
// --- privKeyTweakAdd ---
results +=
bench(
name = "privKeyTweakAdd",
warmup = 1000,
iterations = 50000,
nativeOp = { native.privKeyTweakAdd(privKey.copyOf(), privKey2) },
kotlinOp = {
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1.privKeyTweakAdd(
privKey,
privKey2,
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1.pubKeyCompress(
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1
.pubkeyCreate(privKey),
)
},
)
@@ -252,39 +243,25 @@ class Secp256k1Benchmark {
},
)
// --- compressedPubKeyFor (create + compress combined) ---
// --- privKeyTweakAdd ---
// NOTE: The ACINQ wrapper mutates its first argument, requiring .copyOf() on
// the native side. This adds one allocation to the native measurement.
results +=
bench(
name = "compressedPubKeyFor",
name = "privKeyTweakAdd",
warmup = 1000,
iterations = 5000,
nativeOp = { native.pubKeyCompress(native.pubkeyCreate(privKey)) },
iterations = 50000,
nativeOp = { native.privKeyTweakAdd(privKey.copyOf(), privKey2) },
kotlinOp = {
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1.pubKeyCompress(
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1
.pubkeyCreate(privKey),
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1.privKeyTweakAdd(
privKey,
privKey2,
)
},
)
// --- pubKeyTweakMulCompact (old pattern via pubKeyTweakMul) ---
val pub2xOnly = hexToBytes("c2f9d9948dc8c7c38321e4b85c8558872eafa0641cd269db76848a6073e69133")
results +=
bench(
name = "tweakMulCompact (old)",
warmup = 1000,
iterations = 3000,
nativeOp = { native.pubKeyTweakMul(h02 + pub2xOnly, privKey).copyOfRange(1, 33) },
kotlinOp = {
com.vitorpamplona.quartz.utils.secp256k1.Secp256k1
.pubKeyTweakMul(
h02 + pub2xOnly,
privKey,
).copyOfRange(1, 33)
},
)
// --- ecdhXOnly (actual Nostr ECDH path) ---
// --- ecdhXOnly (Nostr NIP-04/NIP-44 ECDH path) ---
// Native has no ecdhXOnly — compare against pubKeyTweakMul + extract x.
results +=
bench(
name = "ecdhXOnly (Nostr)",
@@ -300,7 +277,7 @@ class Secp256k1Benchmark {
// Print results
println()
println("=".repeat(90))
println("secp256k1 Benchmark: Native (ACINQ/JNI) vs Pure Kotlin")
println("secp256k1 Benchmark: Native (ACINQ/JNI) vs Pure Kotlin on JVM (HotSpot C2)")
println("=".repeat(90))
for (r in results) {
println(r)
@@ -308,7 +285,9 @@ class Secp256k1Benchmark {
println("=".repeat(90))
// ==================== Batch verification benchmark ====================
// Same pubkey, n events — the typical Nostr pattern (feed from one author)
// Same pubkey, n events — the typical Nostr pattern (feed from one author).
// Batch verify uses scalar+point summation: one mulDoubleG for the whole batch
// instead of n individual mulDoubleG calls.
val batchPub = kotlinXOnlyPub
for (batchSize in intArrayOf(4, 8, 16, 32)) {
val sigs = mutableListOf<ByteArray>()