Commit Graph

1942 Commits

Author SHA1 Message Date
Claude fbcd0d6326 feat: expose verifySchnorrFast in Secp256k1InstanceOurs + Android benchmark
Wire verifySchnorrFast (x-check only, no y-parity inversion) into:
- Secp256k1InstanceOurs wrapper for app-level usage
- Android benchmark as verifySchnorrFastOurs for Pixel 8 measurement

Expected: ~15% faster than verifySchnorrOurs (~100μs vs ~120μs on Pixel 8)

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 12:33:13 +00:00
Claude 6e589c693c perf: eliminate ~7 allocations per signature in batch verify
The per-signature loop in verifySchnorrBatch allocated ~7 objects per
signature: 4 LongArray(4) for r/s/e/rx/ry, 1 ByteArray for hash input,
1 ByteArray(32) for sha256 output, plus ScalarN.reduce intermediates.
For a batch of 16 signatures, that's ~112 allocations.

Replace with pre-allocated scratch buffers from PointScratch:
- r, s, e → scalarTmp1/2/3
- rx, ry → entryTmp/entryTmp2
- hashInput → hashBuf (reused across iterations)
- sha256 → sha256Into with bytesTmp1
- ScalarN.reduce → ScalarN.reduceTo (in-place)
- liftX → 4-arg variant with zInv as temp

Also eliminated 2 MutablePoint allocations outside the loop by reusing
scratch (entryResult for addPoints output).

JVM batch(32): 62,949 → 82,121 ev/s (+30%)
JVM batch(16): 64,116 → 81,270 ev/s (+27%)

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 03:33:04 +00:00
Claude e872a37e86 feat: add verifySchnorrFast for Nostr — skip y-parity inversion
BIP-340 verification checks both R.x == r AND R.y is even. The y-parity
check requires a full field inversion (~270 field ops, ~14% of verify).

verifySchnorrFast skips the y-parity check, verifying only the
x-coordinate in Jacobian coordinates (2 field ops, no inversion).

WHY THIS IS SAFE FOR NOSTR:
For a given x on secp256k1, there are exactly 2 points: (x, y_even) and
(x, y_odd). A signature producing the correct x but wrong y-parity would
require solving the discrete log — equivalent to forging the signature.
The y-parity check is defense-in-depth, not a distinct security boundary.

DO NOT use for Bitcoin/financial protocols — use verifySchnorr for strict
BIP-340 compliance.

JVM benchmark:
  verifySchnorrFast:  20,706 ops/s  (1.4× vs native C)
  verifySchnorr:      18,038 ops/s  (1.6× vs native C)
  Improvement: ~15% faster

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 03:00:41 +00:00
Claude c915c2b883 perf: eliminate ~15 allocations per signSchnorr, ~3 per verifySchnorr
Allocation audit from Android benchmark showed 19 allocs in signSchnorr
and 4 in verifySchnorr. Most were intermediate ByteArray/LongArray that
can be replaced with pre-allocated scratch buffers.

Changes:
- Add sha256Into() (expect/actual) that writes digest into existing buffer
  instead of allocating a new ByteArray(32) per call. Uses
  MessageDigest.digest(buf,off,len) on JVM/Android, CC_SHA256 on Apple.
- Add scratch byte buffers to PointScratch: hashBuf(256), bytesTmp1/2(32),
  scalarTmp1/2/3 for intermediate scalar results.
- Add ScalarN.reduceTo() allocation-free variant.
- Rewrite signSchnorrInternal to reuse scratch buffers for:
  - dBytes serialization (bytesTmp1 instead of U256.toBytes alloc)
  - AUX_PREFIX+auxrand hash (hashBuf instead of array concatenation)
  - auxHash XOR (scalarTmp1/2/3 instead of U256.fromBytes allocs)
  - nonce/challenge hash inputs (hashBuf instead of ByteArray alloc)
  - nonce scalar (scalarTmp1 instead of ScalarN.reduce alloc)
  - challenge scalar (scalarTmp3 instead of allocs)
  - e*d and k+e*d (splitK1/entryTmp2 instead of ScalarN.mul/add allocs)
- Rewrite verifySchnorr to use hashBuf and sha256Into for challenge hash.

