Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ jobs:
with:
distribution: temurin
java-version: ${{ matrix.java-version }}
cache: maven
# Only the JDK 21 job resolves the full dependency tree. Both jobs share one cache key,
# so letting the near-empty JDK 17 validate job save first left every cache at ~0 MB
# and made each build download from Maven Central (HTTP 429 rate limits).
cache: ${{ matrix.java-version == '21' && 'maven' || '' }}

- name: Make release script executable
run: chmod +x scripts/release.sh
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ The format is inspired by Keep a Changelog, and this project adheres to semantic

## [Unreleased]

## [2.4.5] - 2026-11-27

### Changed
- Performance: `Schnorr.verify` keeps a bounded cache (1024 entries) of lifted public keys,
configured with BouncyCastle's wider basepoint wNAF window, so repeat verifies under one key
reuse its multiplication tables. Verify drops about 31% (140 to 96 us) and `Schnorr.sign`
about 10% (its self-check goes through the same cache; sign and `genPubKey` also stay on
BouncyCastle points instead of round-tripping through `Point`). Outputs and verdicts are
unchanged, checked against a plain BigInteger transcription of the BIP-340 reference.

## [2.4.4] - 2026-11-25

### Changed
Expand Down
2 changes: 1 addition & 1 deletion docs/howto/multi-relay-publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ send a private direct message.
<dependency>
<groupId>xyz.tcheeric</groupId>
<artifactId>nostr-java-api</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion nostr-java-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>xyz.tcheeric</groupId>
<artifactId>nostr-java</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion nostr-java-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>xyz.tcheeric</groupId>
<artifactId>nostr-java</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion nostr-java-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>xyz.tcheeric</groupId>
<artifactId>nostr-java</artifactId>
<version>2.4.4</version>
<version>2.4.5</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
98 changes: 79 additions & 19 deletions nostr-java-core/src/main/java/nostr/crypto/schnorr/Schnorr.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;

import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
import org.bouncycastle.math.ec.FixedPointUtil;
import org.bouncycastle.math.ec.WNafUtil;
import java.math.BigInteger;
import java.util.concurrent.ConcurrentHashMap;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
Expand Down Expand Up @@ -41,11 +49,11 @@ public static byte[] sign(byte[] msg, byte[] secKey, byte[] auxRand) throws Schn
&& secKey0.compareTo(Point.getn().subtract(BigInteger.ONE)) <= 0)) {
throw new SchnorrException("The secret key must be an integer in the range 1..n-1.");
}
Point P = Point.mul(Point.getG(), secKey0);
if (!P.hasEvenY()) {
ECPoint P = COMB.multiply(BC_G, secKey0).normalize();
if (P.getAffineYCoord().testBitZero()) {
secKey0 = Point.getn().subtract(secKey0);
}
byte[] pBytes = P.toBytes();
byte[] pBytes = NostrUtil.bytesFromBigInteger(P.getAffineXCoord().toBigInteger());
byte[] t = NostrUtil.xor(NostrUtil.bytesFromBigInteger(secKey0), taggedHashUnchecked("BIP0340/aux", auxRand));

if (t == null) {
Expand All @@ -61,14 +69,9 @@ public static byte[] sign(byte[] msg, byte[] secKey, byte[] auxRand) throws Schn
if (k0.compareTo(BigInteger.ZERO) == 0) {
throw new SchnorrException("Failure. This happens only with negligible probability.");
}
Point R = Point.mul(Point.getG(), k0);
BigInteger k;
if (!R.hasEvenY()) {
k = Point.getn().subtract(k0);
} else {
k = k0;
}
byte[] rBytes = R.toBytes();
ECPoint R = COMB.multiply(BC_G, k0).normalize();
BigInteger k = R.getAffineYCoord().testBitZero() ? Point.getn().subtract(k0) : k0;
byte[] rBytes = NostrUtil.bytesFromBigInteger(R.getAffineXCoord().toBigInteger());
buf = new byte[rBytes.length + pBytes.length + msg.length];
System.arraycopy(rBytes, 0, buf, 0, rBytes.length);
System.arraycopy(pBytes, 0, buf, rBytes.length, pBytes.length);
Expand All @@ -81,8 +84,8 @@ public static byte[] sign(byte[] msg, byte[] secKey, byte[] auxRand) throws Schn
System.arraycopy(rBytes, 0, sig, 0, rBytes.length);
System.arraycopy(kesBytes, 0, sig, rBytes.length, kesBytes.length);
// The public key point is already known, so the self-check skips lifting it again.
Point pEven = P.hasEvenY() ? P : new Point(P.getX(), Point.getp().subtract(P.getY()));
if (!verify(msg, pBytes, pEven, sig)) {
ECPoint pEven = liftCached(pBytes);
if (pEven == null || !verify(msg, pBytes, pEven, sig)) {
throw new SchnorrException("The signature does not pass verification.");
}
return sig;
Expand Down Expand Up @@ -110,14 +113,70 @@ public static boolean verify(byte[] msg, byte[] pubkey, byte[] sig) throws Schno
throw new SchnorrException("The signature must be a 64-byte array.");
}

Point P = Point.liftX(pubkey);
ECPoint P = liftCached(pubkey);
if (P == null) {
return false;
}
return verify(msg, pubkey, P, sig);
}

private static boolean verify(byte[] msg, byte[] pubkey, Point P, byte[] sig)
private static final X9ECParameters BC = CustomNamedCurves.getByName("secp256k1");
private static final ECPoint BC_G = BC.getG();
private static final FixedPointCombMultiplier COMB = new FixedPointCombMultiplier();

static {
FixedPointUtil.precompute(BC_G);
}

private static final int LIFTED_CAPACITY = 1024;
private static final ConcurrentHashMap<ByteKey, ECPoint> LIFTED = new ConcurrentHashMap<>();

private record ByteKey(byte[] b) {
@Override
public boolean equals(Object o) {
return o instanceof ByteKey k && Arrays.equals(b, k.b);
}

@Override
public int hashCode() {
return Arrays.hashCode(b);
}
}

/**
* The even-y point for an x-only key, or {@code null} if x is not on the curve.
*
* <p>Verifiers see few distinct keys, and BouncyCastle keeps its multiplication tables on the
* point object, so a cached point (configured with the wider basepoint wNAF window) keeps its
* tables across calls. Bounded; cleared wholesale when full, which is cheap and never wrong.
*/
static ECPoint liftCached(byte[] x) {
ByteKey key = new ByteKey(x);
ECPoint cached = LIFTED.get(key);
if (cached != null) {
return cached;
}
if (new BigInteger(1, x).compareTo(Point.getp()) >= 0) {
return null;
}
byte[] enc = new byte[33];
enc[0] = 0x02;
System.arraycopy(x, 0, enc, 1, 32);
ECPoint point;
try {
point = BC.getCurve().decodePoint(enc);
} catch (IllegalArgumentException notOnCurve) {
return null;
}
WNafUtil.configureBasepoint(point);
if (LIFTED.size() >= LIFTED_CAPACITY) {
LIFTED.clear();
}
LIFTED.put(new ByteKey(x.clone()), point);
return point;
}

private static boolean verify(byte[] msg, byte[] pubkey, ECPoint P, byte[] sig)
throws SchnorrException {
BigInteger r = NostrUtil.bigIntFromBytes(Arrays.copyOfRange(sig, 0, 32));
BigInteger s = NostrUtil.bigIntFromBytes(Arrays.copyOfRange(sig, 32, 64));
Expand All @@ -131,8 +190,10 @@ private static boolean verify(byte[] msg, byte[] pubkey, Point P, byte[] sig)
System.arraycopy(msg, 0, buf, 32 + pubkey.length, msg.length);
BigInteger e =
NostrUtil.bigIntFromBytes(taggedHashUnchecked("BIP0340/challenge", buf)).mod(Point.getn());
Point R = Point.mulSum(Point.getG(), s, P, Point.getn().subtract(e));
return R != null && !R.isInfinite() && R.hasEvenY() && R.getX().compareTo(r) == 0;
ECPoint R = ECAlgorithms.sumOfTwoMultiplies(BC_G, s, P, Point.getn().subtract(e)).normalize();
return !R.isInfinity()
&& !R.getAffineYCoord().testBitZero()
&& R.getAffineXCoord().toBigInteger().equals(r);
}

/**
Expand Down Expand Up @@ -178,8 +239,7 @@ public static byte[] genPubKey(byte[] secKey) throws SchnorrException {
&& x.compareTo(Point.getn().subtract(BigInteger.ONE)) <= 0)) {
throw new SchnorrException("The secret key must be an integer in the range 1..n-1.");
}
Point ret = Point.mul(Point.G, x);
return Point.bytesFromPoint(ret);
return NostrUtil.bytesFromBigInteger(COMB.multiply(BC_G, x).normalize().getAffineXCoord().toBigInteger());
}

private static byte[] taggedHashUnchecked(String tag, byte[] msg) {
Expand Down
Loading
Loading