本文整理汇总了Python中gmpy2.mpz方法的典型用法代码示例。如果您正苦于以下问题:Python gmpy2.mpz方法的具体用法?Python gmpy2.mpz怎么用?Python gmpy2.mpz使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gmpy2
的用法示例。
在下文中一共展示了gmpy2.mpz方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_pq
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def find_pq(N_string):
# find p an and q such that N = p * q
# and |p - q| < 2 * fourthroot_of(N)
A = None
N = mpz(N_string)
# Get the ceiling of sqrt(N)
A, r = gmpy2.isqrt_rem(N)
if r > 0:
A += 1
A_squared_minus_N = A**2 - N
x = gmpy2.isqrt(A_squared_minus_N)
p = A - x
q = A + x
N_from_pq = gmpy2.mul(p, q)
assert N == N_from_pq
return p.digits(), q.digits()
示例2: challenge1
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def challenge1(N_string):
# find p an and q such that a given N = p * q
# and |p - q| < 2 * fourthroot_of(N)
A = None
N = mpz(N_string)
# Get the ceiling of sqrt(N)
A, r = gmpy2.isqrt_rem(N)
if r > 0:
A += 1
A_squared_minus_N = A**2 - N
x = gmpy2.isqrt(A_squared_minus_N)
p = A - x
q = A + x
N_from_pq = gmpy2.mul(p, q)
assert N == N_from_pq
return p.digits(), q.digits()
# --------------------------------------------------
# CHALLENGE TWO
# --------------------------------------------------
示例3: challenge3
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def challenge3(N_string):
# find p an and q such that a given N = p * q
# and |p - q| < 2^11 * fourthroot_of(N)
N = mpz(N_string)
start = gmpy2.isqrt(N) + 1
end = start + mpz(2)**mpz(20)
for A in range(start, end):
A_squared_minus_N = A**2 - N
x, r = gmpy2.isqrt_rem(A_squared_minus_N)
if r == 0:
p = A - x
q = A + x
if N == gmpy2.mul(p, q):
return p.digits(), q.digits()
return None, None
示例4: is_python_rational
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def is_python_rational(var):
"""Tests whether var is a Rational.
This accounts for: long, int, float, Fraction, mpz, mpq (if available).
"""
if type(var) == mpz_type or type(var) == mpq_type:
return True
if type(var) == pyFraction:
return True
if PY2 and type(var) == long:
return True
if type(var) == int:
return True
if type(var) == float:
return True
return False
示例5: test_integer
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def test_integer(self):
"""Create Int using different constant types."""
from pysmt.constants import HAS_GMPY
from six import PY2
v_base = Integer(1)
c_base = self.mgr.Int(v_base)
v_int = int(1)
c_int = self.mgr.Int(v_int)
self.assertIs(c_base, c_int)
if PY2:
v_long = long(1)
c_long = self.mgr.Int(v_long)
self.assertIs(c_base, c_long)
if HAS_GMPY:
from gmpy2 import mpz
v_mpz = mpz(1)
c_mpz = self.mgr.Int(v_mpz)
self.assertIs(c_base, c_mpz)
示例6: getprimeover
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def getprimeover(N):
"""Return a random N-bit prime number using the System's best
Cryptographic random source.
Use GMP if available, otherwise fallback to PyCrypto
"""
if HAVE_GMP:
randfunc = random.SystemRandom()
r = gmpy2.mpz(randfunc.getrandbits(N))
r = gmpy2.bit_set(r, N - 1)
return int(gmpy2.next_prime(r))
elif HAVE_CRYPTO:
return number.getPrime(N, os.urandom)
else:
randfunc = random.SystemRandom()
n = randfunc.randrange(2**(N-1), 2**N) | 1
while not is_prime(n):
n += 2
return n
示例7: mpz
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def mpz( x ):
return x
示例8: powMod
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def powMod( x, y, mod ):
"""
(Efficiently) Calculate and return `x' to the power of `y' mod `mod'.
If possible, the three numbers are converted to GMPY's bignum
representation which speeds up exponentiation. If GMPY is not installed,
built-in exponentiation is used.
"""
x = mpz(x)
y = mpz(y)
mod = mpz(mod)
return pow(x, y, mod)
示例9: run
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def run(p_string, g_string, h_string):
p = mpz(p_string)
g = mpz(g_string)
h = mpz(h_string)
B = mpz(2) ** mpz(20)
assert is_prime(p)
assert g < p
assert h < p
x = find_x(h, g, p, B)
assert h == powmod(g, x, p)
return x
示例10: compute_d
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [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()
示例11: phi
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def phi(N, p, q):
return (mpz(N) - mpz(p) - mpz(q) + 1).digits()
示例12: decrypt_pipeline
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def decrypt_pipeline(ciphertext, d, N):
m_decimal = decrypt(mpz(ciphertext), mpz(d), mpz(N))
m_bytes = int.to_bytes(int(m_decimal), TOTAL_LENGTH, "big")
assert m_bytes[0] == 2
for b in range(0, TOTAL_LENGTH):
if m_bytes[b] == 0:
return (m_bytes[b+1:]).decode('utf8')
return None
示例13: _decode_hex_string
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def _decode_hex_string(number_str):
return mpz("0x{0}".format("".join(number_str.split())))
示例14: generate_secret
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def generate_secret(p, num_bits=1024):
return mpz(random.SystemRandom().getrandbits(num_bits)) % p
示例15: getprimeover
# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def getprimeover(n):
"""return a random n-bit prime number
"""
r = gmpy2.mpz(random.SystemRandom().getrandbits(n))
r = gmpy2.bit_set(r, n - 1)
return int(gmpy2.next_prime(r))