Guarda come scaricare robot di trading gratuitamente
Ci trovi su Twitter!
Unisciti alla nostra fan page
Script interessante?
Pubblica il link!
lasciare che altri lo valutino
Ti è piaciuto lo script? Provalo nel Terminale MetaTrader 5
Librerie

PostQuantumCrypto: ML-KEM (Kyber) and ML-DSA (Dilithium) post-quantum encryption keys and signatures for MQL5 - libreria per MetaTrader 5

Visualizzazioni:
53
Pubblicato:
\MQL5\Include\PostQuantumCrypto\
Sha3.mqh (19.74 KB) visualizza
MlKem.mqh (64.27 KB) visualizza
MlDsa.mqh (80.21 KB) visualizza
Freelance MQL5 Hai bisogno di un robot o indicatore basato su questo codice? Ordinalo su Freelance Vai a Freelance

PostQuantumCrypto brings the post-quantum standards NIST published in 2024 to MQL5, 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>

Features

  • ML-KEM-512, ML-KEM-768 and ML-KEM-1024: key pairs, encapsulation (send a fresh secret to a public key) and decapsulation (get it back with the private key). A changed ciphertext gives a different secret instead of an error, as the standard requires, and both key checks of FIPS 203 run on every key.
  • ML-DSA-44, ML-DSA-65 and ML-DSA-87: key pairs, signing with an optional context of up to 255 bytes (a label such as "signals-v1", so a signature made for one purpose is refused for another), and verification. Signing is deterministic by default, so it needs no random numbers.
  • SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128 and SHAKE256, one-shot or fed in pieces, and Keccak-256 as Ethereum uses it.
  • Fast: in the run in the picture an ML-KEM-768 key exchange takes about 0.15 ms, within 15 percent of the reference C code compiled on the same machine, and an ML-DSA-65 signature about 0.5 ms. Key objects load a key once for many operations.
  • Checked against 1,965 published test cases, NIST's official test vectors and Google's Wycheproof vectors (inputs made to trip up implementations), and against accumulated tests that chain up to 10,000 operations into one hash. Keys, ciphertexts and signatures made by OpenSSL and by Go's standard library are read correctly, and the ones this library makes from the same seeds are identical to theirs.
  • Every call that can fail returns PQC_OK or an error code, and PqcErrorText says what it means. The library never prints anything by itself.

Example

#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=44 encapsulate_us=48 decapsulate_us=62
speed set=ML-DSA-65 keygen_us=150 sign_us=521 verify_us=139

The picture is a real run on a demo account, with the speed of all six parameter sets in microseconds.

The Experts log of the example: self test, key exchange, signed signal, refusals and speed

API

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

Limits

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.

Broker Symbol Compatibility Scanner Broker Symbol Compatibility Scanner

Scans the broker's complete MT5 symbol catalogue, resolves common prefixes, suffixes and aliases, checks trading specifications and exports a compatibility report to CSV.

Candlestick Pattern Scanner with honest statistics (engulfing, pin bar, stars, no repaint) Candlestick Pattern Scanner with honest statistics (engulfing, pin bar, stars, no repaint)

Seven candlestick patterns with exact rules, trend / swing / size filters against clutter, and a statistics table that compares every pattern with the base rate of the same chart. Closed candles only, no repaint, buffers for EAs.

SafeRequest - complete and check every MqlTradeRequest in one place SafeRequest - complete and check every MqlTradeRequest in one place

An include file that fills every field of a trade request on purpose, rounds prices to the tick grid, respects stops and freeze levels, and never removes a take profit by accident.

MACD Signals MACD Signals

Indicator edition for new platform.