Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

HW1

93 views
unlisted
ubuntu2404
# Define elliptic curve: \( y^2 = x^3 + ax + b \) F = GF(23) # Finite field of order 23 E = EllipticCurve(F, [-2, 4]) # Curve over finite field print("Elliptic Curve:", E)
Elliptic Curve: Elliptic Curve defined by y^2 = x^3 + 21*x + 4 over Finite Field of size 23
# Plot the curve (optional visual) plot(E) # Find some points on the curve points = E.points()[::6] # First 6 points print("First 6 points:", points)
First 6 points: [(0 : 1 : 0), (2 : 13 : 1), (6 : 22 : 1), (11 : 18 : 1), (14 : 12 : 1)]
# Create points P = E(0, 2) # Point (0, 2) on E Q = E(2, 13) # Point (2, 13) on E # Add points R = P + Q print(f"P + Q = {R}") print(f"Coordinates: ({R[0]}, {R[1]}))")
P + Q = (11 : 18 : 1) Coordinates: (11, 18))
''' Check: Verify that R lies on the curve by substituting coordinates into the equation. Let x=11 x^3 +21x+4 1331-21*11+4=1566 1566%23=2 y^2=18^2=324 324%23=2 (11,18) Lies on the curve'''
' Check: Verify that R lies on the curve by substituting coordinates into the equation.\nLet x=11\nx^3 +21x+4\n1331-21*11+4=1566\n1566%23=2\ny^2=18^2=324\n324%23=2\n(11,18) Lies on the curve'
# Compute 5 * P n = 5 nP = n * P print(f"{n} * P = {nP}") print(f"Coordinates: ({nP[0]}, {nP[1]}))")
5 * P = (2 : 13 : 1) Coordinates: (2, 13))
# Task: Try changing n to 3 and compute again. n = 3 nP = n * P print(f"{n} * P = {nP}") print(f"Coordinates: ({nP[0]}, {nP[1]}))")
3 * P = (10 : 15 : 1) Coordinates: (10, 15))
# Generate a private/public key pair private_key = 1234567 # In practice, use a secure random number G = E.gens()[0] # Generator point of the curve public_key = private_key * G print(f"Private key: {private_key}") print(f"Public key: {public_key}")
Private key: 1234567 Public key: (5 : 21 : 1)
def simple_ecies_encrypt(message, recipient_pub_key): """ Simplified ECIES encryption (for educational purposes only) """ # 1. Generate ephemeral key pair k = randint(1, 1000) # Random ephemeral private key R = k * G # Ephemeral public key # 2. Compute shared secret S = k * recipient_pub_key # 3. Derive symmetric key (simplified) symmetric_key = str(hash(str(S[0]))) # Use x-coordinate of shared point 7r37r3545fre # 4. Encrypt message (simplified XOR encryption) HELLO encrypted_msg = "" for i, char in enumerate(message): key_char = symmetric_key[i % len(symmetric_key)] encrypted_msg += chr(ord(char) ^^ ord(key_char)) return R, encrypted_msg # Example usage message = "HELLO" encrypted = simple_ecies_encrypt(message, public_key) print(f"Ephemeral public key R: {encrypted[0]}") print(f"Encrypted message: {encrypted[1]}")
Ephemeral public key R: (10 : 15 : 1) Encrypted message: e}{y|
def simple_ecies_decrypt(encrypted_data, private_key): """ Simplified ECIES decryption """ R, ciphertext = encrypted_data # 1. Compute shared secret using private key S = private_key * R # Same as k * recipient_pub_key # 2. Derive symmetric key (same as encryption) symmetric_key = str(hash(str(S[0]))) # 3. Decrypt message decrypted_msg = "" for i, char in enumerate(ciphertext): key_char = symmetric_key[i % len(symmetric_key)] decrypted_msg += chr(ord(char) ^^ ord(key_char)) return decrypted_msg # Decrypt the message decrypted = simple_ecies_decrypt(encrypted, private_key) print(f"Decrypted message: {decrypted}")
Decrypted message: HELLO
# Full ECIES example with a different curve print("\n=== Complete ECIES Example ===") # Use a smaller curve for demonstration E2 = EllipticCurve(GF(23), [1, 1]) # \( y^2 = x^3 + x + 1 \mod 23 \) G2 = E2.gens()[0] # Key generation alice_private = 6 alice_public = alice_private * G2 print(f"Alice's public key: {alice_public}") # Bob encrypts a message for Alice message = "CAT" print(f"Original message: {message}") # Encryption k = 15 # Bob's random ephemeral key R = k * G2 S = k * alice_public # Simplified encryption (just to show the concept) encrypted = [R, message] # In reality, encrypt with derived key print(f"Bob sends: R={R}, encrypted message") # Alice decrypts S_alice = alice_private * R print(f"Alice computes shared secret: {S_alice}") print(f"Bob's shared secret was: {S}") print(f"Secrets match: {S == S_alice}")
=== Complete ECIES Example === Alice's public key: (7 : 11 : 1) Original message: CAT Bob sends: R=(0 : 1 : 1), encrypted message Alice computes shared secret: (7 : 11 : 1) Bob's shared secret was: (7 : 11 : 1) Secrets match: True
# Define elliptic curve: \( y^2 = x^3 + ax + b \) F1 = GF(37) # Finite field of order 23 E1 = EllipticCurve(F1, [1, 3]) # Curve over finite field print("Elliptic Curve:", E1)
Elliptic Curve: Elliptic Curve defined by y^2 = x^3 + x + 3 over Finite Field of size 37
# Generate a private/public key pair private_key1 = 14451642 # In practice, use a secure random number G1 = E1.gens()[0] # Generator point of the curve public_key1 = private_key1 * G1 print(f"Private key: {private_key1}") print(f"Public key: {public_key1}")
Private key: 14451642 Public key: (6 : 22 : 1)
def simple_ecies_encrypt1(message, recipient_pub_key): """ Simplified ECIES encryption (for educational purposes only) """ # 1. Generate ephemeral key pair k = randint(1, 1000) # Random ephemeral private key R = k * G1 # Ephemeral public key # 2. Compute shared secret S = k * recipient_pub_key # 3. Derive symmetric key (simplified) symmetric_key = str(hash(str(S[0]))) # Use x-coordinate of shared point 7r37r3545fre # 4. Encrypt message (simplified XOR encryption) HELLO encrypted_msg = "" for i, char in enumerate(message): key_char = symmetric_key[i % len(symmetric_key)] encrypted_msg += chr(ord(char) ^^ ord(key_char)) return R, encrypted_msg def simple_ecies_decrypt1(encrypted_data, private_key): """ Simplified ECIES decryption """ R, ciphertext = encrypted_data # 1. Compute shared secret using private key S = private_key * R # Same as k * recipient_pub_key # 2. Derive symmetric key (same as encryption) symmetric_key = str(hash(str(S[0]))) # 3. Decrypt message decrypted_msg = "" for i, char in enumerate(ciphertext): key_char = symmetric_key[i % len(symmetric_key)] decrypted_msg += chr(ord(char) ^^ ord(key_char)) return decrypted_msg
# Example usage name = "saraouf" nameencrypted = simple_ecies_encrypt1(name, public_key1) print(f"Ephemeral public key R: {nameencrypted[0]}") print(f"Encrypted message: {nameencrypted[1]}")
Ephemeral public key R: (35 : 17 : 1) Encrypted message: BWAR[DQ
# Decrypt the message namedecrypted = simple_ecies_decrypt1(nameencrypted, private_key1) print(f"Decrypted message: {namedecrypted}")
Decrypted message: saraouf