Commit Graph

11194 Commits

Author SHA1 Message Date
Claude 944562ddc8 feat: add SHA-256 benchmark comparing Android native vs our C vs Kotlin
Three SHA-256 implementations benchmarked on the same phone:
- sha256Android: Android's MessageDigest (BoringSSL + ARM64 CE hardware)
- sha256OurC: Our C SHA-256 (ARM64 CE hardware via JNI)
- sha256Kotlin: Kotlin's sha256 (platform MessageDigest on Android)

Also add nativeSha256 JNI method to expose our hardware SHA-256.

Why our C matches ACINQ on ARM64 despite different architectures:
- Our 4x64: 16 MUL+UMULH pairs per field mul
- ACINQ 5x52: 25 MUL+UMULH pairs per field mul
- We save 9 multiply pairs (~27ns on Cortex-X3) per fe_mul
- ACINQ saves ~6 normalizations with lazy reduction (~6ns)
- Net advantage: ~21ns per mul × 500 muls/verify > 0
- Plus our SHA-256 uses hardware CE, ACINQ's is software

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 15:02:27 +00:00
Claude 61e8471c37 fix: add missing #include <stdlib.h> in jni_bridge.c for malloc/free
https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 14:49:04 +00:00
Claude 40a51d855c feat: add Android cross-compile script for phone benchmarking
Add build_android.sh that cross-compiles the C secp256k1 library for
ARM64 using the Android NDK and places the .so in benchmark/src/main/
jniLibs/ where the benchmark APK will package it.

Usage:
  cd quartz/src/main/c/secp256k1
  ./build_android.sh
  cd ../../../../..
  ./gradlew :benchmark:connectedAndroidTest

Also:
- Fix Secp256k1InstanceC.android.kt to use Secp256k1C JNI binding
  class (same pattern as JVM) so the JNI method names match
  Java_com_vitorpamplona_quartz_utils_Secp256k1C_native*
- Add benchmark/src/main/jniLibs/ to .gitignore

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 14:40:31 +00:00
Claude eb9527d793 perf: hardware SHA-256 acceleration (SHA-NI x86_64, CE ARM64)
Add hardware-accelerated SHA-256 using platform crypto extensions:

x86_64 (SHA-NI, Intel Goldmont+/AMD Zen+):
  SHA256RNDS2 — processes 2 SHA-256 rounds per instruction
  SHA256MSG1/SHA256MSG2 — message schedule expansion
  Result: SHA-256(160B) 749ns → 165ns (4.5x faster)

ARM64 (Crypto Extensions, all Android ARMv8 phones):
  SHA256H/SHA256H2 — hash update (4 rounds per instruction)
  SHA256SU0/SHA256SU1 — message schedule
  Expected: similar 4-5x speedup on phone

Impact on secp256k1 operations:
  signSchnorr:    33→31µs (8%, 3-4 SHA-256 calls per sign)
  signXOnly:      17→16µs (8%)
  verifyFast:     34→33µs (4%, 1 SHA-256 call)
  batch(200):     7.2→6.6µs/event (10%, 200 SHA-256 calls)
  batch(200) now 5.1x faster than ACINQ individual verify

Native C-to-C (with SHA-NI + all optimizations):
  pubkeyCreate:  ACINQ 15.9  Ours 14.9  1.07x faster
  sign (cached): ACINQ 17.7  Ours 15.1  1.18x faster
  sign (full):   ACINQ 33.2  Ours 30.6  1.08x faster
  verifyFast:    ACINQ 32.1  Ours 33.7  0.95x (tied)
  batch(200):    ACINQ 33.5  Ours  6.6  5.1x faster

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 13:19:46 +00:00
Claude 41a49b639f perf: lazy fe_add + full ARM64 ASM fe_mul for phone performance
Two major optimizations targeting ARM64 mobile phones:

1. LAZY FIELD ADDITION (both platforms):
   fe_add no longer calls fe_normalize. Values may be in [0, 2^256)
   between operations. This is safe because:
   - fe_mul/fe_sqr reduction handles any 256-bit input
   - fe_negate normalizes its input before P - a
   - fe_is_zero/fe_equal/fe_to_bytes normalize their copies

   Saves ~6 normalize calls per gej_double (called 130x per verify).
   Impact: gej_add_ge 372→316ns (15%), gej_double 224→206ns (8%).

