Skip to content

turkananation/pqcrypto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

13 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

pqcrypto: Pure Dart Post-Quantum Cryptography

pqcrypto is a pure Dart library implementing Post-Quantum Cryptography (PQC) algorithms, targeting compatibility with Flutter and the Dart web ecosystem.

The current release provides a production-hardened implementation of ML-KEM (Kyber), adhering to the FIPS 203 (Module-Lattice-Based Key-Encapsulation Mechanism) standard.


๐Ÿš€ Features

  • Full FIPS 203 Compliance:
    • Algorithm Support: ML-KEM-512, ML-KEM-768, ML-KEM-1024
    • Secure Primitives:
      • SHAKE-128/256 based matrix generation and hashing.
      • Centered Binomial Distribution (CBD) for secure noise sampling.
    • Key Encapsulation: Correct (rho, sigma) := G(d) derivation.
    • Fujisaki-Okamoto Transform: Robust re-encryption check to prevent chosen-ciphertext attacks (IND-CCA2 security).
  • Platform Agnostic:
    • 100% Pure Dart. Works on Android, iOS, Windows, Linux, macOS, and Web (dart2js/dart2wasm).
    • Zero native dependencies (uses pointycastle for SHA3 primitives).

๐Ÿ›ก๏ธ ML-KEM FIPS 203 Compliance Status

This implementation is fully compliant with FIPS 203.

Algorithm Status NIST KAT Vectors Security Level
ML-KEM-512 โœ… Ready 1000/1000 PASS NIST Level 1 (AES-128)
ML-KEM-768 โœ… Ready 1000/1000 PASS NIST Level 3 (AES-192)
ML-KEM-1024 โœ… Ready 1000/1000 PASS NIST Level 5 (AES-256)

Total Verified Vectors: 3000/3000 (Validated December 2024)


๐Ÿ› ๏ธ Implementation Highlights

This library adheres strictly to the FIPS 203 specification structure.

1. Number Theoretic Transform (NTT)

Uses pure modular arithmetic (not Montgomery) matching the FIPS 203 Algorithms 8 and 9:

  • NTT/InvNTT: Cooley-Tukey butterfly operations with modular reduction.
  • Base Multiplication: Karatsuba-style in NTT domain using $\gamma$ coefficients (Algorithm 10).
  • Polynomial Ring: Operations in $\mathbb{Z}_q[X]/(X^{256}+1)$ where $q = 3329$.

2. Compression & Serialization

All compression functions implement FIPS 203 Definitions 4.7-4.8 with bit-exact correctness:

  • compress(x, d): Standard rounding logic $\lceil (2^d/q) \cdot x \rfloor \bmod 2^d$.
  • Formula: (2 * x * 2^d + q) / (2 * q) with edge-case clamping.
  • ByteEncode support:
    • 12-bit: Public Keys (ByteEncodeโ‚โ‚‚)
    • 11-bit: ML-KEM-1024 Ciphertext $u$ (ByteEncodeโ‚โ‚)
    • 10-bit: ML-KEM-768 Ciphertext $u$ (ByteEncodeโ‚โ‚€)
    • 5-bit: ML-KEM-1024 Ciphertext $v$ (ByteEncodeโ‚…)
    • 4-bit: ML-KEM-512/768 Ciphertext $v$ (ByteEncodeโ‚„)
    • 1-bit: Messages (ByteEncodeโ‚)

3. Cryptographic Primitives

  • XOF: SHAKE-128 for matrix generation (Algorithm 7).
  • PRF: SHAKE-256 for noise sampling.
  • Hash Functions: SHA3-256, SHA3-512 for key derivation.
  • CBD Sampling: Centered Binomial Distribution with $\eta \in {2,3}$.

4. Security Hardening

  • Implicit Rejection: Implementation of the modified Fujisaki-Okamoto transform guarantees that invalid ciphertexts produce a pseudo-random shared secret (derived from internal secret $z$) rather than failing. This prevents chosen-ciphertext timing attacks.
  • Domain Separation: All hash calls include the standardized domain separation bytes.

๐Ÿ“‚ Project Structure

lib/
โ”œโ”€โ”€ pqcrypto.dart                 # ๐Ÿ“ฆ Library Entrypoint
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ algos/
    โ”‚   โ””โ”€โ”€ kyber/
    โ”‚       โ”œโ”€โ”€ kem.dart          # ๐Ÿš€ ML-KEM High-Level API (Algorithms 16-19)
    โ”‚       โ”‚                     # - KeyGen_internal (Algorithm 15)
    โ”‚       โ”‚                     # - ML-KEM.KeyGen (Algorithm 16)
    โ”‚       โ”‚                     # - ML-KEM.Encaps (Algorithm 17)
    โ”‚       โ”‚                     # - ML-KEM.Decaps (Algorithm 18)
    โ”‚       โ”‚
    โ”‚       โ”œโ”€โ”€ indcpa.dart       # ๐Ÿ” IND-CPA Encryption K-PKE (Algorithms 12-14)
    โ”‚       โ”‚                     # - K-PKE.KeyGen (Algorithm 12)
    โ”‚       โ”‚                     # - K-PKE.Encrypt (Algorithm 13)
    โ”‚       โ”‚                     # - K-PKE.Decrypt (Algorithm 14)
    โ”‚       โ”‚
    โ”‚       โ”œโ”€โ”€ pack.dart         # ๐Ÿ’พ Serialization & Compression (Defs 4.7-4.8)
    โ”‚       โ”‚                     # - ByteEncode/ByteDecode (Algorithms 4-5)
    โ”‚       โ”‚                     # - Compress/Decompress (d=1,4,5,10,11,12)
    โ”‚       โ”‚
    โ”‚       โ””โ”€โ”€ params.dart       # ๐Ÿ“ Security Parameters
    โ”‚                             # - Constants for k, eta1, eta2, du, dv
    โ”‚
    โ””โ”€โ”€ common/
        โ”œโ”€โ”€ poly.dart             # ๐Ÿงฎ Polynomial Arithmetic & NTT
        โ”‚                         # - NTT / InvNTT (Algorithms 8-9)
        โ”‚                         # - BaseMul [MultiplyNTTs] (Algorithm 10)
        โ”‚                         # - SampleNTT [Parse] (Algorithm 7)
        โ”‚                         # - PolyAdd, PolySub, PolyReduce
        โ”‚
        โ””โ”€โ”€ shake.dart            # ๐ŸŽฒ Cryptographic Primitives
                                  # - SHAKE-128 / SHAKE-256 wrappers

