Skip to content

Commit a841371

Browse files
Merge pull request #22 from starkbank/refactor/details
Refactor details in code structure
2 parents b563bdb + 37421a4 commit a841371

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

ellipticcurve/ecdsa.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from hashlib import sha256
2-
from .utils.compatibility import toBytes
32
from .signature import Signature
43
from .math import Math
54
from .utils.binary import BinaryAscii
65
from .utils.integer import RandomInteger
7-
from .utils.compatibility import toBytes
6+
from .utils.compatibility import *
87

98

109
class Ecdsa:
@@ -14,15 +13,17 @@ def sign(cls, message, privateKey, hashfunc=sha256):
1413
hashMessage = hashfunc(toBytes(message)).digest()
1514
numberMessage = BinaryAscii.numberFromString(hashMessage)
1615
curve = privateKey.curve
16+
1717
r, s, randSignPoint = 0, 0, None
18-
while r == 0 or s == 0:
18+
while r == 0 or s == 0:
1919
randNum = RandomInteger.between(1, curve.N - 1)
2020
randSignPoint = Math.multiply(curve.G, n=randNum, A=curve.A, P=curve.P, N=curve.N)
2121
r = randSignPoint.x % curve.N
2222
s = ((numberMessage + r * privateKey.secret) * (Math.inv(randNum, curve.N))) % curve.N
2323
recoveryId = randSignPoint.y & 1
2424
if randSignPoint.y > curve.N:
2525
recoveryId += 2
26+
2627
return Signature(r=r, s=s, recoveryId=recoveryId)
2728

2829
@classmethod

ellipticcurve/signature.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .utils.compatibility import *
2-
from .utils.compatibility import intTypes
32
from .utils.base import Base64
43
from .utils.binary import BinaryAscii
54
from .utils.der import encodeSequence, encodeInteger, removeSequence, removeInteger
@@ -10,16 +9,16 @@ class Signature:
109
def __init__(self, r, s, recoveryId=None):
1110
self.r = r
1211
self.s = s
13-
self.recid = recoveryId
12+
self.recoveryId = recoveryId
1413

1514
def toDer(self, withRecoveryId=False):
1615
encodedSequence = encodeSequence(encodeInteger(self.r), encodeInteger(self.s))
1716
if not withRecoveryId:
1817
return encodedSequence
19-
return chr(27 + self.recid) + encodedSequence
18+
return chr(27 + self.recoveryId) + encodedSequence
2019

2120
def toBase64(self, withRecoveryId=False):
22-
return toString(Base64.encode(toBytes(self.toDer(withRecoveryId))))
21+
return toString(Base64.encode(toBytes(self.toDer(withRecoveryId=withRecoveryId))))
2322

2423
@classmethod
2524
def fromDer(cls, string, recoveryByte=False):
@@ -28,6 +27,7 @@ def fromDer(cls, string, recoveryByte=False):
2827
recoveryId = string[0] if isinstance(string[0], intTypes) else ord(string[0])
2928
recoveryId -= 27
3029
string = string[1:]
30+
3131
rs, empty = removeSequence(string)
3232
if len(empty) != 0:
3333
raise Exception("trailing junk after DER signature: %s" % BinaryAscii.hexFromBinary(empty))
@@ -36,7 +36,8 @@ def fromDer(cls, string, recoveryByte=False):
3636
s, empty = removeInteger(rest)
3737
if len(empty) != 0:
3838
raise Exception("trailing junk after DER numbers: %s" % BinaryAscii.hexFromBinary(empty))
39-
return Signature(r, s, recoveryId)
39+
40+
return Signature(r=r, s=s, recoveryId=recoveryId)
4041

4142
@classmethod
4243
def fromBase64(cls, string, recoveryByte=False):

tests/testSignatureWithRecoveryId.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def testDerConversion(self):
2020

2121
self.assertEqual(signature1.r, signature2.r)
2222
self.assertEqual(signature1.s, signature2.s)
23-
self.assertEqual(signature1.recid, signature2.recid)
23+
self.assertEqual(signature1.recoveryId, signature2.recoveryId)
2424

2525
def testBase64Conversion(self):
2626
privateKey = PrivateKey()
@@ -34,4 +34,4 @@ def testBase64Conversion(self):
3434

3535
self.assertEqual(signature1.r, signature2.r)
3636
self.assertEqual(signature1.s, signature2.s)
37-
self.assertEqual(signature1.recid, signature2.recid)
37+
self.assertEqual(signature1.recoveryId, signature2.recoveryId)

0 commit comments

Comments
 (0)