Skip to content

Commit 6e8f4fd

Browse files
committed
Refactoring in LMS
1 parent 13a7c84 commit 6e8f4fd

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/lms/HSSPrivateKeyParameters.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ else if (src instanceof byte[])
129129
{
130130
// old style single LMS key.
131131
LMSPrivateKeyParameters lmsKey = LMSPrivateKeyParameters.getInstance(src);
132-
return new HSSPrivateKeyParameters(lmsKey, lmsKey.getIndex(), lmsKey.getIndex() + lmsKey.getUsagesRemaining());
132+
return new HSSPrivateKeyParameters(lmsKey, lmsKey.getIndex(), lmsKey.getIndexLimit());
133133
}
134134
}
135135
finally
@@ -212,7 +212,7 @@ long getIndexLimit()
212212

213213
public long getUsagesRemaining()
214214
{
215-
return indexLimit - index;
215+
return getIndexLimit() - getIndex();
216216
}
217217

218218
LMSPrivateKeyParameters getRootKey()
@@ -233,32 +233,33 @@ public HSSPrivateKeyParameters extractKeyShard(int usageCount)
233233
{
234234
synchronized (this)
235235
{
236-
237-
if (getUsagesRemaining() < usageCount)
236+
if (usageCount < 0)
237+
{
238+
throw new IllegalArgumentException("usageCount cannot be negative");
239+
}
240+
if (usageCount > indexLimit - index)
238241
{
239242
throw new IllegalArgumentException("usageCount exceeds usages remaining in current leaf");
240243
}
241244

242-
long maxIndexForShard = index + usageCount;
243-
long shardStartIndex = index;
245+
long shardIndex = index;
246+
long shardIndexLimit = index + usageCount;
244247

245-
//
246-
// Move this keys index along
247-
//
248-
index += usageCount;
248+
// Move this key's index along
249+
index = shardIndexLimit;
249250

250251
List<LMSPrivateKeyParameters> keys = new ArrayList<LMSPrivateKeyParameters>(this.getKeys());
251252
List<LMSSignature> sig = new ArrayList<LMSSignature>(this.getSig());
252253

253-
HSSPrivateKeyParameters shard = makeCopy(new HSSPrivateKeyParameters(l, keys, sig, shardStartIndex, maxIndexForShard, true));
254+
HSSPrivateKeyParameters shard = makeCopy(
255+
new HSSPrivateKeyParameters(l, keys, sig, shardIndex, shardIndexLimit, true));
254256

255257
resetKeyToIndex();
256258

257259
return shard;
258260
}
259261
}
260262

261-
262263
synchronized List<LMSPrivateKeyParameters> getKeys()
263264
{
264265
return keys;

core/src/main/java/org/bouncycastle/pqc/crypto/lms/LMSPrivateKeyParameters.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,22 @@ public LMSPrivateKeyParameters extractKeyShard(int usageCount)
253253
{
254254
synchronized (this)
255255
{
256-
if (q + usageCount >= maxQ)
256+
if (usageCount < 0)
257+
{
258+
throw new IllegalArgumentException("usageCount cannot be negative");
259+
}
260+
if (usageCount > maxQ - q)
257261
{
258262
throw new IllegalArgumentException("usageCount exceeds usages remaining");
259263
}
260-
LMSPrivateKeyParameters keyParameters = new LMSPrivateKeyParameters(this, q, q + usageCount);
261-
q += usageCount;
262264

263-
return keyParameters;
265+
int shardIndex = q;
266+
int shardIndexLimit = q + usageCount;
267+
268+
// Move this key's index along
269+
q = shardIndexLimit;
270+
271+
return new LMSPrivateKeyParameters(this, shardIndex, shardIndexLimit);
264272
}
265273
}
266274

@@ -284,9 +292,15 @@ public byte[] getMasterSecret()
284292
return Arrays.clone(masterSecret);
285293
}
286294

295+
public int getIndexLimit()
296+
{
297+
return maxQ;
298+
}
299+
300+
// TODO Only needs 'int'
287301
public long getUsagesRemaining()
288302
{
289-
return maxQ - getIndex();
303+
return getIndexLimit() - getIndex();
290304
}
291305

292306
public LMSPublicKeyParameters getPublicKey()

core/src/test/java/org/bouncycastle/pqc/crypto/lms/HSSTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ else if (line.startsWith("Signature:"))
543543

544544
assertEquals(1024, keyPair.getUsagesRemaining());
545545
assertEquals(1024, keyPair.getIndexLimit());
546+
assertEquals(0, keyPair.getIndex());
546547

547548
//
548549
// Split the space up with a shard.
@@ -555,7 +556,6 @@ else if (line.startsWith("Signature:"))
555556
HSSPrivateKeyParameters pair = shard1;
556557

557558
int c = 0;
558-
String exhaustionMessage = null;
559559
for (int i = 0; i < keyPair.getIndexLimit(); i++)
560560
{
561561
if (i == 500)
@@ -640,6 +640,7 @@ public void testRemaining()
640640

641641
HSSPrivateKeyParameters shard = keyPair.extractKeyShard(10);
642642

643+
assertEquals(10, shard.getUsagesRemaining());
643644
assertEquals(15, shard.getIndexLimit());
644645
assertEquals(5, shard.getIndex());
645646

core/src/test/java/org/bouncycastle/pqc/crypto/test/LMSTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public void testKeyGenAndSignTwoSigsWithShard()
112112
LMSSigner signer = new LMSSigner();
113113

114114
assertEquals(2, privKey.getUsagesRemaining());
115+
assertEquals(2, privKey.getIndexLimit());
115116
assertEquals(0, privKey.getIndex());
116117

117118
signer.init(true, privKey);

0 commit comments

Comments
 (0)