Skip to content

Commit e23335e

Browse files
author
gefeili
committed
Refactor on Snova is almost done
1 parent 9dda406 commit e23335e

File tree

13 files changed

+543
-315
lines changed

13 files changed

+543
-315
lines changed

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

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,55 @@ public static void decodeMergeInHalf(byte[] byteArray, byte[] gf16Array, int nGf
3030
}
3131
}
3232

33-
public static void gf16mTranMul(byte[] a, byte[] b, byte[] c, int rank)
33+
public static void gf16mTranMulMul(byte[] sign, byte[] a, byte[] b, byte[] q1, byte[] q2, byte[] tmp,
34+
byte[] left, byte[] right, int rank)
3435
{
35-
for (int i = 0, cOff = 0; i < rank; i++)
36+
for (int i = 0, leftOff = 0, dOff = 0; i < rank; i++, leftOff += rank)
3637
{
37-
for (int j = 0, jl = 0; j < rank; j++, jl += rank)
38+
for (int j = 0; j < rank; j++)
39+
{
40+
byte result = 0;
41+
for (int k = 0, aOff = j, bOff = i; k < rank; ++k, aOff += rank, bOff += rank)
42+
{
43+
result ^= GF16.mul(sign[aOff], q1[bOff]);
44+
}
45+
tmp[j] = result;
46+
}
47+
48+
for (int j = 0, jxl = 0; j < rank; j++, jxl += rank)
49+
{
50+
byte result = 0;
51+
for (int k = 0; k < rank; ++k)
52+
{
53+
result ^= GF16.mul(a[jxl + k], tmp[k]);
54+
}
55+
left[i + jxl] = result;
56+
}
57+
for (int j = 0; j < rank; j++)
58+
{
59+
tmp[j] = GF16.innerProduct(q2, leftOff, sign, j, rank);
60+
}
61+
62+
for (int j = 0; j < rank; j++)
3863
{
39-
c[cOff++] = GF16.dotProduct(a, i, b, j, rank);
64+
right[dOff++] = GF16.innerProduct(tmp, 0, b, j, rank);
65+
}
66+
}
67+
}
68+
69+
// tmp = a * b, d = tmp * c -> d = (a * b) * c
70+
public static void gf16mMulMul(byte[] a, byte[] b, byte[] c, byte[] tmp, byte[] d, int rank)
71+
{
72+
for (int i = 0, leftOff = 0, dOff = 0; i < rank; i++, leftOff += rank)
73+
{
74+
for (int j = 0; j < rank; j++)
75+
{
76+
tmp[j] = GF16.innerProduct(a, leftOff, b, j, rank);
77+
}
78+
79+
for (int j = 0; j < rank; j++)
80+
{
81+
d[dOff++] = GF16.innerProduct(tmp, 0, c, j, rank);
4082
}
4183
}
4284
}
@@ -52,6 +94,22 @@ public static void gf16mMul(byte[] a, byte[] b, byte[] c, int rank)
5294
}
5395
}
5496

97+
public static void gf16mMulMulTo(byte[] a, byte[] b, byte[] c, byte[] tmp, byte[] d, int rank)
98+
{
99+
for (int i = 0, leftOff = 0, dOff = 0; i < rank; i++, leftOff += rank)
100+
{
101+
for (int j = 0; j < rank; j++)
102+
{
103+
tmp[j] = GF16.innerProduct(a, leftOff, b, j, rank);
104+
}
105+
106+
for (int j = 0; j < rank; j++)
107+
{
108+
d[dOff++] ^= GF16.innerProduct(tmp, 0, c, j, rank);
109+
}
110+
}
111+
}
112+
55113
public static void gf16mMulTo(byte[] a, byte[] b, byte[] c, int rank)
56114
{
57115
for (int i = 0, aOff = 0, cOff = 0; i < rank; i++, aOff += rank)
@@ -63,6 +121,42 @@ public static void gf16mMulTo(byte[] a, byte[] b, byte[] c, int rank)
63121
}
64122
}
65123

