本文整理汇总了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
示例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
示例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
示例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
示例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.