本文整理汇总了Python中builtins.pow方法的典型用法代码示例。如果您正苦于以下问题:Python builtins.pow方法的具体用法?Python builtins.pow怎么用?Python builtins.pow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类builtins
的用法示例。
在下文中一共展示了builtins.pow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_generator
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def find_generator(p, factors):
"""Find generator of cyclic group of order p-1
Args:
p(int)
factors(dict): factorization of p-1, {2:3, 17:1,}
Returns:
int: generator of Zp*
"""
g = 1
for one_factor in factors:
b = 1
while b != 1:
a = randint(3, p-1)
if gcd(a, p-1) != 1:
continue
b = pow(a, (p-1)//one_factor, p)
y = pow(a, (p-1)//(one_factor**factors[one_factor]), p)
g *= y
return g
示例2: test_wiener
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def test_wiener(tries=10):
print("\nTest: wiener")
for _ in range(tries):
n_size = 1024
p = random_prime(n_size // 2)
q = random_prime(n_size // 2)
n = p * q
phi = (p - 1) * (q - 1)
while True:
d = getrandbits(n_size // 4)
if gcd(phi, d) == 1 and 81 * pow(d, 4) < n:
break
e = invmod(d, phi)
key = RSAKey.construct(int(n), int(e))
key_recovered = wiener(key.publickey())
if key_recovered:
assert key_recovered.d == d
else:
print("Not recovered")
示例3: test_dsks
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def test_dsks():
print("\nTest Duplicate-Signature Key Selection on RSA")
keys_sizes = [1024, 2048]
for key_size in keys_sizes:
for j in range(2):
print('test {}, key size {}'.format(j, key_size))
key = RSAKey.generate(key_size)
message = randint(1, key.size - 1)
signature = key.decrypt(message)
n_p, p_order_factors, q_order_factors, e_p, d_p = dsks(message, signature, key.n,
smooth_bit_size=30, hash_function=None)
key_p = RSAKey(n_p, e=e_p, d=d_p)
p_p = product(p_order_factors) + 1
q_p = product(q_order_factors) + 1
assert p_p * q_p == n_p
assert key_p.p * key_p.q == n_p
assert pow(signature, e_p, n_p) == message
assert pow(message, d_p, n_p) == signature
示例4: small_e_msg
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [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
示例5: tonelli_shanks
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def tonelli_shanks(n, p):
"""Find r such that r^2 = n % p, r2 == p-r"""
if legendre(n, p) != 1:
log.critical_error("Not a square root")
s = 0
q = p-1
while q & 1 == 0:
s += 1
q >>= 1
if s == 1:
return pow(n, (p+1)//4, p)
z = 1
while legendre(z, p) != -1:
z += 1
c = pow(z, q, p)
r = pow(n, (q+1)//2, p)
t = pow(n, q, p)
m = s
while t != 1:
i = 1
while i < m:
if pow(t, 2**i, p) == 1:
break
i += 1
b = pow(c, 2**(m-i-1), p)
r = (r*b) % p
t = (t * (b**2)) % p
c = pow(b, 2, p)
m = i
return r
示例6: pow
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def pow(x, y, z=_SENTINEL):
"""
pow(x, y[, z]) -> number
With two arguments, equivalent to x**y. With three arguments,
equivalent to (x**y) % z, but may be more efficient (e.g. for ints).
"""
# Handle newints
if isinstance(x, newint):
x = long(x)
if isinstance(y, newint):
y = long(y)
if isinstance(z, newint):
z = long(z)
try:
if z == _SENTINEL:
return _builtin_pow(x, y)
else:
return _builtin_pow(x, y, z)
except ValueError:
if z == _SENTINEL:
return _builtin_pow(x+0j, y)
else:
return _builtin_pow(x+0j, y, z)
# ``future`` doesn't support Py3.0/3.1. If we ever did, we'd add this:
# callable = __builtin__.callable
示例7: legendre
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def legendre(a, p):
"""Legendre symbol"""
tmp = pow(a, (p-1)//2, p)
return -1 if tmp == p-1 else tmp
示例8: babystep_giantstep
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def babystep_giantstep(g, h, p, upper_bound):
m = int(math.ceil(math.sqrt(upper_bound)))
log.debug('babystep-giantstep with loops of size {}'.format(m))
g_j = {}
g_j_tmp = 1
for j in range(m):
g_j[g_j_tmp] = j
g_j_tmp = (g_j_tmp*g) % p
g_m = invmod(pow(g, m, p), p)
y = h
for i in range(m):
if y in g_j:
return (i*m + g_j[y]) % p
y = (y*g_m) % p
示例9: test_faulty
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def test_faulty():
print("\nTest: faulty")
for _ in range(5):
key = RSAKey.generate(1024)
m = randint(0x13373371, key.n)
sp = pow(m, key.d % (key.p - 1), key.p)
sq = pow(m, key.d % (key.q - 1), key.q)
sq_f = sq ^ randint(1, sq) # random error
s_f = crt([sp, sq_f], [key.p, key.q]) % key.n
s = crt([sp, sq], [key.p, key.q]) % key.n
key.texts.append({'cipher': s_f, 'plain': m})
key_recovered = faulty(key.publickey())
assert key_recovered and key_recovered.d == key.d
key.texts = [{'cipher': s}, {'cipher': s_f}]
key_recovered = faulty(key.publickey())
assert key_recovered and key_recovered.d == key.d
key.texts = [{'cipher': s}, {'cipher': s_f}, {'cipher': randint(1, key.n)},
{'cipher': randint(1, key.n), 'plain': randint(1, key.n)}]
key_recovered = faulty(key.publickey())
assert key_recovered and key_recovered.d == key.d
key.texts = [{'cipher': s, 'plain': m}]
key_recovered = faulty(key.publickey())
assert key_recovered is None
示例10: encrypt
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def encrypt(plaintext, key):
plaintext = b2i(plaintext)
ciphertext = pow(plaintext, key.e, key.n)
return i2b(ciphertext)
示例11: decrypt
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def decrypt(ciphertext, key):
ciphertext = b2i(ciphertext)
plaintext = pow(ciphertext, key.d, key.n)
return i2b(plaintext)
示例12: verify
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def verify(message, signature, key):
# message = add_rsa_signature_padding(message, size=key.size, hash_function='sha1')
message = b2i(message)
signature = b2i(signature)
if pow(signature, key.e, key.n) == message:
return True
return False
示例13: parity_oracle
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def parity_oracle(ciphertext):
key = key_1024
ciphertext = b2i(ciphertext)
message = pow(ciphertext, key.d, key.n)
if message & 1 == 1:
return 1
return 0
示例14: pkcs15_padding_oracle
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def pkcs15_padding_oracle(ciphertext, **kwargs):
kwargs['pkcs15_padding_oracle_calls'][0] += 1
key = kwargs['oracle_key']
ciphertext = b2i(ciphertext)
message = pow(ciphertext, key.d, key.n)
if message >> (key.size - 16) == 0x0002:
return True
return
示例15: oaep_padding_oracle
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import pow [as 别名]
def oaep_padding_oracle(ciphertext, **kwargs):
kwargs['manger_padding_oracle_calls'][0] += 1
key = kwargs['oracle_key']
ciphertext = b2i(ciphertext)
message = pow(ciphertext, key.d, key.n)
if message >> (key.size - 8) == 0x00:
return True
return