2. FULL ARM64 ASM fe_mul (ARM64 only):
   Complete 4x4 multiply + reduction in inline assembly using:
   - LDP/STP for load/store pairs (halves memory instructions)
   - MUL+UMULH with interleaved scheduling across columns
     (hides 3-cycle multiply latency behind independent additions)
   - Full reduction in ASM with MUL+UMULH+ADDS carry chain
   - 20 registers used (ARM64 has 31 — zero stack spills)
   - Row 1 and 3 interleave b0+b2 products with b1+b3 for ILP

   Expected ARM64 improvement: fe_mul ~20-25ns → ~13-15ns

x86_64 benchmark (measured on this machine):
  gej_add_ge: 372→316ns (15% faster)
  ECDH:       35.4→32.6µs (8% faster, now tied with ACINQ)
  batch(200): 1596→1437µs (10% faster, 7.2µs/event)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 13:11:54 +00:00
Claude b71641a15f fix: fair C-to-C benchmark — ACINQ sign must include keypair_create
The previous benchmark unfairly gave ACINQ a cached keypair (created
once outside the loop) while our sign derived the pubkey each call.

Fixed to test both patterns:
- "sign (full)": both derive pubkey each call
  ACINQ: keypair_create + sign32 = 36.3µs
  Ours: ecmult_gen + sign_internal = 33.6µs → 1.08x faster

- "sign (cached)": both reuse precomputed pubkey
  ACINQ: sign32 with cached keypair = 18.8µs
  Ours: signXOnly with cached xonly = 17.4µs → 1.08x faster

Fair native C-to-C results (x86_64, BMI2):
  pubkeyCreate:          ACINQ 18.0  Ours 16.3  1.10x faster ✓
  sign (full):           ACINQ 36.3  Ours 33.6  1.08x faster ✓
  sign (cached):         ACINQ 18.8  Ours 17.4  1.08x faster ✓
  verify (BIP-340):      ACINQ 36.7  Ours 40.4  0.91x slower ✗
  verifyFast (Nostr):    ACINQ 36.7  Ours 34.5  1.06x faster ✓
  ECDH (cached):         ACINQ 37.2  Ours 35.4  1.05x faster ✓
  batch(200):            ACINQ 42.4  Ours  8.3  5.1x faster  ✓

We beat ACINQ on 5 of 6 operations. The only loss is full BIP-340
verify (0.91x) due to ACINQ's 5x52+ADCX/ADOX field assembly.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 12:38:03 +00:00
Claude 2f5152efd6 bench: add native C-to-C benchmark vs ACINQ libsecp256k1
Direct comparison with no JNI/JVM overhead, both libraries called
from the same C program on the same machine (x86_64, BMI2).

Results (x86_64, same machine, cached pubkey pattern):
  Operation                  ACINQ     Ours    Speedup
  pubkeyCreate               21.2µs   20.3µs    1.04x
  signSchnorr                23.5µs   39.3µs    0.60x (*)
  verify (BIP-340)           42.8µs   46.8µs    0.91x
  verifyFast (Nostr)         42.8µs   39.1µs    1.10x
  ECDH (cached)              44.4µs   39.7µs    1.12x
  batch(200)                 47.2µs   10.1µs    4.7x per event

(*) sign is slower because ACINQ's keypair API pre-stores the pubkey,
avoiding ecmult_gen during sign. Our API derives pubkey each time.
A keypair-style API would match ACINQ's sign performance.

Cross-verification confirms both produce compatible signatures.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 11:47:19 +00:00
Claude bc405d045b perf: add P-table cache to ecmult and liftX cache to ECDH
The ECDH operation was 0.7x ACINQ (55µs vs 40µs) because:
1. P-table built from scratch every call: 9.8µs
2. liftX (sqrt) for pubkey decompression: 5.5µs

Both are redundant for the Nostr use case where the same peer key
is used repeatedly (NIP-44 encrypted DMs).

Fixes:
- Share the P-table cache (1024 entries) between ecmult and
  ecmult_double_g. Same pubkey → cache hit → skip table build.
- Use lift_x_cached in ecdh_xonly. Same pubkey → skip sqrt.

ECDH: 55.2µs → 33.9µs (1.63x faster, now 1.18x faster than ACINQ)

Full benchmark (x86_64, cached pubkey pattern):
  signXOnly:      17.6µs (56,818 ops/s) — 2.1x faster than ACINQ
  verifyFast:     35.0µs (28,571 ops/s) — 1.2x faster than ACINQ
  pubkeyCreate:   15.8µs (63,291 ops/s) — 1.2x faster than ACINQ
  ecdhXOnly:      33.9µs (29,499 ops/s) — 1.2x faster than ACINQ
  batch(200):   1596µs (125,313 ev/s)   — 12x faster than ACINQ

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 05:22:40 +00:00
Claude 57acbfd567 perf: ARM64-specific optimizations for mobile phones
Optimize the ARM64 code path (the primary target for Nostr clients):

