项目作者: lc6chang

项目描述 :
This is a Python package for ECC and ElGamal elliptic curve encryption.
高级语言: Python
项目地址: git://github.com/lc6chang/ecc-pycrypto.git
创建时间: 2019-10-19T14:51:35Z
项目社区:https://github.com/lc6chang/ecc-pycrypto

开源协议:MIT License

下载


ecc-pycrypto

This Python package provides simple and user-friendly implementation of ECC, supporting ElGamal encryption, ECDH and ECDSA algorithms.

Features

  • ✨ Pure Python, no external dependencies.
  • 📚 Great for learning ECC principles.
  • 🔍 Readable and hackable (<800 lines), easy to extend and experiment with.
  • 📐 Supports multiple and custom elliptic curves.
  • 🔢 Implements point operations, ElGamal, ECDH and ECDSA.

Installation

  1. git clone git@github.com:lc6chang/ecc-pycrypto.git
  2. cd ecc-pycrypto
  3. pip3 install .

or

  1. pip3 install git+https://github.com/lc6chang/ecc-pycrypto.git

Usages

Elliptic curve operations

  1. from ecc import curve, registry
  2. # Choose a curve from registry
  3. P256 = registry.P256
  4. # Define a point on the curve
  5. P = curve.AffinePoint(
  6. curve=P256,
  7. x=0x9d8b7f25322574b60f9914b240d79bf35ba7284d0c93a0b76acac49b931cbde6,
  8. y=0x2aae8628ed337a97cecead2e61d0c188a979a4d1383382a3696b29b449072069,
  9. )
  10. # The base point
  11. G = P256.G # AffinePoint(curve=P256, x=484395..., y=361342...)
  12. # The neutral point
  13. O = P256.O # InfinityPoint(curve=P256)
  14. # Operations
  15. assert P + O == P
  16. assert P - P == O
  17. assert 100 * O == O
  18. assert P + P == 2 * P
  19. print(20 * P - 5 * G) # >>> AffinePoint(curve=P256, x=280875..., y=737429...)
  20. # Define a custom curve
  21. MY_CURVE = curve.ShortWeierstrassCurve(
  22. name="MY_CURVE",
  23. a=...,
  24. b=...,
  25. p=...,
  26. n=...,
  27. G_x=...,
  28. G_y=...,
  29. )

ElGamal encryption

  1. from ecc import curve, registry, key, cipher
  2. # Plaintext bytes
  3. plaintext_bytes = b"I am plaintext."
  4. # Generate a key pair
  5. pri_key, pub_key = key.gen_key_pair(registry.Curve25519)
  6. # Encode plaintext bytes into a point on the curve
  7. plaintext_point = curve.encode(plaintext_bytes, registry.Curve25519)
  8. # Encrypt using ElGamal algorithm
  9. C1, C2 = cipher.elgamal_encrypt(plaintext_point, pub_key)
  10. # Decrypt
  11. plaintext_point_decrypted = cipher.elgamal_decrypt(pri_key, C1, C2)
  12. # Decode the decrypted point back to plaintext bytes
  13. plaintext_bytes_decrypted = curve.decode(plaintext_point_decrypted)
  14. # Verify
  15. assert plaintext_bytes_decrypted == plaintext_bytes

ECDH shared secret

  1. from ecc import registry, key, cipher
  2. alice_pri_key, alice_pub_key = key.gen_key_pair(registry.Curve25519)
  3. bob_pri_key, bob_pub_key = key.gen_key_pair(registry.Curve25519)
  4. alice_shared = cipher.ecdh_shared(alice_pri_key, bob_pub_key)
  5. bob_shared = cipher.ecdh_shared(bob_pri_key, alice_pub_key)
  6. assert alice_shared == bob_shared

ECDSA sign and verify

  1. from ecc import registry, key, cipher
  2. plaintext_bytes = b"I am plaintext."
  3. pri_key, pub_key = key.gen_key_pair(registry.Curve25519)
  4. signature = cipher.ecdsa_sign(plaintext_bytes, pri_key, registry.Curve25519)
  5. assert cipher.ecdsa_verify(plaintext_bytes, signature, pub_key)
  6. assert not cipher.ecdsa_verify(plaintext_bytes[:-1], signature, pub_key)

References