本文整理汇总了Python中gmpy2.iroot方法的典型用法代码示例。如果您正苦于以下问题:Python gmpy2.iroot方法的具体用法?Python gmpy2.iroot怎么用?Python gmpy2.iroot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gmpy2
的用法示例。
在下文中一共展示了gmpy2.iroot方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: small_e_msg
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def small_e_msg(key, ciphertexts=None, max_times=100):
"""If both e and plaintext are small, ciphertext may exceed modulus only a little
Args:
key(RSAKey): with small e, at least one ciphertext
ciphertexts(list)
max_times(int): how many times plaintext**e exceeded modulus maximally
Returns:
list: recovered plaintexts
"""
ciphertexts = get_mutable_texts(key, ciphertexts)
recovered = []
for ciphertext in ciphertexts:
log.debug("Find msg for ciphertext {}".format(ciphertext))
times = 0
for k in range(max_times):
msg, is_correct = gmpy2.iroot(ciphertext + times, key.e)
if is_correct and pow(msg, key.e, key.n) == ciphertext:
msg = int(msg)
log.success("Found msg: {}, times=={}".format(i2h(msg), times // key.n))
recovered.append(msg)
break
times += key.n
return recovered
示例2: hastad_unpadded
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def hastad_unpadded(ct_list, mod_list, e):
"""
Implementing Hastad's Broadcast Attack
"""
m_expo = crt(ct_list, mod_list)
if m_expo != -1:
eth_root = gmpy2.iroot(m_expo, e)
if eth_root[1] == False:
print "[+] Cannot calculate e'th root!"
return -1
elif eth_root[1] == True:
return long_to_bytes(eth_root)
else:
print "[+] Cannot calculate CRT"
return -1
示例3: common_modulus
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def common_modulus(e1, e2, n, c1, c2):
g, a, b = egcd(e1, e2)
if a < 0:
c1 = neg_pow(c1, a, n)
else:
c1 = pow(c1, a, n)
if b < 0:
c2 = neg_pow(c2, b, n)
else:
c2 = pow(c2, b, n)
ct = c1*c2 % n
m = int(gmpy2.iroot(ct, g)[0])
return long_to_bytes(m)
示例4: root
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def root(self, a, e):
return (gmpy2.iroot(a,e))
示例5: factor_prime_power
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def factor_prime_power(x): # TODO: move this to a separate math/number theory module
"""Return p and d for a prime power x = p**d."""
if x <= 1:
raise ValueError('number not a prime power')
k = 10
# test whether p is below 2**k, for positive k
p = 2
while p < 1<<k:
if x % p == 0:
d = 0
while x > 1:
x, r = divmod(x, p)
if r == 0:
d += 1
else:
raise ValueError('number not a prime power')
return int(p), d
p = next_prime(p)
# find prime factors of d
p, d = x, 1
while is_square(p):
p, d = isqrt(p), 2*d
e = 3
while k * e <= p.bit_length():
w, b = iroot(p, e)
if b:
p, d = w, e * d
else:
e = next_prime(e)
if is_prime(p):
return int(p), int(d)
raise ValueError('number not a prime power')
示例6: iroot
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def iroot(x, n):
"""Return (y, b) where y is the integer nth root of x and b is True if y is exact."""
if x == 0:
return x, True
k = (x.bit_length() - 1) // n
y = 1<<k
for i in range(k-1, -1, -1):
z = y | 1<<i
if z**n <= x:
y = z
return y, x == y**n
示例7: common_modulus
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import iroot [as 别名]
def common_modulus(e1, e2, n, c1, c2):
g, a, b = egcd(e1, e2)
if a < 0:
c1 = neg_pow(c1, a, n)
else:
c1 = pow(c1, a, n)
if b < 0:
c2 = neg_pow(c2, b, n)
else:
c2 = pow(c2, b, n)
ct = c1*c2 % n
m = int(gmpy2.iroot(ct, g)[0])
return m