Skip to content

Commit cff8aa0

Browse files
author
gefeili
committed
TODO: signDigestCore
1 parent 0e8573c commit cff8aa0

File tree

6 files changed

+410
-197
lines changed

6 files changed

+410
-197
lines changed
Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,105 @@
1-
package org.bouncycastle.pqc.crypto.snova;
2-
3-
class GF16Matrix
4-
{
5-
private final byte[][] data;
6-
private final int rank;
7-
8-
public GF16Matrix(int rank)
9-
{
10-
this.rank = rank;
11-
this.data = new byte[rank][rank];
12-
}
13-
14-
public void set(int x, int y, byte value)
15-
{
16-
data[x][y] = (byte)(value & 0xF);
17-
}
18-
19-
public byte get(int x, int y)
20-
{
21-
return data[x][y];
22-
}
23-
24-
public void add(GF16Matrix other)
25-
{
26-
// for (int i = 0; i < size; i++)
1+
//package org.bouncycastle.pqc.crypto.snova;
2+
//
3+
//class GF16Matrix
4+
//{
5+
// private final byte[][] data;
6+
// private final int rank;
7+
//
8+
// public GF16Matrix(int rank)
9+
// {
10+
// this.rank = rank;
11+
// this.data = new byte[rank][rank];
12+
// }
13+
//
14+
// public void set(int x, int y, byte value)
15+
// {
16+
// data[x][y] = (byte)(value & 0xF);
17+
// }
18+
//
19+
// public byte get(int x, int y)
20+
// {
21+
// return data[x][y];
22+
// }
23+
//
24+
//// public void add(GF16Matrix other)
25+
//// {
26+
////// for (int i = 0; i < size; i++)
27+
////// {
28+
////// for (int j = 0; j < size; j++)
29+
////// {
30+
////// data[i][j] = add(data[i][j], other.data[i][j]);
31+
////// }
32+
////// }
33+
//// }
34+
//
35+
// public void mul(GF16Matrix a, GF16Matrix b)
36+
// {
37+
// byte[][] temp = new byte[rank][rank];
38+
// for (int i = 0; i < rank; i++)
2739
// {
28-
// for (int j = 0; j < size; j++)
40+
// for (int j = 0; j < rank; j++)
2941
// {
30-
// data[i][j] = add(data[i][j], other.data[i][j]);
42+
// byte sum = 0;
43+
//// for (int k = 0; k < size; k++)
44+
//// {
45+
//// sum = add(sum, mul(a.data[i][k], b.data[k][j]));
46+
//// }
47+
// temp[i][j] = sum;
3148
// }
3249
// }
33-
}
34-
35-
public void mul(GF16Matrix a, GF16Matrix b)
36-
{
37-
byte[][] temp = new byte[rank][rank];
38-
for (int i = 0; i < rank; i++)
39-
{
40-
for (int j = 0; j < rank; j++)
41-
{
42-
byte sum = 0;
43-
// for (int k = 0; k < size; k++)
44-
// {
45-
// sum = add(sum, mul(a.data[i][k], b.data[k][j]));
46-
// }
47-
temp[i][j] = sum;
48-
}
49-
}
50-
System.arraycopy(temp, 0, data, 0, temp.length);
51-
}
52-
53-
public void scale(byte scalar)
54-
{
55-
// for (int i = 0; i < size; i++)
50+
// System.arraycopy(temp, 0, data, 0, temp.length);
51+
// }
52+
//
53+
// public void scale(byte scalar)
54+
// {
55+
//// for (int i = 0; i < size; i++)
56+
//// {
57+
//// for (int j = 0; j < size; j++)
58+
//// {
59+
//// data[i][j] = mul(data[i][j], scalar);
60+
//// }
61+
//// }
62+
// }
63+
//
64+
// public void transpose()
65+
// {
66+
// byte[][] temp = new byte[rank][rank];
67+
// for (int i = 0; i < rank; i++)
5668
// {
57-
// for (int j = 0; j < size; j++)
69+
// for (int j = 0; j < rank; j++)
5870
// {
59-
// data[i][j] = mul(data[i][j], scalar);
71+
// temp[j][i] = data[i][j];
6072
// }
6173
// }
62-
}
63-
64-
public void transpose()
65-
{
66-
byte[][] temp = new byte[rank][rank];
67-
for (int i = 0; i < rank; i++)
68-
{
69-
for (int j = 0; j < rank; j++)
70-
{
71-
temp[j][i] = data[i][j];
72-
}
73-
}
74-
System.arraycopy(temp, 0, data, 0, temp.length);
75-
}
76-
77-
public void makeInvertible()
78-
{
79-
// Implementation of be_invertible_by_add_aS
80-
GF16Matrix temp = new GF16Matrix(rank);
81-
if (determinant() == 0)
82-
{
83-
for (byte a = 1; a < 16; a++)
84-
{
85-
temp.scale(a);
86-
add(temp);
87-
if (determinant() != 0)
88-
{
89-
return;
90-
}
91-
}
92-
}
93-
}
94-
95-
private byte determinant()
96-
{
97-
// Simplified determinant calculation for small matrices
98-
// if (rank == 2)
99-
// {
100-
// return add(mul(data[0][0], data[1][1]), mul(data[0][1], data[1][0]));
101-
// }
102-
// Add implementations for larger matrices as needed
103-
throw new UnsupportedOperationException("Determinant for size " + rank + " not implemented");
104-
}
105-
}
74+
// System.arraycopy(temp, 0, data, 0, temp.length);
75+
// }
76+
//
77+
//// public void makeInvertible()
78+
//// {
79+
//// // Implementation of be_invertible_by_add_aS
80+
//// GF16Matrix temp = new GF16Matrix(rank);
81+
//// if (determinant() == 0)
82+
//// {
83+
//// for (byte a = 1; a < 16; a++)
84+
//// {
85+
//// temp.scale(a);
86+
//// add(temp);
87+
//// if (determinant() != 0)
88+
//// {
89+
//// return;
90+
//// }
91+
//// }
92+
//// }
93+
//// }
94+
//
95+
//// private byte determinant()
96+
//// {
97+
//// // Simplified determinant calculation for small matrices
98+
////// if (rank == 2)
99+
////// {
100+
////// return add(mul(data[0][0], data[1][1]), mul(data[0][1], data[1][0]));
101+
////// }
102+
//// // Add implementations for larger matrices as needed
103+
//// throw new UnsupportedOperationException("Determinant for size " + rank + " not implemented");
104+
//// }
105+
//}

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ public static void decode(byte[] input, int inputOffset, byte[] output, int mdec
143143
}
144144
}
145145