Only the 64-byte output signature is allocated per sign call.
Verify allocates nothing for 32-byte messages (hashBuf is large enough).

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 02:32:09 +00:00
Claude 65652d1256 feat: add signSchnorrWithXOnlyPubKey for zero-copy cached signing
BIP-340 public keys always have even y, so the y-parity prefix byte
(0x02) is redundant when the caller already has the 32-byte x-only
pubkey. This new overload takes the x-only pubkey directly, avoiding:

1. The expensive G multiplication to derive the pubkey (~20μs on Android)
2. The 33→32 byte array copy that signSchnorrWithPubKey does internally

Added to:
- Secp256k1.signSchnorrWithXOnlyPubKey (core implementation)
- Secp256k1Instance (expect/actual, falls back to C lib's signSchnorr
  since the native C lib always derives pubkey internally)
- Secp256k1InstanceOurs (uses the optimized pure-Kotlin path)
- Nip01Crypto.signWithPubKey (app-level convenience)

The app's KeyPair already stores the 32-byte x-only pubkey. Callers
like EventAssembler.hashAndSign can pass it through to skip the
G multiplication entirely.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 02:17:25 +00:00
Claude 0c27ed89bc 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
2026-04-09 01:58:54 +00:00
Claude 715e6e598c perf: add -Xno-param/call/receiver-assertions to remove null checks
Kotlin generates Intrinsics.checkNotNullParameter at the entry of every
function taking non-null reference types. Bytecode audit showed:
  Before: 128 checkNotNullParameter calls across secp256k1 classes
  After:  8 (only expression-value checks in non-hot paths)

Per Schnorr verify, this eliminates ~4,000+ invokestatic calls.
On ART (~2-3ns each): saves ~8-12μs per verify.
On HotSpot: neutral (C2 already optimizes null checks to fast branches).

These flags are safe for this module: all internal secp256k1 functions
use non-null LongArray/MutablePoint parameters that are never null.
Applied at the module level (compilerOptions) so all targets benefit.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude ec087304ad perf: Jacobian x-check + inline wNAF zero-checks in verify
Two optimizations for verifySchnorr:

1. Jacobian x-coordinate check before toAffine:
   Instead of converting to affine first (1 inversion = ~270 field ops),
   check X == r·Z² in Jacobian coordinates (1 sqr + 1 mul = 2 ops).
   Invalid signatures (x mismatch) are rejected immediately without
   paying the inversion cost. Valid signatures still need inversion for
   the y-parity check. For Nostr, ~all sigs are valid so this mainly
   helps adversarial/spam rejection.

2. Inline wNAF zero-checks in mulDoubleG inner loop:
   ~70% of wNAF digits are zero. Previously, each still called
   addWnafMixedPP (function call overhead: null checks, frame setup).
   Now the zero-check is inlined before the call, avoiding ~364
   function calls per verify. On ART (5-8ns per call), this saves
   ~2-3μs. On HotSpot, the JIT already optimized this — no change.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude 41e54ca33e perf: use unfused path on JVM (smaller methods for HotSpot inlining)
The fused fieldMulReduceWith + crossinline lambda was designed for ART
which struggles with deep call chains. On HotSpot C2, it creates a
2351-bytecode method (exceeding FreqInlineSize=325) that can't be
inlined into FieldP.mul, and wastes 180 bytecodes on lambda param
shuffling that C2 must clean up.

The unfused path (U256.mulWide + FieldP.reduceWide) produces a tiny
40-bytecode fieldMulReduce that HotSpot easily inlines. HotSpot's 8+
level inlining depth handles the full chain down to the
Math.unsignedMultiplyHigh intrinsic (single MULQ on x86-64).

Benchmark on JVM 21 x86-64: equivalent performance (1.5× verify,
0.9× sign-cached vs native C). Cleaner bytecode with no lambda waste.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude 9334994ce3 feat: add Kotlin/Native benchmark for secp256k1 on linuxX64
Mirrors the JVM benchmark (Secp256k1Benchmark.kt) for cross-platform
comparison of the same Kotlin code across runtimes:

  K/Native LLVM AOT (-opt) on x86-64:
    verifySchnorr:  101,406 ns  (9,861 ops/s)
    signSchnorr:     80,424 ns  (12,434 ops/s)
    FieldP.mul:          43 ns  (22.8M ops/s)

  JVM HotSpot C2 on same machine:
    verifySchnorr:   53,600 ns  (18,640 ops/s)
    signSchnorr:     42,400 ns  (23,518 ops/s)

  K/N vs JVM: ~1.9× slower (vs C: ~2.9×)
  K/N vs C native: ~2.9× slower

