Skip to content

perf(core): speed up Schnorr signing, event serialization and filter matching - #571

Merged
tcheeric merged 10 commits into
developfrom
perf/secp256k1-library-arithmetic
Nov 25, 2026
Merged

tcheeric merged 10 commits into
developfrom
perf/secp256k1-library-arithmetic

Conversation

@tcheeric

@tcheeric tcheeric commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Signing, verification and public key derivation spent almost all their time in a hand-written affine double-and-add on BigInteger (256 steps, one modular inversion per step). Sign took about 29 ms and verify about 14 ms, three orders of magnitude above everything else in the event path. Serialization and filter matching also had cheap wins.

Type of change

  • perf - Performance improvement
  • test - Adding or updating tests
  • docs - Documentation only (CHANGELOG)

What changed?

Start with Point.java and Schnorr.java; the other two files are small.

  • Point.add and Point.mul now run on BouncyCastle's custom secp256k1 curve (already a dependency), with a precomputed comb multiplier for G. The public API and the BigInteger coordinates are unchanged.
  • New Point.mulSum(P, a, Q, b) computes a*P + b*Q in one go; Schnorr.verify uses it for s*G - e*P.
  • Schnorr.sign reuses the public key point it already holds in its self-check, and caches the repeated byte conversions. The self-check itself stays.
  • EventSerializer writes the NIP-01 array straight to a JsonGenerator instead of building a tree first. Output is byte-identical.
  • EventFilter.toPredicate() evaluates in one pass over hash sets instead of chained Predicate.and lambdas and per-event streams. Same matching semantics.

Measured with a throwaway harness (thread CPU time, minimum of 15 rounds, loaded 12-thread machine), geometric mean over seven workloads: 579 us to 63.5 us (9.1x).

Workload Before After
sign 29.1 ms 0.31 ms
verify 14.1 ms 0.15 ms
public key derivation 4.7 ms 0.08 ms
filter over 10k events 2.6 ms 0.95 ms

Behavior changes to know about

  • Schnorr.verify now returns false instead of throwing a NullPointerException when the verification point is the point at infinity.
  • Point.mul builds BouncyCastle points with createPoint, which skips the on-curve check. Points from liftX are on the curve by construction, so this only matters for hand-built Points.

Testing

  • Unit tests pass: mvn test for nostr-java-core, nostr-java-event and nostr-java-identity (94, 122 and 61 tests, 1 pre-existing skip), run on the merged branch
  • Integration tests pass: mvn verify (requires Docker). Not run.

New tests: official BIP-340 vectors 0 and 1, the off-curve public key vector 5 for verify, known multiples of G, mulSum against separate multiplications, and liftX behavior. Outside the repo, outputs were also checked byte-for-byte against the old code (100 public keys, deterministic signatures, 16 event ids and serialized forms, JSON round trip), and liftX was differential-tested against the old implementation on 4006 inputs.

Review focus

  • Is dropping the on-curve check in Point.toBc acceptable, or should Point.mul validate its input?
  • Point only defines equals(Point) (an overload), which I worked around in the tests rather than changing.

Checklist

  • PR title follows conventional commits: type(scope): description
  • Changes are focused and under 300 lines (246 added, 95 removed, 7 files, but not stacked)
  • Tests added/updated for new functionality
  • No new compiler warnings introduced (not checked)
  • CHANGELOG.md updated (for user-facing changes)

🤖 Generated with Claude Code

tcheeric and others added 10 commits October 4, 2026 13:55
Hypothesis: replacing the BigInteger affine double-and-add (one modular
inversion per step, 256 iterations) with BouncyCastle's custom secp256k1
ECPoint, and a precomputed comb multiplier for G, cuts sign, verify and
pubkey time by more than 20x.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Hypothesis: writing the NIP-01 array straight to a JsonGenerator, instead of
building an ArrayNode and running MAPPER.valueToTree(tags) first, removes one
full tree conversion per update() and cuts update_small and update_large time
by about 40%. Output is byte-identical.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Hypothesis: replacing the chained Predicate.and lambdas, List.contains scans
and per-event stream pipelines with a single loop over precomputed hash sets
cuts filter matching over 10k events by about 30%.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Hypothesis: computing s*G - e*P with ECAlgorithms.sumOfTwoMultiplies (GLV +
Shamir) instead of two separate multiplications and an addition cuts verify
time by about 25%. Also returns false instead of dereferencing the point at
infinity.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Hypothesis: sign lifts its own public key x-coordinate again in verify (a
256-bit modular square root) and recomputes P.toBytes() and R.toBytes() several
times. Passing the point it already holds and caching the byte arrays cuts
sign time by about 15%.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Adds known-value checks for Point.mul, Point.mulSum against separate
multiplications, Point.liftX, and BIP-340 signing vectors 0 and 1 plus the
off-curve public key vector 5 for Schnorr.verify.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tcheeric
tcheeric merged commit f3f3cd8 into develop Nov 25, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant