diff --git a/CHANGELOG.md b/CHANGELOG.md index ca29343f..26b73e91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ The format is inspired by Keep a Changelog, and this project adheres to semantic ## [Unreleased] +### 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 diff --git a/nostr-java-core/src/main/java/nostr/crypto/schnorr/Schnorr.java b/nostr-java-core/src/main/java/nostr/crypto/schnorr/Schnorr.java index ae2519b5..edf25780 100644 --- a/nostr-java-core/src/main/java/nostr/crypto/schnorr/Schnorr.java +++ b/nostr-java-core/src/main/java/nostr/crypto/schnorr/Schnorr.java @@ -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; @@ -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) { @@ -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); @@ -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; @@ -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 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. + * + *

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)); @@ -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); } /** @@ -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) { diff --git a/nostr-java-core/src/test/java/nostr/crypto/schnorr/SchnorrReferenceTest.java b/nostr-java-core/src/test/java/nostr/crypto/schnorr/SchnorrReferenceTest.java new file mode 100644 index 00000000..d9571e3e --- /dev/null +++ b/nostr-java-core/src/test/java/nostr/crypto/schnorr/SchnorrReferenceTest.java @@ -0,0 +1,181 @@ +package nostr.crypto.schnorr; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.math.BigInteger; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.Arrays; +import java.util.Random; +import org.junit.jupiter.api.Test; + +/** + * Holds {@link Schnorr} to a plain-BigInteger transcription of the BIP-340 reference code: the + * same signatures for random keys, and the same verdict on valid, corrupted and out-of-range + * inputs. The optimised verify caches lifted keys, so the corrupted cases are run after the key + * is cached, and keys whose x is off the curve must never be cached as valid. + */ +class SchnorrReferenceTest { + + private static final BigInteger P = + new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16); + private static final BigInteger N = + new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16); + private static final BigInteger[] G = { + new BigInteger("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16), + new BigInteger("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16) + }; + + private final Random rnd = new Random(7); + + // ------------------------------------------------------------- BIP-340 reference (affine) + + private static BigInteger[] add(BigInteger[] a, BigInteger[] b) { + if (a == null) return b; + if (b == null) return a; + if (a[0].equals(b[0]) && !a[1].equals(b[1])) return null; + BigInteger lam = + a[0].equals(b[0]) + ? BigInteger.valueOf(3).multiply(a[0].pow(2)).multiply(a[1].shiftLeft(1).modPow(P.subtract(BigInteger.TWO), P)).mod(P) + : b[1].subtract(a[1]).multiply(b[0].subtract(a[0]).modPow(P.subtract(BigInteger.TWO), P)).mod(P); + BigInteger x = lam.pow(2).subtract(a[0]).subtract(b[0]).mod(P); + return new BigInteger[] {x, lam.multiply(a[0].subtract(x)).subtract(a[1]).mod(P)}; + } + + private static BigInteger[] mul(BigInteger[] p, BigInteger k) { + BigInteger[] r = null; + for (int i = k.bitLength() - 1; i >= 0; i--) { + r = add(r, r); + if (k.testBit(i)) r = add(r, p); + } + return r; + } + + private static byte[] b32(BigInteger v) { + byte[] raw = v.toByteArray(); + byte[] out = new byte[32]; + int n = Math.min(raw.length, 32); + System.arraycopy(raw, raw.length - n, out, 32 - n, n); + return out; + } + + private static byte[] tagged(String tag, byte[]... parts) throws Exception { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + byte[] t = md.digest(tag.getBytes(StandardCharsets.UTF_8)); + md.update(t); + md.update(t); + for (byte[] p : parts) md.update(p); + return md.digest(); + } + + private static BigInteger[] liftX(byte[] xb) { + BigInteger x = new BigInteger(1, xb); + if (x.compareTo(P) >= 0) return null; + BigInteger c = x.modPow(BigInteger.valueOf(3), P).add(BigInteger.valueOf(7)).mod(P); + BigInteger y = c.modPow(P.add(BigInteger.ONE).shiftRight(2), P); + if (!y.modPow(BigInteger.TWO, P).equals(c)) return null; + return new BigInteger[] {x, y.testBit(0) ? P.subtract(y) : y}; + } + + static boolean refVerify(byte[] msg, byte[] pk, byte[] sig) throws Exception { + BigInteger[] p = liftX(pk); + if (p == null) return false; + BigInteger r = new BigInteger(1, Arrays.copyOfRange(sig, 0, 32)); + BigInteger s = new BigInteger(1, Arrays.copyOfRange(sig, 32, 64)); + if (r.compareTo(P) >= 0 || s.compareTo(N) >= 0) return false; + BigInteger e = new BigInteger(1, tagged("BIP0340/challenge", Arrays.copyOfRange(sig, 0, 32), pk, msg)).mod(N); + BigInteger[] R = add(mul(G, s), mul(p, N.subtract(e))); + return R != null && !R[1].testBit(0) && R[0].equals(r); + } + + static byte[] refSign(byte[] msg, byte[] sk, byte[] aux) throws Exception { + BigInteger d0 = new BigInteger(1, sk); + BigInteger[] p = mul(G, d0); + BigInteger d = p[1].testBit(0) ? N.subtract(d0) : d0; + byte[] t = b32(d); + byte[] a = tagged("BIP0340/aux", aux); + for (int i = 0; i < 32; i++) t[i] ^= a[i]; + BigInteger k0 = new BigInteger(1, tagged("BIP0340/nonce", t, b32(p[0]), msg)).mod(N); + BigInteger[] R = mul(G, k0); + BigInteger k = R[1].testBit(0) ? N.subtract(k0) : k0; + BigInteger e = new BigInteger(1, tagged("BIP0340/challenge", b32(R[0]), b32(p[0]), msg)).mod(N); + byte[] sig = new byte[64]; + System.arraycopy(b32(R[0]), 0, sig, 0, 32); + System.arraycopy(b32(k.add(e.multiply(d)).mod(N)), 0, sig, 32, 32); + return sig; + } + + // ------------------------------------------------------------------------------- tests + + private byte[] bytes(int n) { + byte[] b = new byte[n]; + rnd.nextBytes(b); + return b; + } + + private static boolean ours(byte[] m, byte[] pk, byte[] sig) { + try { + return Schnorr.verify(m, pk, sig); + } catch (SchnorrException e) { + return false; + } + } + + private static void agree(byte[] m, byte[] pk, byte[] sig) throws Exception { + assertEquals(refVerify(m, pk, sig), ours(m, pk, sig), "verdict for pk " + new BigInteger(1, pk).toString(16)); + } + + @Test + void signaturesAndKeysMatchTheReference() throws Exception { + for (int i = 0; i < 40; i++) { + byte[] sk = bytes(32); + byte[] msg = bytes(32); + byte[] aux = i % 4 == 0 ? new byte[32] : bytes(32); + assertArrayEquals(b32(mul(G, new BigInteger(1, sk))[0]), Schnorr.genPubKey(sk)); + assertArrayEquals(refSign(msg, sk, aux), Schnorr.sign(msg, sk, aux)); + } + } + + @Test + void verdictsMatchTheReferenceOnceKeysAreCached() throws Exception { + for (int i = 0; i < 40; i++) { + byte[] sk = bytes(32); + byte[] msg = bytes(32); + byte[] pk = Schnorr.genPubKey(sk); + byte[] sig = Schnorr.sign(msg, sk, bytes(32)); + agree(msg, pk, sig); // caches pk + byte[] flipped = sig.clone(); + flipped[rnd.nextInt(64)] ^= (byte) (1 << rnd.nextInt(8)); + agree(msg, pk, flipped); + byte[] otherMsg = msg.clone(); + otherMsg[0] ^= 1; + agree(otherMsg, pk, sig); + byte[] otherPk = pk.clone(); + otherPk[rnd.nextInt(32)] ^= 1; // usually off the curve or another key + agree(msg, otherPk, sig); + agree(msg, otherPk, sig); // second time: must not have been cached as valid + } + } + + @Test + void verdictsMatchTheReferenceOnOutOfRangeValues() throws Exception { + byte[] sk = bytes(32); + byte[] msg = bytes(32); + byte[] pk = Schnorr.genPubKey(sk); + byte[] sig = Schnorr.sign(msg, sk, bytes(32)); + byte[] rIsP = sig.clone(); + System.arraycopy(b32(P), 0, rIsP, 0, 32); + agree(msg, pk, rIsP); + byte[] sIsN = sig.clone(); + System.arraycopy(b32(N), 0, sIsN, 32, 32); + agree(msg, pk, sIsN); + agree(msg, b32(P), sig); + agree(msg, b32(P.add(BigInteger.ONE)), sig); + agree(msg, new byte[32], sig); + byte[] ff = new byte[32]; + Arrays.fill(ff, (byte) 0xff); + agree(msg, ff, sig); + agree(msg, pk, new byte[64]); + } +}