def _test_random_key(self, bits):
     elgObj = ElGamal.generate(bits, Random.new().read)
     self._check_private_key(elgObj)
     self._exercise_primitive(elgObj)
     pub = elgObj.publickey()
     self._check_public_key(pub)
     self._exercise_public_primitive(elgObj)
 def _test_random_key(self, bits):
     elgObj = ElGamal.generate(bits, Random.new().read)
     self._check_private_key(elgObj)
     self._exercise_primitive(elgObj)
     pub = elgObj.publickey()
     self._check_public_key(pub)
     self._exercise_public_primitive(elgObj)
Example #3
0
 def initiateContact(self, other: str, pgVals: (int, int) = None):
     if (pgVals is None):
         elGam = ElGamal.generate(
             256, RandBytes)  # Would be larger p in practice
         p = int(elGam.p)
         g = int(elGam.g)
     else:
         p = pgVals[0]
         g = pgVals[1]
     self.pgPairs[other] = (p, g)
     self.private[other] = random.randrange(p)
     self.public[other] = pow(g, self.private[other], p)
     return ((p, g), self.name, other)
def findQNR(p):
    r = random.randint(1, p - 1)
    while isQR(r, p) == 1:
        r = random.randint(1, p - 1)
    return r


# Find a message that is a QR, note: half of messages are not QR
def findQR(p):
    r = random.randint(1, p - 1)
    return pow(r, 2, p)


# Key generation. We use 512-bit only for better performance
print "Generating the key..."
key = ElGamal.generate(512, Random.new().read)

wrong = 0
runs = 1000

print "Running the experiment..."

for i in xrange(runs):
    p = int(key.p)
    pk = int(key.y)

    # Select two messages
    plaintexts = dict()
    plaintexts[0] = findQNR(p)
    plaintexts[1] = findQR(p)