33import java .security .SecureRandom ;
44
55import org .bouncycastle .crypto .CipherParameters ;
6+ import org .bouncycastle .crypto .CryptoServicesRegistrar ;
67import org .bouncycastle .crypto .digests .SHAKEDigest ;
78import org .bouncycastle .crypto .params .ParametersWithRandom ;
89import org .bouncycastle .pqc .crypto .MessageSigner ;
1213import org .bouncycastle .util .Longs ;
1314import 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+ */
1531public 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