Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/util.py
Show All 10 Lines | |||||
from subprocess import CalledProcessError | from subprocess import CalledProcessError | ||||
import inspect | import inspect | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
import random | import random | ||||
import re | import re | ||||
import time | import time | ||||
import unittest | |||||
from . import coverage | from . import coverage | ||||
from .authproxy import AuthServiceProxy, JSONRPCException | from .authproxy import AuthServiceProxy, JSONRPCException | ||||
logger = logging.getLogger("TestFramework.utils") | logger = logging.getLogger("TestFramework.utils") | ||||
# Assert functions | # Assert functions | ||||
################## | ################## | ||||
▲ Show 20 Lines • Show All 579 Lines • ▼ Show 20 Lines | def find_vout_for_address(node, txid, addr): | ||||
given address. Raises runtime error exception if not found. | given address. Raises runtime error exception if not found. | ||||
""" | """ | ||||
tx = node.getrawtransaction(txid, True) | tx = node.getrawtransaction(txid, True) | ||||
for i in range(len(tx["vout"])): | for i in range(len(tx["vout"])): | ||||
if any([addr == a for a in tx["vout"][i]["scriptPubKey"]["addresses"]]): | if any([addr == a for a in tx["vout"][i]["scriptPubKey"]["addresses"]]): | ||||
return i | return i | ||||
raise RuntimeError( | raise RuntimeError( | ||||
"Vout not found for address: txid={}, addr={}".format(txid, addr)) | "Vout not found for address: txid={}, addr={}".format(txid, addr)) | ||||
def modinv(a, n): | |||||
"""Compute the modular inverse of a modulo n using the extended Euclidean | |||||
Algorithm. See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers. | |||||
""" | |||||
# TODO: Change to pow(a, -1, n) available in Python 3.8 | |||||
t1, t2 = 0, 1 | |||||
r1, r2 = n, a | |||||
while r2 != 0: | |||||
q = r1 // r2 | |||||
t1, t2 = t2, t1 - q * t2 | |||||
r1, r2 = r2, r1 - q * r2 | |||||
if r1 > 1: | |||||
return None | |||||
if t1 < 0: | |||||
t1 += n | |||||
return t1 | |||||
class TestFrameworkUtil(unittest.TestCase): | |||||
def test_modinv(self): | |||||
test_vectors = [ | |||||
[7, 11], | |||||
[11, 29], | |||||
[90, 13], | |||||
[1891, 3797], | |||||
[6003722857, 77695236973], | |||||
] | |||||
for a, n in test_vectors: | |||||
self.assertEqual(modinv(a, n), pow(a, n - 2, n)) |