コード例 #1
0
 def test_in_bounds_q_no_zero(self, q: ElementModQ):
     self.assertTrue(q.is_in_bounds_no_zero())
     self.assertFalse(ZERO_MOD_Q.is_in_bounds_no_zero())
     self.assertFalse(
         int_to_q_unchecked(q.to_int() + Q).is_in_bounds_no_zero())
     self.assertFalse(
         int_to_q_unchecked(q.to_int() - Q).is_in_bounds_no_zero())
コード例 #2
0
 def test_in_bounds_q(self, q: ElementModQ):
     self.assertTrue(q.is_in_bounds())
     too_big = q.to_int() + Q
     too_small = q.to_int() - Q
     self.assertFalse(int_to_q_unchecked(too_big).is_in_bounds())
     self.assertFalse(int_to_q_unchecked(too_small).is_in_bounds())
     self.assertEqual(None, int_to_q(too_big))
     self.assertEqual(None, int_to_q(too_small))
コード例 #3
0
    def encrypt(self, ballot: dict, deterministic: bool = False) -> dict:
        if not self.context.joint_key:
            raise MissingJointKey()

        ballot_style: str = self.context.election.ballot_styles[0].object_id
        contests: List[PlaintextBallotContest] = []

        for contest in self.context.election_metadata.get_contests_for(
                ballot_style):
            selections: List[PlaintextBallotSelection] = [
                selection_from(
                    selection, False, selection.object_id
                    in ballot[contest.object_id])
                for selection in contest.ballot_selections
            ]

            contests.append(
                PlaintextBallotContest(contest.object_id, selections))

        plaintext_ballot = PlaintextBallot(self.ballot_id, ballot_style,
                                           contests)

        # TODO: store the audit information somewhere

        encrypted_ballot = serialize(
            encrypt_ballot(plaintext_ballot, self.context.election_metadata,
                           self.context.election_context, ElementModQ(0),
                           self.context.joint_key if deterministic else None,
                           True))

        return encrypted_ballot
コード例 #4
0
    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)
コード例 #5
0
 def test_div_q(self, q: ElementModQ):
     as_int = div_q(q, 1)
     as_elem = div_q(q, ElementModQ(1))
     self.assertEqual(as_int, as_elem)
コード例 #6
0
 def test_a_minus_b_q(self, q: ElementModQ):
     as_int = a_minus_b_q(q, 1)
     as_elem = a_minus_b_q(q, ElementModQ(1))
     self.assertEqual(as_int, as_elem)
コード例 #7
0
 def test_large_values_rejected_by_int_to_q(self, q: ElementModQ):
     oversize = q.to_int() + Q
     self.assertEqual(None, int_to_q(oversize))