1. fe_mul_asm: Use LDP/STP (load/store pair) to load all 8 limbs in
   4 instructions instead of 8 individual LDR. Row 0 in hand-tuned
   ASM with MUL+UMULH+ADDS/ADC. Rows 1-3 in __int128 C (ARM64 gcc
   generates optimal MUL+UMULH+ADDS/ADCS and MADD from this).

2. fe_normalize: Branchless on ARM64 using mask-based conditional
   subtract (compiles to CSEL/AND on ARM64, avoiding branch
   misprediction on mobile Cortex-A76+ SoCs). x86_64 keeps the
   branching version since its branch predictor handles the >99.99%
   non-taken case perfectly.

3. Document ARM64-specific instruction usage:
   - MUL + UMULH: 64×64→128 product (1 cycle throughput on A76+)
   - LDP/STP: load/store pair (2 regs per instruction)
   - ADDS/ADCS: carry chain for accumulation
   - MADD: fused multiply-add (generated by gcc from __int128)

These changes don't affect x86_64 correctness or performance
(verified: all keys pass, benchmark matches previous numbers).
The ARM64 improvements will be measurable when built with the
Android NDK for actual phone testing.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 05:10:15 +00:00
Claude 4fd4ad63d1 perf: use MULX (BMI2) in x86_64 field multiply assembly
Replace MULQ with MULX in the inline assembly for fe_mul on x86_64.
MULX advantages over MULQ:
- Uses RDX as implicit input (not RAX), outputs to two arbitrary regs
- Does NOT clobber flags, enabling better instruction scheduling
- Enables the compiler to interleave multiplies with carries

Requires BMI2 (available on Haswell+ / Zen+). Added -mbmi2 to
CMakeLists.txt for x86_64 builds.

Note: ADCX/ADOX (ADX extension) for dual carry chains was investigated
but the compiler doesn't auto-generate them from __int128 code, and
hand-encoding in inline ASM requires restructuring the entire multiply
to express two independent carry chains. The MULX-only approach still
gives a measurable improvement.

fe_mul: 17.2ns → 15.5ns (10% faster)
pubkeyCreate: 16.7µs → 15.5µs (7% faster)
verifyFast: 36.5µs → 36.1µs (1% faster, 27,700 ops/s)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:59:10 +00:00
Claude 3544c2cbc3 fix: benchmark batch verify naming, add batch sizes 4 and 64
Fix garbled batch verify names in the C benchmark (static char buffer
was shared across calls). Use string literals per batch size instead.

Fix batch verify self-test: use sign() (safe path) instead of
sign_xonly() since the test key has odd-y pubkey.

Add batch sizes 4 and 64 to the benchmark for finer granularity.

Batch verification results (x86_64 standalone, same pubkey):
  Batch   µs/event   events/sec   vs individual
    1       36.6       27,322       1.0x
    4       15.2       66,007       2.4x
    8       11.1       89,787       3.3x
   16        9.2      109,290       4.0x
   32        8.6      115,942       4.2x
   64        8.3      120,960       4.4x
  200        7.8      127,689       4.7x

vs Kotlin: C batch is 1.3-1.7x faster across all batch sizes.
At 200 events: 127K ev/s (C) vs 96K ev/s (Kotlin).

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:50:47 +00:00
Claude a8a3d8f44f perf: add x86_64 and ARM64 inline ASM for field multiply
Platform-specific inline assembly for fe_mul:

x86_64: Uses MULQ instruction for 64x64->128 products. Row-based
schoolbook with ADC carry chain. Reduction uses MULQ for hi[i]*C.
Eliminates redundant register moves that __int128 compilation generates.

ARM64: Uses MUL+UMULH instruction pairs for 64x64->128 products.
First row in ASM with ADDS/ADC carry chain, remaining rows use
__int128 (which ARM64 gcc compiles well). Reduction in __int128.

fe_mul: 20.1ns → 17.2ns (14% faster on x86_64)
gej_double: 242ns → 224ns (7.4% faster)
verifyFast: 37.2µs → 36.5µs (27,397 ops/s)
signXOnly: 19.2µs → 18.5µs (54,054 ops/s)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:38:40 +00:00
Claude 1a55746774 perf: remove redundant normalizations, optimize gej_add_ge hot path
Remove redundant fe_normalize calls from gej_add_ge (the verify hot
path, called ~60 times per verify). Since fe_add/fe_mul/fe_sqr all
normalize their output, the extra fe_normalize calls on h, rr, r->x,
r->y, r->z were no-ops.