146+
public static void decodeMergeInHalf(byte[] byteArray, byte[] gf16Array, int nGf16)
147+
{
148+
int i, half = (nGf16 + 1) >>> 1;
149+
// Process pairs of 4-bit values
150+
for (i = 0; i < half; i++)
151+
{
152+
gf16Array[i] = (byte)(byteArray[i] & 0x0F);
153+
}
154+
// If there is an extra nibble (odd number of nibbles), store it directly in lower 4 bits.
155+
for (i = 0; i < nGf16 >>> 1; i++)
156+
{
157+
gf16Array[i + half] = (byte)((byteArray[i] >>> 4) & 0x0F);
158+
}
159+
}
160+
146161
public static void gf16mMul(byte[] a, byte[] b, byte[] c, int rank)
147162
{
148163

@@ -204,34 +219,34 @@ public static byte inv(byte a)
204219
return INV4B[a & 0xF];
205220
}
206221

207-
static GF16Matrix[][][] create3DArray(int d1, int d2, int d3, int rank)
208-
{
209-
GF16Matrix[][][] arr = new GF16Matrix[d1][d2][d3];
210-
for (int i = 0; i < d1; i++)
211-
{
212-
for (int j = 0; j < d2; j++)
213-
{
214-
for (int k = 0; k < d3; k++)
215-
{
216-
arr[i][j][k] = new GF16Matrix(rank);
217-
}
218-
}
219-
}
220-
return arr;
221-
}
222+
// static GF16Matrix[][][] create3DArray(int d1, int d2, int d3, int rank)
223+
// {
224+
// GF16Matrix[][][] arr = new GF16Matrix[d1][d2][d3];
225+
// for (int i = 0; i < d1; i++)
226+
// {
227+
// for (int j = 0; j < d2; j++)
228+
// {
229+
// for (int k = 0; k < d3; k++)
230+
// {
231+
// arr[i][j][k] = new GF16Matrix(rank);
232+
// }
233+
// }
234+
// }
235+
// return arr;
236+
// }
222237

223-
static GF16Matrix[][] create2DArray(int d1, int d2, int rank)
224-
{
225-
GF16Matrix[][] arr = new GF16Matrix[d1][d2];
226-
for (int i = 0; i < d1; i++)
227-
{
228-
for (int j = 0; j < d2; j++)
229-
{
230-
arr[i][j] = new GF16Matrix(rank);
231-
}
232-
}
233-
return arr;
234-
}
238+
// static GF16Matrix[][] create2DArray(int d1, int d2, int rank)
239+
// {
240+
// GF16Matrix[][] arr = new GF16Matrix[d1][d2];
241+
// for (int i = 0; i < d1; i++)
242+
// {
243+
// for (int j = 0; j < d2; j++)
244+
// {
245+
// arr[i][j] = new GF16Matrix(rank);
246+
// }
247+
// }
248+
// return arr;
249+
// }
235250

