Skip to content

Commit 4d0f95f

Browse files
committed
Refactoring in pqc.crypto
1 parent fc4886b commit 4d0f95f

File tree

11 files changed

+103
-85
lines changed

11 files changed

+103
-85
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/cmce/GF.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ final short gf_iszero(short a)
1111
return (short)((a - 1) >> 31);
1212
}
1313

14-
// final short gf_add(short left, short right)
15-
// {
16-
// return (short)(left ^ right);
17-
// }
18-
1914
abstract protected void gf_mul_poly(int length, int[] poly, short[] out, short[] left, short[] right, int[] temp);
2015
abstract protected void gf_sqr_poly(int length, int[] poly, short[] out, short[] input, int[] temp);
2116

core/src/main/java/org/bouncycastle/pqc/crypto/cmce/GF12.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ protected void gf_mul_poly(int length, int[] poly, short[] out, short[] left, sh
2222

2323
for (int j = 0; j < i; j++)
2424
{
25-
int t = temp[i + j];
26-
t ^= gf_mul_ext(left_i, right[j]);
27-
t ^= gf_mul_ext(left[j], right_i);
28-
temp[i + j] = t;
25+
temp[i + j] ^= gf_mul_ext_par(left_i, right[j], left[j], right_i);
2926
}
3027

3128
temp[i + i] = gf_mul_ext(left_i, right_i);
@@ -130,8 +127,7 @@ protected short gf_mul(short left, short right)
130127

131128
protected int gf_mul_ext(short left, short right)
132129
{
133-
int x = left;
134-
int y = right;
130+
int x = left, y = right;
135131

136132
int z = x * (y & 1);
137133
for (int i = 1; i < 12; i++)
@@ -142,6 +138,22 @@ protected int gf_mul_ext(short left, short right)
142138
return z;
143139
}
144140

141+
private int gf_mul_ext_par(short left0, short right0, short left1, short right1)
142+
{
143+
int x0 = left0, y0 = right0, x1 = left1, y1 = right1;
144+
145+
int z0 = x0 * (y0 & 1);
146+
int z1 = x1 * (y1 & 1);
147+
148+
for (int i = 1; i < 12; i++)
149+
{
150+
z0 ^= x0 * (y0 & (1 << i));
151+
z1 ^= x1 * (y1 & (1 << i));
152+
}
153+
154+
return z0 ^ z1;
155+
}
156+
145157
protected short gf_reduce(int x)
146158
{
147159
// assert (x >>> 24) == 0;

core/src/main/java/org/bouncycastle/pqc/crypto/cmce/GF13.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ protected void gf_mul_poly(int length, int[] poly, short[] out, short[] left, sh
2222

2323
for (int j = 0; j < i; j++)
2424
{
25-
int t = temp[i + j];
26-
t ^= gf_mul_ext(left_i, right[j]);
27-
t ^= gf_mul_ext(left[j], right_i);
28-
temp[i + j] = t;
25+
temp[i + j] ^= gf_mul_ext_par(left_i, right[j], left[j], right_i);
2926
}
3027

3128
temp[i + i] = gf_mul_ext(left_i, right_i);
@@ -112,8 +109,7 @@ protected short gf_mul(short in0, short in1)
112109

113110
protected int gf_mul_ext(short in0, short in1)
114111
{
115-
int x = in0;
116-
int y = in1;
112+
int x = in0, y = in1;
117113

118114
int z = x * (y & 1);
119115
for (int i = 1; i < 13; i++)
@@ -124,6 +120,22 @@ protected int gf_mul_ext(short in0, short in1)
124120
return z;
125121
}
126122

123+
private int gf_mul_ext_par(short in0, short in1, short in2, short in3)
124+
{
125+
int x0 = in0, y0 = in1, x1 = in2, y1 = in3;
126+
127+
int z0 = x0 * (y0 & 1);
128+
int z1 = x1 * (y1 & 1);
129+
130+
for (int i = 1; i < 13; i++)
131+
{
132+
z0 ^= x0 * (y0 & (1 << i));
133+
z1 ^= x1 * (y1 & (1 << i));
134+
}
135+
136+
return z0 ^ z1;
137+
}
138+
127139
protected short gf_reduce(int x)
128140
{
129141
// assert (x >>> 26) == 0;

core/src/main/java/org/bouncycastle/pqc/crypto/picnic/PicnicEngine.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ class PicnicEngine
2626

2727
/* Maximum lengths in bytes */
2828
private static final int PICNIC_MAX_LOWMC_BLOCK_SIZE = 32;
29-
private static final int PICNIC_MAX_PUBLICKEY_SIZE = (2 * PICNIC_MAX_LOWMC_BLOCK_SIZE + 1);
29+
// private static final int PICNIC_MAX_PUBLICKEY_SIZE = (2 * PICNIC_MAX_LOWMC_BLOCK_SIZE + 1);
3030
/**
3131
* < Largest serialized public key size, in bytes
3232
*/
33-
private static final int PICNIC_MAX_PRIVATEKEY_SIZE = (3 * PICNIC_MAX_LOWMC_BLOCK_SIZE + 2);
33+
// private static final int PICNIC_MAX_PRIVATEKEY_SIZE = (3 * PICNIC_MAX_LOWMC_BLOCK_SIZE + 2);
3434
/**
3535
* < Largest serialized private key size, in bytes
3636
*/
37-
private static final int PICNIC_MAX_SIGNATURE_SIZE = 209522;
37+
// private static final int PICNIC_MAX_SIGNATURE_SIZE = 209522;
3838
/**
3939
* < Largest signature size, in bytes
4040
*/
@@ -874,7 +874,7 @@ private int verify_picnic3(Signature2 sig, int[] pubKey, int[] plaintext, byte[]
874874
* We simulate the MPC with one fewer party; the unopned party's values are all set to zero. */
875875
int unopened = sig.challengeP[indexOf(sig.challengeC, numOpenedRounds, t)];
876876

877-
int tapeLengthBytes = 2 * andSizeBytes;
877+
// int tapeLengthBytes = 2 * andSizeBytes;
878878
if(unopened != last)
879879
{ // sig.proofs[t].aux is only set when P_t != N
880880
tapes[t].setAuxBits(sig.proofs[t].aux);

core/src/main/java/org/bouncycastle/pqc/crypto/picnic/PicnicParameters.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ public class PicnicParameters
77
{
88
private static class L1Constants
99
{
10-
protected static final LowmcConstantsL1 Instance = new LowmcConstantsL1();
10+
static final LowmcConstantsL1 INSTANCE = new LowmcConstantsL1();
1111
}
1212
private static class L3Constants
1313
{
14-
protected static final LowmcConstantsL3 Instance = new LowmcConstantsL3();
14+
static final LowmcConstantsL3 INSTANCE = new LowmcConstantsL3();
1515
}
1616
private static class L5Constants
1717
{
18-
protected static final LowmcConstantsL5 Instance = new LowmcConstantsL5();
18+
static final LowmcConstantsL5 INSTANCE = new LowmcConstantsL5();
1919
}
2020

2121
public static final PicnicParameters picnicl1fs = new PicnicParameters("picnicl1fs", 1);
@@ -55,18 +55,19 @@ PicnicEngine getEngine()
5555
case 2:
5656
case 7:
5757
case 10:
58-
return new PicnicEngine(param, L1Constants.Instance);
58+
return new PicnicEngine(param, L1Constants.INSTANCE);
5959
case 3:
6060
case 4:
6161
case 8:
6262
case 11:
63-
return new PicnicEngine(param, L3Constants.Instance);
63+
return new PicnicEngine(param, L3Constants.INSTANCE);
6464
case 12:
6565
case 5:
6666
case 6:
6767
case 9:
68-
return new PicnicEngine(param, L5Constants.Instance);
69-
default: return null;
68+
return new PicnicEngine(param, L5Constants.INSTANCE);
69+
default:
70+
return null;
7071
}
7172
}
7273
}

core/src/main/java/org/bouncycastle/pqc/crypto/picnic/Tape.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ protected void setAuxBits(byte[] input)
4040
*/
4141
protected void computeAuxTape(byte[] inputs)
4242
{
43-
int[] roundKey = new int[engine.LOWMC_MAX_WORDS];
44-
int[] x = new int[engine.LOWMC_MAX_WORDS];
45-
int[] y = new int[engine.LOWMC_MAX_WORDS];
46-
int[] key = new int[engine.LOWMC_MAX_WORDS];
47-
int[] key0 = new int[engine.LOWMC_MAX_WORDS];
43+
int[] roundKey = new int[PicnicEngine.LOWMC_MAX_WORDS];
44+
int[] x = new int[PicnicEngine.LOWMC_MAX_WORDS];
45+
int[] y = new int[PicnicEngine.LOWMC_MAX_WORDS];
46+
int[] key = new int[PicnicEngine.LOWMC_MAX_WORDS];
47+
int[] key0 = new int[PicnicEngine.LOWMC_MAX_WORDS];
4848

4949
key0[engine.stateSizeWords - 1] = 0;
5050
tapesToParityBits(key0, engine.stateSizeBits);

core/src/main/java/org/bouncycastle/pqc/crypto/picnic/Tree.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Tree
1111
private static final Logger LOG = Logger.getLogger(Tree.class.getName());
1212

1313
private static final int MAX_SEED_SIZE_BYTES = 32;
14-
private final int MAX_AUX_BYTES;
14+
// private final int MAX_AUX_BYTES;
1515

1616

1717
private int depth; /* The depth of the tree */
@@ -36,7 +36,7 @@ protected int getLeavesOffset()
3636
public Tree(PicnicEngine engine, int numLeaves, int dataSize)
3737
{
3838
this.engine = engine;
39-
MAX_AUX_BYTES = ((engine.LOWMC_MAX_AND_GATES + engine.LOWMC_MAX_KEY_BITS) / 8 + 1);
39+
// MAX_AUX_BYTES = ((PicnicEngine.LOWMC_MAX_AND_GATES + PicnicEngine.LOWMC_MAX_KEY_BITS) / 8 + 1);
4040

4141
this.depth = Utils.ceil_log2(numLeaves) + 1;
4242
this.numNodes = ((1 << (this.depth)) - 1) - ((1 << (this.depth - 1)) - numLeaves); /* Num nodes in complete - number of missing leaves */
@@ -287,7 +287,8 @@ protected int revealSeedsSize(int[] hideList, int hideListSize)
287287
{
288288
int[] numNodesRevealed = new int[1];
289289
numNodesRevealed[0] = 0;
290-
int[] revealed = getRevealedNodes(hideList, hideListSize, numNodesRevealed);
290+
// int[] revealed =
291+
getRevealedNodes(hideList, hideListSize, numNodesRevealed);
291292
return numNodesRevealed[0] * engine.seedSizeBytes;
292293
}
293294

@@ -321,7 +322,8 @@ protected int revealSeeds(int[] hideList, int hideListSize, byte[] output, int o
321322
protected int openMerkleTreeSize(int[] missingLeaves, int missingLeavesSize)
322323
{
323324
int[] revealedSize = new int[1];
324-
int[] revealed = this.getRevealedMerkleNodes(missingLeaves, missingLeavesSize, revealedSize);
325+
// int[] revealed =
326+
getRevealedMerkleNodes(missingLeaves, missingLeavesSize, revealedSize);
325327
return revealedSize[0] * engine.digestSizeBytes;
326328
}
327329

@@ -432,7 +434,7 @@ private void computeParentHash(int child, byte[] salt)
432434
/* One node may not have a right child when there's an odd number of leaves */
433435
engine.digest.update(this.nodes[2 * parent + 2],0, engine.digestSizeBytes);
434436
}
435-
engine.digest.update(salt,0, engine.saltSizeBytes);
437+
engine.digest.update(salt,0, PicnicEngine.saltSizeBytes);
436438
engine.digest.update(Pack.intToLittleEndian(parent), 0, 2);
437439
engine.digest.doFinal(this.nodes[parent], 0, engine.digestSizeBytes);
438440
this.haveNode[parent] = true;
@@ -526,10 +528,9 @@ private void expandSeeds(byte[] salt, int repIndex)
526528

527529
private void hashSeed(byte[] digest_arr, byte[] inputSeed, byte[] salt, byte hashPrefix, int repIndex, int nodeIndex)
528530
{
529-
530531
engine.digest.update(hashPrefix);
531532
engine.digest.update(inputSeed, 0, engine.seedSizeBytes);
532-
engine.digest.update(salt, 0, engine.saltSizeBytes);
533+
engine.digest.update(salt, 0, PicnicEngine.saltSizeBytes);
533534
engine.digest.update(Pack.shortToLittleEndian((short)(repIndex & 0xffff)), 0, 2); //todo check endianness
534535
engine.digest.update(Pack.shortToLittleEndian((short)(nodeIndex & 0xffff)), 0, 2); //todo check endianness
535536
engine.digest.doFinal(digest_arr, 0, 2 * engine.seedSizeBytes);

core/src/main/java/org/bouncycastle/pqc/crypto/sike/Fpx.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,11 +1738,11 @@ protected void fp2sub(long[][] a, long[][] b, long[][] c)
17381738
}
17391739

17401740
// GF(p^2) subtraction with correction with 4*p, c = a-b+4p in GF(p^2).
1741-
private void mp2_sub_p4(long[][] a, long[][] b, long[][] c)
1742-
{
1743-
mp_subPRIME_p4(a[0], b[0], c[0]);
1744-
mp_subPRIME_p4(a[1], b[1], c[1]);
1745-
}
1741+
// private void mp2_sub_p4(long[][] a, long[][] b, long[][] c)
1742+
// {
1743+
// mp_subPRIME_p4(a[0], b[0], c[0]);
1744+
// mp_subPRIME_p4(a[1], b[1], c[1]);
1745+
// }
17461746

17471747
// Multiprecision multiplication, c = a*b mod p.
17481748
protected void fpmul_mont(long[] ma, long[] mb, long[] mc)

core/src/main/java/org/bouncycastle/pqc/crypto/sike/SIDH_Compressed.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -883,26 +883,26 @@ protected int EphemeralKeyGeneration_A_extended(byte[] PrivateKeyA, byte[] Compr
883883

884884
// Alice's ephemeral public key generation using compression -- SIDH protocol
885885
// Output: PrivateKeyA[MSG_BYTES + engine.params.SECRETKEY_A_BYTES] <- x(K_A) where K_A = PA + sk_A*Q_A
886-
private int EphemeralKeyGeneration_A(byte[] PrivateKeyA, byte[] CompressedPKA)
887-
{
888-
int[] rs = new int[3],
889-
D = new int[engine.params.DLEN_3];
890-
long[] c0 = new long[engine.params.NWORDS_ORDER],
891-
d0 = new long[engine.params.NWORDS_ORDER],
892-
c1 = new long[engine.params.NWORDS_ORDER],
893-
d1 = new long[engine.params.NWORDS_ORDER];
894-
long[][] a24 = new long[2][engine.params.NWORDS_FIELD];
895-
long[][][] f = new long[4][2][engine.params.NWORDS_FIELD];
896-
long[][][][] As = new long[engine.params.MAX_Alice+1][5][2][engine.params.NWORDS_FIELD];
897-
PointProjFull[] Rs = new PointProjFull[2];
898-
899-
FullIsogeny_A_dual(PrivateKeyA, As, a24, 0);
900-
BuildOrdinary3nBasis_dual(a24, As, Rs, rs, rs, 2);
901-
Tate3_pairings(Rs, f);
902-
Dlogs3_dual(f, D, d0, c0, d1, c1);
903-
Compress_PKA_dual(d0, c0, d1, c1, a24, rs, CompressedPKA);
904-
return 0;
905-
}
886+
// private int EphemeralKeyGeneration_A(byte[] PrivateKeyA, byte[] CompressedPKA)
887+
// {
888+
// int[] rs = new int[3],
889+
// D = new int[engine.params.DLEN_3];
890+
// long[] c0 = new long[engine.params.NWORDS_ORDER],
891+
// d0 = new long[engine.params.NWORDS_ORDER],
892+
// c1 = new long[engine.params.NWORDS_ORDER],
893+
// d1 = new long[engine.params.NWORDS_ORDER];
894+
// long[][] a24 = new long[2][engine.params.NWORDS_FIELD];
895+
// long[][][] f = new long[4][2][engine.params.NWORDS_FIELD];
896+
// long[][][][] As = new long[engine.params.MAX_Alice+1][5][2][engine.params.NWORDS_FIELD];
897+
// PointProjFull[] Rs = new PointProjFull[2];
898+
//
899+
// FullIsogeny_A_dual(PrivateKeyA, As, a24, 0);
900+
// BuildOrdinary3nBasis_dual(a24, As, Rs, rs, rs, 2);
901+
// Tate3_pairings(Rs, f);
902+
// Dlogs3_dual(f, D, d0, c0, d1, c1);
903+
// Compress_PKA_dual(d0, c0, d1, c1, a24, rs, CompressedPKA);
904+
// return 0;
905+
// }
906906

907907
// Bob's ephemeral shared secret computation using compression
908908
// It produces a shared secret key SharedSecretB using his secret key PrivateKeyB and Alice's decompressed data point_R and param_A

core/src/main/java/org/bouncycastle/pqc/crypto/sike/SIKEKEMExtractor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public SIKEKEMExtractor(SIKEPrivateKeyParameters privParams)
2525
private void initCipher(SIKEParameters param)
2626
{
2727
engine = param.getEngine();
28-
SIKEPrivateKeyParameters privateParams = (SIKEPrivateKeyParameters)key;
2928
}
3029

3130
public byte[] extractSecret(byte[] encapsulation)

0 commit comments

Comments
 (0)