PostQuantumCrypto brings the post-quantum standards NIST published in 2024 to MQL4, in plain code with no DLL. RSA and elliptic-curve keys, the kind behind today's HTTPS and most signatures, can be broken by a large enough quantum computer. These replace them: ML-KEM (FIPS 203, known as Kyber) lets two programs agree on a secret key over an open channel, and ML-DSA (FIPS 204, known as Dilithium) signs data so anyone with your public key can check it came from you, unchanged. The SHA-3 and SHAKE hash functions both are built on come with it.
Use it to sign the trade signals one program sends another, so a copier only acts on signals that are really yours; to check a licence or a settings file nobody edited; or to agree on a key with a server and then encrypt with AES.
#include <PostQuantumCrypto\PostQuantumCrypto.mqh> #include <PostQuantumCrypto\PostQuantumCrypto.mqh> void OnStart() { // The signal provider makes a key pair once and publishes the public key. uchar pk[], sk[], seed[]; PqcMlDsaKeyGen(PQC_MLDSA_65, pk, sk, seed); uchar signal[], context[], signature[]; PqcUtf8("{\"symbol\":\"EURUSD\",\"side\":\"buy\",\"lots\":0.10}", signal); PqcUtf8("signals-v1", context); PqcMlDsaSign(PQC_MLDSA_65, sk, signal, context, signature); // 3309 bytes // The copier checks the signal before it trades. int err = PqcMlDsaVerify(PQC_MLDSA_65, pk, signal, context, signature); if (err != PQC_OK) { Print("refused: ", PqcErrorText(err)); // signature does not verify return; } Print("signal is genuine"); }
The attached PostQuantumCryptoExample script runs a self test against values OpenSSL computes for fixed seeds, agrees on a secret with ML-KEM-768, changes one byte of the ciphertext, signs a trade signal with ML-DSA-65, shows it refused when the lot size or the context changes, and times every parameter set:
self test sha3_256=ok mlkem768=ok mldsa65=ok
key exchange set=ML-KEM-768 public_key_bytes=1184 ciphertext_bytes=1088 secret_bytes=32 secrets_match=true
changed ciphertext err=0 secrets_match=false
signed set=ML-DSA-65 public_key_bytes=1952 signature_bytes=3309 signal={"symbol":"EURUSD","side":"buy","lots":0.10}
verified case=same_signal
refused case=lots_changed reason="signature does not verify"
refused case=other_context reason="signature does not verify"
speed set=ML-KEM-768 keygen_us=540 encapsulate_us=604 decapsulate_us=833
speed set=ML-DSA-65 keygen_us=2297 sign_us=6716 verify_us=2216 The picture is a real run on a demo account, with the speed of all six parameter sets in microseconds.
| Call | What it does |
|---|---|
| PqcMlKemKeyGen(set, ek, dk, seed) | Make an ML-KEM key pair: ek is the public key, dk the private one, seed the 64 bytes that recreate both |
| PqcMlKemKeyGenFromSeed(set, seed, ek, dk) | The same key pair from a 64-byte seed you made yourself |
| PqcMlKemEncapsulate(set, ek, ct, secret) | Make a fresh 32-byte secret and the ciphertext that carries it to the owner of ek |
| PqcMlKemDecapsulate(set, dk, ct, secret) | Get the secret back from the ciphertext |
| PqcMlDsaKeyGen(set, pk, sk, seed), PqcMlDsaKeyGenFromSeed(set, seed, pk, sk) | Make an ML-DSA key pair, or recreate it from its 32-byte seed |
| PqcMlDsaSign(set, sk, message, context, signature) | Sign a message, deterministically |
| PqcMlDsaSignWithRandom(set, sk, message, context, rnd, signature) | Sign with 32 random bytes you supply, so the same message signs differently each time |
| PqcMlDsaVerify(set, pk, message, context, signature) | PQC_OK for a valid signature, PQC_ERROR_SIGNATURE otherwise |
| CPqcMlKemEncapsulationKey, CPqcMlKemDecapsulationKey, CPqcMlDsaPublicKey, CPqcMlDsaSecretKey | Load a key once and use it many times, faster than the functions above |
| PqcSha3_256(data, hash), PqcShake256(data, size, out), CPqcKeccak | SHA-3 and SHAKE, one-shot or in pieces (all sizes listed in Sha3.mqh) |
| PqcRandomBytes(out, count) | Bytes for keys and seeds from the best sources the terminal offers |
| PqcErrorText(code), PqcToHex(bytes), PqcFromHex(hex, bytes), PqcUtf8(text, bytes) | Error texts, hex and UTF-8 helpers |
MetaTrader has no cryptographic random number generator without a DLL. PqcRandomBytes mixes every source the terminal offers (clocks, counters, the chart, MathRand) through SHAKE256 and never repeats, but nobody has measured how hard it is to predict. For keys that protect real money, make the seed outside MetaTrader (Python's secrets.token_bytes(64) or openssl rand -hex 64) and pass it to the FromSeed functions.
The code avoids the division and secret-dependent branches that let timing attacks recover keys from other implementations, but MQL gives no guarantee about the machine code the terminal runs, so it is not proven to run in constant time.
AesHmac: AES-256 encryption with tamper check and password keys for MQL4
One include file that encrypts texts and bytes so only a program with the same key can read them. A wrong key or a changed message is refused instead of read as garbage, keys can come from a password, and the code is checked against the official NIST and RFC test vectors.
TesterSafeLots - risk lot size that stays correct in the MT4 Strategy Tester
Risk-based lot size for MQL4 that does not depend on MODE_TICKVALUE in the Strategy Tester, so backtests of USDJPY, USDCHF or USDCAD on a USD account risk what the input says. Includes a test EA that measures the error.
Three Colors
Example: Moving Average indicator filling by different colors
MACD Sample
Classical MACD Sample.