236251
private static final int GF16_MASK = 0x249; // Mask for GF(2^4) reduction
237252

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package org.bouncycastle.pqc.crypto.snova;
2-
3-
class PublicKeyExpanded
4-
{
5-
public final byte[] publicKeySeed;
6-
public final GF16Matrix[][][] P22; // [m][o][o]
7-
public final MapGroup1 map1;
8-
9-
public PublicKeyExpanded(SnovaParameters params)
10-
{
11-
int m = params.getM();
12-
int o = params.getO();
13-
int rank = params.getL();
14-
15-
publicKeySeed = new byte[SnovaKeyPairGenerator.publicSeedLength];
16-
P22 = GF16Utils.create3DArray(m, o, o, rank);
17-
map1 = new MapGroup1(params);
18-
}
19-
}
1+
//package org.bouncycastle.pqc.crypto.snova;
2+
//
3+
//class PublicKeyExpanded
4+
//{
5+
// public final byte[] publicKeySeed;
6+
// public final GF16Matrix[][][] P22; // [m][o][o]
7+
// public final MapGroup1 map1;
8+
//
9+
// public PublicKeyExpanded(SnovaParameters params)
10+
// {
11+
// int m = params.getM();
12+
// int o = params.getO();
13+
// int rank = params.getL();
14+
//
15+
// publicKeySeed = new byte[SnovaKeyPairGenerator.publicSeedLength];
16+
// P22 = GF16Utils.create3DArray(m, o, o, rank);
17+
// map1 = new MapGroup1(params);
18+
// }
19+
//}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SnovaKeyElements
88
public final byte[][][] T12; // [v][o]
99
public final MapGroup2 map2;
1010
public final PublicKey publicKey;
11+
public byte[] ptPrivateKeySeed;
1112
private final int length;
1213
byte[] fixedAbq;
1314

@@ -75,6 +76,25 @@ public void encodeMergerInHalf(byte[] output)
7576
GF16Utils.encodeMergeInHalf(input, length, output);
7677
}
7778

79+
public void skUnpack(byte[] input)
80+
{
81+
byte[] tmp = new byte[input.length << 1];
82+
GF16Utils.decodeMergeInHalf(input, tmp, tmp.length);
83+
int inOff = 0;
84+
inOff = copy3d(tmp, inOff, map1.aAlpha);
85+
inOff = copy3d(tmp, inOff, map1.bAlpha);
86+
inOff = copy3d(tmp, inOff, map1.qAlpha1);
87+
inOff = copy3d(tmp, inOff, map1.qAlpha2);
88+
inOff = copy3d(tmp, inOff, T12);
89+
inOff = copy4d(tmp, inOff, map2.f11);
90+
inOff = copy4d(tmp, inOff, map2.f12);
91+
inOff = copy4d(tmp, inOff, map2.f21);
92+
System.arraycopy(tmp, inOff, publicKey.publicKeySeed, 0, publicKey.publicKeySeed.length);
93+
inOff += publicKey.publicKeySeed.length;
94+
ptPrivateKeySeed = new byte[SnovaKeyPairGenerator.privateSeedLength];
95+
System.arraycopy(tmp, inOff, ptPrivateKeySeed, 0, ptPrivateKeySeed.length);
96+
}
97+
7898
public int copy3d(byte[][][] alpha, byte[] output, int outOff)
7999
{
80100
for (int i = 0; i < alpha.length; ++i)
@@ -96,4 +116,26 @@ public int copy4d(byte[][][][] alpha, byte[] output, int outOff)
96116
}
97117
return outOff;
98118
}
119+
120+
public int copy3d(byte[] input, int inOff, byte[][][] alpha)
121+
{
122+
for (int i = 0; i < alpha.length; ++i)
123+
{
124+
for (int j = 0; j < alpha[i].length; ++j)
125+
{
126+
System.arraycopy(input, inOff, alpha[i][j], 0, alpha[i][j].length);
127+
inOff += alpha[i][j].length;
128+
}
129+
}
130+
return inOff;
131+
}
132+
133+
public int copy4d(byte[] input, int inOff, byte[][][][] alpha)
134+
{
135+
for (int i = 0; i < alpha.length; ++i)
136+
{
137+
inOff = copy3d(alpha[i], input, inOff);
138+
}
139+
return inOff;
140+
}
99141
}

0 commit comments

Comments
 (0)