Skip to content

Commit d1c0380

Browse files
committed
Merge branch 'main' of gitlab.cryptoworkshop.com:root/bc-java
2 parents fc91714 + c629ba0 commit d1c0380

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/mldsa/MLDSAEngine.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.security.SecureRandom;
44

5-
import org.bouncycastle.crypto.Digest;
65
import org.bouncycastle.crypto.digests.SHAKEDigest;
76
import org.bouncycastle.util.Arrays;
87

@@ -30,8 +29,6 @@ class MLDSAEngine
3029
private final int DilithiumPolyW1PackedBytes;
3130
private final int DilithiumPolyEtaPackedBytes;
3231

33-
private final int DilithiumMode;
34-
3532
private final int DilithiumK;
3633
private final int DilithiumL;
3734
private final int DilithiumEta;
@@ -43,7 +40,7 @@ class MLDSAEngine
4340
private final int DilithiumCTilde;
4441

4542
private final int CryptoPublicKeyBytes;
46-
private final int CryptoSecretKeyBytes;
43+
// private final int CryptoSecretKeyBytes;
4744
private final int CryptoBytes;
4845

4946
private final int PolyUniformGamma1NBlocks;
@@ -147,7 +144,6 @@ int getPolyUniformGamma1NBlocks()
147144

148145
MLDSAEngine(int mode, SecureRandom random)
149146
{
150-
this.DilithiumMode = mode;
151147
switch (mode)
152148
{
153149
case 2:
@@ -201,14 +197,14 @@ int getPolyUniformGamma1NBlocks()
201197
this.random = random;
202198
this.DilithiumPolyVecHPackedBytes = this.DilithiumOmega + this.DilithiumK;
203199
this.CryptoPublicKeyBytes = SeedBytes + this.DilithiumK * DilithiumPolyT1PackedBytes;
204-
this.CryptoSecretKeyBytes =
205-
(
206-
2 * SeedBytes
207-
+ TrBytes
208-
+ DilithiumL * this.DilithiumPolyEtaPackedBytes
209-
+ DilithiumK * this.DilithiumPolyEtaPackedBytes
210-
+ DilithiumK * DilithiumPolyT0PackedBytes
211-
);
200+
// this.CryptoSecretKeyBytes =
201+
// (
202+
// 2 * SeedBytes
203+
// + TrBytes
204+
// + DilithiumL * this.DilithiumPolyEtaPackedBytes
205+
// + DilithiumK * this.DilithiumPolyEtaPackedBytes
206+
// + DilithiumK * DilithiumPolyT0PackedBytes
207+
// );
212208
this.CryptoBytes = DilithiumCTilde + DilithiumL * this.DilithiumPolyZPackedBytes + this.DilithiumPolyVecHPackedBytes;
213209

214210
if (this.DilithiumGamma1 == (1 << 17))

core/src/main/java/org/bouncycastle/pqc/crypto/mldsa/Poly.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,7 @@ public void conditionalAddQ()
238238

239239
public void power2Round(Poly a)
240240
{
241-
for (int i = 0; i < DilithiumN; ++i)
242-
{
243-
int[] p2r = Rounding.power2Round(this.getCoeffIndex(i));
244-
this.setCoeffIndex(i, p2r[0]);
245-
a.setCoeffIndex(i, p2r[1]);
246-
}
241+
Rounding.power2RoundAll(this.coeffs, a.coeffs);
247242
}
248243

249244
public byte[] polyt1Pack()

core/src/main/java/org/bouncycastle/pqc/crypto/mldsa/Rounding.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
class Rounding
44
{
5-
public static int[] power2Round(int a)
5+
static void power2RoundAll(int[] c0, int[] c1)
66
{
7-
int[] out = new int[2];
7+
int d = MLDSAEngine.DilithiumD, n = MLDSAEngine.DilithiumN;
8+
int u = (1 << (d - 1)) - 1, v = -1 << d;
89

9-
out[0] = (a + (1 << (MLDSAEngine.DilithiumD - 1)) - 1) >> MLDSAEngine.DilithiumD;
10-
out[1] = a - (out[0] << MLDSAEngine.DilithiumD);
11-
return out;
12-
}
10+
for (int i = 0; i < n; ++i)
11+
{
12+
int a = c0[i];
13+
14+
int t = a + u;
15+
int r1 = a - (t & v);
1316

17+
c0[i] = t >> d;
18+
c1[i] = r1;
19+
}
20+
}
21+
1422
public static int[] decompose(int a, int gamma2)
1523
{
1624
int a1, a0;

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import junit.framework.TestCase;
1616
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
1717
import org.bouncycastle.crypto.CryptoException;
18+
import org.bouncycastle.crypto.Signer;
1819
import org.bouncycastle.crypto.params.ParametersWithRandom;
1920
import org.bouncycastle.pqc.crypto.mldsa.HashMLDSASigner;
2021
import org.bouncycastle.pqc.crypto.mldsa.MLDSAKeyGenerationParameters;
@@ -41,15 +42,21 @@ public class MLDSATest
4142
put("ML-DSA-44", MLDSAParameters.ml_dsa_44);
4243
put("ML-DSA-65", MLDSAParameters.ml_dsa_65);
4344
put("ML-DSA-87", MLDSAParameters.ml_dsa_87);
45+
put("ML-DSA-44-WITH-SHA512", MLDSAParameters.ml_dsa_44_with_sha512);
46+
put("ML-DSA-65-WITH-SHA512", MLDSAParameters.ml_dsa_65_with_sha512);
47+
put("ML-DSA-87-WITH-SHA512", MLDSAParameters.ml_dsa_87_with_sha512);
4448
}
4549
};
4650

4751
private static final MLDSAParameters[] PARAMETER_SETS = new MLDSAParameters[]
48-
{
49-
MLDSAParameters.ml_dsa_44,
50-
MLDSAParameters.ml_dsa_65,
51-
MLDSAParameters.ml_dsa_87,
52-
};
52+
{
53+
MLDSAParameters.ml_dsa_44,
54+
MLDSAParameters.ml_dsa_65,
55+
MLDSAParameters.ml_dsa_87,
56+
MLDSAParameters.ml_dsa_44_with_sha512,
57+
MLDSAParameters.ml_dsa_65_with_sha512,
58+
MLDSAParameters.ml_dsa_87_with_sha512,
59+
};
5360

5461
public void testConsistency()
5562
throws Exception
@@ -72,7 +79,7 @@ public void testConsistency()
7279
{
7380
AsymmetricCipherKeyPair kp = kpg.generateKeyPair();
7481

75-
MLDSASigner signer = new MLDSASigner();
82+
Signer signer = parameters.isPreHash() ? new HashMLDSASigner() : new MLDSASigner();
7683

7784
for (int j = 0; j < 2; ++j)
7885
{

0 commit comments

Comments
 (0)