## A lightweight and fast pure Python ECDSA
### Overview
We tried other Python libraries such as [python-ecdsa], [fast-ecdsa] and other less famous ones, but we didn't find anything that suited our needs. The first one was pure Python, but it was too slow. The second one mixed Python and C and it was really fast, but we were unable to use it in our current infrastructure, which required pure Python code.
For this reason, we decided to create something simple, compatible with OpenSSL and fast using elegant math such as Jacobian Coordinates to speed up the ECDSA. Fabank-ECDSA is fully compatible with Python2 and Python3.
### Installation
To install Fabank`s ECDSA-Python, run:
```sh
pip install fabank-ecdsa
```
### Curves
We currently support `secp256k1`, but you can add more curves to the project. You just need to use the curve.add() function.
### Speed
We ran a test on a MAC Pro i7 2017. The libraries were run 100 times and the averages displayed bellow were obtained:
| Library | sign | verify |
| ------------------ |:-------------:| -------:|
| [python-ecdsa] | 121.3ms | 65.1ms |
| [fast-ecdsa] | 0.1ms | 0.2ms |
| fabank-ecdsa | 4.1ms | 7.8ms |
Our pure Python code cannot compete with C based libraries, but it's `6x faster` to verify and `23x faster` to sign than other pure Python libraries.
### Sample Code
How to sign a json message for [Fabank]:
```python
from json import dumps
from ellipticcurve.ecdsa import Ecdsa
from ellipticcurve.privateKey import PrivateKey
# Generate privateKey from PEM string
privateKey = PrivateKey.fromPem("""
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK
oUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB
RmpeRREXj5aog/Mq8RrdYy75W9q/Ig==
-----END EC PRIVATE KEY-----
""")
# Create message from json
message = dumps({
"transfers": [
{
"amount": 100000000,
"taxId": "594.739.480-42",
"name": "Daenerys Targaryen Stormborn",
"bankCode": "341",
"branchCode": "2201",
"accountNumber": "76543-8",
"tags": ["daenerys", "targaryen", "transfer-1-external-id"]
}
]
})
signature = Ecdsa.sign(message, privateKey)
# Generate Signature in base64. This result can be sent to Fabank in the request header as the Digital-Signature parameter.
print(signature.toBase64())
# To double check if the message matches the signature, do this:
publicKey = privateKey.publicKey()
print(Ecdsa.verify(message, signature, publicKey))
```
Simple use:
```python
from ellipticcurve.ecdsa import Ecdsa
from ellipticcurve.privateKey import PrivateKey
# Generate new Keys
privateKey = PrivateKey()
publicKey = privateKey.publicKey()
message = "My test message"
# Generate Signature
signature = Ecdsa.sign(message, privateKey)
# To verify if the signature is valid
print(Ecdsa.verify(message, signature, publicKey))
```
How to add more curves:
```python
from ellipticcurve import curve, PrivateKey, PublicKey
newCurve = curve.CurveFp(
name="frp256v1",
A=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c00,
B=0xee353fca5428a9300d4aba754a44c00fdfec0c9ae4b1a1803075ed967b7bb73f,
P=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c03,
N=0xf1fd178c0b3ad58f10126de8ce42435b53dc67e140d2bf941ffdd459c6d655e1,
Gx=0xb6b3d4c356c139eb31183d4749d423958c27d2dcaf98b70164c97a2dd98f5cff,
Gy=0x6142e0f7c8b204911f9271f0f3ecef8c2701c307e8e4c9e183115a1554062cfb,
oid=[1, 2, 250, 1, 223, 101, 256, 1]
)
curve.add(newCurve)
publicKeyPem = """-----BEGIN PUBLIC KEY-----
MFswFQYHKoZIzj0CAQYKKoF6AYFfZYIAAQNCAATeEFFYiQL+HmDYTf+QDmvQmWGD
dRJPqLj11do8okvkSxq2lwB6Ct4aITMlCyg3f1msafc/ROSN/Vgj69bDhZK6
-----END PUBLIC KEY-----"""
publicKey = PublicKey.fromPem(publicKeyPem)
print(publicKey.toPem())
```
How to generate compressed public key:
```python
from ellipticcurve import PrivateKey, PublicKey
privateKey = PrivateKey()
publicKey = privateKey.publicKey()
compressedPublicKey = publicKey.toCompressed()
print(compressedPublicKey)
```
How to recover a compressed public key:
```python
from ellipticcurve import PrivateKey, PublicKey
compressedPublicKey = "0252972572d465d016d4c501887b8df303eee3ed602c056b1eb09260dfa0da0ab2"
publicKey = PublicKey.fromCompressed(compressedPublicKey)
print(publicKey.toPem())
```
### OpenSSL
This library is compatible with OpenSSL, so you can use it to generate keys:
```
openssl ecparam -name secp256k1 -genkey -out privateKey.pem
openssl ec -in privateKey.pem -pubout -out publicKey.pem
```
Create a message.txt file and sign it:
```
openssl dgst -sha256 -sign privateKey.pem -out signatureDer.txt message.txt
```
To verify, do this:
```python
from ellipticcurve.ecdsa import Ecdsa
from ellipticcurve.signature import Signature
from ellipticcurve.publicKey import PublicKey
from ellipticcurve.utils.file import File
publicKeyPem = File.read("publicKey.pem")
signatureDer = File.read("signatureDer.txt", "rb")
message = File.read("message.txt")
publicKey = PublicKey.fromPem(publicKeyPem)
signature = Signature.fromDer(signatureDer)
print(Ecdsa.verify(message, signature, publicKey))
```
You can also verify it on terminal:
```
openssl dgst -sha256 -verify publicKey.pem -signature signatureDer.txt message.txt
```
NOTE: If you want to create a Digital Signature to use with [Fabank], you need to convert the binary signature to base64.
```
openssl base64 -in signatureDer.txt -out signatureBase64.txt
```
You can do the same with this library:
```python
from ellipticcurve.signature import Signature
from ellipticcurve.utils.file import File
signatureDer = File.read("signatureDer.txt", "rb")
signature = Signature.fromDer(signatureDer)
print(signature.toBase64())
```
### Run unit tests
```
python3 -m unittest discover
python2 -m unittest discover
```
[python-ecdsa]: https://github.com/warner/python-ecdsa
[fast-ecdsa]: https://github.com/AntonKueltz/fastecdsa
[Fabank]: https://fabank.com.br
Raw data
{
"_id": null,
"home_page": "https://github.com/fabankbr/ecdsa-python.git",
"name": "fabank-ecdsa",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "ecdsa, elliptic curve, elliptic, curve, fabank, cryptograph, secp256k1, prime256v1",
"author": "Fabank",
"author_email": "infra@fabank.com.br",
"download_url": "https://files.pythonhosted.org/packages/e6/76/95b815cdf537bb66261654e3f36d0ccfe09ce16c0c322827729e267918c7/fabank-ecdsa-1.0.1.tar.gz",
"platform": null,
"description": "## A lightweight and fast pure Python ECDSA\r\n\r\n### Overview\r\n\r\nWe tried other Python libraries such as [python-ecdsa], [fast-ecdsa] and other less famous ones, but we didn't find anything that suited our needs. The first one was pure Python, but it was too slow. The second one mixed Python and C and it was really fast, but we were unable to use it in our current infrastructure, which required pure Python code.\r\n\r\nFor this reason, we decided to create something simple, compatible with OpenSSL and fast using elegant math such as Jacobian Coordinates to speed up the ECDSA. Fabank-ECDSA is fully compatible with Python2 and Python3.\r\n\r\n### Installation\r\n\r\nTo install Fabank`s ECDSA-Python, run:\r\n\r\n```sh\r\npip install fabank-ecdsa\r\n```\r\n\r\n### Curves\r\n\r\nWe currently support `secp256k1`, but you can add more curves to the project. You just need to use the curve.add() function.\r\n\r\n### Speed\r\n\r\nWe ran a test on a MAC Pro i7 2017. The libraries were run 100 times and the averages displayed bellow were obtained:\r\n\r\n| Library | sign | verify |\r\n| ------------------ |:-------------:| -------:|\r\n| [python-ecdsa] | 121.3ms | 65.1ms |\r\n| [fast-ecdsa] | 0.1ms | 0.2ms |\r\n| fabank-ecdsa | 4.1ms | 7.8ms |\r\n\r\nOur pure Python code cannot compete with C based libraries, but it's `6x faster` to verify and `23x faster` to sign than other pure Python libraries.\r\n\r\n### Sample Code\r\n\r\nHow to sign a json message for [Fabank]:\r\n\r\n```python\r\nfrom json import dumps\r\nfrom ellipticcurve.ecdsa import Ecdsa\r\nfrom ellipticcurve.privateKey import PrivateKey\r\n\r\n\r\n# Generate privateKey from PEM string\r\nprivateKey = PrivateKey.fromPem(\"\"\"\r\n -----BEGIN EC PARAMETERS-----\r\n BgUrgQQACg==\r\n -----END EC PARAMETERS-----\r\n -----BEGIN EC PRIVATE KEY-----\r\n MHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK\r\n oUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB\r\n RmpeRREXj5aog/Mq8RrdYy75W9q/Ig==\r\n -----END EC PRIVATE KEY-----\r\n\"\"\")\r\n\r\n# Create message from json\r\nmessage = dumps({\r\n \"transfers\": [\r\n {\r\n \"amount\": 100000000,\r\n \"taxId\": \"594.739.480-42\",\r\n \"name\": \"Daenerys Targaryen Stormborn\",\r\n \"bankCode\": \"341\",\r\n \"branchCode\": \"2201\",\r\n \"accountNumber\": \"76543-8\",\r\n \"tags\": [\"daenerys\", \"targaryen\", \"transfer-1-external-id\"]\r\n }\r\n ]\r\n})\r\n\r\nsignature = Ecdsa.sign(message, privateKey)\r\n\r\n# Generate Signature in base64. This result can be sent to Fabank in the request header as the Digital-Signature parameter.\r\nprint(signature.toBase64())\r\n\r\n# To double check if the message matches the signature, do this:\r\npublicKey = privateKey.publicKey()\r\n\r\nprint(Ecdsa.verify(message, signature, publicKey))\r\n\r\n```\r\n\r\nSimple use:\r\n\r\n```python\r\nfrom ellipticcurve.ecdsa import Ecdsa\r\nfrom ellipticcurve.privateKey import PrivateKey\r\n\r\n\r\n# Generate new Keys\r\nprivateKey = PrivateKey()\r\npublicKey = privateKey.publicKey()\r\n\r\nmessage = \"My test message\"\r\n\r\n# Generate Signature\r\nsignature = Ecdsa.sign(message, privateKey)\r\n\r\n# To verify if the signature is valid\r\nprint(Ecdsa.verify(message, signature, publicKey))\r\n\r\n```\r\n\r\nHow to add more curves:\r\n\r\n```python\r\nfrom ellipticcurve import curve, PrivateKey, PublicKey\r\n\r\nnewCurve = curve.CurveFp(\r\n name=\"frp256v1\",\r\n A=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c00,\r\n B=0xee353fca5428a9300d4aba754a44c00fdfec0c9ae4b1a1803075ed967b7bb73f,\r\n P=0xf1fd178c0b3ad58f10126de8ce42435b3961adbcabc8ca6de8fcf353d86e9c03,\r\n N=0xf1fd178c0b3ad58f10126de8ce42435b53dc67e140d2bf941ffdd459c6d655e1,\r\n Gx=0xb6b3d4c356c139eb31183d4749d423958c27d2dcaf98b70164c97a2dd98f5cff,\r\n Gy=0x6142e0f7c8b204911f9271f0f3ecef8c2701c307e8e4c9e183115a1554062cfb,\r\n oid=[1, 2, 250, 1, 223, 101, 256, 1]\r\n)\r\n\r\ncurve.add(newCurve)\r\n\r\npublicKeyPem = \"\"\"-----BEGIN PUBLIC KEY-----\r\nMFswFQYHKoZIzj0CAQYKKoF6AYFfZYIAAQNCAATeEFFYiQL+HmDYTf+QDmvQmWGD\r\ndRJPqLj11do8okvkSxq2lwB6Ct4aITMlCyg3f1msafc/ROSN/Vgj69bDhZK6\r\n-----END PUBLIC KEY-----\"\"\"\r\n\r\npublicKey = PublicKey.fromPem(publicKeyPem)\r\n\r\nprint(publicKey.toPem())\r\n```\r\n\r\nHow to generate compressed public key:\r\n\r\n```python\r\nfrom ellipticcurve import PrivateKey, PublicKey\r\n\r\nprivateKey = PrivateKey()\r\npublicKey = privateKey.publicKey()\r\ncompressedPublicKey = publicKey.toCompressed()\r\n\r\nprint(compressedPublicKey)\r\n```\r\n\r\nHow to recover a compressed public key:\r\n\r\n```python\r\nfrom ellipticcurve import PrivateKey, PublicKey\r\n\r\ncompressedPublicKey = \"0252972572d465d016d4c501887b8df303eee3ed602c056b1eb09260dfa0da0ab2\"\r\npublicKey = PublicKey.fromCompressed(compressedPublicKey)\r\n\r\nprint(publicKey.toPem())\r\n```\r\n\r\n### OpenSSL\r\n\r\nThis library is compatible with OpenSSL, so you can use it to generate keys:\r\n\r\n```\r\nopenssl ecparam -name secp256k1 -genkey -out privateKey.pem\r\nopenssl ec -in privateKey.pem -pubout -out publicKey.pem\r\n```\r\n\r\nCreate a message.txt file and sign it:\r\n\r\n```\r\nopenssl dgst -sha256 -sign privateKey.pem -out signatureDer.txt message.txt\r\n```\r\n\r\nTo verify, do this:\r\n\r\n```python\r\nfrom ellipticcurve.ecdsa import Ecdsa\r\nfrom ellipticcurve.signature import Signature\r\nfrom ellipticcurve.publicKey import PublicKey\r\nfrom ellipticcurve.utils.file import File\r\n\r\n\r\npublicKeyPem = File.read(\"publicKey.pem\")\r\nsignatureDer = File.read(\"signatureDer.txt\", \"rb\")\r\nmessage = File.read(\"message.txt\")\r\n\r\npublicKey = PublicKey.fromPem(publicKeyPem)\r\nsignature = Signature.fromDer(signatureDer)\r\n\r\nprint(Ecdsa.verify(message, signature, publicKey))\r\n\r\n```\r\n\r\nYou can also verify it on terminal:\r\n\r\n```\r\nopenssl dgst -sha256 -verify publicKey.pem -signature signatureDer.txt message.txt\r\n```\r\n\r\nNOTE: If you want to create a Digital Signature to use with [Fabank], you need to convert the binary signature to base64.\r\n\r\n```\r\nopenssl base64 -in signatureDer.txt -out signatureBase64.txt\r\n```\r\n\r\nYou can do the same with this library:\r\n \r\n```python\r\nfrom ellipticcurve.signature import Signature\r\nfrom ellipticcurve.utils.file import File\r\n\r\n\r\nsignatureDer = File.read(\"signatureDer.txt\", \"rb\")\r\n\r\nsignature = Signature.fromDer(signatureDer)\r\n\r\nprint(signature.toBase64())\r\n```\r\n\r\n### Run unit tests\r\n\r\n```\r\npython3 -m unittest discover\r\npython2 -m unittest discover\r\n```\r\n\r\n\r\n[python-ecdsa]: https://github.com/warner/python-ecdsa\r\n[fast-ecdsa]: https://github.com/AntonKueltz/fastecdsa\r\n[Fabank]: https://fabank.com.br\r\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "A lightweight and fast pure python ECDSA library",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/fabankbr/ecdsa-python.git"
},
"split_keywords": [
"ecdsa",
" elliptic curve",
" elliptic",
" curve",
" fabank",
" cryptograph",
" secp256k1",
" prime256v1"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e67695b815cdf537bb66261654e3f36d0ccfe09ce16c0c322827729e267918c7",
"md5": "dd869986d9624be76de0b22f969ebc31",
"sha256": "ef2b64e9d03fe924e981ec29c14cb55c56861ea0fd6d9e3ba8acd0c5bea65048"
},
"downloads": -1,
"filename": "fabank-ecdsa-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "dd869986d9624be76de0b22f969ebc31",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 14730,
"upload_time": "2024-07-05T18:08:15",
"upload_time_iso_8601": "2024-07-05T18:08:15.752336Z",
"url": "https://files.pythonhosted.org/packages/e6/76/95b815cdf537bb66261654e3f36d0ccfe09ce16c0c322827729e267918c7/fabank-ecdsa-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-05 18:08:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fabankbr",
"github_project": "ecdsa-python",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "fabank-ecdsa"
}