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


Python builtins.pow方法代码示例

本文整理汇总了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 
开发者ID:GrosQuildu,项目名称:CryptoAttacks,代码行数:23,代码来源:Math.py

示例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") 
开发者ID:GrosQuildu,项目名称:CryptoAttacks,代码行数:21,代码来源:test_rsa.py

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

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

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

示例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 
开发者ID:Soft8Soft,项目名称:verge3d-blender-addon,代码行数:30,代码来源:misc.py

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

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

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

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

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

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

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

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

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


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