Rabin 随即比特产生器

Rabin随机比特生成器是一种基于大质数的伪随机数生成器,由Michael O. Rabin于1980年提出。

rabin_random_bit_generator 函数,不接受任何参数。其主要逻辑是选择两个大质数pq,计算它们的乘积n,然后生成一个介于1和n-1之间的随机整数x。接下来,计算(x**2) % n,并将结果返回。

第二个版本中,修改了rabin_random_bit_generator函数,使其返回(x**2) % n的二进制表示。

Rabin随机比特生成器的安全性取决于大质数pq的选择。在实际应用中,需要确保pq是足够大的质数,以防止攻击者破解生成的伪随机数。

代码部分:

版本一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import random

def rabin_random_bit_generator():
# Choose two prime numbers
p = 61
q = 53
n = p * q # Calculate the product of the two prime numbers
x = random.randint(1, n-1) # Generate a random number x
return bin((x**2) % n)[2:] # Return the result of x^2 mod n in binary format

# Test function
def test_rabin_random_bit_generator():
# Generate 10 random bits using the Rabin random bit generator
generated_bits = [rabin_random_bit_generator() for _ in range(10)]
print(generated_bits) # Print the generated random bits

test_rabin_random_bit_generator() # Call the test function

版本二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import random

def rabin_random_bit_generator():
# Choose two prime numbers
p = 61
q = 53
n = p * q # Calculate the product of the two prime numbers
x = random.randint(1, n-1) # Generate a random number x
return bin((x**2) % n)[2:] # Return the result of x^2 mod n in binary format

# Test function
def test_rabin_random_bit_generator():
# Generate 10 random bits using the Rabin random bit generator
generated_bits = [rabin_random_bit_generator() for _ in range(10)]
print(generated_bits) # Print the generated random bits

test_rabin_random_bit_generator() # Call the test function

实验结果

1
2
3
4
[1633, 2535, 991, 3120, 2323, 727, 2792, 784, 1827, 2487]
['11010000011', '11010111100', '100011000001', '11010100000', '11011011001', '101110011', '10010001000', '1110101', '101111010111', '100000110000']

Process finished with exit code 0