Skip to content

Commit be0325c

Browse files
committed
Merge bitcoin#27538: test: Remove modinv python util helper function
dc14ba0 test: remove modinv python util helper function (Fabian Jahr) Pull request description: Since bitcoin#27483 was merged the `modinv()` body is just one line calling pythons own implementation of `pow()`. We can just remove the function as it doesn't seem to add any value. Additionally the comment in the function is now outdated and the test is only testing two ways of doing modular inverse but both using python's `pow()` function. ACKs for top commit: theStack: ACK dc14ba0 Tree-SHA512: e8b470c72dc3f9fd53699d0684650517b1ea35ad1d4c01cf9472c80d3e4474c0c72e429c0bd201eb99d204c87eee0d68285e6a388e4c506f30e14b2bff9c1c32
2 parents 5394522 + dc14ba0 commit be0325c

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 random
1414
import unittest
1515

16-
from .util import modinv
17-
1816
# Point with no known discrete log.
1917
H_POINT = "50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"
2018

@@ -78,7 +76,7 @@ def affine(self, p1):
7876
x1, y1, z1 = p1
7977
if z1 == 0:
8078
return None
81-
inv = modinv(z1, self.p)
79+
inv = pow(z1, -1, self.p)
8280
inv_2 = (inv**2) % self.p
8381
inv_3 = (inv_2 * inv) % self.p
8482
return ((inv_2 * x1) % self.p, (inv_3 * y1) % self.p, 1)
@@ -319,7 +317,7 @@ def verify_ecdsa(self, sig, msg, low_s=True):
319317
z = int.from_bytes(msg, 'big')
320318

321319
# Run verifier algorithm on r, s
322-
w = modinv(s, SECP256K1_ORDER)
320+
w = pow(s, -1, SECP256K1_ORDER)
323321
u1 = z*w % SECP256K1_ORDER
324322
u2 = r*w % SECP256K1_ORDER
325323
R = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, u1), (self.p, u2)]))
@@ -397,7 +395,7 @@ def sign_ecdsa(self, msg, low_s=True, rfc6979=False):
397395
k = random.randrange(1, SECP256K1_ORDER)
398396
R = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, k)]))
399397
r = R[0] % SECP256K1_ORDER
400-
s = (modinv(k, SECP256K1_ORDER) * (z + self.secret * r)) % SECP256K1_ORDER
398+
s = (pow(k, -1, SECP256K1_ORDER) * (z + self.secret * r)) % SECP256K1_ORDER
401399
if low_s and s > SECP256K1_ORDER_HALF:
402400
s = SECP256K1_ORDER - s
403401
# 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
@@ -15,7 +15,6 @@
1515
import random
1616
import re
1717
import time
18-
import unittest
1918

2019
from . import coverage
2120
from .authproxy import AuthServiceProxy, JSONRPCException
@@ -537,22 +536,3 @@ def find_vout_for_address(node, txid, addr):
537536
if addr == tx["vout"][i]["scriptPubKey"]["address"]:
538537
return i
539538
raise RuntimeError("Vout not found for address: txid=%s, addr=%s" % (txid, addr))
540-
541-
def modinv(a, n):
542-
"""Compute the modular inverse of a modulo n using the extended Euclidean
543-
Algorithm. See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers.
544-
"""
545-
return pow(a, -1, n)
546-
547-
class TestFrameworkUtil(unittest.TestCase):
548-
def test_modinv(self):
549-
test_vectors = [
550-
[7, 11],
551-
[11, 29],
552-
[90, 13],
553-
[1891, 3797],
554-
[6003722857, 77695236973],
555-
]
556-
557-
for a, n in test_vectors:
558-
self.assertEqual(modinv(a, n), pow(a, n-2, n))

0 commit comments

Comments
 (0)