當前位置: 首頁>>代碼示例>>Python>>正文


Python _fastmath.isPrime方法代碼示例

本文整理匯總了Python中Crypto.PublicKey._fastmath.isPrime方法的典型用法代碼示例。如果您正苦於以下問題:Python _fastmath.isPrime方法的具體用法?Python _fastmath.isPrime怎麽用?Python _fastmath.isPrime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Crypto.PublicKey._fastmath的用法示例。


在下文中一共展示了_fastmath.isPrime方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: getPrime

# 需要導入模塊: from Crypto.PublicKey import _fastmath [as 別名]
# 或者: from Crypto.PublicKey._fastmath import isPrime [as 別名]
def getPrime(N, randfunc=None):
    """getPrime(N:int, randfunc:callable):long
    Return a random N-bit prime number.

    If randfunc is omitted, then Random.new().read is used.
    """
    if randfunc is None:
        _import_Random()
        randfunc = Random.new().read

    number=getRandomNBitInteger(N, randfunc) | 1
    while (not isPrime(number, randfunc=randfunc)):
        number=number+2
    return number 
開發者ID:adde88,項目名稱:hostapd-mana,代碼行數:16,代碼來源:number.py

示例2: isPrime

# 需要導入模塊: from Crypto.PublicKey import _fastmath [as 別名]
# 或者: from Crypto.PublicKey._fastmath import isPrime [as 別名]
def isPrime(N, false_positive_prob=1e-6, randfunc=None):
    """isPrime(N:long, false_positive_prob:float, randfunc:callable):bool
    Return true if N is prime.

    The optional false_positive_prob is the statistical probability
    that true is returned even though it is not (pseudo-prime).
    It defaults to 1e-6 (less than 1:1000000).
    Note that the real probability of a false-positive is far less. This is
    just the mathematically provable limit.

    If randfunc is omitted, then Random.new().read is used.
    """
    if _fastmath is not None:
        return _fastmath.isPrime(long(N), false_positive_prob, randfunc)

    if N < 3 or N & 1 == 0:
        return N == 2
    for p in sieve_base:
        if N == p:
            return 1
        if N % p == 0:
            return 0

    rounds = int(math.ceil(-math.log(false_positive_prob)/math.log(4)))
    return _rabinMillerTest(N, rounds, randfunc)


# Improved conversion functions contributed by Barry Warsaw, after
# careful benchmarking 
開發者ID:adde88,項目名稱:hostapd-mana,代碼行數:31,代碼來源:number.py

示例3: isPrime

# 需要導入模塊: from Crypto.PublicKey import _fastmath [as 別名]
# 或者: from Crypto.PublicKey._fastmath import isPrime [as 別名]
def isPrime(N, false_positive_prob=1e-6, randfunc=None):
    """isPrime(N:long, false_positive_prob:float, randfunc:callable):bool
    Return true if N is prime.

    The optional false_positive_prob is the statistical probability
    that true is returned even though it is not (pseudo-prime).
    It defaults to 1e-6 (less than 1:1000000).
    Note that the real probability of a false-positive is far less. This is
    just the mathematically provable limit.

    If randfunc is omitted, then Random.new().read is used.
    """
    if _fastmath is not None:
        return _fastmath.isPrime(int(N), false_positive_prob, randfunc)

    if N < 3 or N & 1 == 0:
        return N == 2
    for p in sieve_base:
        if N == p:
            return 1
        if N % p == 0:
            return 0

    rounds = int(math.ceil(-math.log(false_positive_prob)/math.log(4)))
    return _rabinMillerTest(N, rounds, randfunc)


# Improved conversion functions contributed by Barry Warsaw, after
# careful benchmarking 
開發者ID:summerwinter,項目名稱:SublimeRemoteGDB,代碼行數:31,代碼來源:number.py

示例4: getPrime

# 需要導入模塊: from Crypto.PublicKey import _fastmath [as 別名]
# 或者: from Crypto.PublicKey._fastmath import isPrime [as 別名]
def getPrime(N, randfunc):
    """getPrime(N:int, randfunc:callable):long
    Return a random N-bit prime number.
    """

    number=getRandomNumber(N, randfunc) | 1
    while (not isPrime(number)):
        number=number+2
    return number 
開發者ID:RunasSudo,項目名稱:helios-server-mixnet,代碼行數:11,代碼來源:number.py

示例5: isPrime

# 需要導入模塊: from Crypto.PublicKey import _fastmath [as 別名]
# 或者: from Crypto.PublicKey._fastmath import isPrime [as 別名]
def isPrime(N):
    """isPrime(N:long):bool
    Return true if N is prime.
    """
    if N == 1:
        return 0
    if N in sieve:
        return 1
    for i in sieve:
        if (N % i)==0:
            return 0

    # Use the accelerator if available
    if _fastmath is not None:
        return _fastmath.isPrime(N)

    # Compute the highest bit that's set in N
    N1 = N - 1L
    n = 1L
    while (n<N):
        n=n<<1L
    n = n >> 1L

    # Rabin-Miller test
    for c in sieve[:7]:
        a=long(c) ; d=1L ; t=n
        while (t):  # Iterate over the bits in N1
            x=(d*d) % N
            if x==1L and d!=1L and d!=N1:
                return 0  # Square root of 1 found
            if N1 & t:
                d=(x*a) % N
            else:
                d=x
            t = t >> 1L
        if d!=1L:
            return 0
    return 1

# Small primes used for checking primality; these are all the primes
# less than 256.  This should be enough to eliminate most of the odd
# numbers before needing to do a Rabin-Miller test at all. 
開發者ID:RunasSudo,項目名稱:helios-server-mixnet,代碼行數:44,代碼來源:number.py


注:本文中的Crypto.PublicKey._fastmath.isPrime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。