Skip to content

Commit 7f0c8f6

Browse files
author
gefeili
committed
TODO: fix the bugs for packing sk of Snova.
1 parent 9a09af9 commit 7f0c8f6

File tree

7 files changed

+163
-193
lines changed

7 files changed

+163
-193
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,46 @@ public static void decode(byte[] m, int mOff, byte[] mdec, int decIndex, int mde
8181
}
8282
}
8383

84+
/**
85+
* Convert two GF16 values to one byte.
86+
*
87+
* @param m the input array of 4-bit values (stored as bytes, only lower 4 bits used)
88+
* @param menc the output byte array that will hold the encoded bytes
89+
* @param mlen the number of nibbles in the input array
90+
*/
91+
public static void encode(byte[] m, byte[] menc, int outOff, int mlen)
92+
{
93+
int i, srcIndex = 0;
94+
// Process pairs of 4-bit values
95+
for (i = 0; i < mlen / 2; i++)
96+
{
97+
int lowerNibble = m[srcIndex] & 0x0F;
98+
int upperNibble = (m[srcIndex + 1] & 0x0F) << 4;
99+
menc[outOff++] = (byte)(lowerNibble | upperNibble);
100+
srcIndex += 2;
101+
}
102+
// If there is an extra nibble (odd number of nibbles), store it directly in lower 4 bits.
103+
if ((mlen & 1) == 1)
104+
{
105+
menc[outOff] = (byte)(m[srcIndex] & 0x0F);
106+
}
107+
}
108+
109+
public static void encodeMergeInHalf(byte[] m, int mlen, byte[] menc)
110+
{
111+
int i, half = (mlen + 1) >>> 1;
112+
// Process pairs of 4-bit values
113+
for (i = 0; i < mlen / 2; i++, half++)
114+
{
115+
menc[i] = (byte)(m[i] | (m[half] << 4));
116+
}
117+
// If there is an extra nibble (odd number of nibbles), store it directly in lower 4 bits.
118+
if ((mlen & 1) == 1)
119+
{
120+
menc[i] = (byte)m[i];
121+
}
122+
}
123+
84124
/**
85125
* Decodes a nibble-packed byte array into an output array.
86126
*

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ public int decode(byte[] input, int len)
3838
return inOff;
3939
}
4040

41-
private int decodeP(byte[] input, int inOff, byte[][][][] p, int len)
41+
// public int encode(byte[] output, int len)
42+
// {
43+
// int outOff = encodeP(p11, output, 0, len);
44+
// outOff += encodeP(p12, output, outOff, len - outOff);
45+
// outOff += encodeP(p21, output, outOff, len - outOff);
46+
// outOff += encodeAlpha(aAlpha, output, outOff, len - outOff);
47+
// outOff += encodeAlpha(bAlpha, output, outOff, len - outOff);
48+
// outOff += encodeAlpha(qAlpha1, output, outOff, len - outOff);
49+
// outOff += encodeAlpha(qAlpha2, output, outOff, len - outOff);
50+
// return outOff;
51+
// }
52+
53+
static int decodeP(byte[] input, int inOff, byte[][][][] p, int len)
4254
{
4355
int rlt = 0;
4456
for (int i = 0; i < p.length; ++i)
@@ -48,7 +60,7 @@ private int decodeP(byte[] input, int inOff, byte[][][][] p, int len)
4860
return rlt;
4961
}
5062

51-
private int decodeAlpha(byte[] input, int inOff, byte[][][] alpha, int len)
63+
private static int decodeAlpha(byte[] input, int inOff, byte[][][] alpha, int len)
5264
{
5365
int rlt = 0;
5466
for (int i = 0; i < alpha.length; ++i)
@@ -64,4 +76,30 @@ private int decodeAlpha(byte[] input, int inOff, byte[][][] alpha, int len)
6476
return rlt;
6577
}
6678

79+
static int encodeP(byte[][][][] p, byte[] output, int outOff, int len)
80+
{
81+
int rlt = 0;
82+
for (int i = 0; i < p.length; ++i)
83+
{
84+
rlt += encodeAlpha(p[i], output, outOff + rlt, len);
85+
}
86+
return rlt;
87+
}
88+
89+
static int encodeAlpha(byte[][][] alpha, byte[] output, int outOff, int len)
90+
{
91+
int rlt = 0;
92+
for (int i = 0; i < alpha.length; ++i)
93+
{
94+
for (int j = 0; j < alpha[i].length; ++j)
95+
{
96+
int tmp = Math.min(alpha[i][j].length, len << 1);
97+
GF16Utils.encode(alpha[i][j], output, outOff + rlt, tmp);
98+
rlt += (tmp + 1) >> 1;
99+
len -= (tmp + 1) >> 1;
100+
}
101+
}
102+
return rlt;
103+
}
104+
67105
}

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

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

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ public void genF(MapGroup2 map2, MapGroup1 map1, byte[][][] T12)
356356
{
357357
for (int index = 0; index < v; index++)
358358
{
359-
GF16Utils.gf16mMul(temp, map1.p11[i][j][index], T12[index][k], l);
360-
GF16Utils.gf16mAdd(map2.f12[i][j][k], map2.f12[i][j][k], temp, l);
359+
GF16Utils.gf16mMul(map1.p11[i][j][index], T12[index][k], temp, l);
360+
GF16Utils.gf16mAdd(map2.f12[i][j][k], temp, map2.f12[i][j][k], l);
361361
}
362362
}
363363
}
@@ -372,8 +372,8 @@ public void genF(MapGroup2 map2, MapGroup1 map1, byte[][][] T12)
372372
{
373373
for (int index = 0; index < v; index++)
374374
{
375-
GF16Utils.gf16mMul(temp, T12[index][j], map1.p11[i][index][k], l);
376-
GF16Utils.gf16mAdd(map2.f21[i][j][k], map2.f21[i][j][k], temp, l);
375+
GF16Utils.gf16mMul(T12[index][j], map1.p11[i][index][k], temp, l);
376+
GF16Utils.gf16mAdd(map2.f21[i][j][k], temp, map2.f21[i][j][k], l);
377377
}
378378
}
379379
}
@@ -402,7 +402,7 @@ private static void copy4DMatrix(byte[][][][] src, byte[][][][] dest,
402402
}
403403
}
404404

405-
public void genP22(byte[] outP22, byte[][][] T12, byte[][][][] P21, byte[][][][] F12, SnovaParameters params)
405+
public void genP22(byte[] outP22, byte[][][] T12, byte[][][][] P21, byte[][][][] F12)
406406
{
407407
int m = params.getM();
408408
int o = params.getO();
@@ -428,24 +428,23 @@ public void genP22(byte[] outP22, byte[][][] T12, byte[][][][] P21, byte[][][][]
428428
for (int index = 0; index < v; index++)
429429
{
430430
// temp1 = T12[index][j] * F12[i][index][k]
431-
GF16Utils.gf16mMul(temp1, T12[index][j], F12[i][index][k], l);
431+
GF16Utils.gf16mMul(T12[index][j], F12[i][index][k], temp1, l);
432432

433433
// temp2 = P21[i][j][index] * T12[index][k]
434-
GF16Utils.gf16mMul(temp2, P21[i][j][index], T12[index][k], l);
434+
GF16Utils.gf16mMul(P21[i][j][index], T12[index][k], temp2, l);
435435

436436
// temp1 += temp2
437-
GF16Utils.gf16mAdd(temp1, temp1, temp2, l);
437+
GF16Utils.gf16mAdd(temp1, temp2, temp1, l);
438438

439439
// P22[i][j][k] += temp1
440-
GF16Utils.gf16mAdd(P22[i][j][k], P22[i][j][k], temp1, l);
440+
GF16Utils.gf16mAdd(P22[i][j][k], temp1, P22[i][j][k], l);
441441
}
442442
}
443443
}
444444
}
445445

446446
// Convert GF16 elements to packed bytes
447-
//TODO
448-
//GF16Utils.decode(P22, outP22, m * o * o *lsq);
447+
MapGroup1.encodeP(P22, outP22, 0, m * o * o *lsq);
449448
}
450449
finally
451450
{

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,55 @@ class SnovaKeyElements
66
public final byte[][][] T12; // [v][o]
77
public final MapGroup2 map2;
88
public final PublicKey publicKey;
9+
private int length;
910

1011
public SnovaKeyElements(SnovaParameters params)
1112
{
13+
int o = params.getO();
14+
int l = params.getL();
15+
int v = params.getV();
16+
int lsq = l * l;
1217
map1 = new MapGroup1(params);
13-
T12 = new byte[params.getV()][params.getO()][16];
18+
T12 = new byte[v][o][lsq];
1419
map2 = new MapGroup2(params);
1520
publicKey = new PublicKey(params);
21+
length = o * params.getAlpha() * lsq * 4 + v * o * lsq + (o * v * v + o * v * o + o * o * v) * lsq;
22+
}
23+
24+
public void encodeMergerInHalf(byte[] output)
25+
{
26+
byte[] input = new byte[length];
27+
int inOff = 0;
28+
inOff = copy3d(map1.aAlpha, input, inOff);
29+
inOff = copy3d(map1.bAlpha, input, inOff);
30+
inOff = copy3d(map1.qAlpha1, input, inOff);
31+
inOff = copy3d(map1.qAlpha2, input, inOff);
32+
inOff = copy3d(T12, input, inOff);
33+
inOff = copy4d(map2.f11, input, inOff);
34+
inOff = copy4d(map2.f12, input, inOff);
35+
inOff = copy4d(map2.f21, input, inOff);
36+
GF16Utils.encodeMergeInHalf(input, length, output);
37+
}
38+
39+
public int copy3d(byte[][][] alpha, byte[] output, int outOff)
40+
{
41+
for (int i = 0; i < alpha.length; ++i)
42+
{
43+
for (int j = 0; j < alpha[i].length; ++j)
44+
{
45+
System.arraycopy(alpha[i][j], 0, output, outOff, alpha[i][j].length);
46+
outOff += alpha[i][j].length;
47+
}
48+
}
49+
return outOff;
50+
}
51+
52+
public int copy4d(byte[][][][] alpha, byte[] output, int outOff)
53+
{
54+
for (int i = 0; i < alpha.length; ++i)
55+
{
56+
outOff = copy3d(alpha[i], output, outOff);
57+
}
58+
return outOff;
1659
}
1760
}

0 commit comments

Comments
 (0)