Skip to content

Commit 03c3aaf

Browse files
committed
style cleanups
1 parent d86fe62 commit 03c3aaf

File tree

6 files changed

+131
-24
lines changed

6 files changed

+131
-24
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/falcon/FalconKeyGen.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ int modp_montymul(int a, int b, int p, int p0i)
105105
long z, w;
106106
int d;
107107

108-
z = Integer.toUnsignedLong(a) * Integer.toUnsignedLong(b);
109-
w = ((z * p0i) & Integer.toUnsignedLong(0x7FFFFFFF)) * p;
108+
z = toUnsignedLong(a) * toUnsignedLong(b);
109+
w = ((z * p0i) & toUnsignedLong(0x7FFFFFFF)) * p;
110110
d = (int)((z + w) >>> 31) - p;
111111
d += p & -(d >>> 31);
112112
return d;
@@ -576,7 +576,7 @@ int zint_mul_small(int[] srcm, int m, int mlen, int x)
576576
{
577577
long z;
578578

579-
z = Integer.toUnsignedLong(srcm[m + u]) * Integer.toUnsignedLong(x) + cc;
579+
z = toUnsignedLong(srcm[m + u]) * toUnsignedLong(x) + cc;
580580
srcm[m + u] = (int)z & 0x7FFFFFFF;
581581
cc = (int)(z >> 31);
582582
}
@@ -655,7 +655,7 @@ void zint_add_mul_small(int[] srcx, int x,
655655

656656
xw = srcx[x + u];
657657
yw = srcy[y + u];
658-
z = Integer.toUnsignedLong(yw) * Integer.toUnsignedLong(s) + Integer.toUnsignedLong(xw) + Integer.toUnsignedLong(cc);
658+
z = toUnsignedLong(yw) * toUnsignedLong(s) + toUnsignedLong(xw) + toUnsignedLong(cc);
659659
srcx[x + u] = (int)z & 0x7FFFFFFF;
660660
cc = (int)(z >>> 31);
661661
}
@@ -946,9 +946,9 @@ void zint_co_reduce_mod(int[] srca, int a, int[] srcb, int b, int[] srcm, int m,
946946
wa = srca[a + u];
947947
wb = srcb[b + u];
948948
za = wa * xa + wb * xb
949-
+ srcm[m + u] * Integer.toUnsignedLong(fa) + cca;
949+
+ srcm[m + u] * toUnsignedLong(fa) + cca;
950950
zb = wa * ya + wb * yb
951-
+ srcm[m + u] * Integer.toUnsignedLong(fb) + ccb;
951+
+ srcm[m + u] * toUnsignedLong(fb) + ccb;
952952
if (u > 0)
953953
{
954954
srca[a + u - 1] = (int)za & 0x7FFFFFFF;
@@ -1203,8 +1203,8 @@ int zint_bezout(int[] srcu, int u, int[] srcv, int v,
12031203
a0 &= ~c1;
12041204
b1 |= b0 & c1;
12051205
b0 &= ~c1;
1206-
a_hi = (Integer.toUnsignedLong(a0) << 31) + Integer.toUnsignedLong(a1);
1207-
b_hi = (Integer.toUnsignedLong(b0) << 31) + Integer.toUnsignedLong(b1);
1206+
a_hi = (toUnsignedLong(a0) << 31) + toUnsignedLong(a1);
1207+
b_hi = (toUnsignedLong(b0) << 31) + toUnsignedLong(b1);
12081208
a_lo = srctmp[a + 0];
12091209
b_lo = srctmp[b + 0];
12101210

@@ -1265,11 +1265,11 @@ int zint_bezout(int[] srcu, int u, int[] srcv, int v,
12651265
* Conditional subtractions.
12661266
*/
12671267
a_lo -= b_lo & -cAB;
1268-
a_hi -= b_hi & -Integer.toUnsignedLong(cAB);
1268+
a_hi -= b_hi & -toUnsignedLong(cAB);
12691269
pa -= qa & -(long)cAB;
12701270
pb -= qb & -(long)cAB;
12711271
b_lo -= a_lo & -cBA;
1272-
b_hi -= a_hi & -Integer.toUnsignedLong(cBA);
1272+
b_hi -= a_hi & -toUnsignedLong(cBA);
12731273
qa -= pa & -(long)cBA;
12741274
qb -= pb & -(long)cBA;
12751275

@@ -1279,11 +1279,11 @@ int zint_bezout(int[] srcu, int u, int[] srcv, int v,
12791279
a_lo += a_lo & (cA - 1);
12801280
pa += pa & ((long)cA - 1);
12811281
pb += pb & ((long)cA - 1);
1282-
a_hi ^= (a_hi ^ (a_hi >> 1)) & -Integer.toUnsignedLong(cA);
1282+
a_hi ^= (a_hi ^ (a_hi >> 1)) & -toUnsignedLong(cA);
12831283
b_lo += b_lo & -cA;
12841284
qa += qa & -(long)cA;
12851285
qb += qb & -(long)cA;
1286-
b_hi ^= (b_hi ^ (b_hi >> 1)) & (Integer.toUnsignedLong(cA) - 1);
1286+
b_hi ^= (b_hi ^ (b_hi >> 1)) & (toUnsignedLong(cA) - 1);
12871287
}
12881288

12891289
/*
@@ -1359,7 +1359,7 @@ void zint_add_scaled_mul_small(int[] srcx, int x, int xlen,
13591359
/*
13601360
* The expression below does not overflow.
13611361
*/
1362-
z = (Integer.toUnsignedLong(wys) * (long)k + Integer.toUnsignedLong(srcx[x + u]) + cc);
1362+
z = (toUnsignedLong(wys) * (long)k + toUnsignedLong(srcx[x + u]) + cc);
13631363
srcx[x + u] = (int)z & 0x7FFFFFFF;
13641364

13651365
/*
@@ -3782,4 +3782,9 @@ void keygen(SHAKE256 rng,
37823782
break;
37833783
}
37843784
}
3785+
3786+
private long toUnsignedLong(int x)
3787+
{
3788+
return x & 0xffffffffL;
3789+
}
37853790
}

core/src/main/jdk1.4/org/bouncycastle/crypto/CryptoServicePurpose.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ private CryptoServicePurpose(int ord)
1717
{
1818
this.ord = ord;
1919
}
20+
21+
public int ordinal()
22+
{
23+
return ord;
24+
}
2025
}

core/src/main/jdk1.4/org/bouncycastle/util/Arrays.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,80 @@ public static boolean isNullOrEmpty(Object[] array)
11001100
{
11011101
return null == array || array.length < 1;
11021102
}
1103-
1103+
1104+
/**
1105+
* Make a copy of a range of bytes from the passed in array. The range can extend beyond the end
1106+
* of the input array, in which case the returned array will be padded with zeroes.
1107+
*
1108+
* @param original
1109+
* the array from which the data is to be copied.
1110+
* @param from
1111+
* the start index at which the copying should take place.
1112+
* @param to
1113+
* the final index of the range (exclusive).
1114+
*
1115+
* @return a new byte array containing the range given.
1116+
*/
1117+
public static byte[] copyOfRange(byte[] original, int from, int to)
1118+
{
1119+
int newLength = getLength(from, to);
1120+
byte[] copy = new byte[newLength];
1121+
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1122+
return copy;
1123+
}
1124+
1125+
public static char[] copyOfRange(char[] original, int from, int to)
1126+
{
1127+
int newLength = getLength(from, to);
1128+
char[] copy = new char[newLength];
1129+
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1130+
return copy;
1131+
}
1132+
1133+
public static int[] copyOfRange(int[] original, int from, int to)
1134+
{
1135+
int newLength = getLength(from, to);
1136+
int[] copy = new int[newLength];
1137+
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1138+
return copy;
1139+
}
1140+
1141+
public static long[] copyOfRange(long[] original, int from, int to)
1142+
{
1143+
int newLength = getLength(from, to);
1144+
long[] copy = new long[newLength];
1145+
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1146+
return copy;
1147+
}
1148+
1149+
public static short[] copyOfRange(short[] original, int from, int to)
1150+
{
1151+
int newLength = getLength(from, to);
1152+
short[] copy = new short[newLength];
1153+
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1154+
return copy;
1155+
}
1156+
1157+
public static BigInteger[] copyOfRange(BigInteger[] original, int from, int to)
1158+
{
1159+
int newLength = getLength(from, to);
1160+
BigInteger[] copy = new BigInteger[newLength];
1161+
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
1162+
return copy;
1163+
}
1164+
1165+
private static int getLength(int from, int to)
1166+
{
1167+
int newLength = to - from;
1168+
if (newLength < 0)
1169+
{
1170+
StringBuffer sb = new StringBuffer(from);
1171+
sb.append(" > ").append(to);
1172+
throw new IllegalArgumentException(sb.toString());
1173+
}
1174+
return newLength;
1175+
}
1176+
11041177
/**
11051178
* Iterator backed by a specific array.
11061179
*/

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

Lines changed: 24 additions & 0 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;
@@ -200,4 +201,27 @@ public void testVectors()
200201
System.out.println("testing successful!");
201202
}
202203
}
204+
205+
public void testKyberRandom()
206+
{
207+
SecureRandom random = new SecureRandom();
208+
KyberKeyPairGenerator keyGen = new KyberKeyPairGenerator();
209+
210+
keyGen.init(new KyberKeyGenerationParameters(random, KyberParameters.kyber1024));
211+
212+
for (int i = 0; i != 1000; i++)
213+
{
214+
AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair();
215+
216+
KyberKEMGenerator kemGen = new KyberKEMGenerator(random);
217+
218+
SecretWithEncapsulation secretEncap = kemGen.generateEncapsulated(keyPair.getPublic());
219+
220+
KyberKEMExtractor kemExtract = new KyberKEMExtractor((KyberPrivateKeyParameters)keyPair.getPrivate());
221+
222+
byte[] decryptedSharedSecret = kemExtract.extractSecret(secretEncap.getEncapsulation());
223+
224+
assertTrue(Arrays.areEqual(secretEncap.getSecret(), decryptedSharedSecret));
225+
}
226+
}
203227
}

pg/src/main/java/org/bouncycastle/openpgp/PGPPublicKeyRing.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public PGPPublicKeyRing(
4949

5050
private static List<PGPPublicKey> checkKeys(List<PGPPublicKey> keys)
5151
{
52-
List<PGPPublicKey> rv = new ArrayList<>(keys.size());
52+
List<PGPPublicKey> rv = new ArrayList<PGPPublicKey>(keys.size());
5353

5454
for (int i = 0; i != keys.size(); i++)
5555
{
@@ -306,7 +306,7 @@ public static PGPPublicKeyRing insertPublicKey(
306306
PGPPublicKeyRing pubRing,
307307
PGPPublicKey pubKey)
308308
{
309-
List<PGPPublicKey> keys = new ArrayList<>(pubRing.keys);
309+
List<PGPPublicKey> keys = new ArrayList<PGPPublicKey>(pubRing.keys);
310310
boolean found = false;
311311
boolean masterFound = false;
312312

pg/src/main/java/org/bouncycastle/openpgp/PGPSecretKeyRing.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class PGPSecretKeyRing
4242

4343
private static List<PGPSecretKey> checkKeys(List<PGPSecretKey> keys)
4444
{
45-
List<PGPSecretKey> rv = new ArrayList<>(keys.size());
45+
List<PGPSecretKey> rv = new ArrayList<PGPSecretKey>(keys.size());
4646

4747
for (int i = 0; i != keys.size(); i++)
4848
{
@@ -411,7 +411,7 @@ public Iterator<PGPSecretKey> iterator()
411411
*/
412412
public static PGPSecretKeyRing replacePublicKeys(PGPSecretKeyRing secretRing, PGPPublicKeyRing publicRing)
413413
{
414-
List<PGPSecretKey> newList = new ArrayList<>(secretRing.keys.size());
414+
List<PGPSecretKey> newList = new ArrayList<PGPSecretKey>(secretRing.keys.size());
415415

416416
for (Iterator<PGPSecretKey> it = secretRing.keys.iterator(); it.hasNext(); )
417417
{
@@ -439,7 +439,7 @@ public static PGPSecretKeyRing insertOrReplacePublicKey(PGPSecretKeyRing secretR
439439

440440
if (secretKey != null)
441441
{
442-
List<PGPSecretKey> newList = new ArrayList<>(secretRing.keys.size());
442+
List<PGPSecretKey> newList = new ArrayList<PGPSecretKey>(secretRing.keys.size());
443443
for (Iterator<PGPSecretKey> it = secretRing.getSecretKeys(); it.hasNext(); )
444444
{
445445
PGPSecretKey sk = (PGPSecretKey)it.next();
@@ -454,7 +454,7 @@ public static PGPSecretKeyRing insertOrReplacePublicKey(PGPSecretKeyRing secretR
454454
}
455455
else
456456
{
457-
List<PGPPublicKey> extras = new ArrayList<>(secretRing.extraPubKeys.size());
457+
List<PGPPublicKey> extras = new ArrayList<PGPPublicKey>(secretRing.extraPubKeys.size());
458458
boolean found = false;
459459

460460
for (Iterator<PGPPublicKey> it = secretRing.getExtraPublicKeys(); it.hasNext(); )
@@ -476,7 +476,7 @@ public static PGPSecretKeyRing insertOrReplacePublicKey(PGPSecretKeyRing secretR
476476
extras.add(publicKey);
477477
}
478478

479-
return new PGPSecretKeyRing(new ArrayList<>(secretRing.keys), extras);
479+
return new PGPSecretKeyRing(new ArrayList<PGPSecretKey>(secretRing.keys), extras);
480480
}
481481
}
482482

@@ -495,7 +495,7 @@ public static PGPSecretKeyRing copyWithNewPassword(
495495
PBESecretKeyEncryptor newKeyEncryptor)
496496
throws PGPException
497497
{
498-
List<PGPSecretKey> newKeys = new ArrayList<>(ring.keys.size());
498+
List<PGPSecretKey> newKeys = new ArrayList<PGPSecretKey>(ring.keys.size());
499499

500500
for (Iterator<PGPSecretKey> keys = ring.getSecretKeys(); keys.hasNext(); )
501501
{
@@ -526,7 +526,7 @@ public static PGPSecretKeyRing insertSecretKey(
526526
PGPSecretKeyRing secRing,
527527
PGPSecretKey secKey)
528528
{
529-
List<PGPSecretKey> keys = new ArrayList<>(secRing.keys);
529+
List<PGPSecretKey> keys = new ArrayList<PGPSecretKey>(secRing.keys);
530530
boolean found = false;
531531
boolean masterFound = false;
532532

@@ -580,7 +580,7 @@ public static PGPSecretKeyRing removeSecretKey(
580580
int count = secRing.keys.size();
581581
long keyID = secKey.getKeyID();
582582

583-
ArrayList<PGPSecretKey> result = new ArrayList<>(count);
583+
ArrayList<PGPSecretKey> result = new ArrayList<PGPSecretKey>(count);
584584
boolean found = false;
585585

586586
for (int i = 0; i < count; ++i)

0 commit comments

Comments
 (0)