Skip to content

Commit 902266f

Browse files
committed
Added test using combined ML-DSA sigGen and sigVer vectors from ACVP.
1 parent 0877c53 commit 902266f

File tree

1 file changed

+138
-11
lines changed

1 file changed

+138
-11
lines changed

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

Lines changed: 138 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.InputStreamReader;
2121
import java.security.SecureRandom;
2222
import java.util.HashMap;
23+
import java.util.Map;
2324

2425
public class MLDSATest extends TestCase
2526
{
@@ -79,8 +80,6 @@ public void testKeyGen() throws IOException
7980
PrivateKeyInfoFactory.createPrivateKeyInfo(kp.getPrivate()));
8081

8182

82-
83-
8483
assertTrue(name + ": public key", Arrays.areEqual(pk, pubParams.getEncoded()));
8584
assertTrue(name + ": secret key", Arrays.areEqual(sk, privParams.getEncoded()));
8685

@@ -169,7 +168,6 @@ public void testSigGen() throws IOException
169168
buf.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
170169
}
171170
}
172-
// System.out.println("testing successful!");
173171
}
174172
}
175173

@@ -191,7 +189,6 @@ public void testSigVer() throws IOException
191189
for (int fileIndex = 0; fileIndex != files.length; fileIndex++)
192190
{
193191
String name = files[fileIndex];
194-
// System.out.println("testing: " + name);
195192
InputStream src = TestResourceFinder.findTestResource("pqc/crypto/dilithium/acvp", name);
196193
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
197194

@@ -212,14 +209,12 @@ public void testSigVer() throws IOException
212209
boolean testPassed = Boolean.parseBoolean((String) buf.get("testPassed"));
213210
String reason = buf.get("reason");
214211
byte[] pk = Hex.decode((String) buf.get("pk"));
215-
byte[] sk = Hex.decode((String) buf.get("sk"));
216212
byte[] message = Hex.decode((String) buf.get("message"));
217213
byte[] signature = Hex.decode((String) buf.get("signature"));
218214

219215
MLDSAParameters parameters = params[fileIndex];
220216

221217
MLDSAPublicKeyParameters pubParams = new MLDSAPublicKeyParameters(parameters, pk);
222-
MLDSAPrivateKeyParameters privParams = new MLDSAPrivateKeyParameters(parameters, sk, null);
223218

224219

225220
MLDSASigner verifier = new MLDSASigner();
@@ -301,17 +296,149 @@ public void testMLDSARandom()
301296

302297
boolean ok = verifier.verifySignature(msg, sigGenerated);
303298

304-
if (!ok) {
299+
if (!ok)
300+
{
305301
System.out.println("Verify failed");
306-
System.out.println("MSG:"+Hex.toHexString(msg));
307-
System.out.println("SIG: "+Hex.toHexString(sigGenerated));
308-
System.out.println("PK: "+Hex.toHexString(pkparam.getEncoded()));
309-
System.out.println("SK: "+Hex.toHexString(skparam.getEncoded()));
302+
System.out.println("MSG:" + Hex.toHexString(msg));
303+
System.out.println("SIG: " + Hex.toHexString(sigGenerated));
304+
System.out.println("PK: " + Hex.toHexString(pkparam.getEncoded()));
305+
System.out.println("SK: " + Hex.toHexString(skparam.getEncoded()));
310306
}
311307

312308
assertTrue("count = " + i, ok);
313309
}
314310
}
315311
}
316312
}
313+
314+
public void testSigGenCombinedVectorSet() throws IOException
315+
{
316+
317+
Map<String, MLDSAParameters> parametersMap = new HashMap<String, MLDSAParameters>()
318+
{
319+
{
320+
put("ML-DSA-44", MLDSAParameters.ml_dsa_44);
321+
put("ML-DSA-65", MLDSAParameters.ml_dsa_65);
322+
put("ML-DSA-87", MLDSAParameters.ml_dsa_87);
323+
}
324+
};
325+
326+
327+
InputStream src = TestResourceFinder.findTestResource("pqc/crypto/mldsa", "ML-DSA-sigGen.txt");
328+
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
329+
330+
String line = null;
331+
HashMap<String, String> buf = new HashMap<String, String>();
332+
while ((line = bin.readLine()) != null)
333+
{
334+
line = line.trim();
335+
336+
if (line.startsWith("#"))
337+
{
338+
continue;
339+
}
340+
if (line.length() == 0)
341+
{
342+
if (buf.size() > 0)
343+
{
344+
boolean deterministic = Boolean.valueOf(buf.get("deterministic"));
345+
byte[] sk = Hex.decode((String) buf.get("sk"));
346+
byte[] message = Hex.decode((String) buf.get("message"));
347+
byte[] signature = Hex.decode((String) buf.get("signature"));
348+
byte[] rnd = null;
349+
if (!deterministic)
350+
{
351+
rnd = Hex.decode((String) buf.get("rnd"));
352+
}
353+
else
354+
{
355+
rnd = new byte[32];
356+
}
357+
358+
MLDSAParameters parameters = parametersMap.get(buf.get("parameterSet"));
359+
360+
MLDSAPrivateKeyParameters privParams = new MLDSAPrivateKeyParameters(parameters, sk, null);
361+
362+
// sign
363+
MLDSASigner signer = new MLDSASigner();
364+
365+
signer.init(true, privParams);
366+
byte[] sigGenerated;
367+
368+
sigGenerated = signer.internalGenerateSignature(message, rnd);
369+
assertTrue(Arrays.areEqual(sigGenerated, signature));
370+
371+
}
372+
buf.clear();
373+
374+
continue;
375+
}
376+
377+
int a = line.indexOf("=");
378+
if (a > -1)
379+
{
380+
buf.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
381+
}
382+
}
383+
}
384+
385+
public void testSigVerCombinedVectorSet() throws IOException
386+
{
387+
Map<String, MLDSAParameters> parametersMap = new HashMap<String, MLDSAParameters>()
388+
{
389+
{
390+
put("ML-DSA-44", MLDSAParameters.ml_dsa_44);
391+
put("ML-DSA-65", MLDSAParameters.ml_dsa_65);
392+
put("ML-DSA-87", MLDSAParameters.ml_dsa_87);
393+
}
394+
};
395+
396+
397+
InputStream src = TestResourceFinder.findTestResource("pqc/crypto/mldsa", "ML-DSA-sigVer.txt");
398+
BufferedReader bin = new BufferedReader(new InputStreamReader(src));
399+
400+
String line = null;
401+
HashMap<String, String> buf = new HashMap<String, String>();
402+
while ((line = bin.readLine()) != null)
403+
{
404+
line = line.trim();
405+
406+
if (line.startsWith("#"))
407+
{
408+
continue;
409+
}
410+
if (line.isEmpty())
411+
{
412+
if (!buf.isEmpty())
413+
{
414+
boolean expectedResult = Boolean.parseBoolean((String) buf.get("testPassed"));
415+
416+
byte[] pk = Hex.decode((String) buf.get("pk"));
417+
byte[] message = Hex.decode((String) buf.get("message"));
418+
byte[] signature = Hex.decode((String) buf.get("signature"));
419+
420+
MLDSAParameters parameters = parametersMap.get(buf.get("parameterSet"));
421+
422+
MLDSAPublicKeyParameters pubParams = new MLDSAPublicKeyParameters(parameters, pk);
423+
424+
MLDSASigner verifier = new MLDSASigner();
425+
verifier.init(false, pubParams);
426+
427+
428+
boolean verifyResult = verifier.internalVerifySignature(message, signature);
429+
assertEquals(expectedResult, verifyResult);
430+
}
431+
buf.clear();
432+
433+
continue;
434+
}
435+
436+
int a = line.indexOf("=");
437+
if (a > -1)
438+
{
439+
buf.put(line.substring(0, a).trim(), line.substring(a + 1).trim());
440+
}
441+
}
442+
443+
}
317444
}

0 commit comments

Comments
 (0)