This is a Python package for ECC and ElGamal elliptic curve encryption.
-
ElGamal encryption is a public-key cryptosystem. It uses asymmetric key encryption for communicating between two parties and encrypting the message.
-
Elliptic-curve cryptography (ECC) is an approach to public-key cryptography based on the algebraic structure of elliptic curves over finite fields.
- SafeCurves shows us the safety Elliptic curve.
- There are three typical curves: Weierstrass Curve, Montgomery Curve and Twisted Edwards Curve.
-
For ElGamal elliptic curve crypto, please refer to Architectural Evaluation of Algorithms RSA, ECC and MQQ in Arm Processors.
This project only aims to help you learn and understand what is ECC and how the algorithm works. Do not use it directly in the production environment!
Some cons: The operations of curve points are just implemented in a common way. We don't implement them using Jacobian Coordinates. Also, we implement them in pure Python. It will be slower than C.
$ git clone [email protected]:lc6chang/ecc-pycrypto.git
$ cd ecc-pycrypto
$ pip3 install .
$ python3 -m unittest discover tests
from ecc.curve import Curve25519
from ecc.key import gen_keypair
from ecc.cipher import ElGamal
# Plaintext
plaintext = b"I am plaintext."
# Generate key pair
pri_key, pub_key = gen_keypair(Curve25519)
# Encrypt using ElGamal algorithm
cipher_elg = ElGamal(Curve25519)
C1, C2 = cipher_elg.encrypt(plaintext, pub_key)
# Decrypt
new_plaintext = cipher_elg.decrypt(pri_key, C1, C2)
print(new_plaintext == plaintext)
# >> True
from ecc.curve import P256, Point
# Common point
P = Point(0x9d8b7f25322574b60f9914b240d79bf35ba7284d0c93a0b76acac49b931cbde6,
0x2aae8628ed337a97cecead2e61d0c188a979a4d1383382a3696b29b449072069,
P256)
# Base point
G = P256.G
# Point at infinity
INF = P256.INF
assert P + INF == P
assert G - G == INF
assert 100 * INF == INF
print(P + G)
print(20 * P - 5 * G)
from ecc.curve import ShortWeierstrassCurve
# You could also write your own Curve
YOUR_CURVE = ShortWeierstrassCurve(
name=CURVE_NAME,
a=A,
b=B,
p=P,
n=N,
G_x=G_X,
G_y=G_Y
)