Skip to content

Commit 2431e06

Browse files
committed
fixed inverted tests in signature unpack and use of unpack.
1 parent 267c1c6 commit 2431e06

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/DilithiumEngine.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ public byte[] signSignature(byte[] msg, int msglen, byte[] rho, byte[] key, byte
427427
// System.out.println(h.toString("h"));
428428
// System.out.println("Signature before pack = ");
429429
// Helper.printByteArray(outSig);
430-
431430
outSig = Packing.packSignature(outSig, z, h, this);
432431

433432
rej = false;
@@ -474,7 +473,7 @@ public boolean signVerify(byte[] sig, int siglen, byte[] msg, int msglen, byte[]
474473
// System.out.println("rho = ");
475474
// Helper.printByteArray(rho);
476475

477-
if (Packing.unpackSignature(z, h, sig, this))
476+
if (!Packing.unpackSignature(z, h, sig, this))
478477
{
479478
return false;
480479
}

core/src/main/java/org/bouncycastle/pqc/crypto/crystals/dilithium/Packing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ static boolean unpackSignature(PolyVecL z, PolyVecK h, byte[] sig, DilithiumEngi
172172
}
173173
for (j = k; j < engine.getDilithiumOmega(); ++j)
174174
{
175-
if ((sig[end + j] & 0xFF) == 0)
176-
{
175+
if ((sig[end + j] & 0xFF) != 0)
176+
{
177177
return false;
178178
}
179179
}

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.BufferedReader;
44
import java.io.InputStream;
55
import java.io.InputStreamReader;
6+
import java.security.SecureRandom;
67
import java.util.HashMap;
78

89
import junit.framework.TestCase;
@@ -15,6 +16,7 @@
1516
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumPublicKeyParameters;
1617
import org.bouncycastle.pqc.crypto.crystals.dilithium.DilithiumSigner;
1718
import org.bouncycastle.util.Arrays;
19+
import org.bouncycastle.util.Strings;
1820
import org.bouncycastle.util.encoders.Hex;
1921

2022
public class CrystalsDilithiumTest
@@ -219,33 +221,33 @@ public void testVectors()
219221
}
220222
}
221223

222-
// public void testDilithiumRandom()
223-
// {
224-
// byte[] msg = Strings.toByteArray("Hello World!");
225-
// DilithiumKeyPairGenerator keyGen = new DilithiumKeyPairGenerator();
226-
//
227-
// SecureRandom random = new SecureRandom();
228-
//
229-
// keyGen.init(new DilithiumKeyGenerationParameters(random, DilithiumParameters.dilithium3));
230-
//
231-
// for (int i = 0; i != 1000; i++)
232-
// {
233-
// AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
234-
//
235-
// // sign
236-
// DilithiumSigner signer = new DilithiumSigner();
237-
// DilithiumPrivateKeyParameters skparam = (DilithiumPrivateKeyParameters)keyPair.getPrivate();
238-
// ParametersWithRandom skwrand = new ParametersWithRandom(skparam, random);
239-
// signer.init(true, skwrand);
240-
//
241-
// byte[] sigGenerated = signer.generateSignature(msg);
242-
//
243-
// // verify
244-
// DilithiumSigner verifier = new DilithiumSigner();
245-
// DilithiumPublicKeyParameters pkparam = (DilithiumPublicKeyParameters)keyPair.getPublic();
246-
// verifier.init(false, pkparam);
247-
//
248-
// assertTrue("count = " + i, verifier.verifySignature(msg, sigGenerated));
249-
// }
250-
// }
224+
public void testDilithiumRandom()
225+
{
226+
byte[] msg = Strings.toByteArray("Hello World!");
227+
DilithiumKeyPairGenerator keyGen = new DilithiumKeyPairGenerator();
228+
229+
SecureRandom random = new SecureRandom();
230+
231+
keyGen.init(new DilithiumKeyGenerationParameters(random, DilithiumParameters.dilithium3));
232+
233+
for (int i = 0; i != 1000; i++)
234+
{
235+
AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
236+
237+
// sign
238+
DilithiumSigner signer = new DilithiumSigner();
239+
DilithiumPrivateKeyParameters skparam = (DilithiumPrivateKeyParameters)keyPair.getPrivate();
240+
ParametersWithRandom skwrand = new ParametersWithRandom(skparam, random);
241+
signer.init(true, skwrand);
242+
243+
byte[] sigGenerated = signer.generateSignature(msg);
244+
245+
// verify
246+
DilithiumSigner verifier = new DilithiumSigner();
247+
DilithiumPublicKeyParameters pkparam = (DilithiumPublicKeyParameters)keyPair.getPublic();
248+
verifier.init(false, pkparam);
249+
250+
assertTrue("count = " + i, verifier.verifySignature(msg, sigGenerated));
251+
}
252+
}
251253
}

0 commit comments

Comments
 (0)