def test_urandom(self): for c in self.cv: G = c.G pG = ECPoint(c, G) pN = ECPoint(c) pR = [] for i in range(0, 100): pR.append(ECPoint.urandom(c)) for i in range(0, 100): pC = ECPoint(c, str(pR[i])) self.assertEqual(pC, pR[i]) self.assertNotEqual(pC, pG) self.assertNotEqual(pC, pN) for j in range(i+1, 100): self.assertNotEqual(pC, pR[j])
def test_str_and_bytes(self): for c in self.cv: G = c.G pG = ECPoint(c, G) pN = ECPoint(c) pR = [] for i in range(0, 100): pR.append(ECPoint.urandom(c)) for i in range(0, 100): pC = ECPoint(c, str(pR[i])) self.assertEqual(pC, pR[i]) self.assertNotEqual(pC, pG) self.assertNotEqual(pC, pN) self.assertEqual(pC.compressed(), unhexlify(str(pC))) pCC = ECPoint(c, pC.compressed()) self.assertEqual(pC, pCC) pCC = ECPoint(c, pC.uncompressed()) self.assertEqual(pC, pCC)
def test_encrypt_decrypt(self): for c in self.cv: G = ECPoint(c, c.G) sK = FieldElement.urandom(c.n) pK = G * sK ptxt = ECPoint.urandom(c) ctxt = ECElgamalCiphertext.encrypt(pK, ptxt) pdec = ctxt.decrypt(sK) assert (pdec == ptxt) Cpt = ctxt.C Dpt = ctxt.D ctxt2 = ECElgamalCiphertext(Cpt, Dpt) pdec2 = ctxt2.decrypt(sK) assert (pdec2 == ptxt) for _ in range(0, 100): sK2 = FieldElement.urandom(c.n) pdec2 = ctxt.decrypt(sK2) assert (pdec2 != pdec)