Repository navigation
perf(core): speed up Schnorr signing, event serialization and filter matching - #571
Merged
Merged
Conversation
chore(release): 2.4.2
chore(release): 2.4.3
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>
This was referenced Nov 25, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 improvementtest- Adding or updating testsdocs- Documentation only (CHANGELOG)What changed?
Start with
Point.javaandSchnorr.java; the other two files are small.Point.addandPoint.mulnow run on BouncyCastle's custom secp256k1 curve (already a dependency), with a precomputed comb multiplier for G. The public API and theBigIntegercoordinates are unchanged.Point.mulSum(P, a, Q, b)computesa*P + b*Qin one go;Schnorr.verifyuses it fors*G - e*P.Schnorr.signreuses the public key point it already holds in its self-check, and caches the repeated byte conversions. The self-check itself stays.EventSerializerwrites the NIP-01 array straight to aJsonGeneratorinstead of building a tree first. Output is byte-identical.EventFilter.toPredicate()evaluates in one pass over hash sets instead of chainedPredicate.andlambdas 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).
Behavior changes to know about
Schnorr.verifynow returnsfalseinstead of throwing aNullPointerExceptionwhen the verification point is the point at infinity.Point.mulbuilds BouncyCastle points withcreatePoint, which skips the on-curve check. Points fromliftXare on the curve by construction, so this only matters for hand-builtPoints.Testing
mvn testfornostr-java-core,nostr-java-eventandnostr-java-identity(94, 122 and 61 tests, 1 pre-existing skip), run on the merged branchmvn 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,mulSumagainst separate multiplications, andliftXbehavior. 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), andliftXwas differential-tested against the old implementation on 4006 inputs.Review focus
Point.toBcacceptable, or shouldPoint.mulvalidate its input?Pointonly definesequals(Point)(an overload), which I worked around in the tests rather than changing.Checklist
type(scope): description🤖 Generated with Claude Code