Inspiration
The dawn of the quantum computing era presents a monumental threat to our current digital infrastructure. Cryptographic systems like RSA and ECC, the bedrock of today's internet security, will be rendered obsolete, leaving our data vulnerable. As an R&D-focused developer passionate about tackling foundational technological challenges, this "quantum threat" was not just a problem but a call to action. Inspired by the groundbreaking work from the NIST Post--Quantum Cryptography (PQC) standardization process, my goal was to move beyond theory and build a practical, working implementation of a next-generation security protocol. The project, named "e-har-lak innovations" (อิเล็กทรอนิกส์-รากฐาน), aims to create a new, quantum-resistant foundation for secure communication on the modern web.
What it does
This project is a fully functional Hybrid Post-Quantum Cryptography (PQC) System designed for secure client-server communication in modern web applications. It seamlessly integrates two powerful, NIST-standardized lattice-based algorithms to protect against both classical and quantum adversaries:
- Secure Handshake with CRYSTALS-Kyber: It uses Kyber, a Key Encapsulation Mechanism (KEM), to establish a secure shared secret between the client and server. This initial "quantum handshake" ensures that no eavesdropper can intercept the secret key.
- Quantum-Resistant Messaging with NTRU: The shared secret from Kyber is then used as a deterministic seed to generate a pair of NTRU keys for each party. NTRU, a Public Key Encryption (PKE) scheme, is then used to encrypt and decrypt the actual messages exchanged between the client and server.
- React Hook for Easy Integration: The entire client-side cryptographic logic is encapsulated within a single, easy-to-use React Hook,
useQuantumKyberNtru, making it simple to integrate cutting-edge PQC into any Next.js or React application.
The result is an end-to-end secure communication channel where the key establishment and message encryption are both protected by the next generation of cryptography.
How we built it
This project was built from the ground up as a 100% custom R&D endeavor with zero third-party crypto dependencies, demonstrating a deep, fundamental approach to security.
- Stack: The entire system is built using Next.js with a focus on pure, functional ECMAScript 2024 Modules.
- Cryptographic Primitives:
- Kyber (IND-CCA2): A complete implementation of CRYSTALS-Kyber based on the NIST standard. [cite_start]This includes the core polynomial operations in the ring $\mathbb{Z}_q[X]/(X^N - 1)$, Number Theoretic Transform (NTT) for fast multiplication, and the full Fujisaki-Okamoto transform to achieve IND-CCA2 security, including the critical "implicit rejection" mechanism during decapsulation [cite: 446-450].
- NTRU: A from-scratch implementation of NTRU, also featuring its own polynomial arithmetic. [cite_start]A key feature is the ability to toggle between standard convolution multiplication and a highly efficient NTT-based multiplication using the Chinese Remainder Theorem (CRT) for significant performance gains [cite: 100-102, 99]. This optimization is enabled via the
enableNTTOptimization(true)call during initialization.
- Hybrid Protocol: The core innovation is the protocol that links Kyber and NTRU. The
doKyberKeyExchangefunction establishes the shared secret, which is then passed directly togenerateNTRUKeysWithSeedto create the encryption keys. This creates a tightly coupled system where the entire cryptographic state is derived from a single, quantum-secure handshake. - Architecture: The client-side logic resides within the
useQuantumKyberNtruhook, which manages the state machine (idle,initializing,ready,error) and exposes simpleencryptMessageanddecryptMessagefunctions. This hook communicates with a backend over a set of defined API endpoints (e.g.,/api/kyber-public-key) to exchange public keys and ciphertexts.
Challenges we ran into
- Implementing Lattice-Based Math from Scratch: The mathematics behind Kyber and NTRU are profoundly complex. [cite_start]Implementing polynomial arithmetic in finite fields, especially operations like finding the modular inverse of a polynomial using the Extended Euclidean Algorithm [cite: 111-112], was a significant hurdle that required meticulous attention to detail.
- Performance of Polynomial Multiplication: A naive implementation of polynomial multiplication has a complexity of $O(N^2)$, which is too slow for real-time applications. [cite_start]The biggest challenge was correctly implementing the Number Theoretic Transform (NTT) and the Chinese Remainder Theorem (CRT) [cite: 95-99] to accelerate this process to a near-linear $O(N \log N)$, making the system practical for web use.
- Ensuring Constant-Time Execution: A critical aspect of cryptographic implementation is defending against side-channel attacks. [cite_start]A major challenge was ensuring that code, particularly the ciphertext comparison in Kyber's implicit rejection step [cite: 446-450] [cite_start]and the final key selection [cite: 456-458], executes in constant time, regardless of the input, to avoid leaking secret information through timing variations.
- Building a Secure Foundation: Without relying on any external libraries, we had to build everything, including our own secure random byte generators (CSPRNG) and utilities, which added a significant layer of complexity and testing requirements.
Accomplishments that we're proud of
- Successfully building a fully functional, end-to-end hybrid PQC system that demonstrates a practical path away from vulnerable legacy crypto.
- Implementing two different NIST-standard lattice-based algorithms from scratch in pure JavaScript, a testament to deep cryptographic and mathematical understanding.
- The innovative Kyber-seeds-NTRU protocol design, which creates a robust and elegant link between the key exchange and encryption mechanisms.
- Successfully implementing the NTT performance optimization, which is crucial for making PQC viable in a low-latency environment like the web.
- Achieving this as a zero-dependency R&D project, giving us complete control and understanding of every single line of code that handles our security.
What we learned
This project was an incredible learning journey into the heart of modern cryptography. We gained a profound, practical understanding of lattice-based cryptography, moving from academic papers to running code. We learned the immense complexities of implementing advanced mathematics like Number Theoretic Transforms and the critical importance of seemingly small details, such as ensuring constant-time code execution to prevent side-channel attacks. Most importantly, we learned how to architect a complete cryptographic protocol, combining different primitives (a KEM and a PKE) to build a system that is greater than the sum of its parts.
What's next for e_har_lak_innovations
The future is bright and quantum-resistant. Our next steps are focused on expanding and hardening the project:
Add Digital Signature Support: Integrate a PQC digital signature algorithm like CRYSTALS-Dilithium or Falcon to provide message authentication and non-repudiation.
Port to WebAssembly (WASM): For a massive performance boost, we plan to recompile the core C/Rust implementations of these algorithms into WebAssembly and integrate them into our hook. This will achieve near-native speeds directly in the browser.
Formal Security Audit: Subject our implementation to a rigorous third-party security audit to identify any potential vulnerabilities and further harden the codebase.
Integrate with Chrome's Built-in AI: Explore using the new on-device AI capabilities in Chrome to enhance our security protocol. For example, we could build a system that analyzes network conditions and application behavior to dynamically select the most optimal PQC security parameters in real-time, perfectly balancing performance and security without server-side intervention.
Built With
- javascript
- qasm
- rust