Skip to content

Commit

Permalink
New commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizhanovskyi committed Aug 8, 2024
1 parent 0c44b23 commit 96c74d2
Show file tree
Hide file tree
Showing 17 changed files with 580 additions and 0 deletions.
23 changes: 23 additions & 0 deletions quantum_cryptography/protocols/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Quantum Cryptographic Protocols

This directory contains detailed descriptions, example implementations, and tests for various quantum cryptographic protocols.

## Contents

- `qkd.md`: Detailed description of the QKD protocol.
- `qkd_example.py`: Example code for the QKD protocol.
- `test_qkd_example.py`: Tests for QKD example code.
- `bb84_example.py`: Example code for the BB84 protocol.
- `test_bb84_example.py`: Tests for BB84 example code.
- `ecc_qkd_example.py`: Example code for ECC in QKD.
- `test_ecc_qkd_example.py`: Tests for ECC in QKD example code.
- `grover_example.py`: Example code for Grover's algorithm.
- `test_grover_example.py`: Tests for Grover's algorithm example code.
- `quantum_teleportation_example.py`: Example code for quantum teleportation.
- `test_quantum_teleportation_example.py`: Tests for quantum teleportation example code.
- `bb84_qiskit_example.py`: Example code for BB84 on Qiskit.
- `test_bb84_qiskit_example.py`: Tests for BB84 on Qiskit example code.
- `quantum_encryption_example.py`: Example code for quantum encryption.
- `test_quantum_encryption_example.py`: Tests for quantum encryption example code.
- `quantum_superposition_entanglement_example.py`: Example code for quantum superposition and entanglement.
- `test_quantum_superposition_entanglement_example.py`: Tests for quantum superposition and entanglement example code.
48 changes: 48 additions & 0 deletions quantum_cryptography/protocols/bb84_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import random

# Generate random bits and basis
def generate_random_bits_and_basis(length):
bits = [random.randint(0, 1) for _ in range(length)]
basis = [random.choice(['+', 'x']) for _ in range(length)]
return bits, basis

# Encode bits into photons based on basis
def encode_bits_to_photons(bits, basis):
photons = []
for bit, b in zip(bits, basis):
if b == '+':
photon = 'H' if bit == 0 else 'V'
else:
photon = 'D' if bit == 0 else 'A'
photons.append(photon)
return photons

# Measure photons using basis
def measure_photons(photons, basis):
measured_bits = []
for photon, b in zip(photons, basis):
if b == '+':
bit = 0 if photon == 'H' else 1
else:
bit = 0 if photon == 'D' else 1
measured_bits.append(bit)
return measured_bits

# Sift keys based on matching basis
def sift_keys(sender_basis, receiver_basis, sender_bits, receiver_bits):
sifted_key = []
for sb, rb, sbit, rbit in zip(sender_basis, receiver_basis, sender_bits, receiver_bits):
if sb == rb:
sifted_key.append(sbit)
return sifted_key

# Example usage
length = 100
alice_bits, alice_basis = generate_random_bits_and_basis(length)
alice_photons = encode_bits_to_photons(alice_bits, alice_basis)

bob_basis, _ = generate_random_bits_and_basis(length)
bob_bits = measure_photons(alice_photons, bob_basis)

sifted_key = sift_keys(alice_basis, bob_basis, alice_bits, bob_bits)
print(f'Sifted Key: {sifted_key}')
50 changes: 50 additions & 0 deletions quantum_cryptography/protocols/bb84_qiskit_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import random
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram

# BB84 protocol implementation

def generate_random_bits(length):
return [random.randint(0, 1) for _ in range(length)]

def generate_random_bases(length):
return [random.choice(['+', 'x']) for _ in range(length)]

def encode_bits_to_photons(bits, bases):
photons = []
for bit, base in zip(bits, bases):
if base == '+':
photon = '0' if bit == 0 else '1'
else:
photon = '+0' if bit == 0 else '+1'
photons.append(photon)
return photons

def measure_photons(photons, bases):
measured_bits = []
for photon, base in zip(photons, bases):
if base == '+':
bit = 0 if photon in ['0', '+0'] else 1
else:
bit = 0 if photon in ['+0', '0'] else 1
measured_bits.append(bit)
return measured_bits

def sift_keys(sender_bases, receiver_bases, sender_bits, receiver_bits):
sifted_key = []
for sb, rb, sb_bit, rb_bit in zip(sender_bases, receiver_bases, sender_bits, receiver_bits):
if sb == rb:
sifted_key.append(sb_bit)
return sifted_key

# Example usage
length = 100
alice_bits = generate_random_bits(length)
alice_bases = generate_random_bases(length)
alice_photons = encode_bits_to_photons(alice_bits, alice_bases)

bob_bases = generate_random_bases(length)
bob_bits = measure_photons(alice_photons, bob_bases)

sifted_key = sift_keys(alice_bases, bob_bases, alice_bits, bob_bits)
print(f'Sifted Key: {sifted_key}')
35 changes: 35 additions & 0 deletions quantum_cryptography/protocols/ecc_qkd_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from Crypto.PublicKey import ECC
from Crypto.Signature import DSS
from Crypto.Hash import SHA256

# Generate ECC keys
def generate_ecc_keys():
private_key = ECC.generate(curve='P-256')
public_key = private_key.public_key()
return private_key, public_key

# Sign a message using ECC private key
def sign_message(message, private_key):
h = SHA256.new(message)
signer = DSS.new(private_key, 'fips-186-3')
signature = signer.sign(h)
return signature

# Verify a signature using ECC public key
def verify_signature(message, signature, public_key):
h = SHA256.new(message)
verifier = DSS.new(public_key, 'fips-186-3')
try:
verifier.verify(h, signature)
return True
except ValueError:
return False