Includes field-level micro-benchmarks (FieldP.mul/sqr/add/sub/inv,
unsignedMultiplyHigh, uLt) for isolating LLVM codegen quality.

Also adds -opt to native test compilations for benchmark accuracy
(without it, debug mode is ~12× slower).

Run: ./gradlew :quartz:linuxX64Test --tests "*.Secp256k1NativeBenchmark"

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude 6f3793e7ed docs: add performance rationale to secp256k1 optimization decisions
Document WHY each approach was chosen, what alternatives were tested,
and what the measured impact was — so future contributors don't
accidentally revert optimizations or repeat failed experiments.

Key decisions documented:
- uLt() expect/actual: why XOR on Android, Long.compareUnsigned on JVM,
  and why a shared inline fun in commonMain caused 30% JVM regression
- fieldMulReduceWith: why fused mul+reduce, why inline+crossinline,
  why NOT 5x52 limbs, why NOT single-method with all API branches
- @JvmField: why it's required on MutablePoint/AffinePoint/PointScratch,
  with bytecode counts showing ~7,450 + ~2,000 virtual getter calls
  eliminated per verify

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude 30151295ad perf: make uLt() platform-specific to avoid JVM regression
The inline uLt() using XOR trick in commonMain regressed JVM by ~30%
because HotSpot's Long.compareUnsigned is a JIT intrinsic (single
unsigned CMP + SETB), while the XOR trick generates 2 extra XOR insns
that HotSpot doesn't optimize away.

Convert uLt to expect/actual:
- JVM: Long.compareUnsigned (HotSpot intrinsic)
- Android: XOR trick (avoids ULong.constructor-impl NOOP calls)
- Native: XOR trick (no JVM intrinsics available)

JVM verify: 2.1x → 1.5x (restored, slightly better than 1.6x baseline)
Android: unchanged (still uses XOR trick)

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude aa9470e6bb perf: add @JvmField to eliminate ~7,450 virtual getter calls per verify
Bytecode analysis showed MutablePoint.x/y/z, AffinePoint.x/y, and all
PointScratch properties compile to invokevirtual getter calls instead
of direct field reads (getfield). Per verify:

  - MutablePoint getX/Y/Z: ~7,450 invokevirtual → getfield
  - PointScratch getT/getW/etc: ~2,000+ invokevirtual → getfield

Each invokevirtual has ~3-5ns overhead on ART vs ~1ns for getfield.
@JvmField eliminates the getter method entirely, compiling property
access to a direct field read. On non-JVM targets (iOS), @JvmField
is silently ignored.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude ab3a9076e3 perf: eliminate ULong.constructor-impl NOOP calls in unsigned comparisons
Bytecode analysis revealed that every `a.toULong() < b.toULong()`
comparison generates 2 invokestatic calls to ULong.constructor-impl
(NOOPs that return the input unchanged) plus Long.compareUnsigned.
Across all secp256k1 hot paths, this produced 554 NOOP invokestatic
calls in the bytecode, translating to ~18,000 wasted calls per verify.

Replace all toULong() comparisons with an inline uLt() helper that
uses the XOR-with-MIN_VALUE trick directly:

  (a xor Long.MIN_VALUE) < (b xor Long.MIN_VALUE)

This produces pure arithmetic bytecode (lxor, lcmp, ifge) with ZERO
method calls, eliminating all ULong.constructor-impl overhead.

Before: lload, invokestatic ULong.constructor-impl, lload,
        invokestatic ULong.constructor-impl, invokestatic
        Long.compareUnsigned, ifge  (6 bytecodes, 3 method calls)

After:  lload, ldc MIN_VALUE, lxor, lload, ldc MIN_VALUE, lxor,
        lcmp, ifge  (8 bytecodes, 0 method calls)

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude 5add793abe perf: split Android fieldMulReduce into per-API methods for ART JIT
The previous approach inlined all 3 API-level branches into a single
fieldMulReduce method (~600 DEX instructions). ART's register allocator
produces suboptimal code for such large methods, causing stack spills
that negate the benefit of eliminating wrapper calls.

