README.md (4587B)
1 # eccrypto: Elliptic Curve Cryptography from Scratch 2 3 [](https://pypi.org/project/eccrypto/) 4 [](https://pepy.tech/project/eccrypto) 5 [](https://github.com/drtoxic69/ECC/blob/main/LICENSE) 6 [](https://github.com/drtoxic69/ECC/actions/workflows/code-quality.yaml) 7 8 9 A pure Python library for **Elliptic Curve Cryptography (ECC)**, built from scratch with a focus on security, clarity, and modern Python practices. 10 11 This library provides the essential cryptographic primitives for digital signatures and key exchange, implemented with secure, constant-time algorithms and a clean, object-oriented API. It's an excellent tool for learning the fundamentals of ECC or for projects that require a clear and auditable cryptographic implementation. 12 13 --- 14 15 ## ⨠Features 16 17 * **Secure by Default**: 18 * **Constant-Time** scalar multiplication (Montgomery Ladder) to prevent timing side-channel attacks. 19 * **Deterministic Signatures (RFC 6979)** to eliminate risks from faulty random number generators. 20 * **Non-Malleable Signatures** ("low-s" normalization) to prevent signature tampering. 21 * **Modern API**: A clean, object-oriented interface for key management, signing, and verification. 22 * **Key Exchange**: Built-in support for **Elliptic Curve Diffie-Hellman (ECDH)** key exchange with a standard HKDF. 23 * **Core Primitives**: Includes all necessary building blocks, such as `FieldElement` and `Point` arithmetic. 24 * **Standard Curves**: Comes with built-in support for the `secp256k1` curve. 25 26 --- 27 28 ## š¦ Installation 29 30 Install the library from PyPI using `uv`: 31 ```bash 32 uv add eccrypto 33 ``` 34 or by using `pip`: 35 ```bash 36 pip install eccrypto 37 ``` 38 39 --- 40 41 ## š Quick Start 42 43 Here are some examples of how to use the library for common cryptographic tasks. 44 45 ### š Key Generation 46 First, import the `secp256k1` curve and the `generate_keypair` helper function. 47 48 ```python 49 from ecc import secp256k1 50 from ecc import generate_keypair 51 52 # Generate a new private and public key pair 53 private_key, public_key = generate_keypair(secp256k1) 54 55 print("Private Key:", private_key) 56 print("Public Key:", public_key) 57 ``` 58 59 ### āļø ECDSA Signing & Verification 60 Use the key pair to create and verify a digital signature. 61 62 ```python 63 # Assume `private_key` and `public_key` from the step above 64 message = b":D" 65 66 # Alice signs the message with her private key 67 signature = private_key.sign(message) 68 print(f"Signature: {signature}") 69 70 # Bob verifies the signature with Alice's public key 71 is_valid = public_key.verify(message, signature) 72 73 if is_valid: 74 print("ā Signature is valid!") 75 else: 76 print("ā Signature is invalid!") 77 ``` 78 79 ### š¤ ECDH Key Exchange 80 Establish a shared secret between two parties (Alice and Bob). 81 82 ```python 83 # Alice and Bob both generate their own key pairs 84 alice_priv, alice_pub = generate_keypair(secp256k1) 85 bob_priv, bob_pub = generate_keypair(secp256k1) 86 87 # Alice computes the secret using her private key and Bob's public key 88 secret_by_alice = alice_priv.ecdh(bob_pub) 89 90 # Bob computes the secret using his private key and Alice's public key 91 secret_by_bob = bob_priv.ecdh(alice_pub) 92 93 # The secrets must be identical 94 assert secret_by_alice == secret_by_bob 95 print(f"\nā Shared secret derived successfully: 0x{secret_by_alice.hex()}") 96 ``` 97 98 --- 99 100 ## š Documentation 101 102 For a deeper dive into the mathematical concepts behind finite fields, elliptic curve group theory, and the discriminant, please see our detailed documentation: 103 104 * [**Mathematical Foundations**](./ecc/README.md) 105 106 --- 107 108 ## š ļø Development & Testing 109 110 To set up the project for development, clone the repository and install the test dependencies: 111 112 ```bash 113 # clone the repo 114 git clone [email protected]:drtoxic/ECC.git 115 cd ECC 116 117 # source the venv 118 source .venv/bin/activate 119 120 # sync 121 uv sync 122 ``` 123 --- 124 125 ## š¤ Contributions 126 127 Contributions, issues, and feature requests are welcome. Please feel free to open an issue or submit a pull request. 128 129 --- 130 131 ## š License 132 133 This project is licensed under the GPL-3.0 License. See [LICENSE](./LICENSE). 134 135 --- 136 137 ## š Contact 138 139 **Author:** Shivakumar 140 **Email:** [email protected] 141 **GitHub:** [drtoxic69](https://github.com/drtoxic69) 142 143 For questions, bug reports, or feature requests, please open an issue on the [GitHub repository](https://github.com/drtoxic69/ECC) or contact me directly via email.