124+
// d = a * b, e = b * c
125+
public static void gf16mMulToTo(byte[] a, byte[] b, byte[] c, byte[] d, byte[] e, int rank)
126+
{
127+
for (int i = 0, leftOff = 0, outOff = 0; i < rank; i++, leftOff += rank)
128+
{
129+
for (int j = 0; j < rank; j++)
130+
{
131+
d[outOff] ^= GF16.innerProduct(a, leftOff, b, j, rank);
132+
e[outOff++] ^= GF16.innerProduct(b, leftOff, c, j, rank);
133+
}
134+
}
135+
}
136+
137+
public static void gf16mMulTo(byte[] a, byte[] b, byte[] c, int cOff, int rank)
138+
{
139+
for (int i = 0, aOff = 0; i < rank; i++, aOff += rank)
140+
{
141+
for (int j = 0; j < rank; j++)
142+
{
143+
c[cOff++] ^= GF16.innerProduct(a, aOff, b, j, rank);
144+
}
145+
}
146+
}
147+
148+
// d ^= a * b + c * d
149+
public static void gf16mMulTo(byte[] a, byte[] b, byte[] c, byte[] d, byte[] e, int eOff, int rank)
150+
{
151+
for (int i = 0, leftOff = 0; i < rank; i++, leftOff += rank)
152+
{
153+
for (int j = 0; j < rank; j++)
154+
{
155+
e[eOff++] ^= GF16.innerProduct(a, leftOff, b, j, rank) ^ GF16.innerProduct(c, leftOff, d, j, rank);
156+
}
157+
}
158+
}
159+
66160
public static void gf16mMulTo(byte[] a, byte[] b, int bOff, byte[] c, int cOff, int rank)
67161
{
68162
for (int i = 0, aOff = 0; i < rank; i++, aOff += rank)

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

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,9 @@ public MapGroup1(SnovaParameters params)
2828
qAlpha2 = new byte[m][alpha][lsq];
2929
}
3030

31-
public void decode(byte[] input, int len, boolean isl4or5)
31+
void decode(byte[] input, int len, boolean isl4or5)
3232
{
33-
// int m = params.getM();
34-
// int v = params.getV();
35-
// int o = params.getO();
36-
// int alpha = params.getAlpha();
37-
// int lsq = params.getLsq();
38-
// if ((lsq & 1) == 0)
39-
// {
40-
33+
//TODO: when (lsq & 1) == 1
4134
int inOff = decodeP(input, 0, p11, len);
4235
inOff += decodeP(input, inOff, p12, len - inOff);
4336
inOff += decodeP(input, inOff, p21, len - inOff);
@@ -48,38 +41,8 @@ public void decode(byte[] input, int len, boolean isl4or5)
4841
inOff += decodeAlpha(input, inOff, qAlpha1, len - inOff);
4942
decodeAlpha(input, inOff, qAlpha2, len - inOff);
5043
}
51-
// }
52-
// else
53-
// {
54-
//
55-
// }
5644
}
5745

58-
// public boolean decodeArrayLsqOdd(byte[] input, int inOff, boolean isLower, byte[] output, int lsqHalf)
59-
// {
60-
// int outOff = 0;
61-
// if (isLower)
62-
// {
63-
// for (int i = 0; i < lsqHalf; ++i)
64-
// {
65-
// output[outOff++] = (byte)(input[inOff] & 0x0F);
66-
// output[outOff++] = (byte)((input[inOff++] >>> 4) & 0x0F);
67-
// }
68-
// output[outOff] = (byte)(input[inOff] & 0x0F);
69-
// return false;
70-
// }
71-
// else
72-
// {
73-
// for (int i = 0; i < lsqHalf; ++i)
74-
// {
75-
// output[outOff++] = (byte)((input[inOff++] >>> 4) & 0x0F);
76-
// output[outOff++] = (byte)(input[inOff] & 0x0F);
77-
// }
78-
// output[outOff] = (byte)((input[inOff] >>> 4) & 0x0F);
79-
// return true;
80-
// }
81-
// }
82-
8346
static int decodeP(byte[] input, int inOff, byte[][][][] p, int len)
8447
{
8548
int rlt = 0;
@@ -114,29 +77,7 @@ static int decodeArray(byte[] input, int inOff, byte[][] array, int len)
11477
return rlt;
11578
}
11679

117-
// private int decodeP(byte[] input, int inOff, boolean isLower,byte[][][][] p, int lsqHalf)
118-
// {
119-
// for (int i = 0; i < p.length; ++i)
120-
// {
121-
// inOff = decodeAlpha(input, inOff, p[i]);
122-
// }
123-
// return inOff;
124-
// }
125-
126-
// private boolean decodeAlpha(byte[] input, int inOff, boolean isLower, byte[][][] alpha, int lsqHalf)
127-
// {
128-
// for (int i = 0; i < alpha.length; ++i)
129-
// {
130-
// for (int j = 0; j < alpha[i].length; ++j)
131-
// {
132-
// isLower = decodeArrayLsqOdd(input, inOff, isLower, alpha[i][j], lsqHalf);
133-
// inOff += lsqHalf + (isLower ? 1 : 0);
134-
// }
135-
// }
136-
// return isLower;
137-
// }
138-
139-
public void fill(byte[] input, boolean isl4or5)
80+
void fill(byte[] input, boolean isl4or5)
14081
{
14182
int inOff = fillP(input, 0, p11, input.length);
14283
inOff += fillP(input, inOff, p12, input.length - inOff);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public MapGroup2(SnovaParameters params)
1111
int m = params.getM();
1212
int v = params.getV();
1313
int o = params.getO();
14-
int lsq = params.getL() * params.getL();
14+
int lsq = params.getLsq();
1515
f11 = new byte[m][v][v][lsq];
1616
f12 = new byte[m][v][o][lsq];
1717
f21 = new byte[m][o][v][lsq];

0 commit comments

Comments
 (0)