ECC#
The ECC class implements the Elliptic Curve Cryptography (ECC) algorithm, which is based on the difficulty of the Elliptic Curve Discrete Logarithm Problem (ECDLP).
- class ECC#
Creates a new ECC instance.
- Parameters:
curve_name (str) – The name of the elliptic curve to use. Default is ‘P-256’, also known as ‘sep256r1’. Options are currently limited to [‘Curve25519’, ‘P-256’, ‘secp256k1’].
Note
- The implementation of ECC in this module is based primarily on the following two resources:
Check References for the entire list.
Attention
For Montgomery curves (like Curve25519), the signature scheme has not been implemented yet and hence, the sign and verify functions do not yield any result. Use the ECC cryptosystem accordingly.
Introduction#
Elliptic Curve Cryptography (ECC) is a family of public-key cryptosystems based on the mathematics of elliptic curves. It provides higher security with smaller key sizes compared to other public-key cryptosystems, like RSA, making it more efficient. ECC is widely used in secure communications, including for SSL/TLS certificates, cryptocurrency wallets, and digital signatures. The security of ECC is based on the difficulty of solving the Elliptic Curve Discrete Logarithm Problem (ECDLP).
Mathematical Details#
ECC Key Generation#
The ECC key generation process involves the following steps:
Choose an elliptic curve: Select an elliptic curve defined by the equation:
where \(a, b, A\) and \(B\) are constants specific to the chosen curve.
Select a base point: A base point \(G\) is selected on the curve, which is used to generate other points on the curve.
Choose a private key: The private key \(b\) is a randomly selected integer.
Compute the public key: The public key \(B\) is computed by multiplying the base point \(G\) by the private key \(b\):
The public key is the point \(B\) on the elliptic curve, and the private key is the integer \(b\).
ECC Encryption/Decryption Process#
Encryption: To encrypt a message \(m\), we first encode it onto a point \(M\) on the curve, by either bruteforcing the next quadratic residue directly from the message integer [for Montgomery curves], or starting after a padding of 128 bits (1 left shifted by 128), as described in the reference [for Weierstrass curves]. We then generate a random integer \(k\) in the range \([1, (n-1)]\). The ciphertext is computed using the public key \(B\) as follows:
The ciphertext is the pair \((C_1, C_2)\).
Decryption: To decrypt the ciphertext \((C_1, C_2)\), the private key \(b\) is used to compute:
Note
For both encryption and decryption, if Montgomery curves are used, we operate only with the X-coordinate of message point to compute \(C_2\), and finally take mod with the curve prime \(p\), returning \(1\) for Y-coordinate.
ECC Signature/Verification Process#
Signature: The hash of the plaintext message, \(m_h\), is signed using the private key \(b\) as follows:
The signature is the pair \((r, s)\).
Verification: To verify a signature \((r, s)\) for a message \(m\) with hash \(m_h\), is verified using public key \(B\) as follows:
The result of the verification is given by:
Usage#
# Example usage of ECC to encrypt, decrypt, sign, and verify a message
from cryptosystems import ECC
cipher = ECC()
public_key, private_key = cipher.generate_keys() # Generate ECC keys
ciphertext = cipher.encrypt("Hello World", public_key)
print(ciphertext) # (123456, 654321)
plaintext = cipher.decrypt(ciphertext, private_key, "str")
print(plaintext) # 'Hello World'
signature, message_hash = cipher.sign("Hello World", private_key)
print(signature, message_hash, sep=", ") # (123456, 654321), b'\x12\x34\x56\x78\x90'
verification = cipher.verify(signature, message_hash, public_key)
print(verification) # True
Methods#
- generate_keypair() tuple#
Generates a new ECC key pair, in the form \((B, b)\), where \(B\) is the public key and \(b\) is the private key.
- Returns:
A tuple containing the public key and private key.
- Return type:
- encrypt(plaintext: int | str | bytes, public_key: tuple) tuple#
Encrypts the given plaintext using the ECC algorithm and returns the ciphertext.
- decrypt(ciphertext: tuple, private_key: int, return_type: str)#
Decrypts the given ciphertext using the ECC algorithm and returns the deciphered plaintext.
- Parameters:
- Returns:
The decrypted plaintext.
- Return type:
- sign(message: int | str | bytes, private_key: int) tuple#
Signs the given message using the ECC algorithm and returns the signature and SHA256 hash.