Use __builtin_expect to hint the aliasing check (r == p) as unlikely
in the hot path, helping branch prediction.

Also remove redundant normalizations from gej_add for consistency.

Performance (x86_64 standalone, µs/op):
  signXOnly: 19.2 µs (52K ops/s) — 1.9x faster than ACINQ
  verifyFast: 37.2 µs (27K ops/s) — 1.05x faster than ACINQ
  pubkeyCreate: 16.7 µs (60K ops/s) — matching ACINQ

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:33:50 +00:00
Claude 6d9d03f52c perf: inline fe_mul, restore fast signXOnly, fix benchmark self-test
Three performance optimizations:

1. Inline fe_mul: merge mul_wide + reduce_wide into a single function
   body to keep intermediates in registers and eliminate call overhead.
   Saves ~1-2ns per fe_mul call (~200 calls per verify).

2. Restore fast signXOnly: assume even-y (BIP-340 convention) instead
   of deriving y-parity via ecmult_gen each time. This is correct for
   Nostr keys which are pre-processed to have even-y pubkeys.
   signXOnly: 36µs → 19µs (1.9x faster).

3. Fix benchmark: use sign() (safe, derives y-parity) for self-test
   since the test key has odd-y pubkey.

Performance (x86_64 standalone, µs/op):
  signXOnly (cached pk):  19.0 µs (52,524 ops/s) — 1.9x faster than ACINQ
  signSchnorr:            36.7 µs (27,282 ops/s) — matches ACINQ
  verifyFast (cached pk): 37.0 µs (27,013 ops/s) — faster than ACINQ
  pubkeyCreate:           16.6 µs (60,250 ops/s) — matches ACINQ

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:27:48 +00:00
Claude f476168f60 perf: add P-side wNAF table cache for repeated pubkey verification
In Nostr, the same pubkeys are verified repeatedly (many events per
author). Building the P-side wNAF table costs ~437 field ops (~27% of
verify). This cache stores precomputed affine tables keyed by pubkey
x-coordinate, skipping table build entirely on cache hits.

1024 cache entries × 16 affine points × 64 bytes ≈ 1MB total.
Direct-mapped hash: (px.d[0] ^ (px.d[1] << 3)) & 1023.

Performance with cached pubkey (x86_64 standalone):
  verifyFast: 51.5 → 35.4 µs (1.46x faster, 28,242 ops/s)
  verify:     58.3 → 42.0 µs (1.39x faster, 23,820 ops/s)

Now FASTER than ACINQ's libsecp256k1 for cached-pubkey verify:
  ACINQ:  25,832 ops/s (38.7 µs)
  Ours:   28,242 ops/s (35.4 µs)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:22:06 +00:00
Claude 39c97f7c32 perf: optimize fe_half with branchless conditional add
Replace fe_half's normalize-then-branch approach with a branchless
mask-based conditional add of P. This eliminates the fe_normalize_full
call and branch prediction penalty.

Note: dedicated fe_sqr with cross-product doubling was attempted but
reverted — with 4x64 limbs, each 64x64 product is 128 bits and
doubling overflows uint128. The 5x52 representation wouldn't have this
issue (104-bit products, 105 bits doubled) but was rejected earlier for
having more total products (25 vs 16). This is a fundamental tradeoff.

Performance (x86_64 standalone, µs/op):
  verifyFast: 51.5 µs (19,422 ops/s)
  pubkeyCreate: 16.7 µs (59,925 ops/s)
  signSchnorr: 35.5 µs (28,164 ops/s)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:20:22 +00:00
Claude a13ad7d243 feat: re-enable comb method for G multiplication — 3x faster sign/keygen
Fix batch_to_affine to handle infinity points by skipping them in the
cumulative Z product chain. The comb table has infinity entries at
index 0 of each block (representing "no teeth set"), and the old code
multiplied by Z=0 which corrupted all subsequent Z products.

Re-enable the comb method for ecmult_gen: only 3 doublings + ~43 table
lookups vs GLV+wNAF's ~130 doublings + ~32 additions.