Split each API-level path into its own private function (~200 DEX
instructions each). ART JIT-compiles only the hot one (fieldMulApi35
on API 35+) with full optimization, while the tiny dispatch function
(fieldMulReduce) is easily devirtualized and inlined.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:20 +00:00
Claude 084230937b perf: fuse field multiply+reduce to eliminate ART wrapper overhead
On Android (ART), the unsignedMultiplyHigh wrapper function has per-call
branching for API level detection that prevents ART's JIT from inlining
the intrinsic into the hot loop. This creates ~10,000 extra function
calls per signature verify (20 wrapper calls × 500 field muls).

This commit introduces fieldMulReduce/fieldSqrReduce as expect/actual
functions that fuse U256.mulWide + FieldP.reduceWide into a single
compilation unit. The multiply-high intrinsic is passed as a crossinline
lambda and inlined at each call site, producing platform-specific code
with zero wrapper overhead:

- Android API 35+: Math.unsignedMultiplyHigh inlined directly (UMULH)
- Android API 31-34: Math.multiplyHigh + correction inlined (SMULH)
- Android API <31: pure-Kotlin fallback inlined
- JVM: Math.unsignedMultiplyHigh inlined directly
- Native: pure-Kotlin fallback inlined

The API level check happens ONCE per fieldMulReduce call (outermost
branch) rather than per multiply-high call (innermost loop), so ART
profiles and JIT-compiles only the hot path.

