Skip to content

Commit af3c793

Browse files
committed
SLH-DSA: Don't recompute PK.root when signing
The HT constructor always built the top-layer XMSS tree to derive htPubKey, so every signature repeated the whole of key generation (about 1/d of the signing work) to compute a value the private key already holds in PK.root. Move the root computation into an explicit HT.pkGen() that only key generation calls, and call it once there rather than once per key half.
1 parent b1c75bf commit af3c793

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/signers/slhdsa/HT.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ class HT
1111
SLHDSAEngine engine;
1212
WotsPlus wots;
1313

14-
final byte[] htPubKey;
15-
1614
public HT(SLHDSAEngine engine, byte[] skSeed, byte[] pkSeed)
1715
{
1816
this.skSeed = skSeed;
1917
this.pkSeed = pkSeed;
2018

2119
this.engine = engine;
2220
this.wots = new WotsPlus(engine);
21+
}
2322

23+
/**
24+
* Compute the hypertree public key (PK.root): the root of the top-layer XMSS tree.
25+
* <p>
26+
* Only key generation needs this. Signing already knows PK.root, so it must not be recomputed there:
27+
* it costs a full XMSS tree build, i.e. about 1/d of the signing work.
28+
*/
29+
byte[] pkGen()
30+
{
2431
ADRS adrs = new ADRS();
2532
adrs.setLayerAddress(engine.D - 1);
2633
adrs.setTreeAddress(0);
2734

28-
if (skSeed != null)
29-
{
30-
htPubKey = xmss_PKgen(skSeed, pkSeed, adrs);
31-
}
32-
else
33-
{
34-
htPubKey = null;
35-
}
35+
return xmss_PKgen(skSeed, pkSeed, adrs);
3636
}
3737

3838
byte[] sign(byte[] M, long idx_tree, int idx_leaf)

‎core/src/main/java/org/bouncycastle/crypto/signers/slhdsa/SLHDSAEngine.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,11 @@ public static AsymmetricCipherKeyPair implGenerateKeyPair(SLHDSAParameters param
455455

456456
engine.init(pkSeed);
457457

458+
byte[] pkRoot = new HT(engine, skSeed, pkSeed).pkGen();
459+
458460
return new AsymmetricCipherKeyPair(
459-
new SLHDSAPublicKeyParameters(params, Arrays.concatenate(pkSeed, new HT(engine, skSeed, pkSeed).htPubKey)),
460-
new SLHDSAPrivateKeyParameters(params, skSeed, skPrf, pkSeed, new HT(engine, skSeed, pkSeed).htPubKey));
461+
new SLHDSAPublicKeyParameters(params, Arrays.concatenate(pkSeed, pkRoot)),
462+
new SLHDSAPrivateKeyParameters(params, skSeed, skPrf, pkSeed, pkRoot));
461463
}
462464

463465
public static boolean internalVerifySignature(SLHDSAParameters params, byte[] pkSeed, byte[] pkRoot, byte[] msgPrefix, byte[] msg,

0 commit comments

Comments
 (0)