def test_in_bounds_p_no_zero(self, p: ElementModP): self.assertTrue(p.is_in_bounds_no_zero()) self.assertFalse(ZERO_MOD_P.is_in_bounds_no_zero()) self.assertFalse( int_to_p_unchecked(p.to_int() + P).is_in_bounds_no_zero()) self.assertFalse( int_to_p_unchecked(p.to_int() - P).is_in_bounds_no_zero())
def test_in_bounds_p(self, p: ElementModP): self.assertTrue(p.is_in_bounds()) too_big = p.to_int() + P too_small = p.to_int() - P self.assertFalse(int_to_p_unchecked(too_big).is_in_bounds()) self.assertFalse(int_to_p_unchecked(too_small).is_in_bounds()) self.assertEqual(None, int_to_p(too_big)) self.assertEqual(None, int_to_p(too_small))
def test_schnorr_proofs_bounds_checking(self, keypair: ElGamalKeyPair, nonce: ElementModQ) -> None: proof = make_schnorr_proof(keypair, nonce) proof2 = SchnorrProof(ZERO_MOD_P, proof.h, proof.u) proof3 = SchnorrProof(int_to_p_unchecked(P), proof.h, proof.u) self.assertFalse(proof2.is_valid()) self.assertFalse(proof3.is_valid())
def elements_mod_p_no_zero(draw: _DrawType): """ Generates an arbitrary element from [1,P). :param draw: Hidden argument, used by Hypothesis. """ return int_to_p_unchecked(draw(integers(min_value=1, max_value=P - 1)))
def _discrete_log_uncached(e: ElementModP) -> int: count = 0 g_inv = int_to_p_unchecked(pow(G, -1, P)) while e != ONE_MOD_P: e = mult_p(e, g_inv) count = count + 1 return count
def testPsNotEqualToQs(self, q: ElementModQ, q2: ElementModQ): p = int_to_p_unchecked(q.to_int()) p2 = int_to_p_unchecked(q2.to_int()) # same value should imply they're equal self.assertEqual(p, q) self.assertEqual(q, p) if q.to_int() != q2.to_int(): # these are genuinely different numbers self.assertNotEqual(q, q2) self.assertNotEqual(p, p2) self.assertNotEqual(q, p2) self.assertNotEqual(p, q2) # of course, we're going to make sure that a number is equal to itself self.assertEqual(p, p) self.assertEqual(q, q)
def combine_election_keys(request: CombineElectionKeysRequest) -> ElectionJointKey: """ Combine public election keys into a final one :return: Combine Election key """ public_keys = [] for key in request.election_public_keys: public_keys.append(int_to_p_unchecked(key)) joint_key = elgamal_combine_public_keys(public_keys) return ElectionJointKey(joint_key=write_json_object(joint_key))
def set_deserializers(): electionguard.serializable.set_deserializer( lambda p, cls, **_: int_to_p_unchecked(maybe_base64_to_int(p)), ElementModP # type: ignore ) electionguard.serializable.set_deserializer( lambda q, cls, **_: int_to_q_unchecked(maybe_base64_to_int(q)), ElementModQ # type: ignore ) electionguard.serializable.set_deserializer( lambda i, cls, **_: maybe_base64_to_int(i), int # type: ignore )