test/
โ”œโ”€โ”€ kat_evaluator.dart            # ๐Ÿงช NIST KAT Runner (FIPS 203 Validated)
โ”œโ”€โ”€ pack_test.dart                # ๐Ÿ“ฆ Serialization Unit Tests (Round-trip)
โ”œโ”€โ”€ cbd_test.dart                 # ๐Ÿ“Š Statistical Distribution Tests
โ””โ”€โ”€ data/
    โ”œโ”€โ”€ kat_MLKEM_512.rsp         # โœ… Official NIST Vectors (Level 1)
    โ”œโ”€โ”€ kat_MLKEM_768.rsp         # โœ… Official NIST Vectors (Level 3)
    โ””โ”€โ”€ kat_MLKEM_1024.rsp        # โœ… Official NIST Vectors (Level 5)

๐Ÿ’ป Usage

Quick Start

Serverpod Users: Check out the Full Stack Integration Guide for a complete backend + client implementation pattern.

import 'package:pqcrypto/pqcrypto.dart';

void main() {
  // 1. Select the security level
  // Options: PqcKem.kyber512, PqcKem.kyber768, PqcKem.kyber1024
  final kem = PqcKem.kyber768;

  // 2. Generate Keypair (Server Side)
  // Returns Public Key (pk) and Secret Key (sk)
  final (pk, sk) = kem.generateKeyPair();
  print('Public Key size: ${pk.length} bytes');
  print('Secret Key size: ${sk.length} bytes');

  // 3. Encapsulate (Client Side)
  // Uses the Public Key to generate a Shared Secret and Ciphertext
  final (ct, ssAlice) = kem.encapsulate(pk);
  print('Ciphertext size: ${ct.length} bytes');

  // 4. Decapsulate (Server Side)
  // Server recovers the same Shared Secret using Secret Key
  final ssBob = kem.decapsulate(sk, ct);

  // Check that secrets match
  assert(ssAlice.toString() == ssBob.toString());
  print('Shared Secret derived successfully!');
}

๐Ÿงช Verification & Testing

The quality of this cryptographic library is verified through three comprehensive layers:

1. NIST Known Answer Tests (KAT)

Validates against the official test vectors from NIST (GitHub: post-quantum-cryptography/KAT).

  • Parser: test/kat_evaluator.dart handles .rsp files using FIPS 203 ct_n/ss_n format.
  • Coverage:
    • โœ… ML-KEM-512: 100/100 vectors
    • โœ… ML-KEM-768: 100/100 vectors
    • โœ… ML-KEM-1024: 100/100 vectors

2. Unit & Property Tests

  • Serialization (test/pack_test.dart): Round-trip validation for all bit-depths (1, 4, 5, 10, 11, 12) checking exact reconstruction.
  • NTT Correctness (test/ntt_test.dart): Verifies NTT/InvNTT reversibility and polynomial multiplication.
  • Statistical (test/cbd_test.dart): Verifies the output distribution of the CBD sampler matches theoretical binomial probabilities.

3. Negative Testing (Implicit Rejection)

  • test/failure_test.dart: Confirms that decapsulating a modified/invalid ciphertext does NOT crash but instead deterministically derives a secure random key, preserving IND-CCA2 security.

โšก Performance

Benchmarks on commodity hardware (Dart VM, JIT):

Algorithm Key Generation Encapsulation Decapsulation Security Level
ML-KEM-512 ~0.7 ms ~0.7 ms ~0.6 ms 128-bit security
ML-KEM-768 ~1.3 ms ~1.4 ms ~1.0 ms 192-bit security
ML-KEM-1024 ~1.8 ms ~1.8 ms ~1.7 ms 256-bit security
(Measured on Linux x64, Dart 3.x JIT)

๐Ÿ”ฎ Roadmap

  • Phase 1: Foundation (Project structure, Poly math)
  • Phase 2: Correctness (GenMatrix, CBD, FO Transform)
  • Phase 3: FIPS 203 Compliance (NTT, Compression, ByteEncode)
  • Phase 4: Full Suite (ML-KEM-512/768/1024 support)
  • Phase 5: Optimization (SIMD, WASM via dart:wasm)
  • Phase 6: Expansion (ML-DSA / Dilithium signatures)

Installation

Add to pubspec.yaml:

dependencies:
  pqcrypto: ^0.1.0
  pointycastle: ^4.0.0

Releases

No releases published

Packages

No packages published

Languages