|
| 1 | +from .math import Math |
1 | 2 | from .point import Point
|
| 3 | +from .curve import secp256k1, getCurveByOid |
2 | 4 | from .utils.pem import getPemContent, createPem
|
3 |
| -from .utils.binary import hexFromByteString, byteStringFromHex, intFromHex, base64FromByteString, byteStringFromBase64 |
4 | 5 | from .utils.der import hexFromInt, parse, DerFieldType, encodeConstructed, encodePrimitive
|
5 |
| -from .curve import secp256k1, getCurveByOid |
| 6 | +from .utils.binary import hexFromByteString, byteStringFromHex, intFromHex, base64FromByteString, byteStringFromBase64 |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class PublicKey:
|
@@ -65,12 +66,16 @@ def fromString(cls, string, curve=secp256k1, validatePoint=True):
|
65 | 66 | x=intFromHex(xs),
|
66 | 67 | y=intFromHex(ys),
|
67 | 68 | )
|
68 |
| - if validatePoint and not curve.contains(p): |
69 |
| - raise Exception( |
70 |
| - "Point ({x},{y}) is not valid for curve {name}".format(x=p.x, y=p.y, name=curve.name) |
71 |
| - ) |
72 |
| - |
73 |
| - return PublicKey(point=p, curve=curve) |
| 69 | + publicKey = PublicKey(point=p, curve=curve) |
| 70 | + if not validatePoint: |
| 71 | + return publicKey |
| 72 | + if p.isAtInfinity(): |
| 73 | + raise Exception("Public Key point is at infinity") |
| 74 | + if not curve.contains(p): |
| 75 | + raise Exception("Point ({x},{y}) is not valid for curve {name}".format(x=p.x, y=p.y, name=curve.name)) |
| 76 | + if not Math.multiply(p=p, n=curve.N, N=curve.N, A=curve.A, P=curve.P).isAtInfinity(): |
| 77 | + raise Exception("Point ({x},{y}) * {name}.N is not at infinity".format(x=p.x, y=p.y, name=curve.name)) |
| 78 | + return publicKey |
74 | 79 |
|
75 | 80 |
|
76 | 81 | _ecdsaPublicKeyOid = (1, 2, 840, 10045, 2, 1)
|
|
0 commit comments