https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
2026-04-09 01:29:19 +00:00
Claude 91eef089dc fix: handle external join in processCommit and relax KeyPackage ciphersuite check
processCommit now detects ExternalInit proposals and allows the sender
leaf index to equal tree.leafCount (the new joiner's slot), matching
RFC 9420 Section 12.4.3.2 external commit semantics.

MlsKeyPackage.decodeTls no longer rejects non-0x0001 ciphersuites so
that interop test vectors with other suites round-trip correctly.

https://claude.ai/code/session_01LpR6qsF6nep1RnXA9KbXGs
2026-04-08 14:14:24 +00:00
Claude b5ee266169 feat: implement Ed25519 and X25519 crypto for Apple/iOS and Linux native platforms
Replace TODO stubs with pure Kotlin implementations of Ed25519 (RFC 8032) and
X25519 (RFC 7748) for native targets, completing Marmot MLS support on iOS.
Shared field arithmetic over GF(2^255-19) is extracted to nativeMain. Also
fixes MarmotSubscriptionManagerTest assertions to account for the ownKeyPackageFilter
added to buildFilters().

https://claude.ai/code/session_01LpR6qsF6nep1RnXA9KbXGs
2026-04-08 13:19:38 +00:00
Claude ec78be6328 fix: update CommitOrderingTest and MarmotPipelineTest for new EpochCommitTracker API
EpochCommitTracker was refactored to use (groupId, epoch) keys instead
of epoch-only keys, but the tests weren't updated to match.

https://claude.ai/code/session_01WixuPGynMsDXj1tWkJQNsh
2026-04-08 12:54:21 +00:00
Vitor Pamplona d4959473d7 Merge pull request #2170 from vitorpamplona/claude/improve-secp256k1-performance-nFGT8
Optimize secp256k1 with unrolled arithmetic and caching
2026-04-08 08:41:06 -04:00
Vitor Pamplona a010f01fbf Avoids warnings 2026-04-08 08:31:54 -04:00
Claude 491c31d625 perf: increase cache sizes from 256 to 1024 for ~1000 followed pubkeys
With 256 slots and ~1000 followed users, the direct-mapped caches had
~75% collision rate (multiple pubkeys mapping to the same slot, causing
constant eviction and recomputation). Increasing to 1024 slots covers
most follow lists with minimal collisions.

Memory impact:
  pubkeyCache: 1024 × ~96 bytes = ~96KB (was ~24KB)
  pTableCache: 1024 × ~1KB = ~1MB (was ~256KB)
Total ~1.1MB — acceptable for mobile.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 03:06:46 +00:00
Claude df9c8a9f4b feat: add same-pubkey batch Schnorr verification (5-6x throughput)
Add verifySchnorrBatch(pub, signatures, messages) that verifies n
signatures from the same public key using scalar and point summation
instead of n individual mulDoubleG calls.

The batch equation exploits linearity of Schnorr signatures:
  (Σ sᵢ)·G = (Σ Rᵢ) + (Σ eᵢ)·P
This combines n verifications into:
  - n scalar additions for S = Σsᵢ and E = Σeᵢ (trivial)
  - n liftX + (n-1) addMixed for R_sum = ΣRᵢ (cheap point additions)
  - ONE mulDoubleG(S, P, -E) (the expensive EC operation, done once)
  - Check result - R_sum = O (no toAffine needed)

JVM benchmark results (same pubkey, 1000 iterations, 500 warmup):
  batch( 4):  4.1x faster than individual  (40,121 events/s)
  batch( 8):  5.3x faster                  (49,637 events/s)
  batch(16):  5.5x faster                  (49,815 events/s)
  batch(32):  5.9x faster                  (51,272 events/s)

This fits the Nostr pattern perfectly: when loading a profile or
connecting to a relay, many events from the same followed author
arrive together and can be batch-verified.

If the batch fails (returns false), the caller falls back to
individual verification to identify which signature(s) are invalid.

Security: by linearity, individual errors cannot cancel without
solving the discrete log. For extra hardening, duplicate events
from multiple relays can be verified individually as cross-checks.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 02:56:45 +00:00
Claude cf7e2d4b0d perf: specialize FieldP.sub and neg for secp256k1 prime structure
Inline the P = [P0, -1, -1, -1] constant into sub and neg instead
of going through generic U256.addTo/subTo + P array reads.

For sub (add-back P on borrow): when carry from limb 0 is 1, adding
P[1..3]=-1 with carry is identity (no-op). When carry is 0, just
subtract 1 with borrow propagation through limbs 1-3. Replaces a
full 4-limb generic addition with a conditional decrement.

For neg (P - a): uses bitwise NOT for limbs 1-3 (since P[i]=-1,
P[i]-a[i] = ~a[i]) with simple borrow propagation from limb 0.
Replaces a full 4-limb generic subtraction with P array reads.

These are called ~500 times per verify. On JVM/HotSpot the gain is
marginal (inlining already handles this), but on Android ART the
reduced function call depth and eliminated array loads should help.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 01:35:54 +00:00
Claude f912520ee2 perf: reuse pre-allocated scratch for all entry-point allocations
Pool toAffine/toAffineX temp LongArrays (zInv, zInv2, zInv3) into
PointScratch. Add scratch-aware overloads that hot paths now use,
keeping allocating convenience overloads for one-time init paths
(buildCombTable).

Consolidate verify/sign/pubCreate/ECDH scratch into shared entry*
fields in PointScratch (entryPx, entryPy, entryPoint, entryResult,
entryTmp, entryTmp2). This replaces the old verify-specific fields
(verifyPx/Py/R/S/E/Rx/Ry/PPoint/Result) with fewer, shared buffers.

Update all Secp256k1 entry points (pubkeyCreate, signSchnorr,
signSchnorrInternal, verifySchnorr, pubKeyTweakMul, ecdhXOnly)
to use scratch instead of per-call allocations.

Add KeyCodec.liftX overload accepting a temp buffer to avoid 1
LongArray allocation per call.

Eliminate 2 longArrayOf allocations in ScalarN.reduceWideTo by
reusing the output array as temporary storage for hi limbs.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 01:22:05 +00:00
Claude fc690d66c7 refactor: reorganize secp256k1 for clarity without affecting performance
Extract MutablePoint, AffinePoint, and PointScratch from Point.kt into
PointTypes.kt (140 lines). Rename Point.kt to ECPoint.kt (897 lines)
to match the single top-level declaration (ktlint convention).

Remove dead code:
- addWnafJacobian: private function never called (replaced by
  addWnafMixedPP with effective-affine tables)

Remove 5 KeyCodec wrapper functions from ECPoint that just delegated
(liftX, hasEvenY, parsePublicKey, serializeUncompressed,
serializeCompressed). Callers in Secp256k1.kt and PointTest.kt now
use KeyCodec directly, making ownership clear: KeyCodec owns key
encoding/decoding, ECPoint owns point arithmetic.

Update Secp256k1.kt header documentation with current benchmark
numbers (verify 1.7x, sign 0.9x faster than native, etc.) and
a concise list of optimizations.

No functional or performance changes — all 170 tests pass, benchmark
numbers unchanged.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 01:10:01 +00:00
Claude c7b164b845 perf: cache P-side wNAF tables in mulDoubleG for repeated pubkeys
In mulDoubleG (verify), the P-side wNAF-5 affine table (8 odd multiples
of P plus 8 GLV λ counterparts) is rebuilt from scratch on every call.
This costs ~437 field ops: 1 doublePoint + 7 addPoints + 8 β-multiplies
+ 1 batch inversion with full field inversion (~270 ops alone).

Add a 256-entry direct-mapped cache that stores these affine tables
keyed by the point's x-coordinate. On cache hit, the table build is
skipped entirely — the cached arrays are used directly (zero-copy).

This saves ~27% of mulDoubleG cost (~20% of total verify) for repeated
pubkeys. Combined with the liftX cache from the previous commit,
repeated-pubkey verifications now skip ~33% of the work.

JVM benchmark: verify ratio improved from 2.0x to 1.6-1.8x vs native C
(with the benchmark's 100% cache hit rate). Original baseline was 3.0x.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 00:55:54 +00:00
Claude bdf7ab1a15 perf: eliminate allocations in verify path and add pubkey cache
Add in-place ScalarN operations (mulTo, addTo, negTo) that write to
caller-provided output arrays instead of allocating. Add
Glv.splitScalarInto that uses pre-allocated scratch buffers from
PointScratch, eliminating ~26 LongArray allocations per call (called
2x per verify = ~52 allocs saved). Both mul and mulDoubleG now use
the allocation-free split path.

Pool all LongArray and MutablePoint allocations in verifySchnorr
into thread-local PointScratch (verifyPx/Py/R/S/E/Rx/Ry, verifyPPoint,
verifyResult). Add U256.fromBytesInto for decoding into pre-allocated
arrays. Total: ~14 object allocations eliminated per verify call.

Add pubkey decompression cache (256-entry direct-mapped) that skips
the liftX square root (~280 field ops) for repeated pubkeys. In Nostr,
the same authors are verified repeatedly, so cache hit rate is high.
This saves ~13% of verify cost per cache hit.

Increase benchmark warmup/iterations for more stable measurements
(e.g., verifySchnorr: 200/500 -> 2000/5000).

JVM benchmark result: verify ratio improved from 3.0x to 2.0x vs
native C (with 100% cache hit rate on the benchmark's single pubkey).

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-08 00:44:41 +00:00
Claude 8a6d1f1d44 perf: increase benchmark warmup and iterations for stable results
The previous counts (200 warmup / 500 iterations for verify) were too
low for reliable measurements on noisy VMs. Increase to 2000+ warmup
and 3000-50000 iterations depending on operation cost, giving the JIT
time to fully optimize and reducing variance between runs.

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-07 23:46:59 +00:00
Claude 1dd8edcd41 perf: optimize secp256k1 field arithmetic and point operations for Android
Unroll hot-path loops in U256 (mulWide, sqrWide, addTo, subTo) to
eliminate loop control overhead and array bounds checks that ART JIT
on Android does not optimize as aggressively as HotSpot. These functions
are called ~1,900× per signature verification.

Optimize unsignedMultiplyHighFallback to compute the unsigned 128-bit
product directly from 32-bit sub-products, avoiding the signed
multiplyHigh + correction path. Saves ~8 instructions per call on
Android < API 31 (~30,000 calls per verify).

Unroll FieldP.reduceWide carry propagation and inline the reduceSelf
subtraction (avoiding P array access and U256.subTo call). Unroll
FieldP.half with P[1..3]=-1 inlined as mask.

Replace copyFrom pattern in mul, mulG, and mulDoubleG with ping-pong
point buffers — alternate between two MutablePoints via reference swap
instead of copying 12 Longs after every addition. Eliminates ~170
copyFrom calls per verify and avoids the internal copy buffer in
doublePoint's out===inp path (~130 per verify). Also removes the
MutablePoint() allocation in mulG (3 LongArrays per sign/pubCreate).

https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx
2026-04-07 23:14:11 +00:00
Claude 4edfb816d2 feat: add removeMemberFromGroup and updateGroupMetadata to AccountViewModel
- Wire removeMember through AccountViewModel for UI access
- Add updateGroupMetadata to MlsGroupManager with Mutex protection

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:05:59 +00:00
Claude 11412b8873 feat: add remove member, edit group metadata, and formatting fixes
- Add removeMember() to MarmotManager and AccountViewModel
- Add updateGroupMetadata() to MarmotManager for MIP-01 name/description
- Add MarmotGroupData.toExtension() for encoding metadata as MLS extension
- Add proposeGroupContextExtensions() to MlsGroup
- Apply spotlessApply formatting fixes
- SecretTree: prune consumed generations below current minimum

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:05:44 +00:00
Claude 7d8937ca48 fix: SecretTree skipped keys, sentKeys cleanup, markAsRead, key zeroing
- H11: Cache skipped message keys in SecretTree for out-of-order decryption
- L1: Prune sentKeys map when exceeding 10000 entries
- L2: Prune consumed generations below current minimum
- H17: Call markAsRead when chat view is opened
- Key zeroing: Zero old private keys in KeyPackageRotationManager

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:04:49 +00:00
Claude 6e3ad1f86e fix: remaining HIGH/MEDIUM bugs - outer epoch fallback, atomic writes, unread UI
- H10: Add outer decryption epoch fallback using retained exporter secrets
- H17: Display unread count badge in MarmotGroupListScreen
- M24: Log corrupted state before deletion in restoreAll
- M25: Atomic write-to-temp-then-rename in AndroidMlsGroupStateStore
- Thread safety: Add Mutex to KeyPackageRotationManager

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:04:29 +00:00
Claude 4526beb4be fix: MEDIUM/LOW bugs - validation, unread tracking, TLS bounds, KeyPackage checks
- H17: Add unread count tracking to MarmotGroupChatroom
- M8: Add MAX_OPAQUE_SIZE bounds check to TLS deserialization
- M13: Add version/ciphersuite validation on KeyPackage deserialization
- M24: Add logging before deleting corrupted group state in restoreAll
- L1: Add size limit to sentKeys map in MlsGroup
- Additional UI fixes: leave group cleanup, error handling improvements
- Fix MarmotSubscriptionManagerTest for updated API

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:04:09 +00:00
Claude 8c8ab4bb2c fix: HIGH/MEDIUM Marmot bugs - thread safety, dedup, UI error handling
- H8: Add Mutex to MarmotSubscriptionManager for thread safety
- H10: Add retained exporter secret to MlsGroupState/RetainedEpochSecrets
  for outer decryption of out-of-order messages
- H14: Add error handling to CreateGroupScreen and MarmotGroupChatView
- H15: Add removeGroup() to MarmotGroupList, clean up after leave
- M3: Use SecureRandom for nostrGroupId generation
- M5: Add event deduplication to MarmotInboundProcessor
- M6: Add KeyPackage credential validation in MarmotManager.addMember()
- M7: Validate nostrGroupId matches WelcomeEvent h-tag

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:03:27 +00:00
Claude 29d1610d1a fix: critical Marmot/MLS bugs - crashes, crypto, protocol compliance
- C1: Fix leaveGroup() crash (used group state after deletion)
- C2: Fix X25519.bigIntegerToBytes ArrayIndexOutOfBoundsException
- C3: Add all-zeros DH check to prevent small-subgroup attacks
- C4: Fix path secret derivation off-by-one (RFC 9420 Section 7.4)
- C5: Use constant-time comparison for confirmation/membership tags
- C6: Stage signing keys in proposeSigningKeyRotation, promote on commit
- C7: Make commit conflict tracker per-(group,epoch) not just per-epoch
- C8: Fix tree deserialization leafCount for trimmed trees
- H8: Add Mutex-based thread safety to MlsGroupManager
- H9: Fix extractPrivateKeyBytes little-endian padding
- H12: Blank direct path on addLeaf (RFC 9420 Section 7.7)
- M14: Track actual leaf index from addLeaf for Welcome generation

https://claude.ai/code/session_018gVkmmYgMFtBH7G31pCk9N
2026-04-07 23:02:09 +00:00
Vitor Pamplona d64f018bc2 adds support for the Wake notification event 2026-04-07 18:15:13 -04:00
Vitor Pamplona 292f8a3850 Migrates the SHA256 hasher out of the threadpool 2026-04-06 19:22:24 -04:00
Vitor Pamplona 9b4d6247f0 Adds an android benchmark comparison for the new Secp 2026-04-06 18:00:46 -04:00
Vitor Pamplona fe5f07da3f Moves the hex operations to our own functions 2026-04-06 17:55:54 -04:00
Vitor Pamplona 094586ff41 Fixes the sign issue in the fallback function 2026-04-06 17:55:17 -04:00
Vitor Pamplona 7a8b7a5e9f Merge branch 'claude/port-secp256k1-kotlin-Ir8yz' of https://github.com/vitorpamplona/amethyst into claude/port-secp256k1-kotlin-Ir8yz 2026-04-06 17:13:21 -04:00
Vitor Pamplona 57eb0ef803 Merge branch 'main' into claude/port-secp256k1-kotlin-Ir8yz 2026-04-06 17:13:01 -04:00
Vitor Pamplona 8a914d3130 removes some warnings 2026-04-06 17:12:00 -04:00
Claude 034c1ab2d1 fix: replace ThreadLocal with KMP-compatible ScratchLocal expect/actual
ThreadLocal.withInitial is a JVM-only API that doesn't compile on
Kotlin/Native or iOS targets. Replace all usages in commonMain
(FieldP.kt, Point.kt) with a new ScratchLocal expect/actual:

- jvmMain/androidMain: delegates to java.lang.ThreadLocal (same behavior)
- nativeMain: holds value directly (Kotlin/Native coroutines are
  cooperative, scratch buffers don't need thread isolation)

https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg
2026-04-06 21:06:44 +00:00
Vitor Pamplona b60a0bcae8 Reverts the use of our own Secp256K1 until faster than native 2026-04-06 16:31:38 -04:00
Claude e0fc42e955 perf: Android API-level-gated unsignedMultiplyHigh + eliminate per-call branch
Three Android-specific optimizations:

1. Use Math.unsignedMultiplyHigh on API 35+ (Android 15): single UMULH
   instruction, eliminates the 4-insn signed→unsigned correction that
   the fallback path requires. Same optimization as our JVM 18+ path.

2. Use Math.multiplyHigh + correction on API 31-34 (Android 12-14):
   avoids the pure-Kotlin 4×32-bit sub-product fallback entirely.

3. Resolve API level check ONCE at class load via static final fields
   (HAS_MULTIPLY_HIGH, HAS_UNSIGNED_MULTIPLY_HIGH) instead of checking
   Build.VERSION.SDK_INT on every call. These functions are called 16×
   per field multiply (~12,000× per signature verify), so eliminating
   the per-call branch matters.

Performance tiers on Android:
  API 35+ (Android 15):  ~same as JVM 18+ (UMULH intrinsic)
  API 31-34 (Android 12-14): SMULH + 3 correction insns per product
  API 26-30 (Android 8-11): pure-Kotlin fallback (4 sub-products)

https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg
2026-04-06 20:14:45 +00:00
Claude 2bf911379f docs: update Secp256k1.kt with instruction-level C comparison
Add precise per-operation cost analysis from comparing our Kotlin
implementation with bitcoin-core/secp256k1's C source:

- doublePoint: 1,516 insns (Kotlin) vs 530 insns (C) = 2.9×
  - mul/sqr accounts for 76% of gap (UMULH+MUL+carry vs single MUL)
  - add/neg/half accounts for 24% (reduceSelf vs lazy magnitude tracking)
- Update performance numbers to Java 21 results (verify 3.4×, sign 1.1×)
- Document all optimizations implemented during this session
- Note that lazy reduction penalty (4.2× on cheap ops) is the main
  remaining algorithmic opportunity, but requires 5×52-bit limb change

https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg
2026-04-06 19:58:51 +00:00
Claude f9ec821107 docs+cleanup: inline hot-path functions, fix stale comments
- Add `inline` to hot-path tiny functions: U256.isZero, U256.testBit,
  FieldP.reduceSelf, FieldP.neg, MutablePoint.isInfinity. These are
  called thousands of times per EC operation; inline eliminates virtual
  call overhead (mostly helps Kotlin/Native; JVM JIT already inlines).
- Fix stale comment: G_TABLE_SIZE is 1024 for w=12 (was "64 for w=8")

https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg
2026-04-06 19:42:47 +00:00