# Example usage
private_key, public_key = generate_ecc_keys()
message = b'This is a secret message'
signature = sign_message(message, private_key)
print(f'Signature: {signature}')

is_valid = verify_signature(message, signature, public_key)
print(f'Signature valid: {is_valid}')
44 changes: 44 additions & 0 deletions quantum_cryptography/protocols/grover_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram
from qiskit.circuit.library import GroverOperator
from qiskit.algorithms import AmplificationProblem

# Define the oracle for the problem
def oracle(circuit, n):
circuit.cz(0, n-1)
circuit.h(0)

# Grover's algorithm
def grover_algorithm(n):
# Create quantum and classical registers
qc = QuantumCircuit(n)

# Apply Hadamard gates
qc.h(range(n))

# Apply the oracle
oracle(qc, n)

# Apply the Grover diffusion operator
grover_op = GroverOperator(qc)
qc.append(grover_op, range(n))

# Measure the qubits
qc.measure_all()

return qc

# Example usage
n = 3 # Number of qubits
qc = grover_algorithm(n)
qc.draw('mpl')

# Simulate the circuit
backend = Aer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
qobj = assemble(tqc)
result = execute(qc, backend).result()
counts = result.get_counts()

# Plot the results
plot_histogram(counts)
50 changes: 50 additions & 0 deletions quantum_cryptography/protocols/qkd_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import random

# Define basis and bits
def generate_basis_and_bits(length):
basis = [random.choice(['+', 'x']) for _ in range(length)]
bits = [random.randint(0, 1) for _ in range(length)]
return basis, bits

# Encode bits to photons
def encode_bits(basis, bits):
photons = []
for b, bit in zip(basis, bits):
if b == '+':
photon = 'H' if bit == 0 else 'V'
else:
photon = 'D' if bit == 0 else 'A'
photons.append(photon)
return photons

# Measure photons
def measure_photons(received_photons, basis):
measured_bits = []
for photon, b in zip(received_photons, basis):
if b == '+':
bit = 0 if photon in ['H', 'D'] else 1
else:
bit = 0 if photon in ['D', 'H'] else 1
measured_bits.append(bit)
return measured_bits

# Key sifting
def sift_keys(sender_basis, receiver_basis, sender_bits, receiver_bits):
sifted_key = []
for sb, rb, sb_bit, rb_bit in zip(sender_basis, receiver_basis, sender_bits, receiver_bits):
if sb == rb:
sifted_key.append(sb_bit)
return sifted_key

# Example usage
length = 100
alice_basis, alice_bits = generate_basis_and_bits(length)
alice_photons = encode_bits(alice_basis, alice_bits)

# Bob chooses his basis
bob_basis, _ = generate_basis_and_bits(length)
bob_bits = measure_photons(alice_photons, bob_basis)

# Key sifting
sifted_key = sift_keys(alice_basis, bob_basis, alice_bits, bob_bits)
print(f'Sifted Key: {sifted_key}')
45 changes: 45 additions & 0 deletions quantum_cryptography/protocols/quantum_encryption_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram

def quantum_encryption():
qc = QuantumCircuit(3, 3)

# Create Bell pair
qc.h(1)
qc.cx(1, 2)

# Prepare the state to be encrypted (superposition state as an example)
qc.h(0)
qc.barrier()

# Apply the encryption protocol
qc.cx(0, 1)
qc.h(0)
qc.barrier()

# Measure qubits 0 and 1
qc.measure([0, 1], [0, 1])
qc.barrier()

# Apply conditional operations based on measurement
qc.cx(1, 2)
qc.cz(0, 2)

# Measure the final qubit
qc.measure(2, 2)

return qc

# Example usage
qc = quantum_encryption()
qc.draw('mpl')

# Simulate the circuit
backend = Aer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
qobj = assemble(tqc)
result = execute(qc, backend).result()
counts = result.get_counts()

# Plot the results
plot_histogram(counts)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram

def quantum_superposition_entanglement():
qc = QuantumCircuit(2, 2)

# Create superposition on the first qubit
qc.h(0)

# Create entanglement between the first and second qubit
qc.cx(0, 1)
qc.barrier()

# Measure both qubits
qc.measure([0, 1], [0, 1])

return qc

# Example usage
qc = quantum_superposition_entanglement()
qc.draw('mpl')

# Simulate the circuit
backend = Aer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
qobj = assemble(tqc)
result = execute(qc, backend).result()
counts = result.get_counts()

# Plot the results
plot_histogram(counts)
44 changes: 44 additions & 0 deletions quantum_cryptography/protocols/quantum_teleportation_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from qiskit import QuantumCircuit, Aer, transpile, assemble, execute
from qiskit.visualization import plot_histogram

def quantum_teleportation():
qc = QuantumCircuit(3, 3)

# Create Bell pair
qc.h(1)
qc.cx(1, 2)

# Prepare the state to be teleported
qc.x(0) # This is the state |1>

# Apply the teleportation protocol
qc.cx(0, 1)
qc.h(0)
qc.barrier()

# Measure qubits 0 and 1
qc.measure([0, 1], [0, 1])
qc.barrier()

# Apply conditional operations
qc.cx(1, 2)
qc.cz(0, 2)

# Measure the final qubit
qc.measure(2, 2)

return qc

# Example usage
qc = quantum_teleportation()
qc.draw('mpl')

# Simulate the circuit
backend = Aer.get_backend('qasm_simulator')
tqc = transpile(qc, backend)
qobj = assemble(tqc)
result = execute(qc, backend).result()
counts = result.get_counts()

# Plot the results
plot_histogram(counts)
Loading

0 comments on commit 96c74d2

Please sign in to comment.