本文整理汇总了Python中gmpy2.invert方法的典型用法代码示例。如果您正苦于以下问题:Python gmpy2.invert方法的具体用法?Python gmpy2.invert怎么用?Python gmpy2.invert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gmpy2
的用法示例。
在下文中一共展示了gmpy2.invert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: invert
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def invert(a, b):
"""
The multiplicitive inverse of a in the integers modulo b.
:return int: x, where a * x == 1 mod b
"""
if HAVE_GMP:
s = int(gmpy2.invert(a, b))
# according to documentation, gmpy2.invert might return 0 on
# non-invertible element, although it seems to actually raise an
# exception; for consistency, we always raise the exception
if s == 0:
raise ZeroDivisionError('invert() no inverse exists')
return s
else:
r, s, _ = extended_euclidean_algorithm(a, b)
if r != 1:
raise ZeroDivisionError('invert() no inverse exists')
return s % b
示例2: build_table
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def build_table(h, g, p, B):
table, z = {}, h
g_inverse = invert(g, p)
table[h] = 0
for x1 in range(1, B):
z = t_mod(mul(z, g_inverse), p)
table[z] = x1
return table
示例3: compute_d
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def compute_d(e, N, p, q):
# d * e mod phi(N) = 1
# where phi(N) = N - p - q + 1
phiN = phi(N, p, q)
d = invert(mpz(e), mpz(phiN))
x = mul(mpz(d), mpz(e))
assert t_mod(x, mpz(phiN)) == 1
return d.digits()
示例4: sign
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def sign(self, m, k):
h = int(hashlib.md5(m).hexdigest(), 16)
r = pow(self.g, k, self.p) % self.q
s = int(((self.x * r + h) * gmpy2.invert(k, self.q)) % self.q)
return (r, s)
示例5: verify
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def verify(self, m, r, s):
if 0 < r and r < self.q and 0 < s and s < self.q:
h = int(hashlib.md5(m).hexdigest(), 16)
w = gmpy2.invert(s, self.q)
u1 = (h * w) % self.q
u2 = (r * w) % self.q
v = ((pow(self.g, u1, self.p) * pow(self.y, u2, self.p)) % self.p) % self.q
return v == r
return None
示例6: crt
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def crt(list_a, list_m):
"""
Reference: https://crypto.stanford.edu/pbc/notes/numbertheory/crt.html
Returns the output after computing Chinese Remainder Theorem on
x = a_1 mod m_1
x = a_2 mod m_2
...
x = a_n mod m_n
input parameter list_a = [a_1, a_2, ..., a_n]
input parameter list_m = [m_1, m_2, ..., m_n]
Returns -1 if the operation is unsuccessful due to some exceptions
"""
try:
assert len(list_a) == len(list_m)
except:
print "[+] Length of list_a should be equal to length of list_m"
return -1
for i in range(len(list_m)):
for j in range(len(list_m)):
if GCD(list_m[i], list_m[j])!= 1 and i!=j:
print "[+] Moduli should be pairwise co-prime"
return -1
M = 1
for i in list_m:
M *= i
list_b = [M/i for i in list_m]
assert len(list_b) == len(list_m)
try:
list_b_inv = [int(gmpy2.invert(list_b[i], list_m[i])) for i in range(len(list_m))]
except:
print "[+] Encountered an unusual error while calculating inverse using gmpy2.invert()"
return -1
x = 0
for i in range(len(list_m)):
x += list_a[i]*list_b[i]*list_b_inv[i]
return x % M
示例7: neg_pow
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def neg_pow(a, b, n):
assert b < 0
assert GCD(a, n) == 1
res = int(gmpy2.invert(a, n))
res = pow(res, b*(-1), n)
return res
# e1 --> Public Key exponent used to encrypt message m and get ciphertext c1
# e2 --> Public Key exponent used to encrypt message m and get ciphertext c2
# n --> Modulus
# The following attack works only when m^{GCD(e1, e2)} < n
示例8: invert
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def invert(a, b):
"""return int: x, where a * x == 1 mod b
"""
x = int(gmpy2.invert(a, b))
if x == 0:
raise ZeroDivisionError('invert(a, b) no inverse exists')
return x
示例9: EEA
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def EEA(n1, n2):
print('x*y == 1 (mod p)')
print('y = invmod(x, p)')
print('\n\033[1;34m[*]\033[0m Calculating...\n')
n3 = gmpy2.invert(n1, n2)
n3 = str(n3)
n3 = n3.replace('mpz(', '')
n3 = n3.replace(')', '')
print('\033[1;34m[*]\033[0m ' + str(n1) + ' * ' + str(n2) + ' == 1 (mod ' + str(n2) + ')')
print('\033[1;32m[+]\033[0m invmod(' + str(n1) + ', ' + str(n2) + ') = ' + str(n3))
示例10: inverse
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def inverse(n1,n2):
try:
n3 = gmpy2.invert(n1,n2)
except ZeroDivisionError:
n3 = 'ERR'
return n3
示例11: embedPriv
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def embedPriv(p, q, e):
p = bytes_to_long(long_to_bytes(p))
q = bytes_to_long(long_to_bytes(q))
e = bytes_to_long(long_to_bytes(e))
n = p * q
n = bytes_to_long(long_to_bytes(n))
phi = (p - 1) * (q - 1)
d = gmpy2.invert(e, phi)
d = bytes_to_long(long_to_bytes(d))
key = RSA.construct((n,e,d,p,q))
key = RSA._RSAobj.exportKey(key).decode('utf-8')
return key
示例12: RSAsolverpqec
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def RSAsolverpqec(p, q, e, c):
print('\033[1;34m[*]\033[0m Calculating Plain-Text')
n = int(p) * int(q)
phi = (int(p) - 1) * (int(q) - 1)
d = gmpy2.invert(int(e), phi)
m = pow(int(c),int(d),int(n))
return m
示例13: fermatAttack
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def fermatAttack(n, e):
try:
(p, q) = Fermat.fermat(n, e)
except:
print("\n\033[1;31m[-]\033[0m This RSA key is not valid for a Fermat Attack\n")
exit()
phi = indicatrice_euler(p,q)
d = gmpy2.invert(e, phi)
return d
示例14: invert
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def invert(x, m):
"""Return y such that x*y == 1 (mod m), assuming m is prime.
Raises ZeroDivisionError if no inverse exists.
"""
if m == 2:
y = x%2
else:
y = pow(x, m-2, m)
if y == 0:
raise ZeroDivisionError
return y
示例15: neg_pow
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def neg_pow(a, b, n):
assert b < 0
assert GCD(a, n) == 1
res = int(gmpy2.invert(a, n))
res = pow(res, b*(-1), n)
return res