Skip to content

Commit d54df59

Browse files
author
gefeili
committed
Add java doc for the two main classes of Mayo
1 parent e78fa25 commit d54df59

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
import org.bouncycastle.util.Arrays;
1010
import org.bouncycastle.util.Longs;
1111

12+
/**
13+
* Implementation of the MAYO asymmetric key pair generator following the MAYO signature scheme specifications.
14+
* <p>
15+
* This generator produces {@link MayoPublicKeyParameters} and {@link MayoPrivateKeyParameters} based on the
16+
* MAYO algorithm parameters. The implementation follows the specification defined in the official MAYO
17+
* documentation and reference implementation.
18+
* </p>
19+
*
20+
* <p>References:</p>
21+
* <ul>
22+
* <li><a href="https://pqmayo.org/">MAYO Official Website</a></li>
23+
* <li><a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Specification Document</a></li>
24+
* <li><a href="https://github.com/PQCMayo/MAYO-C">MAYO Reference Implementation (C)</a></li>
25+
* </ul>
26+
*
27+
*/
1228
public class MayoKeyPairGenerator
1329
implements AsymmetricCipherKeyPairGenerator
1430
{
@@ -21,6 +37,23 @@ public void init(KeyGenerationParameters param)
2137
this.random = param.getRandom();
2238
}
2339

40+
/**
41+
* Generates a new asymmetric key pair following the MAYO algorithm specifications.
42+
* <p>
43+
* The key generation process follows these steps:
44+
* </p>
45+
* <ol>
46+
* <li>Initializes parameter dimensions from {@link MayoParameters}</li>
47+
* <li>Generates secret key seed using a secure random generator</li>
48+
* <li>Derives public key seed using SHAKE-256</li>
49+
* <li>Expands matrix parameters P1 and P2</li>
50+
* <li>Performs GF(16) matrix operations for key material generation</li>
51+
* <li>Assembles and packages the public key components</li>
52+
* <li>Securely clears temporary buffers containing sensitive data</li>
53+
* </ol>
54+
*
55+
* @return A valid MAYO key pair containing public and private key parameters
56+
*/
2457
@Override
2558
public AsymmetricCipherKeyPair generateKeyPair()
2659
{

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.security.SecureRandom;
44

55
import org.bouncycastle.crypto.CipherParameters;
6+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
67
import org.bouncycastle.crypto.digests.SHAKEDigest;
78
import org.bouncycastle.crypto.params.ParametersWithRandom;
89
import org.bouncycastle.pqc.crypto.MessageSigner;
@@ -12,6 +13,21 @@
1213
import org.bouncycastle.util.Longs;
1314
import org.bouncycastle.util.Pack;
1415

16+
/**
17+
* Implementation of the MAYO digital signature scheme as specified in the MAYO documentation.
18+
* This class provides functionality for both signature generation and verification.
19+
*
20+
* <p>MAYO is a candidate in the <b>NIST Post-Quantum Cryptography: Additional Digital Signature Schemes</b> project,
21+
* currently in Round 2 of evaluations. For more details about the NIST standardization process, see:
22+
* <a href="https://csrc.nist.gov/Projects/pqc-dig-sig">NIST PQC Additional Digital Signatures</a>.</p>
23+
*
24+
* <p>References:</p>
25+
* <ul>
26+
* <li><a href="https://pqmayo.org/">MAYO Official Website</a></li>
27+
* <li><a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Specification Document</a></li>
28+
* <li><a href="https://github.com/PQCMayo/MAYO-C">MAYO Reference Implementation (C)</a></li>
29+
* </ul>
30+
*/
1531
public class MayoSigner
1632
implements MessageSigner
1733
{
@@ -20,6 +36,17 @@ public class MayoSigner
2036
private MayoPublicKeyParameters pubKey;
2137
private MayoPrivateKeyParameters privKey;
2238

39+
/**
40+
* Initializes the signer for either signature generation or verification.
41+
*
42+
* @param forSigning {@code true} for signing mode, {@code false} for verification
43+
* @param param CipherParameters containing:
44+
* <ul>
45+
* <li>{@link ParametersWithRandom} with {@link MayoPrivateKeyParameters} (for signing)</li>
46+
* <li>{@link MayoPublicKeyParameters} (for verification)</li>
47+
* </ul>
48+
* @throws IllegalArgumentException if invalid parameters are provided
49+
*/
2350
@Override
2451
public void init(boolean forSigning, CipherParameters param)
2552
{
@@ -37,7 +64,7 @@ public void init(boolean forSigning, CipherParameters param)
3764
else
3865
{
3966
privKey = (MayoPrivateKeyParameters)param;
40-
random = null;
67+
random = CryptoServicesRegistrar.getSecureRandom();
4168
}
4269
params = privKey.getParameters();
4370
}
@@ -50,6 +77,14 @@ public void init(boolean forSigning, CipherParameters param)
5077
}
5178
}
5279

80+
/**
81+
* Generates a MAYO signature for the given message using the initialized private key.
82+
* Follows the signature generation process outlined in the MAYO specification document.
83+
*
84+
* @param message The message to be signed
85+
* @return The signature bytes concatenated with the original message
86+
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 8 and 10</a>
87+
*/
5388
@Override
5489
public byte[] generateSignature(byte[] message)
5590
{
@@ -192,7 +227,7 @@ public byte[] generateSignature(byte[] message)
192227

193228
Utils.decode(V, k * vbytes, r, ok);
194229

195-
if (sampleSolution(params, A, y, r, x))
230+
if (sampleSolution(A, y, r, x))
196231
{
197232
break;
198233
}
@@ -235,6 +270,15 @@ public byte[] generateSignature(byte[] message)
235270
}
236271
}
237272

273+
/**
274+
* Verifies a MAYO signature against the initialized public key and message.
275+
* Implements the verification process specified in the MAYO documentation.
276+
*
277+
* @param message The original message
278+
* @param signature The signature to verify
279+
* @return {@code true} if the signature is valid, {@code false} otherwise
280+
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 9 and 11</a>
281+
*/
238282
@Override
239283
public boolean verifySignature(byte[] message, byte[] signature)
240284
{
@@ -554,7 +598,17 @@ private static void transpose16x16Nibbles(long[] M, int offset)
554598
}
555599
}
556600

557-
boolean sampleSolution(MayoParameters params, byte[] A, byte[] y, byte[] r, byte[] x)
601+
/**
602+
* Samples a solution for the MAYO signature equation using the provided parameters.
603+
*
604+
* @param A Coefficient matrix
605+
* @param y Target vector
606+
* @param r Randomness vector
607+
* @param x Output solution vector
608+
* @return {@code true} if a valid solution was found, {@code false} otherwise
609+
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 2</a>
610+
*/
611+
boolean sampleSolution(byte[] A, byte[] y, byte[] r, byte[] x)
558612
{
559613
final int k = params.getK();
560614
final int o = params.getO();
@@ -641,6 +695,7 @@ boolean sampleSolution(MayoParameters params, byte[] A, byte[] y, byte[] r, byte
641695
* @param A the input matrix, stored rowwise; each element is in [0,15]
642696
* @param nrows the number of rows
643697
* @param ncols the number of columns (GF(16) elements per row)
698+
* @see <a href="https://pqmayo.org/assets/specs/mayo.pdf">MAYO Spec Algorithm 1</a>
644699
*/
645700
void ef(byte[] A, int nrows, int ncols)
646701
{

0 commit comments

Comments
 (0)