Skip to content

Commit d1b1204

Browse files
author
gefeili
committed
Refactor for Mayo
1 parent 01dbe52 commit d1b1204

File tree

5 files changed

+153
-328
lines changed

5 files changed

+153
-328
lines changed

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

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,6 @@ public static void mulAddMUpperTriangularMatXMat(int mVecLimbs, long[] bsMat, by
101101
}
102102
}
103103

104-
/**
105-
* Computes P1_times_O.
106-
* <p>
107-
* In C:
108-
* P1_times_O(p, P1, O, acc) calls:
109-
* mul_add_m_upper_triangular_mat_x_mat(PARAM_m_vec_limbs(p), P1, O, acc, PARAM_v(p), PARAM_v(p), PARAM_o(p), 1);
110-
*
111-
* @param p the parameter object.
112-
* @param P1 the P1 matrix as a long[] array.
113-
* @param O the O matrix as a byte[] array.
114-
* @param acc the output accumulator (long[] array).
115-
*/
116-
public static void P1TimesO(MayoParameters p, long[] P1, byte[] O, long[] acc)
117-
{
118-
int mVecLimbs = p.getMVecLimbs();
119-
int paramV = p.getV();
120-
int paramO = p.getO();
121-
// Here, bsMatRows and bsMatCols are both paramV, and matCols is paramO, triangular=1.
122-
mulAddMUpperTriangularMatXMat(mVecLimbs, P1, O, acc, paramV, paramV, paramO, 1);
123-
}
124-
125104
/**
126105
* Multiplies the transpose of a single matrix with m matrices and adds the result into acc.
127106
*
@@ -304,10 +283,7 @@ public static long mulFx8(byte a, long b)
304283
int aa = a & 0xFF;
305284
// Carryless multiplication: for each bit in 'aa' (considering only the lower 4 bits),
306285
// if that bit is set, multiply 'b' (by 1, 2, 4, or 8) and XOR the result.
307-
long p = ((aa & 1) * b)
308-
^ ((aa & 2) * b)
309-
^ ((aa & 4) * b)
310-
^ ((aa & 8) * b);
286+
long p = ((aa & 1) * b) ^ ((aa & 2) * b) ^ ((aa & 4) * b) ^ ((aa & 8) * b);
311287

312288
// Reduction mod (x^4 + x + 1): process each byte in parallel.
313289
long topP = p & 0xf0f0f0f0f0f0f0f0L;
@@ -366,28 +342,6 @@ public static void matAdd(byte[] a, int aOff, byte[] b, int bOff, byte[] c, int
366342
}
367343
}
368344

369-
/**
370-
* Returns 0x00 if a equals b, otherwise returns 0xFF.
371-
* This operation is performed in constant time.
372-
*
373-
* @param a an 8-bit value
374-
* @param b an 8-bit value
375-
* @return 0x00 if a == b, 0xFF if a != b
376-
*/
377-
public static byte ctCompare8(byte a, byte b)
378-
{
379-
// Compute the difference between a and b using XOR.
380-
// Masking with 0xFF ensures we work with values in 0..255.
381-
int diff = (a ^ b) & 0xFF;
382-
// Negate the difference.
383-
int negDiff = -diff;
384-
// Right shift by 31 bits (since 8*sizeof(uint32_t)-1 equals 31 for 32-bit integers).
385-
// If diff is 0, then -diff is 0, and shifting yields 0.
386-
// If diff is nonzero, -diff is negative, so the arithmetic shift yields -1 (0xFFFFFFFF),
387-
// which when cast to a byte becomes 0xFF.
388-
return (byte) (negDiff >> 31);
389-
}
390-
391345
public static void efUnpackMVector(int legs, long[] packedRow, int packedRowOff, byte[] out)
392346
{
393347
int outIndex = 0;

core/src/main/java/org/bouncycastle/pqc/crypto/mayo/MayoKeyPairGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public AsymmetricCipherKeyPair generateKeyPair()
7272
System.arraycopy(P, p1Limbs, P2, 0, P2.length);
7373

7474
// Compute P1 * O + P2 and store the result in P2.
75-
GF16Utils.P1TimesO(p, P, O, P2);
75+
// GF16Utils.P1TimesO(p, P, O, P2);
76+
// Here, bsMatRows and bsMatCols are both paramV, and matCols is paramO, triangular=1.
77+
GF16Utils.mulAddMUpperTriangularMatXMat(mVecLimbs, P, O, P2, v, v, o, 1);
7678

7779
// Compute P3 = O^T * (P1*O + P2).
7880
// Here, treat P2 as the bsMat for the multiplication.

core/src/main/java/org/bouncycastle/pqc/crypto/mayo/MayoParameters.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MayoParameters
1818
40, // r_bytes
1919
120159, // P1_bytes
2020
24336, // P2_bytes
21-
1404, // P3_bytes
21+
// P3_bytes
2222
24, // csk_bytes
2323
1420, // cpk_bytes
2424
454, // sig_bytes
@@ -46,7 +46,7 @@ public class MayoParameters
4646
34, // r_bytes
4747
66560, // P1_bytes
4848
34816, // P2_bytes
49-
4896, // P3_bytes
49+
// P3_bytes
5050
24, // csk_bytes
5151
4912, // cpk_bytes
5252
186, // sig_bytes
@@ -74,7 +74,7 @@ public class MayoParameters
7474
55, // r_bytes
7575
317844, // P1_bytes
7676
58320, // P2_bytes
77-
2970, // P3_bytes
77+
// P3_bytes
7878
32, // csk_bytes
7979
2986, // cpk_bytes
8080
681, // sig_bytes
@@ -102,7 +102,7 @@ public class MayoParameters
102102
72, // r_bytes
103103
720863, // P1_bytes
104104
120984, // P2_bytes
105-
5538, // P3_bytes
105+
// P3_bytes
106106
40, // csk_bytes
107107
5554, // cpk_bytes
108108
964, // sig_bytes
@@ -129,7 +129,6 @@ public class MayoParameters
129129
private final int rBytes;
130130
private final int P1Bytes;
131131
private final int P2Bytes;
132-
private final int P3Bytes;
133132
private final int cskBytes;
134133
private final int cpkBytes;
135134
private final int sigBytes;
@@ -141,7 +140,7 @@ public class MayoParameters
141140
private final int skSeedBytes;
142141

143142
private MayoParameters(String name, int n, int m, int mVecLimbs, int o, int v, int ACols, int k, int q,
144-
int mBytes, int OBytes, int vBytes, int rBytes, int P1Bytes, int P2Bytes, int P3Bytes,
143+
int mBytes, int OBytes, int vBytes, int rBytes, int P1Bytes, int P2Bytes,
145144
int cskBytes, int cpkBytes, int sigBytes, int[] fTail, byte[] fTailArr,
146145
int saltBytes, int digestBytes, int pkSeedBytes, int skSeedBytes)
147146
{
@@ -160,7 +159,6 @@ private MayoParameters(String name, int n, int m, int mVecLimbs, int o, int v, i
160159
this.rBytes = rBytes;
161160
this.P1Bytes = P1Bytes;
162161
this.P2Bytes = P2Bytes;
163-
this.P3Bytes = P3Bytes;
164162
this.cskBytes = cskBytes;
165163
this.cpkBytes = cpkBytes;
166164
this.sigBytes = sigBytes;
@@ -315,13 +313,5 @@ public int getP3Limbs()
315313
{
316314
return ((o * (o + 1)) / 2) * mVecLimbs;
317315
}
318-
319-
/**
320-
* Computes: P1_limbs + P2_limbs + P3_limbs
321-
*/
322-
public int getEPKLimbs()
323-
{
324-
return getP1Limbs() + getP2Limbs() + getP3Limbs();
325-
}
326316
}
327317

0 commit comments

Comments
 (0)