Skip to content

Commit 26783d5

Browse files
author
gefeili
committed
Add more constant values.
1 parent 1bb0a76 commit 26783d5

File tree

6 files changed

+121
-115
lines changed

6 files changed

+121
-115
lines changed

core/src/main/java/org/bouncycastle/pqc/crypto/snova/MapGroup1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public MapGroup1(SnovaParameters params)
1818
int v = params.getV();
1919
int o = params.getO();
2020
int alpha = params.getAlpha();
21-
int lsq = params.getL() * params.getL();
21+
int lsq = params.getLsq();
2222
p11 = new byte[m][v][v][lsq];
2323
p12 = new byte[m][v][o][lsq];
2424
p21 = new byte[m][o][v][lsq];

core/src/main/java/org/bouncycastle/pqc/crypto/snova/SnovaEngine.java

Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,42 @@ class SnovaEngine
1515
{
1616
private final SnovaParameters params;
1717
private final int l;
18+
private final int lsq;
19+
private final int m;
20+
private final int v;
21+
private final int o;
22+
private final int alpha;
23+
private final int n;
1824
final byte[][] S;
1925
final int[][] xS;
2026

2127
public SnovaEngine(SnovaParameters params)
2228
{
2329
this.params = params;
2430
this.l = params.getL();
25-
int lsq = l * l;
26-
S = new byte[l][lsq];
27-
xS = new int[l][lsq];
28-
be_aI(S[0], 0, (byte)1);
29-
beTheS(S[1]);
30-
for (int index = 2; index < l; ++index)
31-
{
32-
GF16Utils.gf16mMul(S[index - 1], S[1], S[index], l);
33-
}
34-
35-
for (int index = 0; index < l; ++index)
36-
{
37-
for (int ij = 0; ij < lsq; ++ij)
38-
{
39-
xS[index][ij] = GF16Utils.gf16FromNibble(S[index][ij]);
40-
}
41-
}
31+
this.lsq = params.getLsq();
32+
this.m = params.getM();
33+
this.v = params.getV();
34+
this.o = params.getO();
35+
this.alpha = params.getAlpha();
36+
this.n = params.getN();
37+
S = SnovaParameters.sSet.get(l);
38+
xS = SnovaParameters.xSSet.get(l);
4239
}
4340

44-
public void be_aI(byte[] target, int off, byte a)
41+
static void be_aI(byte[] target, int off, byte a, int l)
4542
{
46-
// // Mask 'a' to ensure it's a valid 4-bit GF16 element
47-
// a = (byte)(a & 0x0F);
43+
// Ensure 'a' iss a valid 4-bit GF16 element
4844
int l1 = l + 1;
4945
for (int i = 0; i < l; ++i, off += l1)
5046
{
5147
target[off] = a;
5248
}
5349
}
5450

55-
private void beTheS(byte[] target)
56-
{
57-
// Set all elements to 8 - (i + j) in GF16 (4-bit values)
58-
for (int i = 0, il = 0; i < l; ++i, il += l)
59-
{
60-
for (int j = 0; j < l; ++j)
61-
{
62-
int value = 8 - (i + j);
63-
target[il + j] = (byte)(value & 0x0F); // Mask to 4 bits
64-
}
65-
}
66-
67-
// Special case for rank 5
68-
if (l == 5)
69-
{
70-
target[24] = (byte)9; // Set (4,4) to 9
71-
}
72-
}
73-
7451
// Constant-time GF16 matrix generation
7552
public void genAFqSCT(byte[] c, int cOff, byte[] ptMatrix)
7653
{
77-
int lsq = l * l;
7854
int[] xTemp = new int[lsq];
7955
int l1 = l + 1;
8056
// Initialize diagonal with c[0]
@@ -102,11 +78,6 @@ public void genAFqSCT(byte[] c, int cOff, byte[] ptMatrix)
10278
for (int ij = 0; ij < lsq; ij++)
10379
{
10480
xTemp[ij] ^= cX * xS[l - 1][ij];
105-
}
106-
107-
// Convert to nibbles and clear temp
108-
for (int ij = 0; ij < lsq; ij++)
109-
{
11081
ptMatrix[ij] = GF16Utils.gf16ToNibble(xTemp[ij]);
11182
}
11283
Arrays.fill(xTemp, 0); // Secure clear
@@ -298,7 +269,7 @@ private byte determinant5x5(byte[] m, int off)
298269

299270
private void generateASMatrixTo(byte[] target, int off, byte a)
300271
{
301-
for (int i = 0; i < l; i++)
272+
for (int i = 0, ixl = off; i < l; i++, ixl += l)
302273
{
303274
for (int j = 0; j < l; j++)
304275
{
@@ -307,15 +278,15 @@ private void generateASMatrixTo(byte[] target, int off, byte a)
307278
{
308279
coefficient = 9;
309280
}
310-
target[i * l + j + off] ^= GF16.mul(coefficient, a);
281+
target[ixl + j] ^= GF16.mul(coefficient, a);
311282
}
312283
}
313284
}
314285

315286
public void genAFqS(byte[] c, int cOff, byte[] ptMatrix, int off)
316287
{
317288
// Initialize with be_aI
318-
be_aI(ptMatrix, off, c[cOff]);
289+
be_aI(ptMatrix, off, c[cOff], l);
319290

320291
// Process middle terms
321292
for (int i = 1; i < l - 1; ++i)
@@ -341,12 +312,6 @@ private void gf16mScaleTo(byte[] a, byte k, byte[] c, int cOff)
341312

342313
public void genF(MapGroup2 map2, MapGroup1 map1, byte[][][] T12)
343314
{
344-
int m = params.getM();
345-
int v = params.getV();
346-
int o = params.getO();
347-
int l = params.getL();
348-
int lsq = l * l;
349-
350315
// Copy initial matrices
351316
copy4DMatrix(map1.p11, map2.f11, m, v, v, lsq);
352317
copy4DMatrix(map1.p12, map2.f12, m, v, o, lsq);
@@ -386,12 +351,6 @@ private static void copy4DMatrix(byte[][][][] src, byte[][][][] dest, int dim1,
386351

387352
public void genP22(byte[] outP22, byte[][][] T12, byte[][][][] P21, byte[][][][] F12)
388353
{
389-
int m = params.getM();
390-
int o = params.getO();
391-
int v = params.getV();
392-
int l = params.getL();
393-
int lsq = l * l;
394-
395354
// Initialize P22 with zeros
396355
byte[] P22 = new byte[m * o * o * lsq];
397356

@@ -420,8 +379,8 @@ public void genP22(byte[] outP22, byte[][][] T12, byte[][][][] P21, byte[][][][]
420379

421380
void genSeedsAndT12(byte[][][] T12, byte[] skSeed)
422381
{
423-
int bytesPrngPrivate = (params.getV() * params.getO() * params.getL() + 1) >>> 1;
424-
int gf16sPrngPrivate = params.getV() * params.getO() * params.getL();
382+
int bytesPrngPrivate = (v * o * l + 1) >>> 1;
383+
int gf16sPrngPrivate = v * o * l;
425384
byte[] prngOutput = new byte[bytesPrngPrivate];
426385

427386
// Generate PRNG output using SHAKE-256
@@ -435,10 +394,9 @@ void genSeedsAndT12(byte[][][] T12, byte[] skSeed)
435394

436395
// Generate T12 matrices
437396
int ptArray = 0;
438-
int l = params.getL();
439-
for (int j = 0; j < params.getV(); j++)
397+
for (int j = 0; j < v; j++)
440398
{
441-
for (int k = 0; k < params.getO(); k++)
399+
for (int k = 0; k < o; k++)
442400
{
443401
//gen_a_FqS_ct
444402
genAFqSCT(gf16PrngOutput, ptArray, T12[j][k]);
@@ -449,14 +407,6 @@ void genSeedsAndT12(byte[][][] T12, byte[] skSeed)
449407

450408
void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
451409
{
452-
int l = params.getL();
453-
int lsq = l * l;
454-
int m = params.getM();
455-
int alpha = params.getAlpha();
456-
int v = params.getV();
457-
int o = params.getO();
458-
int n = v + o;
459-
460410
int gf16sPrngPublic = lsq * (2 * m * alpha + m * (n * n - m * m)) + l * 2 * m * alpha;
461411
byte[] qTemp = new byte[(m * alpha * lsq + m * alpha * lsq) / l];
462412
byte[] prngOutput = new byte[(gf16sPrngPublic + 1) >> 1];
@@ -500,7 +450,6 @@ void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
500450
ctrCipher.init(true, params);
501451
int blockSize = ctrCipher.getBlockSize(); // typically 16 bytes
502452
byte[] zeroBlock = new byte[blockSize]; // block of zeros
503-
byte[] blockOut = new byte[blockSize];
504453

505454
int offset = 0;
506455
// Process full blocks
@@ -512,21 +461,21 @@ void genABQP(MapGroup1 map1, byte[] pkSeed, byte[] fixedAbq)
512461
// Process any remaining partial block.
513462
if (offset < prngOutput.length)
514463
{
515-
ctrCipher.processBlock(zeroBlock, 0, blockOut, 0);
464+
ctrCipher.processBlock(zeroBlock, 0, zeroBlock, 0);
516465
int remaining = prngOutput.length - offset;
517-
System.arraycopy(blockOut, 0, prngOutput, offset, remaining);
466+
System.arraycopy(zeroBlock, 0, prngOutput, offset, remaining);
518467
}
519468
}
520-
// if ((lsq & 1) == 0)
521-
// {
522-
// map1.decode(prngOutput, params, (gf16sPrngPublic - qTemp.length) >> 1);
523-
// }
524-
// else
525-
// {
526-
byte[] temp = new byte[gf16sPrngPublic - qTemp.length];
527-
GF16.decode(prngOutput, temp, temp.length);
528-
map1.fill(temp);
529-
// }
469+
if ((lsq & 1) == 0)
470+
{
471+
map1.decode(prngOutput, params, (gf16sPrngPublic - qTemp.length) >> 1);
472+
}
473+
else
474+
{
475+
byte[] temp = new byte[gf16sPrngPublic - qTemp.length];
476+
GF16.decode(prngOutput, temp, temp.length);
477+
map1.fill(temp);
478+
}
530479
if (l >= 4)
531480
{
532481
GF16.decode(prngOutput, (gf16sPrngPublic - qTemp.length) >> 1, qTemp, 0, qTemp.length);

core/src/main/java/org/bouncycastle/pqc/crypto/snova/SnovaKeyElements.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ class SnovaKeyElements
1313
private final int length;
1414
byte[] fixedAbq;
1515

16-
public SnovaKeyElements(SnovaParameters params, SnovaEngine engine)
16+
public SnovaKeyElements(SnovaParameters params)
1717
{
1818
int o = params.getO();
1919
int l = params.getL();
2020
int v = params.getV();
21-
int alpha = params.getAlpha();
2221
int lsq = l * l;
2322
map1 = new MapGroup1(params);
2423
T12 = new byte[v][o][lsq];
@@ -27,27 +26,7 @@ public SnovaKeyElements(SnovaParameters params, SnovaEngine engine)
2726
length = o * params.getAlpha() * lsq * 4 + v * o * lsq + (o * v * v + o * v * o + o * o * v) * lsq;
2827
if (l < 4)
2928
{
30-
fixedAbq = new byte[4 * o * alpha * lsq];
31-
//genABQ(byte[] abqSeed)
32-
byte[] rngOut = new byte[o * alpha * (lsq + l)];
33-
byte[] q12 = new byte[2 * o * alpha * l];
34-
byte[] seed = "SNOVA_ABQ".getBytes();
35-
SHAKEDigest shake = new SHAKEDigest(256);
36-
shake.update(seed, 0, seed.length);
37-
shake.doFinal(rngOut, 0, rngOut.length);
38-
GF16.decode(rngOut, fixedAbq, 2 * o * alpha * lsq);
39-
GF16.decode(rngOut, alpha * lsq, q12, 0, 2 * o * alpha * l);
40-
// Post-processing for invertible matrices
41-
for (int pi = 0; pi < o; ++pi)
42-
{
43-
for (int a = 0; a < alpha; ++a)
44-
{
45-
engine.makeInvertibleByAddingAS(fixedAbq, (pi * alpha + a) * lsq);
46-
engine.makeInvertibleByAddingAS(fixedAbq, ((o + pi) * alpha + a) * lsq);
47-
engine.genAFqS(q12, (pi * alpha + a) * l, fixedAbq, ((2 * o + pi) * alpha + a) * lsq);
48-
engine.genAFqS(q12, ((o + pi) * alpha + a) * l, fixedAbq, ((3 * o + pi) * alpha + a) * lsq);
49-
}
50-
}
29+
fixedAbq = SnovaParameters.fixedAbqSet.get(o);
5130
}
5231
}
5332

core/src/main/java/org/bouncycastle/pqc/crypto/snova/SnovaKeyPairGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public AsymmetricCipherKeyPair generateKeyPair()
4646
byte[] ptPublicKeySeed = Arrays.copyOfRange(seedPair, 0, publicSeedLength);
4747
byte[] ptPrivateKeySeed = Arrays.copyOfRange(seedPair, publicSeedLength, seedPair.length);
4848

49-
SnovaKeyElements keyElements = new SnovaKeyElements(params, engine);
49+
SnovaKeyElements keyElements = new SnovaKeyElements(params);
5050
generateKeysCore(keyElements, ptPublicKeySeed, ptPrivateKeySeed);
5151

5252
// Pack public key components

0 commit comments

Comments
 (0)