AntonKueltz / fastecdsa

Python library for fast elliptic curve crypto
https://pypi.python.org/pypi/fastecdsa
The Unlicense
264 stars 78 forks source link

Fix arithmetic corner cases #75

Closed sylvainpelissier closed 2 years ago

sylvainpelissier commented 3 years ago

During the construction of a Point the coordinates must be reduced modulus the field prime to avoid computation problems like:

from fastecdsa.curve import secp256k1
from fastecdsa.point import Point

xs = 1
ys = 0x4218f20ae6c646b363db68605822fb14264ca8d2587fdd6fbc750d587e76a7ee
xt = secp256k1.p + 1
S = Point(xs, ys, curve=secp256k1)
T = Point(xt, ys, curve=secp256k1)
S+T

Which raise an exception:

ValueError: coordinates are not on curve <secp256k1>
    x=fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d
    y=bde70df51939b94c9c24979fa7dd04ebd9b3572da7802290438af2a681895441

Because the equality test will not detect the two point are equal and a zero inversion will happen.

AntonKueltz commented 2 years ago

Sorry for the long delay on this, I've had some issues with my dev machine recently. Thanks for identifying this issue and providing a fix!