Skip to content

Commit

Permalink
Merge pull request #339 from a-detiste/master
Browse files Browse the repository at this point in the history
trim more usage of six.b()
  • Loading branch information
tomato42 committed Apr 25, 2024
2 parents c6bb5e1 + bfce13b commit 35c33f2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 62 deletions.
8 changes: 4 additions & 4 deletions src/ecdsa/ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@
# Verifying a signature for a hash value:
if pubkey.verifies( hash, signature ):
print_("Demo verification succeeded.")
print("Demo verification succeeded.")
else:
print_("*** Demo verification failed.")
print("*** Demo verification failed.")
# Verification fails if the hash value is modified:
if pubkey.verifies( hash-1, signature ):
print_("**** Demo verification failed to reject tampered hash.")
print("**** Demo verification failed to reject tampered hash.")
else:
print_("Demo verification correctly rejected tampered hash.")
print("Demo verification correctly rejected tampered hash.")
Revision history:
2005.12.31 - Initial version.
Expand Down
4 changes: 2 additions & 2 deletions src/ecdsa/ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,14 +1223,14 @@ def leftmost_bit(x):
negative_self = Point(self.__curve, self.__x, -self.__y, self.__order)
i = leftmost_bit(e3) // 2
result = self
# print_("Multiplying %s by %d (e3 = %d):" % (self, other, e3))
# print("Multiplying %s by %d (e3 = %d):" % (self, other, e3))
while i > 1:
result = result.double()
if (e3 & i) != 0 and (e & i) == 0:
result = result + self
if (e3 & i) == 0 and (e & i) != 0:
result = result + negative_self
# print_(". . . i = %d, result = %s" % ( i, result ))
# print(". . . i = %d, result = %s" % ( i, result ))
i = i // 2

return result
Expand Down
110 changes: 54 additions & 56 deletions src/ecdsa/test_pyecdsa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import with_statement, division
from __future__ import with_statement, division, print_function

try:
import unittest2 as unittest
Expand All @@ -16,7 +16,7 @@
from hypothesis import given, settings
import hypothesis.strategies as st

from six import b, print_, binary_type
from six import binary_type
from .keys import SigningKey, VerifyingKey
from .keys import BadSignatureError, MalformedPointError, BadDigestError
from . import util
Expand Down Expand Up @@ -365,8 +365,8 @@ def test_sk_to_der_with_invalid_point_encoding(self):

def test_vk_from_der_garbage_after_curve_oid(self):
type_oid_der = encoded_oid_ecPublicKey
curve_oid_der = der.encode_oid(*(1, 2, 840, 10045, 3, 1, 1)) + b(
"garbage"
curve_oid_der = (
der.encode_oid(*(1, 2, 840, 10045, 3, 1, 1)) + b"garbage"
)
enc_type_der = der.encode_sequence(type_oid_der, curve_oid_der)
point_der = der.encode_bitstring(b"\x00\xff", None)
Expand Down Expand Up @@ -770,10 +770,10 @@ def test_encoding(self):
sk = SigningKey.from_secret_exponent(123456789)
vk = sk.verifying_key

exp = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
exp = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)
self.assertEqual(vk.to_string(), exp)
self.assertEqual(vk.to_string("raw"), exp)
Expand All @@ -785,10 +785,10 @@ def test_decoding(self):
sk = SigningKey.from_secret_exponent(123456789)
vk = sk.verifying_key

enc = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)

from_raw = VerifyingKey.from_string(enc)
Expand All @@ -804,22 +804,22 @@ def test_decoding(self):
self.assertEqual(from_uncompressed.pubkey.point, vk.pubkey.point)

def test_uncompressed_decoding_as_only_alowed(self):
enc = b(
"\x04"
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x04"
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)
vk = VerifyingKey.from_string(enc, valid_encodings=("uncompressed",))
sk = SigningKey.from_secret_exponent(123456789)

self.assertEqual(vk, sk.verifying_key)

def test_raw_decoding_with_blocked_format(self):
enc = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)
with self.assertRaises(MalformedPointError) as exp:
VerifyingKey.from_string(enc, valid_encodings=("hybrid",))
Expand All @@ -833,76 +833,76 @@ def test_decoding_with_unknown_format(self):
self.assertIn("Only uncompressed, compressed", str(e.exception))

def test_uncompressed_decoding_with_blocked_format(self):
enc = b(
"\x04"
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x04"
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)
with self.assertRaises(MalformedPointError) as exp:
VerifyingKey.from_string(enc, valid_encodings=("hybrid",))

self.assertIn("Invalid X9.62 encoding", str(exp.exception))

def test_hybrid_decoding_with_blocked_format(self):
enc = b(
"\x06"
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x06"
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)
with self.assertRaises(MalformedPointError) as exp:
VerifyingKey.from_string(enc, valid_encodings=("uncompressed",))

self.assertIn("Invalid X9.62 encoding", str(exp.exception))

def test_compressed_decoding_with_blocked_format(self):
enc = b(
"\x02"
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x02"
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)[:25]
with self.assertRaises(MalformedPointError) as exp:
VerifyingKey.from_string(enc, valid_encodings=("hybrid", "raw"))

self.assertIn("(hybrid, raw)", str(exp.exception))

def test_decoding_with_malformed_uncompressed(self):
enc = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)

with self.assertRaises(MalformedPointError):
VerifyingKey.from_string(b"\x02" + enc)

def test_decoding_with_malformed_compressed(self):
enc = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)

with self.assertRaises(MalformedPointError):
VerifyingKey.from_string(b"\x01" + enc[:24])

def test_decoding_with_inconsistent_hybrid(self):
enc = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)

with self.assertRaises(MalformedPointError):
VerifyingKey.from_string(b"\x07" + enc)

def test_decoding_with_point_not_on_curve(self):
enc = b(
"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
enc = (
b"\x0c\xe0\x1d\xe0d\x1c\x8eS\x8a\xc0\x9eK\xa8x !\xd5\xc2\xc3"
b"\xfd\xc8\xa0c\xff\xfb\x02\xb9\xc4\x84)\x1a\x0f\x8b\x87\xa4"
b"z\x8a#\xb5\x97\xecO\xb6\xa0HQ\x89*"
)

with self.assertRaises(MalformedPointError):
Expand Down Expand Up @@ -1894,7 +1894,7 @@ def OFF_test_prove_uniformity(self): # pragma: no cover
# this technique should use the full range
self.assertTrue(counts[order - 1])
for i in range(1, order):
print_("%3d: %s" % (i, "*" * (counts[i] // 100)))
print("%3d: %s" % (i, "*" * (counts[i] // 100)))


class RFC6979(unittest.TestCase):
Expand Down Expand Up @@ -1981,9 +1981,7 @@ def test_1(self):
),
secexp=int("09A4D6792295A7F730FC3F2B49CBC0F62E862272F", 16),
hsh=unhexlify(
b(
"AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF"
)
b"AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF"
),
hash_func=hashlib.sha256,
expected=int("23AF4074C90A02B3FE61D286D5C87F425E6BDD81B", 16),
Expand Down

0 comments on commit 35c33f2

Please sign in to comment.