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
@@ -36,8 +36,19 @@ import org.junit.runner.RunWith
* Android microbenchmark for all secp256k1 operations used by Nostr.
*
* Uses AndroidX Benchmark (Jetpack Microbenchmark) for accurate, warmed-up
* measurements on real Android hardware/emulator. Results appear in Android
* Studio's benchmark output with ns/op, allocations, and percentiles.
* measurements on real Android hardware. Results appear in Android Studio's
* benchmark output with ns/op, allocations, and percentiles.
*
* Tests are structured as matched pairs: "Foo" uses the native C library (ACINQ/JNI),
* "FooOurs" uses the pure-Kotlin implementation. This allows direct comparison of
* each operation between native and Kotlin on the same device.
*
* NOTE: verifySchnorr uses the same pubkey repeatedly, so it benefits 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.
*
* Test data is the same across all 3 platform benchmarks (JVM, Android, K/Native)
* for cross-platform comparability.
*
* Run with: ./gradlew :benchmark:connectedAndroidTest
*/
@@ -45,7 +56,7 @@ import org.junit.runner.RunWith
class Secp256k1Benchmark {
@get:Rule val benchmarkRule = BenchmarkRule()
// Test data
// Test data (same across all 3 platform benchmarks for comparability)
private val privKey = "67E56582298859DDAE725F972992A07C6C4FB9F62A8FFF58CE3CA926A1063530".hexToByteArray()
private val msg32 = "243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89".hexToByteArray()
private val auxRand = "0000000000000000000000000000000000000000000000000000000000000001".hexToByteArray()
@@ -54,19 +65,12 @@ class Secp256k1Benchmark {
private val compressedPubKey = Secp256k1Instance.compressedPubKeyFor(privKey)
private val xOnlyPub = compressedPubKey.copyOfRange(1, 33)
private val signature = Secp256k1Instance.signSchnorr(msg32, privKey, auxRand)
private val uncompressedPubKey by lazy {
val seckey2 = "3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3".hexToByteArray()
val compressed = Secp256k1Instance.compressedPubKeyFor(seckey2)
// We need an uncompressed key for pubKeyCompress benchmark
// Use the x-coordinate and compute full key
compressed // will be compressed for the benchmark
}
// ECDH test data
private val privKey2 = "3982F19BEF1615BCCFBB05E321C10E1D4CBA3DF0E841C2E41EEB6016347653C3".hexToByteArray()
private val pubKey2XOnly = Secp256k1Instance.compressedPubKeyFor(privKey2).copyOfRange(1, 33)
// ==================== Core Nostr operations ====================
// ==================== Core Nostr operations (Native C via ACINQ/JNI) ====================
@Test
fun verifySchnorr() {
@@ -89,8 +93,6 @@ class Secp256k1Benchmark {
}
}
// ==================== Key operations ====================
@Test
fun secKeyVerify() {
benchmarkRule.measureRepeated {
@@ -98,14 +100,6 @@ class Secp256k1Benchmark {
}
}
@Test
fun pubKeyCompress() {
// Compress an already-compressed key (fast path)
benchmarkRule.measureRepeated {
assertNotNull(Secp256k1Instance.compressedPubKeyFor(privKey))
}
}
@Test
fun privateKeyAdd() {
benchmarkRule.measureRepeated {
@@ -113,8 +107,6 @@ class Secp256k1Benchmark {
}
}
// ==================== ECDH (NIP-04, NIP-44) ====================
@Test
fun pubKeyTweakMulCompact() {
benchmarkRule.measureRepeated {
@@ -122,6 +114,8 @@ class Secp256k1Benchmark {
}
}
// ==================== Core Nostr operations (Pure Kotlin) ====================
@Test
fun verifySchnorrOurs() {
benchmarkRule.measureRepeated {
@@ -143,8 +137,6 @@ class Secp256k1Benchmark {
}
}
// ==================== Key operations ====================
@Test
fun secKeyVerifyOurs() {
benchmarkRule.measureRepeated {
@@ -152,14 +144,6 @@ class Secp256k1Benchmark {
}
}
@Test
fun pubKeyCompressOurs() {
// Compress an already-compressed key (fast path)
benchmarkRule.measureRepeated {
assertNotNull(Secp256k1InstanceOurs.compressedPubKeyFor(privKey))
}
}
@Test
fun privateKeyAddOurs() {
benchmarkRule.measureRepeated {
@@ -167,8 +151,6 @@ class Secp256k1Benchmark {
}
}
// ==================== ECDH (NIP-04, NIP-44) ====================
@Test
fun pubKeyTweakMulCompactOurs() {
benchmarkRule.measureRepeated {