#!/usr/bin/env python3 import os from Crypto.Util.number import getPrime, getRandomRange def isSquare(a, p): return pow(a, (p-1)//2, p) != p-1 class SquareRNG(object): def __init__(self, p, sa, sb): assert sa != 0 and sb != 0 (self.p, self.sa, self.sb) = (p, sa, sb) self.x = 0 def int(self, nbits): v, s = 0, 1 for _ in range(nbits): self.x = (self.x + 1) % p s += pow(self.sa, self.x, self.p) * pow(self.sb, self.x, self.p) s %= self.p v = (v << 1) | int(isSquare(s, self.p)) return v def bool(self): self.x = (self.x + 1) % self.p t = (pow(self.sa, self.x, self.p) + pow(self.sb, self.x, self.p)) t %= self.p return isSquare(t, self.p) p = getPrime(256) sb1 = int(input("Bob's seed 1: ")) % p sb2 = int(input("Bob's seed 2: ")) % p for _ in range(77): sa = getRandomRange(1, p) r1 = SquareRNG(p, sa, sb1) print("Random 1:", hex(r1.int(32))) r2 = SquareRNG(p, sa, sb2) print("Random 2:", hex(r2.int(32))) guess = int(input("Guess next bool [0 or 1]: ")) if guess == int(r1.bool()): print("OK!") else: print("NG...") break else: print("Congratz!") print(os.getenv("FLAG", "nek0pts{*** REDACTED ***}"))
时间: 2024-04-16 21:28:15 浏览: 138
hbcxx:使用“#!/ usr / bin / env hbcxx”使C ++源代码可执行-开源
以下是您提供的Python代码的翻译版本:
```python
#!/usr/bin/env python3
import os
from Crypto.Util.number import getPrime, getRandomRange
def isSquare(a, p):
return pow(a, (p-1)//2, p) != p-1
class SquareRNG(object):
def __init__(self, p, sa, sb):
assert sa != 0 and sb != 0
(self.p, self.sa, self.sb) = (p, sa, sb)
self.x = 0
def int(self, nbits):
v, s = 0, 1
for _ in range(nbits):
self.x = (self.x + 1) % p
s += pow(self.sa, self.x, self.p) * pow(self.sb, self.x, self.p)
s %= self.p
v = (v << 1) | int(isSquare(s, self.p))
return v
def bool(self):
self.x = (self.x + 1) % self.p
t = (pow(self.sa, self.x, self.p) + pow(self.sb, self.x, self.p))
t %= self.p
return isSquare(t, self.p)
p = getPrime(256)
sb1 = int(input("Bob's seed 1: ")) % p
sb2 = int(input("Bob's seed 2: ")) % p
for _ in range(77):
sa = getRandomRange(1, p)
r1 = SquareRNG(p, sa, sb1)
print("Random 1:", hex(r1.int(32)))
r2 = SquareRNG(p, sa, sb2)
print("Random 2:", hex(r2.int(32)))
guess = int(input("Guess next bool [0 or 1]: "))
if guess == int(r1.bool()):
print("OK!")
else:
print("NG...")
break
else:
print("Congratz!")
print(os.getenv("FLAG", "nek0pts{*** REDACTED ***}"))
```
请注意,这是直接的翻译,可能需要根据您的具体需求进行进一步调整和优化。如果您有任何其他问题,请随时提问。
阅读全文