Skip to content

Commit 394093c

Browse files
author
royb
committed
Picnic: deferred static initializers
1 parent e6e1b65 commit 394093c

File tree

11 files changed

+299
-270
lines changed

11 files changed

+299
-270
lines changed

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

Lines changed: 52 additions & 219 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.bouncycastle.pqc.crypto.picnic;
2+
3+
import org.bouncycastle.util.Exceptions;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.Properties;
8+
9+
public class LowmcConstantsL1
10+
extends LowmcConstants
11+
{
12+
LowmcConstantsL1()
13+
{
14+
InputStream input = LowmcConstants.class.getResourceAsStream("lowmcL1.properties");
15+
Properties props = new Properties();
16+
17+
// load a properties file
18+
try
19+
{
20+
props.load(input);
21+
}
22+
catch (IOException e)
23+
{
24+
throw Exceptions.illegalStateException("unable to load Picnic properties: " + e.getMessage(), e);
25+
}
26+
27+
// Parameters for security level L1
28+
// Block/key size: 128
29+
// Rounds: 20
30+
linearMatrices = ReadFromProperty(props, "linearMatrices", 40960);
31+
roundConstants = ReadFromProperty(props, "roundConstants", 320);
32+
keyMatrices = ReadFromProperty(props, "keyMatrices", 43008);
33+
34+
LMatrix = new KMatrices(20, 128, 4, linearMatrices);
35+
KMatrix = new KMatrices(21, 128, 4, keyMatrices);
36+
RConstants = new KMatrices(0, 1, 4, roundConstants);
37+
38+
// Parameters for security level L1, full s-box layer
39+
// Block/key size: 129
40+
// Rounds: 4
41+
// Note that each 129-bit row of the matrix is zero padded to 160 bits (the next multiple of 32)
42+
linearMatrices_full = ReadFromProperty(props, "linearMatrices_full", 12800);
43+
keyMatrices_full = ReadFromProperty(props, "keyMatrices_full", 12900);
44+
keyMatrices_inv = ReadFromProperty(props, "keyMatrices_inv", 2850);
45+
linearMatrices_inv = ReadFromProperty(props, "linearMatrices_inv", 12800);
46+
roundConstants_full = ReadFromProperty(props, "roundConstants_full", 80);
47+
48+
LMatrix_full = new KMatrices(4, 129, 5, linearMatrices_full);
49+
LMatrix_inv = new KMatrices(4, 129, 5, linearMatrices_inv);
50+
KMatrix_full = new KMatrices(5, 129, 5, keyMatrices_full);
51+
KMatrix_inv = new KMatrices(1, 129, 5, keyMatrices_inv);
52+
RConstants_full = new KMatrices(4, 1, 5, roundConstants_full);
53+
54+
}
55+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.bouncycastle.pqc.crypto.picnic;
2+
3+
import org.bouncycastle.util.Exceptions;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.Properties;
8+
9+
public class LowmcConstantsL3
10+
extends LowmcConstants
11+
{
12+
LowmcConstantsL3()
13+
{
14+
InputStream input = LowmcConstants.class.getResourceAsStream("lowmcL3.properties");
15+
Properties props = new Properties();
16+
17+
// load a properties file
18+
try
19+
{
20+
props.load(input);
21+
}
22+
catch (IOException e)
23+
{
24+
throw Exceptions.illegalStateException("unable to load Picnic properties: " + e.getMessage(), e);
25+
}
26+
27+
// Parameters for security level L3
28+
// Block/key size: 192
29+
// Rounds: 30
30+
linearMatrices = ReadFromProperty(props, "linearMatrices", 138240);
31+
roundConstants = ReadFromProperty(props, "roundConstants", 720);
32+
keyMatrices = ReadFromProperty(props, "keyMatrices", 142848);
33+
34+
LMatrix = new KMatrices(30, 192, 6, linearMatrices);
35+
KMatrix = new KMatrices(31, 192, 6, keyMatrices);
36+
RConstants = new KMatrices(30, 1, 6, roundConstants);
37+
38+
// Parameters for security level L3, full s-box layer
39+
// Block/key size: 192
40+
// S-boxes: 64
41+
// Rounds: 4
42+
linearMatrices_full = ReadFromProperty(props, "linearMatrices_full", 18432);
43+
linearMatrices_inv = ReadFromProperty(props, "linearMatrices_inv", 18432);
44+
roundConstants_full = ReadFromProperty(props, "roundConstants_full", 96);
45+
keyMatrices_full = ReadFromProperty(props, "keyMatrices_full", 23040);
46+
keyMatrices_inv = ReadFromProperty(props, "keyMatrices_inv", 4608);
47+
48+
LMatrix_full = new KMatrices(4, 192, 6, linearMatrices_full);
49+
LMatrix_inv = new KMatrices(4, 192, 6, linearMatrices_inv);
50+
KMatrix_full = new KMatrices(5, 192, 6, keyMatrices_full);
51+
KMatrix_inv = new KMatrices(1, 192, 6, keyMatrices_inv);
52+
RConstants_full = new KMatrices(4, 1, 6, roundConstants_full);
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.bouncycastle.pqc.crypto.picnic;
2+
3+
import org.bouncycastle.util.Exceptions;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.Properties;
8+
9+
public class LowmcConstantsL5
10+
extends LowmcConstants
11+
{
12+
LowmcConstantsL5()
13+
{
14+
InputStream input = LowmcConstants.class.getResourceAsStream("lowmcL5.properties");
15+
Properties props = new Properties();
16+
17+
// load a properties file
18+
try
19+
{
20+
props.load(input);
21+
}
22+
catch (IOException e)
23+
{
24+
throw Exceptions.illegalStateException("unable to load Picnic properties: " + e.getMessage(), e);
25+
}
26+
// Parameters for security level L5
27+
// Block/key size: 256
28+
// Rounds: 38
29+
linearMatrices = ReadFromProperty(props, "linearMatrices", 311296);
30+
roundConstants = ReadFromProperty(props, "roundConstants", 1216);
31+
keyMatrices = ReadFromProperty(props, "keyMatrices", 319488);
32+
33+
LMatrix = new KMatrices(38, 256, 8, linearMatrices);
34+
KMatrix = new KMatrices(39, 256, 8, keyMatrices);
35+
RConstants = new KMatrices(38, 1, 8, roundConstants);
36+
37+
// Parameters for security level L5, full nonlinear layer
38+
// Block/key size: 255
39+
// S-boxes: 85
40+
// Rounds: 4
41+
linearMatrices_full = ReadFromProperty(props, "linearMatrices_full", 32768);
42+
linearMatrices_inv = ReadFromProperty(props, "linearMatrices_inv", 32768);
43+
roundConstants_full = ReadFromProperty(props, "roundConstants_full", 128);
44+
keyMatrices_full = ReadFromProperty(props, "keyMatrices_full", 40960);
45+
keyMatrices_inv = ReadFromProperty(props, "keyMatrices_inv", 8160);
46+
47+
LMatrix_full = new KMatrices(4, 255, 8, linearMatrices_full);
48+
LMatrix_inv = new KMatrices(4, 255, 8, linearMatrices_inv);
49+
KMatrix_full = new KMatrices(5, 255, 8, keyMatrices_full);
50+
KMatrix_inv = new KMatrices(1, 255, 8, keyMatrices_inv);
51+
RConstants_full = new KMatrices(4, 1, 8, roundConstants_full);
52+
}
53+
}

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ public int getTrueSignatureSize()
8989
{
9090
return signatureLength;
9191
}
92+
93+
protected final LowmcConstants lowmcConstants;
9294

93-
PicnicEngine(int picnicParams)
95+
PicnicEngine(int picnicParams, LowmcConstants lowmcConstants)
9496
{
97+
this.lowmcConstants = lowmcConstants;
9598
parameters = picnicParams;
9699
switch (parameters)
97100
{
@@ -531,7 +534,7 @@ void mpc_LowMC_verify(View view1, View view2, Tape tapes, int[] tmp, int[] plain
531534

532535
mpc_xor_constant_verify(tmp, plaintext, 0, stateSizeWords, challenge);
533536

534-
KMatricesWithPointer current = LowmcConstants.KMatrix(this, 0);
537+
KMatricesWithPointer current = lowmcConstants.KMatrix(this, 0);
535538
matrix_mul_offset(tmp, 0,
536539
view1.inputShare, 0,
537540
current.getData(), current.getMatrixPointer());
@@ -543,7 +546,7 @@ void mpc_LowMC_verify(View view1, View view2, Tape tapes, int[] tmp, int[] plain
543546

544547
for (int r = 1; r <= numRounds; ++r)
545548
{
546-
current = LowmcConstants.KMatrix(this, r);
549+
current = lowmcConstants.KMatrix(this, r);
547550
matrix_mul_offset(tmp, 0,
548551
view1.inputShare, 0,
549552
current.getData(), current.getMatrixPointer());
@@ -553,12 +556,12 @@ void mpc_LowMC_verify(View view1, View view2, Tape tapes, int[] tmp, int[] plain
553556

554557
mpc_substitution_verify(tmp, tapes, view1, view2);
555558

556-
current = LowmcConstants.LMatrix(this, r - 1);
559+
current = lowmcConstants.LMatrix(this, r - 1);
557560
mpc_matrix_mul(tmp, 2*stateSizeWords,
558561
tmp, 2*stateSizeWords,
559562
current.getData(), current.getMatrixPointer(), 2);
560563

561-
current = LowmcConstants.RConstant(this, r - 1);
564+
current = lowmcConstants.RConstant(this, r - 1);
562565
mpc_xor_constant_verify(tmp, current.getData(), current.getMatrixPointer(), stateSizeWords, challenge);
563566
mpc_xor(tmp, tmp, stateSizeWords, 2);
564567
}
@@ -1541,7 +1544,7 @@ private void mpc_LowMC(Tape tapes, View[] views, int[] plaintext, int[] slab)
15411544

15421545
mpc_xor_constant(slab, 3*stateSizeWords, plaintext, 0, stateSizeWords);
15431546

1544-
KMatricesWithPointer current = LowmcConstants.KMatrix(this, 0);
1547+
KMatricesWithPointer current = lowmcConstants.KMatrix(this, 0);
15451548
for (int player = 0; player < 3; player++)
15461549
{
15471550
matrix_mul_offset(slab, player * stateSizeWords, views[player].inputShare, 0,
@@ -1552,7 +1555,7 @@ private void mpc_LowMC(Tape tapes, View[] views, int[] plaintext, int[] slab)
15521555

15531556
for (int r = 1; r <= numRounds; r++)
15541557
{
1555-
current = LowmcConstants.KMatrix(this, r);
1558+
current = lowmcConstants.KMatrix(this, r);
15561559
for (int player = 0; player < 3; player++)
15571560
{
15581561
matrix_mul_offset(slab, player * stateSizeWords,
@@ -1562,12 +1565,12 @@ private void mpc_LowMC(Tape tapes, View[] views, int[] plaintext, int[] slab)
15621565

15631566
mpc_substitution(slab, tapes, views);
15641567

1565-
current = LowmcConstants.LMatrix(this, r - 1);
1568+
current = lowmcConstants.LMatrix(this, r - 1);
15661569
mpc_matrix_mul(slab, 3*stateSizeWords,
15671570
slab, 3*stateSizeWords,
15681571
current.getData(), current.getMatrixPointer(), 3);
15691572

1570-
current = LowmcConstants.RConstant(this, r - 1);
1573+
current = lowmcConstants.RConstant(this, r - 1);
15711574
mpc_xor_constant(slab, 3*stateSizeWords,
15721575
current.getData(), current.getMatrixPointer(), stateSizeWords);
15731576

@@ -2032,7 +2035,7 @@ private int simulateOnline(int[] maskedKey, Tape tape, int[] tmp_shares,
20322035
int[] roundKey = new int[LOWMC_MAX_WORDS];
20332036
int[] state = new int[LOWMC_MAX_WORDS];
20342037

2035-
KMatricesWithPointer current = LowmcConstants.KMatrix(this,0);
2038+
KMatricesWithPointer current = lowmcConstants.KMatrix(this,0);
20362039
matrix_mul(roundKey, maskedKey, current.getData(), current.getMatrixPointer()); // roundKey = maskedKey * KMatrix[0]
20372040
xor_array(state, roundKey, plaintext,0, stateSizeWords); // state = plaintext + roundKey
20382041

@@ -2041,13 +2044,13 @@ private int simulateOnline(int[] maskedKey, Tape tape, int[] tmp_shares,
20412044
tapesToWords(tmp_shares, tape);
20422045
mpc_sbox(state, tmp_shares, tape, msg);
20432046

2044-
current = LowmcConstants.LMatrix(this, r - 1);
2047+
current = lowmcConstants.LMatrix(this, r - 1);
20452048
matrix_mul(state, state, current.getData(), current.getMatrixPointer()); // state = state * LMatrix (r-1)
20462049

2047-
current = LowmcConstants.RConstant(this,r - 1);
2050+
current = lowmcConstants.RConstant(this,r - 1);
20482051
xor_array(state, state, current.getData(), current.getMatrixPointer(), stateSizeWords); // state += RConstant
20492052

2050-
current = LowmcConstants.KMatrix(this, r);
2053+
current = lowmcConstants.KMatrix(this, r);
20512054
matrix_mul(roundKey, maskedKey, current.getData(), current.getMatrixPointer());
20522055
xor_array(state, roundKey, state, 0, stateSizeWords); // state += roundKey
20532056
}
@@ -2365,22 +2368,22 @@ private void LowMCEnc(int[] plaintext, int[] output, int[] key)
23652368
System.arraycopy(plaintext, 0, output, 0, stateSizeWords);
23662369
}
23672370

2368-
KMatricesWithPointer current = LowmcConstants.KMatrix(this,0);
2371+
KMatricesWithPointer current = lowmcConstants.KMatrix(this,0);
23692372
matrix_mul(roundKey, key, current.getData(), current.getMatrixPointer());
23702373

23712374
xor_array(output, output, roundKey, 0, stateSizeWords);
23722375

23732376
for (int r = 1; r <= numRounds; r++)
23742377
{
2375-
current = LowmcConstants.KMatrix(this, r);
2378+
current = lowmcConstants.KMatrix(this, r);
23762379
matrix_mul(roundKey, key, current.getData(), current.getMatrixPointer());
23772380

23782381
substitution(output);
23792382

2380-
current = LowmcConstants.LMatrix(this,r-1);
2383+
current = lowmcConstants.LMatrix(this,r-1);
23812384
matrix_mul(output, output, current.getData(), current.getMatrixPointer());
23822385

2383-
current = LowmcConstants.RConstant(this,r-1);
2386+
current = lowmcConstants.RConstant(this,r-1);
23842387
xor_array(output, output, current.getData(), current.getMatrixPointer(), stateSizeWords);
23852388
xor_array(output, output, roundKey, 0, stateSizeWords);
23862389
}

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
public class PicnicParameters
66
implements CipherParameters
77
{
8+
private static class L1Constants
9+
{
10+
protected static final LowmcConstantsL1 Instance = new LowmcConstantsL1();
11+
}
12+
private static class L3Constants
13+
{
14+
protected static final LowmcConstantsL3 Instance = new LowmcConstantsL3();
15+
}
16+
private static class L5Constants
17+
{
18+
protected static final LowmcConstantsL5 Instance = new LowmcConstantsL5();
19+
}
20+
821
public static final PicnicParameters picnicl1fs = new PicnicParameters("picnicl1fs", 1);
922
public static final PicnicParameters picnicl1ur = new PicnicParameters("picnicl1ur", 2);
1023
public static final PicnicParameters picnicl3fs = new PicnicParameters("picnicl3fs", 3);
@@ -36,6 +49,24 @@ public String getName()
3649

3750
PicnicEngine getEngine()
3851
{
39-
return new PicnicEngine(param);
52+
switch (param)
53+
{
54+
case 1:
55+
case 2:
56+
case 7:
57+
case 10:
58+
return new PicnicEngine(param, L1Constants.Instance);
59+
case 3:
60+
case 4:
61+
case 8:
62+
case 11:
63+
return new PicnicEngine(param, L3Constants.Instance);
64+
case 12:
65+
case 5:
66+
case 6:
67+
case 9:
68+
return new PicnicEngine(param, L5Constants.Instance);
69+
default: return null;
70+
}
4071
}
4172
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected void computeAuxTape(byte[] inputs)
5454
// {System.out.printf("%08x ", key0[i]);}System.out.println();
5555

5656
// key = key0 x KMatrix[0]^(-1)
57-
KMatricesWithPointer current = LowmcConstants.KMatrixInv(engine);
57+
KMatricesWithPointer current = engine.lowmcConstants.KMatrixInv(engine);
5858
engine.matrix_mul(key, key0, current.getData(), current.getMatrixPointer());
5959

6060
// System.out.print("key: ");
@@ -70,12 +70,12 @@ protected void computeAuxTape(byte[] inputs)
7070

7171
for (int r = engine.numRounds; r > 0; r--)
7272
{
73-
current = LowmcConstants.KMatrix(engine, r);
73+
current = engine.lowmcConstants.KMatrix(engine, r);
7474
engine.matrix_mul(roundKey, key, current.getData(), current.getMatrixPointer()); // roundKey = key * KMatrix(r)
7575

7676
engine.xor_array(x, x, roundKey, 0, engine.stateSizeWords);
7777

78-
current = LowmcConstants.LMatrixInv(engine, r-1);
78+
current = engine.lowmcConstants.LMatrixInv(engine, r-1);
7979
engine.matrix_mul(y, x, current.getData(), current.getMatrixPointer());
8080

8181
if(r == 1)

core/src/main/resources/org/bouncycastle/pqc/crypto/picnic/lowmc.properties

Lines changed: 0 additions & 30 deletions
This file was deleted.

core/src/main/resources/org/bouncycastle/pqc/crypto/picnic/lowmcL1.properties

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

core/src/main/resources/org/bouncycastle/pqc/crypto/picnic/lowmcL3.properties

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)