Skip to content

Commit 3960c1b

Browse files
kwvgPastaPastaPasta
authored andcommitted
merge bitcoin#27538: remove modinv python util helper function
1 parent 13c8dc5 commit 3960c1b

File tree

3 files changed

+4
-28
lines changed

3 files changed

+4
-28
lines changed

test/functional/test_framework/key.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import sys
1414
import unittest
1515

16-
from .util import modinv
17-
1816
def TaggedHash(tag, data):
1917
ss = hashlib.sha256(tag.encode('utf-8')).digest()
2018
ss += ss
@@ -77,7 +75,7 @@ def affine(self, p1):
7775
x1, y1, z1 = p1
7876
if z1 == 0:
7977
return None
80-
inv = modinv(z1, self.p)
78+
inv = pow(z1, -1, self.p)
8179
inv_2 = (inv**2) % self.p
8280
inv_3 = (inv_2 * inv) % self.p
8381
return ((inv_2 * x1) % self.p, (inv_3 * y1) % self.p, 1)
@@ -318,7 +316,7 @@ def verify_ecdsa(self, sig, msg, low_s=True):
318316
z = int.from_bytes(msg, 'big')
319317

320318
# Run verifier algorithm on r, s
321-
w = modinv(s, SECP256K1_ORDER)
319+
w = pow(s, -1, SECP256K1_ORDER)
322320
u1 = z*w % SECP256K1_ORDER
323321
u2 = r*w % SECP256K1_ORDER
324322
R = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, u1), (self.p, u2)]))
@@ -383,7 +381,7 @@ def sign_ecdsa(self, msg, low_s=True):
383381
k = random.randrange(1, SECP256K1_ORDER)
384382
R = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, k)]))
385383
r = R[0] % SECP256K1_ORDER
386-
s = (modinv(k, SECP256K1_ORDER) * (z + self.secret * r)) % SECP256K1_ORDER
384+
s = (pow(k, -1, SECP256K1_ORDER) * (z + self.secret * r)) % SECP256K1_ORDER
387385
if low_s and s > SECP256K1_ORDER_HALF:
388386
s = SECP256K1_ORDER - s
389387
# Represent in DER format. The byte representations of r and s have

test/functional/test_framework/muhash.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import hashlib
77
import unittest
88

9-
from .util import modinv
10-
119
def rot32(v, bits):
1210
"""Rotate the 32-bit value v left by bits bits."""
1311
bits %= 32 # Make sure the term below does not throw an exception
@@ -88,7 +86,7 @@ def remove(self, data):
8886

8987
def digest(self):
9088
"""Extract the final hash. Does not modify this object."""
91-
val = (self.numerator * modinv(self.denominator, self.MODULUS)) % self.MODULUS
89+
val = (self.numerator * pow(self.denominator, -1, self.MODULUS)) % self.MODULUS
9290
bytes384 = val.to_bytes(384, 'little')
9391
return hashlib.sha256(bytes384).digest()
9492

test/functional/test_framework/util.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import shutil
1717
import re
1818
import time
19-
import unittest
2019

2120
from . import coverage
2221
from .authproxy import AuthServiceProxy, JSONRPCException
@@ -597,22 +596,3 @@ def find_vout_for_address(node, txid, addr):
597596
if any([addr == a for a in tx["vout"][i]["scriptPubKey"]["addresses"]]):
598597
return i
599598
raise RuntimeError("Vout not found for address: txid=%s, addr=%s" % (txid, addr))
600-
601-
def modinv(a, n):
602-
"""Compute the modular inverse of a modulo n using the extended Euclidean
603-
Algorithm. See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers.
604-
"""
605-
return pow(a, -1, n)
606-
607-
class TestFrameworkUtil(unittest.TestCase):
608-
def test_modinv(self):
609-
test_vectors = [
610-
[7, 11],
611-
[11, 29],
612-
[90, 13],
613-
[1891, 3797],
614-
[6003722857, 77695236973],
615-
]
616-
617-
for a, n in test_vectors:
618-
self.assertEqual(modinv(a, n), pow(a, n-2, n))

0 commit comments

Comments
 (0)