Full description of the algorithm - DevsDaddy/quarkdash GitHub Wiki
Below is a complete description of the QuarkDash Crypto library's operating algorithm. This algorithm provides production ready and fast quantum-resistant hybrid encryption protocol
QuarkDash is a hybrid post-quantum protocol combining:
- Asymmetric key exchange based on Ring-LWE (resistant to quantum attacks);
- Symmetric encryption with a choice of stream ciphers (ChaCha20 or Gimli);
- Quantum-resistant KDF based on SHAKE256;
- Message authentication via SHAKE256-MAC;
- Protection against replay attacks using timestamps and sequence numbers;
Full QuarkDash comparison with popular encryption algorithms can be found here
Ring parameters:
- Dimension
N = 256 - Modulus
Q = 7681(a prime number suitable for NTT) - Primitive root
Ο = 7(of order N)
Key pair generation process:
- A polynomial
a(x)with uniform coefficients from ``Z_Q` is randomly selected. - A secret polynomial
s(x)with small coefficients from{-1, 0, 1}is randomly selected. - An error polynomial
e(x)with small coefficients from{-1, 0, 1}is randomly selected. - Calculate
b(x) = a(x) * s(x) + e(x)(multiplication in the ringZ_Q[x]/(x^N+1)). - Public key =
(a, b)(serialized into bytes). - Private key =
s(x).
Mathematically:
b = a β s + e (multiplication via NTT, addition coefficient-wise).
Initiator (for example Client):
- Obtains Recipient's public key
(a, b). - Generates random small polynomials
s'ande'. - Computes
u = a β s' + e'(this is the ciphertext). - Computes
w = b β s'(the approximate shared secret). - Rounds the coefficients of
wto bits (the most significant bit of each coefficient) β sharedSecret (256 bits). - Sends
uto Recipient.
Recipient (for example Server):
- Receives
u. - Using his secret key
s, calculatesw' = u β s. - Rounds
w'the same way as Initiator β sharedSecret (matches with high probability).
Why does the secret match?
w' = u β s = (aβs' + e') β s = aβs'βs + e'βs.
w = b β s' = (aβs + e) βββ s' = aβsβs' + eβs'
The difference w - w' = eβs' - e'βs is a small error that does not affect rounding.
- Uses SHAKE256 (Keccak implementation).
-
Input:
sharedSecret (256 bits),random salt (32 bytes),"session-key"label. -
Output: 64 bytes of
key material. - Divided into:
-
-
sessionKey (32 bytes)β for encryption.
-
-
-
macKey (32 bytes)β for authentication.
-
- A fixed nonce of 12 zero bytes is generated (for simplicity, since the session key is one-time).
- The cipher algorithm (ChaCha20 or Gimli) is selected.
- A cipher instance is created.
-
Input: plaintext
P (Uint8Array). -
Formation of
metadata (12 bytes): -
- Bytes 0-7: current timestamp (uint64, little-endian).
-
- Bytes 8-11: sequence number (uint32, little-endian, incrementing).
-
Encryption:
--
C = cipher.encrypt(P)(stream cipher:XOR with gamma). - MAC Calculation:
-
-
Input for MAC:
metadata||C.
-
Input for MAC:
-
-
Key:
macKey.
-
Key:
-
-
Algorithm:
MAC = SHAKE256(macKey || data, 32).
-
Algorithm:
- Formation of final message:
-
-
E = metadata || C || MAC.
-
-
Input: encrypted message
E. - Parsing:
-
metadata = E[0:12]
-
C = E[12:-32]
-
MAC = E[-32:]
- MAC Check:
-
- Calculate
expectedMACsimilarly and compare using constant-time.
- Calculate
- Timestamp Check:
-
- Extract the
timestampfrom themetadataand compare it with the current time. The acceptable deviation is 5 minutes.
- Extract the
- Sequence Number Check:
-
- Extract the
seqand verify that it has not been repeated (store it in a window of the last 1000 packets).
- Extract the
- Decryption:
-
-
P = cipher.decrypt(C).
-
-
- Return
P.
- Return
The data below are approximate values ββfrom synthetic tests of the algorithm.
QuarkDash Crypto Results:
- Encryption speed: ~2.5 GB/s;
- Decryption speed: ~2.5 GB/s;
- Session creation speed: ~10-15 ms;
- Public key size: ~2KB;
- Private key size: ~1KB;
- Overhead: 44 bytes;
- The Difficulty of Quantum Hacking: 2^256