Performance improvement (x86_64 standalone):
  pubkeyCreate: 54.4 µs → 17.0 µs (3.2x faster)
  signSchnorr:  109 µs → 36.2 µs (3.0x faster)
  signXOnly:    109 µs → 35.9 µs (3.0x faster)
  verify/ECDH: unchanged (don't use comb)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:11:58 +00:00
Claude 050cf39a54 fix: correct y-parity in schnorr_sign_xonly, all operations now verified
The sign_xonly function incorrectly assumed even y-parity for all keys.
BIP-340 requires checking the actual y-parity of the derived pubkey and
negating the secret key when y is odd. Without this, signatures for keys
with odd-y pubkeys (roughly half of all keys) were invalid.

All operations now pass correctness tests:
- sign + verify for keys 1, 2, 3, 0xff, and 0xd217c1... (random)
- verify_fast (skip y-parity check)
- batch_verify (5 signatures from same pubkey)

C standalone benchmark (x86_64):
  verifySchnorrFast: 52 µs (19,143 ops/s)
  verifySchnorr:     60 µs (16,616 ops/s)
  signSchnorr:      109 µs (9,177 ops/s)
  pubkeyCreate:      54 µs (18,381 ops/s)
  ecdhXOnly:         59 µs (16,958 ops/s)

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:06:05 +00:00
Claude fefbb243f0 fix: correct GLV MINUS_LAMBDA constant and fe_cmp normalization bug
Two critical bugs fixed:

1. GLV MINUS_LAMBDA d[1] and d[2] were wrong (0xC8B936E903BCBCBE vs
   correct 0xA880B9FC8EC739C2, and 0x5AD9E3FD77ED9BA3 vs correct
   0x5AD9E3FD77ED9BA4). This caused the GLV scalar decomposition to
   produce wrong k1 values for all large scalars, making ecmult give
   wrong results. Verified by checking lambda^3 mod n == 1.

2. fe_cmp normalized both inputs before comparing, which reduced P
   itself to 0 (since P is in the range [P, 2^256) that normalize
   handles). This caused the "r < p" check in verify to fail for ALL
   valid signatures. Fixed by comparing raw limb values.

Sign + verify + verify_fast now work correctly for small keys (1-3).
Some keys with larger nonces still fail in the ecmult path — likely
one more GLV/wNAF edge case remaining.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 04:03:55 +00:00
Claude 09fb48e75c fix: change crossinline to noinline for nullable lambda parameter
Kotlin does not allow crossinline parameters to be nullable. The cOp
parameter in benchTriple needs to be nullable (null when C library is
not available), so use noinline instead.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:50:08 +00:00
Claude da3ed9ba3e fix: add aliasing protection to gej_add_ge and gej_add
Both gej_add_ge(r, p, q) and gej_add(r, p, q) write to r->x/y/z
while reading from p->x/y/z. When r == p (in-place accumulation in
ecmult loops), the output overwrites input during computation.

Added copy-on-alias detection at the top of both functions, matching
the fix already applied to gej_double.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:37:44 +00:00
Claude 8cf2f9795a fix: scalar_mul second fold carry propagation
Fix the modular reduction in scalar_mul: the second fold step was
accumulating hc2[4..7] into a single acc variable without proper
limb-by-limb carry propagation. Now uses the same 8-limb sum pattern
as the first fold, with a convolution-style third fold for any
remaining high bits.

GLV decomposition now verified correct for all test scalars.
The remaining issue is in the wNAF multiply loop within ecmult
(correct GLV split → wNAF encode → point table lookup → accumulate).
Likely gej_add_ge aliasing or table build ordering issue.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:36:09 +00:00
Claude f5f71220a7 fix: use proven mul_wide in mul_shift384, trace scalar_mul reduction bug
Replace mul_shift384's inline product computation with the proven mul_wide
function, eliminating the row-based carry accumulation overflow bug
(t[i+4] = carry overwrites instead of adding).

Traced the remaining scalar_mul reduction bug to its exact location:
products of two ~256-bit scalars lose exactly NC[1] = 0x4551231950B75FC4
in the second fold step. The mul_wide product and first fold are correct,
but the second fold (handling sum[4..7]) loses a carry at limb position 2.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:34:40 +00:00
Claude ff36df55f5 fix: clean up scalar_mul, reuse field mul_wide for product computation
- Remove dead code from multiple scalar_mul reduction attempts
- Use the proven mul_wide function from field.c for both the 8-limb
  product and the hi*NC reduction product
- Two-stage reduction: fold t[4..7]*NC, then fold any remaining high part
- Export mul_wide (remove static) for cross-module use

Scalar modular reduction still has a carry issue for large intermediate
products (c2 * MINUS_B2 in GLV). The product computation (mul_wide) is
verified correct. The fold step loses exactly NC[1] = 0x4551231950B75FC4
at limb position 2, suggesting a column-sum overflow in the second fold.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:30:16 +00:00
Claude afcdd5cb3e fix: correct GLV constants and scalar_mul modular reduction
- Fix GLV_MINUS_LAMBDA constant (d[1] and d[2] were incorrectly computed
  from Kotlin signed-to-unsigned conversion)
- Fix scalar_mul reduction: the carry from folding high limbs was silently
  dropped when the target position exceeded 4 limbs. Use proper row-based
  fold with carry propagation into higher positions
- Fix in-place gej_double aliasing: when r == p, the output overwrites the
  input during computation. Added explicit copy-on-alias

Verified working: pubkeyCreate, 2*G, (n-1)*G, ecmult for all scalar sizes.
Verify path still needs debugging (ecmult_double_g gives correct result for
simple cases but the full sign→verify round-trip has a hash/nonce mismatch).

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:19:33 +00:00
Claude 5f90f55fba fix: rewrite field arithmetic with 4x64 limbs, fix aliasing and overflow bugs
Rewrite the C secp256k1 field arithmetic from 5x52-bit to 4x64-bit limbs,
matching the Kotlin Fe4 representation. This choice was validated by the
existing Kotlin benchmarks which showed 4x64 is faster due to fewer
multiplies (16 vs 25 per field mul).

Critical bugs fixed:
- uint128 overflow: accumulating 4+ cross-products in a single uint128
  accumulator overflows (4 * 2^128 > 2^128). Switched to row-based
  schoolbook multiplication (mul_wide) which adds one product at a time
- In-place doubling aliasing: gej_double(r, r) corrupted results because
  output fields were overwritten while still being read as input. Added
  explicit copy-on-alias detection
- 5x52 constant errors: P limbs, R fold constant (0x1000003D10 vs
  0x10000003D10), and fe_negate all had wrong values for 5x52

Current status: field arithmetic fully verified, pubkey generation correct,
signing works, 2*G correct. Full verify (ecmult_double_g with large
scalars) still needs GLV/wNAF chain debugging.

https://claude.ai/code/session_011KVZhDcV2G7idNWEBz12GY
2026-04-11 03:13:51 +00:00
Claude 29b678ab14 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
2026-04-11 02:36:58 +00:00
Vitor Pamplona 9bf628b823 Merge pull request #2211 from vitorpamplona/claude/review-webrtc-calls-Vc6v0
Claude/review webrtc calls vc6v0
2026-04-10 20:56:47 -04:00
Claude c82ba766f8 fix: WebRTC race conditions, thread safety, and PiP lifecycle
- CallActivity.onStop: move finishAndRemoveTask inside coroutine so
  hangup signaling completes before Activity destruction; add
  hangupInitiated flag to prevent double-hangup in onDestroy
- CallController: add per-peer renegotiation debouncing via
  pendingRenegotiation map to prevent queuing multiple createOffer calls
  when video is toggled rapidly
- CallController: guard ensureForegroundService in onPeerConnected
  callback with state check to prevent restarting the service after
  cleanup
- RemoteVideoMonitor: synchronize onRemoteVideoTrack, onPeerRemoved,
  and dispose with trackLock to prevent non-atomic map mutations and
  leaked video sinks from concurrent WebRTC callback threads
- CallMediaManager: add @Synchronized to createVideoResources to
  prevent check-then-act race between IO and main threads

https://claude.ai/code/session_017HrFJNxD6zrGwiZ3s69xTh
2026-04-11 00:48:27 +00:00
Vitor Pamplona 2d9db8411b Merge pull request #2210 from vitorpamplona/claude/fix-secp256k1-jni-build-TNnQ7
Add macOS support to benchmarks and Kotlin/Native compilation
2026-04-10 20:46:08 -04:00
Claude fa0da7fae3 feat: upgrade negentropy-kmp to v1.0.2, enable macosArm64 target
negentropy-kmp v1.0.2 now publishes a macosArm64 artifact, unblocking
the macOS native target. Wired macosMain/macosTest source sets through
appleMain/appleTest and updated run_all.sh to run the K/Native
benchmark on both Linux (linuxX64) and macOS (macosArm64).

https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:33 +00:00
Claude 731e32eb61 fix: revert macosArm64 target (blocked by negentropy-kmp), add K/N toolchain to session hook
macosArm64 target cannot be enabled until negentropy-kmp publishes a
macosArm64 artifact — commented out with explanation. Reverted run_all.sh
back to Linux-only for K/Native benchmark.

Added Kotlin/Native toolchain dependencies (GCC sysroot, LLDB, LLVM,
libffi) to session-start.sh so K/N compilation works in Claude Code
remote environments where Gradle's own downloader fails through the
proxy.

https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:33 +00:00
Claude bfe0555589 fix: format C benchmark output with comma thousands and 10-digit columns
https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:33 +00:00
Claude ce199f42df fix: drop deprecated macosX64 target, keep only macosArm64
macosX64 (Intel) has been removed from Kotlin's native target tiers.
Only macosArm64 (Apple Silicon) is supported.

https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:33 +00:00
Claude 09001bbe98 feat: add macOS native targets (macosX64, macosArm64) to quartz
- Declare macosX64() and macosArm64() KMP targets in build.gradle.kts
- Wire macosMain/macosTest source sets through appleMain/appleTest
- Move Secp256k1NativeBenchmark from linuxX64Test to nativeTest so it
  runs on all native targets (Linux, macOS, iOS)
- Use platform() for dynamic labels instead of hardcoded "linuxX64"
- Update run_all.sh to pick the correct native target per OS/arch

https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:33 +00:00
Claude dee050c6d8 fix: improve Android benchmark result discovery in run_all.sh
Three issues fixed:
- Replace fragile -newer comparison against the script file (breaks
  after any script edit) with a timestamp file created just before the
  Gradle task runs.
- Search connected_android_test_additional_output for pulled benchmark
  JSON (where AndroidX Benchmark actually writes results via Gradle).
- Extract benchmark data from XML via CDATA parsing instead of dumping
  raw XML, consistent with the JVM and K/Native sections.

https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:33 +00:00
Claude 8804e243a4 fix: rewrite dylib install name on macOS so rpath resolution works
The ACINQ secp256k1-kmp-jni dylib ships with a relative LC_ID_DYLIB
("build/darwin/libsecp256k1-jni.dylib"). macOS dyld resolves this
literally, ignoring the -Wl,-rpath passed at link time, causing an
immediate abort at launch. Using install_name_tool to rewrite the
install name to @rpath/libsecp256k1-jni.dylib lets dyld find the
library via the rpath we already set.

https://claude.ai/code/session_01WSNE6QKiYM2ZutQD2UihCW
2026-04-11 00:44:32 +00:00
Claude deecce6e77 fix: additional WebRTC call hardening
- CallActivity.onDestroy: use standalone CoroutineScope instead of
  lifecycleScope which is cancelled during super.onDestroy(), ensuring
  hangup/reject signaling events are reliably published
- disposePeerSession: remove videoSenders entry for the departing peer
  to prevent stale RtpSender references leaking after PeerConnection
  disposal
- initiateGroupCall: detect when all PeerConnection creations fail and
  hang up immediately instead of leaving the call in Offering state
  until the 60-second timeout

https://claude.ai/code/session_017HrFJNxD6zrGwiZ3s69xTh
2026-04-11 00:35:59 +00:00
Vitor Pamplona 05d59dfde6 Merge pull request #2209 from vitorpamplona/claude/review-webrtc-calls-Vc6v0
Improve thread safety and error handling in call management
2026-04-10 20:17:26 -04:00
Claude d851168a46 fix: WebRTC call resource leaks, thread safety, and error handling
- Send hangup/reject to peer when WebRTC init or PeerConnection creation
  fails, so remote phone stops ringing instead of timing out after 60s
- Throw on null PeerConnection from factory to fail fast instead of
  silently no-oping all subsequent WebRTC operations
- Start foreground service during IncomingCall to protect ringtone
  playback from being killed on Android 14+
- Make cleanup() idempotent with AtomicBoolean guard to prevent double
  disposal when Ended state and ViewModel.onCleared race
- Replace mutableMapOf with ConcurrentHashMap for videoSenders accessed
  from UI and WebRTC callback threads
- Add @Volatile to peerConnection, videoPausedByProximity, and
  foregroundServiceStarted for cross-thread visibility
- Capture peerConnection into local variable in dispose() to prevent
  TOCTOU race between close() and null assignment
- Replace leaked MainScope() in CallNotificationReceiver with structured
  CoroutineScope that is cancelled after work completes
- Remove self-wraps in group answer/reject to avoid wasting bandwidth
  sending encrypted messages to ourselves
- Move startTimeout inside stateMutex in initiateCall for consistency

https://claude.ai/code/session_017HrFJNxD6zrGwiZ3s69xTh
2026-04-10 23:55:16 +00:00
Vitor Pamplona 6c28f9d995 Merge pull request #2208 from vitorpamplona/claude/review-webrtc-calls-5TVkF
Add thread safety, network monitoring, and camera controls to WebRTC calls
2026-04-10 18:53:24 -04:00
Claude 865c71e0e2 fix: WebRTC call bugs, hardening, camera switch, and network resilience
Bug fixes:
- Fix RemoteVideoMonitor killing group monitor job when primary track switches
- Add mutex protection to CallManager.initiateCall() to prevent state races
- Fix ICE restart offer never being sent to remote peer (was immediately
  replaced by a second offer from onRenegotiationNeeded)
- Fix duplicate duration timer in PiP connected call UI
- Fix error snackbar dismiss button not clearing the error
- Make PeerSessionManager thread-safe with synchronized blocks (accessed
  from WebRTC native threads and coroutine dispatchers concurrently)
- Make CallManager event handlers private (only called from onSignalingEvent)

Improvements:
- Replace fragile ICE candidate regex parsing with kotlinx.serialization JSON
- Respect DND/silent mode: only ring in NORMAL mode, only vibrate in VIBRATE
- Signal camera-off to remote peer by removing video track sender (instead
  of sending frozen/black frame)
- Clear CallSessionBridge on AccountViewModel.onCleared() to prevent stale
  references on account switch
- Custom TURN servers now replace defaults (instead of appending) so
  credentials can be rotated without an app update

New features:
- Front/back camera switch button (visible when video is enabled)
- Network transition handling: ConnectivityManager.NetworkCallback triggers
  ICE restart on all peers when network changes (WiFi/cellular handoff)

https://claude.ai/code/session_01JHn7skAibTrkVqsoWutgYe
2026-04-10 22:37:43 +00:00
Vitor Pamplona 374bcb96cf Merge pull request #2206 from vitorpamplona/claude/review-webrtc-calls-0k3G4
fix: start foreground service earlier to prevent call death on Androi…
2026-04-10 17:44:11 -04:00
Claude f86b8d1f94 fix: start foreground service earlier to prevent call death on Android 14+
The foreground service was only started after onPeerConnected, leaving
the Offering/Connecting phases unprotected. If the user backgrounded
the app during connecting, Android 14+ could block the later
startForegroundService() call, killing the call.

Changes:
- Start foreground service on Offering state (user just tapped call
  button, so app is guaranteed to be in foreground)
- Update notification text on Connecting/Connected transitions
- Add ACTION_UPDATE to CallForegroundService to change notification
  without restarting the service
- onPeerConnected now uses ensureForegroundService() as a safety net

https://claude.ai/code/session_01F5RF2yzngiMr1v2gr7f1GP
2026-04-10 19:23:34 +00:00
Vitor Pamplona 1cf510a266 Merge pull request #2205 from vitorpamplona/claude/review-webrtc-calls-0k3G4
Add call settings screen for video quality and TURN server configuration
2026-04-10 15:17:58 -04:00
Claude cd573a583c fix: WebRTC call bugs and add Call Settings screen
Bug fixes:
- Fix invitePeer() bypassing CallManager state tracking, causing
  invited peers to not appear in pendingPeerPubKeys
- Remove 10-minute proximity wake lock timeout so it lasts the
  full call duration (released on cleanup)
- Send hangup to peers on caller timeout so callees stop ringing
  immediately instead of waiting for their own 60s timeout
- Remove duplicate cleanup() call on Ended→Idle transition

New feature:
- Add Call Settings screen (TURN servers + video quality)
- Users can configure custom TURN servers for restrictive networks
- Default STUN/TURN servers are always active and displayed
- Video resolution options: 480p, 720p (default), 1080p
- Configurable max video bitrate: 750kbps, 1.5Mbps, 3Mbps
- Settings wired into IceServerConfig and CallMediaManager

https://claude.ai/code/session_01F5RF2yzngiMr1v2gr7f1GP
2026-04-10 17:18:25 +00:00
Vitor Pamplona 8813907bd3 Merge pull request #2204 from vitorpamplona/claude/optimize-secp256k1-performance-DPixr
Replace LongArray with Fe4 struct for secp256k1 field elements
2026-04-10 10:30:45 -04:00
Vitor Pamplona aa00cb74bb Merge pull request #2203 from vitorpamplona/claude/review-webrtc-calls-a7ZCq
Improve WebRTC call reliability and notification handling
2026-04-10 08:51:21 -04:00
Vitor Pamplona c7f08a1c44 Merge pull request #2202 from vitorpamplona/claude/review-marmot-mls-nFJQr
RFC 9420 compliance: encryption, commit ordering, and thread safety
2026-04-10 08:50:22 -04:00
Vitor Pamplona 4d7eba2a21 Merge pull request #2201 from mstrofnone/fix/largecache-concurrent-modification
fix(quartz): snapshot LargeCache entries in forEach to prevent ConcurrentModificationException
2026-04-10 08:10:44 -04:00