当前位置: 首页>>代码示例>>Python>>正文


Python gmpy2.mpz方法代码示例

本文整理汇总了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() 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:22,代码来源:script2.py

示例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
# -------------------------------------------------- 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:27,代码来源:factoring.py

示例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 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:22,代码来源:factoring.py

示例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 
开发者ID:pysmt,项目名称:pysmt,代码行数:18,代码来源:constants.py

示例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) 
开发者ID:pysmt,项目名称:pysmt,代码行数:24,代码来源:test_formula.py

示例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 
开发者ID:data61,项目名称:python-paillier,代码行数:21,代码来源:util.py

示例7: mpz

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def mpz( x ):
            return x 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:4,代码来源:modexp.py

示例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) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:15,代码来源:modexp.py

示例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 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:17,代码来源:mitm.py

示例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() 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:10,代码来源:publickeysystem.py

示例11: phi

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import mpz [as 别名]
def phi(N, p, q):
    return (mpz(N) - mpz(p) - mpz(q) + 1).digits() 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:4,代码来源:publickeysystem.py

示例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 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:14,代码来源:publickeysystem.py

示例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()))) 
开发者ID:FederatedAI,项目名称:FATE,代码行数:4,代码来源:diffie_hellman.py

示例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 
开发者ID:FederatedAI,项目名称:FATE,代码行数:4,代码来源:diffie_hellman.py

示例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)) 
开发者ID:FederatedAI,项目名称:FATE,代码行数:9,代码来源:gmpy_math.py


注:本文中的gmpy2.mpz方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。