This repository contains a simple, interactive web-based demonstration of a core principle behind Quantum Key Distribution (QKD), specifically the "measurement problem" as applied in protocols like BB84. The demo illustrates how the act of observing a quantum state inherently disturbs it, a property that can be leveraged to detect eavesdropping during cryptographic key exchange.
Disclaimer: This is a highly simplified conceptual model for educational purposes only. It is NOT a secure cryptographic implementation and should NEVER be used for real-world security applications.
-
Bits (0s and 1s): The secret information Alice aims to share securely with Bob.
-
Bases (Rectilinear '+' or Diagonal 'x'): These represent conceptual polarization filters used to encode and measure the "photons."
-
Rectilinear (0): Measures horizontal (H) or vertical (V) polarization.
-
Diagonal (1): Measures diagonal (D1 or D2) polarization.
-
-
Measurement Disturbance: A fundamental quantum mechanics principle where observing a quantum state (like a photon's polarization) changes it, especially if measured in a mismatched basis.
The JavaScript code simulates the interactions between Alice, Bob, and an optional eavesdropper (Eve) to demonstrate QKD.
The script begins by initializing global JavaScript arrays (e.g., aliceBits, aliceBases, bobMeasuredBits) to maintain the state of the simulation. It also retrieves references to various HTML elements (buttons, input fields, and display areas) to interact with the user interface.
-
updateOutput(element, data): A utility function to update the text content of the display boxes in the HTML. -
showMessage(message, type): A helper function to display status or error messages to the user within theresultMessageDiv, applying different styling based on the message type (success, warning, error).
-
Action: When the "1. Alice Generates Bits & Bases" button is clicked.
-
Process:
-
The
keyLengthis read from the input field (validated to be between 4 and 64 bits). -
All simulation state arrays are reset.
-
Alice generates a sequence of random
keyLengthbits (0 or 1) and stores them inaliceBits. -
For each bit, she also randomly selects a "polarization basis" (0 for Rectilinear, 1 for Diagonal) and stores it in
aliceBases. -
Display: The
alicePhotonsDivshows a conceptual representation of Alice's "sent photons" (e.g., 'H(0)', 'V(1)', 'D1(0)', 'D2(1)' based on bit and basis), andaliceBasesDivshows her chosen bases ('Rect' or 'Diag').
-
-
Outcome: A success message confirms Alice has generated her data and conceptually sent the photons.
-
Action: When the "2. Eve Intercepts (if enabled)" button is clicked.
-
Process:
-
The
eavesdropChance(percentage) is read from the input field. -
The code iterates through each "photon" Alice sent.
-
Eavesdropping Logic: Based on the
eavesdropChance, Eve decides whether to intercept a photon:-
If Eve intercepts:
-
Eve randomly chooses a
basisto measure the photon. -
If Eve's chosen
basismatches Alice's originalbasis, Eve measures the correct bit. -
Crucially: If Eve's chosen
basisdoes not match Alice's originalbasis, Eve's measurement yields a random bit (50% chance of being incorrect). More importantly, this incorrect measurement modifies thealiceBits[i]value, simulating the quantum disturbance. -
Eve's measured bit and chosen basis are recorded in
eveMeasuredBitsandeveBases.
-
-
If Eve does NOT intercept: The photon passes through undisturbed, and 'N/A' is recorded for Eve's measurements.
-
-
-
Outcome: The
eveMeasuredDivandeveBasesDivare updated. A message indicates how many photons Eve intercepted and if any disturbance occurred.
-
Action: When the "3. Bob Measures Photons" button is clicked.
-
Process:
-
Bob iterates through the photons (which might have been altered by Eve).
-
For each photon, Bob randomly chooses a
basisto measure it (bobBases). -
If Bob's chosen
basismatches Alice's original sending basis (aliceBases[i]), he measures the correct bit (or the bit that Eve might have altered). -
If Bob's chosen
basisdoes not match Alice's original sending basis, his measurement yields a random bit (50% chance of being incorrect). -
Bob's measured bits and chosen bases are recorded.
-
-
Outcome: The
bobMeasuredDivandbobBasesDivare updated, and a success message is displayed.
-
Action: When the "4. Key Reconciliation & Eavesdropping" button is clicked.
-
Process:
-
Alice and Bob (conceptually) publicly compare their chosen bases (
aliceBasesandbobBases). -
They only keep the bits where their bases matched. These form
rawAliceKeyandrawBobKey. -
They then compare these
rawAliceKeyandrawBobKeybit by bit to counterrorsDetected. -
Eavesdropping Detection:
-
If
errorsDetectedis 0, all bits where their bases matched are identical. This implies no significant disturbance, and they successfully establish afinalSharedKey. -
If
errorsDetectedis greater than 0, it means discrepancies exist. This is the tell-tale sign that Eve (or significant channel noise) intercepted and disturbed the photons. In a real QKD protocol, they would immediately discard this key and attempt to establish a new one.
-
-
-
Outcome: The
sharedKeyDivis updated with the final key (or empty if discarded), and a message indicates whether the key was established or discarded due to detected eavesdropping.
-
Open the HTML file: Save the provided HTML code as an
.htmlfile (e.g.,qkd_demo.html) and open it in a web browser. -
Adjust Settings:
-
Key Length (bits): Choose how many "photons" Alice will send (between 4 and 64).
-
Eve's Eavesdrop Chance (%): Set the probability (0-100) that Eve will attempt to intercept each photon.
-
-
Follow the Steps:
-
Click "1. Alice Generates Bits & Bases" to start the process.
-
Click "2. Eve Intercepts (if enabled)" to simulate Eve's actions.
-
Click "3. Bob Measures Photons" to simulate Bob's measurements.
-
Click "4. Reconcile Keys & Check for Eavesdropping" to see if a shared key was established and if eavesdropping was detected.
-
-
Observe: Pay attention to the output boxes and the final message to understand how Eve's interference (when her chance is > 0 and she chooses a different basis) leads to detectable errors.
-
HTML5: Provides the structure of the web page.
-
Tailwind CSS: Used for responsive and modern styling.
-
JavaScript: Implements the core logic of the QKD simulation and handles user interactions.
-
Simulated Randomness:
Math.random()is used to simulate the quantum randomness in bit generation, basis choices, and Eve's interception.