Skip to content

Commit ad11e9b

Browse files
author
gefeili
committed
Refactor of decode (which can be re-used in Snova).
1 parent d54df59 commit ad11e9b

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public AsymmetricCipherKeyPair generateKeyPair()
9494
// o ← Decode_o(S[ param_pk_seed_bytes : param_pk_seed_bytes + O_bytes ])
9595
// Decode nibbles from S starting at offset param_pk_seed_bytes into O,
9696
// with expected output length = param_v * param_o.
97-
Utils.decode(seed_pk, pkSeedBytes, O, O.length);
97+
Utils.decode(seed_pk, pkSeedBytes, O, 0, O.length);
9898

9999
// Expand P1 and P2 into the array P using seed_pk.
100100
Utils.expandP1P2(p, P, seed_pk);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public byte[] generateSignature(byte[] message)
135135

136136
// Decode the portion of S after the first param_pk_seed_bytes into O.
137137
// (In C, this is: decode(S + param_pk_seed_bytes, O, param_v * param_o))
138-
Utils.decode(seed_pk, pk_seed_bytes, O, v * o);
138+
Utils.decode(seed_pk, pk_seed_bytes, O, 0, v * o);
139139

140140
// Expand P1 and P2 into the long array P using seed_pk.
141141
Utils.expandP1P2(params, P, seed_pk);
@@ -225,7 +225,7 @@ public byte[] generateSignature(byte[] message)
225225
// A[(i + 1) * (ok + 1) - 1] = 0;
226226
// }
227227

228-
Utils.decode(V, k * vbytes, r, ok);
228+
Utils.decode(V, k * vbytes, r, 0, ok);
229229

230230
if (sampleSolution(A, y, r, x))
231231
{
@@ -240,7 +240,7 @@ public byte[] generateSignature(byte[] message)
240240

241241
// Compute final signature components
242242

243-
for (int i = 0, io = 0, in = 0, iv = 0; i < k; i++, io += o, in+= n, iv += v)
243+
for (int i = 0, io = 0, in = 0, iv = 0; i < k; i++, io += o, in += n, iv += v)
244244
{
245245
GF16Utils.matMul(O, x, io, Ox, o, v);
246246
Bytes.xor(v, Vdec, iv, Ox, s, in);
@@ -274,8 +274,8 @@ public byte[] generateSignature(byte[] message)
274274
* Verifies a MAYO signature against the initialized public key and message.
275275
* Implements the verification process specified in the MAYO documentation.
276276
*
277-
* @param message The original message
278-
* @param signature The signature to verify
277+
* @param message The original message
278+
* @param signature The signature to verify
279279
* @return {@code true} if the signature is valid, {@code false} otherwise
280280
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 9 and 11</a>
281281
*/
@@ -601,10 +601,10 @@ private static void transpose16x16Nibbles(long[] M, int offset)
601601
/**
602602
* Samples a solution for the MAYO signature equation using the provided parameters.
603603
*
604-
* @param A Coefficient matrix
605-
* @param y Target vector
606-
* @param r Randomness vector
607-
* @param x Output solution vector
604+
* @param A Coefficient matrix
605+
* @param y Target vector
606+
* @param r Randomness vector
607+
* @param x Output solution vector
608608
* @return {@code true} if a valid solution was found, {@code false} otherwise
609609
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 2</a>
610610
*/

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.bouncycastle.util.Arrays;
1010
import org.bouncycastle.util.Pack;
1111

12-
public class Utils
12+
class Utils
1313
{
1414
/**
1515
* Decodes an encoded byte array.
@@ -34,7 +34,7 @@ public static void decode(byte[] m, byte[] mdec, int mdecLen)
3434
// If there is an extra nibble (odd number of nibbles), decode only the lower nibble
3535
if ((mdecLen & 1) == 1)
3636
{
37-
mdec[decIndex] = (byte)((m[i] & 0xFF) & 0x0F);
37+
mdec[decIndex] = (byte)(m[i] & 0x0F);
3838
}
3939
}
4040

@@ -56,28 +56,6 @@ public static void decode(byte[] m, int mOff, byte[] mdec, int decIndex, int mde
5656
}
5757
}
5858

59-
/**
60-
* Decodes a nibble-packed byte array into an output array.
61-
*
62-
* @param input the input byte array.
63-
* @param inputOffset the offset in input from which to start decoding.
64-
* @param output the output byte array to hold the decoded nibbles.
65-
* @param mdecLen the total number of nibbles to decode.
66-
*/
67-
public static void decode(byte[] input, int inputOffset, byte[] output, int mdecLen)
68-
{
69-
int decIndex = 0, blocks = mdecLen >> 1;
70-
for (int i = 0; i < blocks; i++)
71-
{
72-
output[decIndex++] = (byte)(input[inputOffset] & 0x0F);
73-
output[decIndex++] = (byte)((input[inputOffset++] >> 4) & 0x0F);
74-
}
75-
if ((mdecLen & 1) == 1)
76-
{
77-
output[decIndex] = (byte)(input[inputOffset] & 0x0F);
78-
}
79-
}
80-
8159
/**
8260
* Encodes an array of 4-bit values into a byte array.
8361
* Two 4-bit values are packed into one byte, with the first nibble stored in the lower 4 bits

0 commit comments

Comments
 (0)