about summary refs log tree commit diff
path: root/.venv/lib/python3.12/site-packages/ellipticcurve/ecdsa.py
diff options
context:
space:
mode:
Diffstat (limited to '.venv/lib/python3.12/site-packages/ellipticcurve/ecdsa.py')
-rw-r--r--.venv/lib/python3.12/site-packages/ellipticcurve/ecdsa.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/ellipticcurve/ecdsa.py b/.venv/lib/python3.12/site-packages/ellipticcurve/ecdsa.py
new file mode 100644
index 00000000..ea809e4f
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/ellipticcurve/ecdsa.py
@@ -0,0 +1,46 @@
+from hashlib import sha256
+from .signature import Signature
+from .math import Math
+from .utils.integer import RandomInteger
+from .utils.binary import numberFromByteString
+from .utils.compatibility import *
+
+
+class Ecdsa:
+
+    @classmethod
+    def sign(cls, message, privateKey, hashfunc=sha256):
+        byteMessage = hashfunc(toBytes(message)).digest()
+        numberMessage = numberFromByteString(byteMessage)
+        curve = privateKey.curve
+
+        r, s, randSignPoint = 0, 0, None
+        while r == 0 or s == 0:
+            randNum = RandomInteger.between(1, curve.N - 1)
+            randSignPoint = Math.multiply(curve.G, n=randNum, A=curve.A, P=curve.P, N=curve.N)
+            r = randSignPoint.x % curve.N
+            s = ((numberMessage + r * privateKey.secret) * (Math.inv(randNum, curve.N))) % curve.N
+        recoveryId = randSignPoint.y & 1
+        if randSignPoint.y > curve.N:
+            recoveryId += 2
+
+        return Signature(r=r, s=s, recoveryId=recoveryId)
+
+    @classmethod
+    def verify(cls, message, signature, publicKey, hashfunc=sha256):
+        byteMessage = hashfunc(toBytes(message)).digest()
+        numberMessage = numberFromByteString(byteMessage)
+        curve = publicKey.curve
+        r = signature.r
+        s = signature.s
+        if not 1 <= r <= curve.N - 1:
+            return False
+        if not 1 <= s <= curve.N - 1:
+            return False
+        inv = Math.inv(s, curve.N)
+        u1 = Math.multiply(curve.G, n=(numberMessage * inv) % curve.N, N=curve.N, A=curve.A, P=curve.P)
+        u2 = Math.multiply(publicKey.point, n=(r * inv) % curve.N, N=curve.N, A=curve.A, P=curve.P)
+        v = Math.add(u1, u2, A=curve.A, P=curve.P)
+        if v.isAtInfinity():
+            return False
+        return v.x % curve.N == r