From 593343a26881746a809dfa97d9d49531fad0784c Mon Sep 17 00:00:00 2001 From: Brites Date: Wed, 13 Oct 2021 20:05:31 +0200 Subject: [PATCH 01/21] Refactor code for new PolynomialCoefficients class --- src/electionguard/__init__.py | 2 ++ src/electionguard/election_polynomial.py | 28 +++++++++++++++--------- src/electionguard/key_ceremony.py | 9 ++++---- tests/unit/test_election_polynomial.py | 7 +++--- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/electionguard/__init__.py b/src/electionguard/__init__.py index a4203786..5db2484f 100644 --- a/src/electionguard/__init__.py +++ b/src/electionguard/__init__.py @@ -189,6 +189,7 @@ sequence_order_sort, ) from electionguard.election_polynomial import ( + PolynomialCoefficients, ElectionPolynomial, LagrangeCoefficientsRecord, PUBLIC_COMMITMENT, @@ -449,6 +450,7 @@ "ElectionPartialKeyBackup", "ElectionPartialKeyChallenge", "ElectionPartialKeyVerification", + "PolynomialCoefficients", "ElectionPolynomial", "ElectionPublicKey", "ElectionType", diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index c846b4a4..393b8083 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -21,6 +21,20 @@ SECRET_COEFFICIENT = ElementModQ # Secret coefficient of election polynomial PUBLIC_COMMITMENT = ElementModP # Public commitment of election polynomial +@dataclass +class PolynomialCoefficients: + """ + A set of coefficients that define an Election Polynomal + """ + + values: SECRET_COEFFICIENT + """The secret coefficient `a_ij` """ + + commitments: PUBLIC_COMMITMENT + """The public key `K_ij` generated from secret coefficient""" + + proofs: SchnorrProof + """A proof of possession of the private key for the secret coefficient""" @dataclass class ElectionPolynomial: @@ -31,14 +45,8 @@ class ElectionPolynomial: be discovered by a quorum of n guardians corresponding to n coefficients. """ - coefficients: List[SECRET_COEFFICIENT] - """The secret coefficients `a_ij` """ - - coefficient_commitments: List[PUBLIC_COMMITMENT] - """The public keys `K_ij`generated from secret coefficients""" - - coefficient_proofs: List[SchnorrProof] - """A proof of posession of the private key for each secret coefficient""" + coefficients: List[PolynomialCoefficients] + """A list of value, commitment and proof coefficients""" def generate_polynomial( @@ -67,7 +75,7 @@ def generate_polynomial( coefficients.append(coefficient) commitments.append(commitment) proofs.append(proof) - return ElectionPolynomial(coefficients, commitments, proofs) + return ElectionPolynomial(PolynomialCoefficients(coefficients, commitments, proofs)) def compute_polynomial_coordinate( @@ -84,7 +92,7 @@ def compute_polynomial_coordinate( exponent_modifier = ElementModQ(exponent_modifier) computed_value = ZERO_MOD_Q - for (i, coefficient) in enumerate(polynomial.coefficients): + for (i, coefficient) in enumerate(polynomial.coefficients.values): exponent = pow_q(exponent_modifier, i) factor = mult_q(coefficient, exponent) computed_value = add_q(computed_value, factor) diff --git a/src/electionguard/key_ceremony.py b/src/electionguard/key_ceremony.py index 3e5a630c..91ccfdc6 100644 --- a/src/electionguard/key_ceremony.py +++ b/src/electionguard/key_ceremony.py @@ -10,6 +10,7 @@ from .election_polynomial import ( PUBLIC_COMMITMENT, compute_polynomial_coordinate, + PolynomialCoefficients, ElectionPolynomial, generate_polynomial, verify_polynomial_coordinate, @@ -91,8 +92,8 @@ def share(self) -> ElectionPublicKey: self.owner_id, self.sequence_order, self.key_pair.public_key, - self.polynomial.coefficient_commitments, - self.polynomial.coefficient_proofs, + self.polynomial.coefficients.commitments, + self.polynomial.coefficients.proofs, ) @@ -301,8 +302,8 @@ def generate_election_partial_key_challenge( backup.designated_id, backup.designated_sequence_order, compute_polynomial_coordinate(backup.designated_sequence_order, polynomial), - polynomial.coefficient_commitments, - polynomial.coefficient_proofs, + polynomial.coefficients.commitments, + polynomial.coefficients.proofs, ) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index a973b78d..f4c3cf21 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -2,6 +2,7 @@ from electionguard.election_polynomial import ( compute_polynomial_coordinate, + PolynomialCoefficients, ElectionPolynomial, generate_polynomial, verify_polynomial_coordinate, @@ -25,11 +26,11 @@ def test_generate_polynomial(self): def test_compute_polynomial_coordinate(self): # Arrange - polynomial = ElectionPolynomial( + polynomial = ElectionPolynomial(PolynomialCoefficients( [ONE_MOD_Q, TWO_MOD_Q], [ONE_MOD_P, TWO_MOD_P], [], - ) + )) # Act value = compute_polynomial_coordinate(TEST_EXPONENT_MODIFIER, polynomial) @@ -47,6 +48,6 @@ def test_verify_polynomial_coordinate(self): # Assert self.assertTrue( verify_polynomial_coordinate( - value, TEST_EXPONENT_MODIFIER, polynomial.coefficient_commitments + value, TEST_EXPONENT_MODIFIER, polynomial.coefficients.commitments ) ) From 7631ef9af6aa4caba5a6916d7665746972239680 Mon Sep 17 00:00:00 2001 From: Brites Date: Wed, 13 Oct 2021 20:11:44 +0200 Subject: [PATCH 02/21] Refactor code for new PolynomialCoefficients class --- src/electionguard/election_polynomial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 393b8083..6067777b 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -46,7 +46,7 @@ class ElectionPolynomial: """ coefficients: List[PolynomialCoefficients] - """A list of value, commitment and proof coefficients""" + """List of coefficient values, commitments and proofs""" def generate_polynomial( From d74c8f92248754410feb96e54b3dd305401b0c1a Mon Sep 17 00:00:00 2001 From: Brites Date: Wed, 13 Oct 2021 23:15:35 +0200 Subject: [PATCH 03/21] fixed typo in the filepath to run tests --- docs/Build_and_Run.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Build_and_Run.md b/docs/Build_and_Run.md index 02888847..484728f5 100644 --- a/docs/Build_and_Run.md +++ b/docs/Build_and_Run.md @@ -81,5 +81,5 @@ make test OR ``` -pipenv run python -m pytest /testss +pipenv run python -m pytest /tests ``` From f819722227052b76a3deaa2f0c7b0732243c746a Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 13 Oct 2021 23:33:43 +0200 Subject: [PATCH 04/21] removed unsed import Removed unused PolynomialCoefficients import added in previous commit --- src/electionguard/key_ceremony.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/electionguard/key_ceremony.py b/src/electionguard/key_ceremony.py index 91ccfdc6..029f6451 100644 --- a/src/electionguard/key_ceremony.py +++ b/src/electionguard/key_ceremony.py @@ -10,7 +10,6 @@ from .election_polynomial import ( PUBLIC_COMMITMENT, compute_polynomial_coordinate, - PolynomialCoefficients, ElectionPolynomial, generate_polynomial, verify_polynomial_coordinate, From 8364602a4431eea76e98235179feec727cb4eb89 Mon Sep 17 00:00:00 2001 From: Brites Date: Wed, 20 Oct 2021 14:26:15 +0200 Subject: [PATCH 05/21] added methods to access commiment and proof --- src/electionguard/__init__.py | 3 +-- src/electionguard/election_polynomial.py | 19 +++++++++++++------ src/electionguard/key_ceremony.py | 8 ++++---- tests/unit/test_election_polynomial.py | 12 ++++++------ 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/electionguard/__init__.py b/src/electionguard/__init__.py index 5db2484f..94e1adcd 100644 --- a/src/electionguard/__init__.py +++ b/src/electionguard/__init__.py @@ -189,7 +189,6 @@ sequence_order_sort, ) from electionguard.election_polynomial import ( - PolynomialCoefficients, ElectionPolynomial, LagrangeCoefficientsRecord, PUBLIC_COMMITMENT, @@ -450,7 +449,7 @@ "ElectionPartialKeyBackup", "ElectionPartialKeyChallenge", "ElectionPartialKeyVerification", - "PolynomialCoefficients", + "Coefficient", "ElectionPolynomial", "ElectionPublicKey", "ElectionType", diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 6067777b..cd8a4197 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -22,12 +22,12 @@ PUBLIC_COMMITMENT = ElementModP # Public commitment of election polynomial @dataclass -class PolynomialCoefficients: +class Coefficient: """ A set of coefficients that define an Election Polynomal """ - values: SECRET_COEFFICIENT + value: SECRET_COEFFICIENT """The secret coefficient `a_ij` """ commitments: PUBLIC_COMMITMENT @@ -46,8 +46,15 @@ class ElectionPolynomial: """ coefficients: List[PolynomialCoefficients] - """List of coefficient values, commitments and proofs""" + """List of coefficient value, commitments and proofs""" + def get_commitments(self) -> List[PUBLIC_COMMITMENT]: + """Access the list of public keys generated from secret coefficient""" + return List[PUBLIC_COMMITMENT] + + def get_proofs(self) -> List[SchnorrProof]: + """Access the list of proof of possesion of the private key for the secret coefficient""" + return List[SchnorrProof] def generate_polynomial( number_of_coefficients: int, nonce: ElementModQ = None @@ -75,7 +82,7 @@ def generate_polynomial( coefficients.append(coefficient) commitments.append(commitment) proofs.append(proof) - return ElectionPolynomial(PolynomialCoefficients(coefficients, commitments, proofs)) + return ElectionPolynomial(Coefficient(coefficient, commitments, proofs)) def compute_polynomial_coordinate( @@ -92,9 +99,9 @@ def compute_polynomial_coordinate( exponent_modifier = ElementModQ(exponent_modifier) computed_value = ZERO_MOD_Q - for (i, coefficient) in enumerate(polynomial.coefficients.values): + for (i, coefficient) in enumerate(polynomial.coefficient): exponent = pow_q(exponent_modifier, i) - factor = mult_q(coefficient, exponent) + factor = mult_q(coefficient.value, exponent) computed_value = add_q(computed_value, factor) return computed_value diff --git a/src/electionguard/key_ceremony.py b/src/electionguard/key_ceremony.py index 91ccfdc6..c00baa92 100644 --- a/src/electionguard/key_ceremony.py +++ b/src/electionguard/key_ceremony.py @@ -92,8 +92,8 @@ def share(self) -> ElectionPublicKey: self.owner_id, self.sequence_order, self.key_pair.public_key, - self.polynomial.coefficients.commitments, - self.polynomial.coefficients.proofs, + self.polynomial.get_commitments, + self.polynomial.get_proofs, ) @@ -302,8 +302,8 @@ def generate_election_partial_key_challenge( backup.designated_id, backup.designated_sequence_order, compute_polynomial_coordinate(backup.designated_sequence_order, polynomial), - polynomial.coefficients.commitments, - polynomial.coefficients.proofs, + polynomial.get_commitments, + polynomial.get_proofs, ) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index f4c3cf21..53698829 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -1,6 +1,7 @@ from tests.base_test_case import BaseTestCase from electionguard.election_polynomial import ( + Coefficient, compute_polynomial_coordinate, PolynomialCoefficients, ElectionPolynomial, @@ -26,11 +27,10 @@ def test_generate_polynomial(self): def test_compute_polynomial_coordinate(self): # Arrange - polynomial = ElectionPolynomial(PolynomialCoefficients( - [ONE_MOD_Q, TWO_MOD_Q], - [ONE_MOD_P, TWO_MOD_P], - [], - )) + polynomial = ElectionPolynomial( + Coefficient(ONE_MOD_Q, TWO_MOD_Q,), + Coefficient(ONE_MOD_P, TWO_MOD_P,) + ) # Act value = compute_polynomial_coordinate(TEST_EXPONENT_MODIFIER, polynomial) @@ -48,6 +48,6 @@ def test_verify_polynomial_coordinate(self): # Assert self.assertTrue( verify_polynomial_coordinate( - value, TEST_EXPONENT_MODIFIER, polynomial.coefficients.commitments + value, TEST_EXPONENT_MODIFIER, polynomial.get_commitments ) ) From 51fdeb3a3e7b31d05431af9df85ad4dc2308ff0f Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 14:32:21 +0200 Subject: [PATCH 06/21] Update election_polynomial.py --- src/electionguard/election_polynomial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index cd8a4197..40cb1158 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -45,7 +45,7 @@ class ElectionPolynomial: be discovered by a quorum of n guardians corresponding to n coefficients. """ - coefficients: List[PolynomialCoefficients] + coefficients: List[Coefficient] """List of coefficient value, commitments and proofs""" def get_commitments(self) -> List[PUBLIC_COMMITMENT]: From 4f39f7ebfe3408cae9038e5fc802fda688c3ddd3 Mon Sep 17 00:00:00 2001 From: Brites Date: Wed, 20 Oct 2021 23:10:51 +0200 Subject: [PATCH 07/21] fixed method calls and tests --- src/electionguard/election_polynomial.py | 29 ++++++++++-------------- src/electionguard/key_ceremony.py | 8 +++---- tests/unit/test_election_polynomial.py | 14 ++++++++---- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index cd8a4197..08894420 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -30,10 +30,10 @@ class Coefficient: value: SECRET_COEFFICIENT """The secret coefficient `a_ij` """ - commitments: PUBLIC_COMMITMENT + commitment: PUBLIC_COMMITMENT """The public key `K_ij` generated from secret coefficient""" - proofs: SchnorrProof + proof: SchnorrProof """A proof of possession of the private key for the secret coefficient""" @dataclass @@ -50,11 +50,11 @@ class ElectionPolynomial: def get_commitments(self) -> List[PUBLIC_COMMITMENT]: """Access the list of public keys generated from secret coefficient""" - return List[PUBLIC_COMMITMENT] + return coefficient.commitment for coefficient in self.coefficients def get_proofs(self) -> List[SchnorrProof]: """Access the list of proof of possesion of the private key for the secret coefficient""" - return List[SchnorrProof] + return return coefficient.proof for coefficient in self.coefficients def generate_polynomial( number_of_coefficients: int, nonce: ElementModQ = None @@ -66,23 +66,18 @@ def generate_polynomial( :param nonce: an optional nonce parameter that may be provided (useful for testing) :return: Polynomial used to share election keys """ - coefficients: List[SECRET_COEFFICIENT] = [] - commitments: List[PUBLIC_COMMITMENT] = [] - proofs: List[SchnorrProof] = [] + coefficients: List[Coefficient] = [] for i in range(number_of_coefficients): # Note: the nonce value is not safe. it is designed for testing only. # this method should be called without the nonce in production. - coefficient = add_q(nonce, i) if nonce is not None else rand_q() + value = add_q(nonce, i) if nonce is not None else rand_q() commitment = g_pow_p(coefficient) - proof = make_schnorr_proof( - ElGamalKeyPair(coefficient, commitment), rand_q() - ) # TODO Alternate schnoor proof method that doesn't need KeyPair - + # TODO Alternate schnoor proof method that doesn't need KeyPair + proof = make_schnorr_proof(ElGamalKeyPair(coefficient, commitment), rand_q()) + coefficient = Coefficient(value, commitment, proof) coefficients.append(coefficient) - commitments.append(commitment) - proofs.append(proof) - return ElectionPolynomial(Coefficient(coefficient, commitments, proofs)) + return ElectionPolynomial(coefficient) def compute_polynomial_coordinate( @@ -99,9 +94,9 @@ def compute_polynomial_coordinate( exponent_modifier = ElementModQ(exponent_modifier) computed_value = ZERO_MOD_Q - for (i, coefficient) in enumerate(polynomial.coefficient): + for (i, coefficient) in enumerate(polynomial.coefficients): exponent = pow_q(exponent_modifier, i) - factor = mult_q(coefficient.value, exponent) + factor = mult_q(coefficients.value, exponent) computed_value = add_q(computed_value, factor) return computed_value diff --git a/src/electionguard/key_ceremony.py b/src/electionguard/key_ceremony.py index c56053bd..12215e98 100644 --- a/src/electionguard/key_ceremony.py +++ b/src/electionguard/key_ceremony.py @@ -91,8 +91,8 @@ def share(self) -> ElectionPublicKey: self.owner_id, self.sequence_order, self.key_pair.public_key, - self.polynomial.get_commitments, - self.polynomial.get_proofs, + self.polynomial.get_commitments(), + self.polynomial.get_proofs(), ) @@ -301,8 +301,8 @@ def generate_election_partial_key_challenge( backup.designated_id, backup.designated_sequence_order, compute_polynomial_coordinate(backup.designated_sequence_order, polynomial), - polynomial.get_commitments, - polynomial.get_proofs, + polynomial.get_commitments(), + polynomial.get_proofs(), ) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 53698829..7b8bc2ad 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -26,11 +26,17 @@ def test_generate_polynomial(self): self.assertIsNotNone(polynomial) def test_compute_polynomial_coordinate(self): + # create proofs + proof = make_schnorr_proof(ElGamalKeyPair([ONE_MOD_Q, TWO_MOD_Q], + [ONE_MOD_P, TWO_MOD_P]), + rand_q()) + # Arrange polynomial = ElectionPolynomial( - Coefficient(ONE_MOD_Q, TWO_MOD_Q,), - Coefficient(ONE_MOD_P, TWO_MOD_P,) - ) + Coefficient([ONE_MOD_Q, TWO_MOD_Q], + [ONE_MOD_P, TWO_MOD_P], + proof) + ) # Act value = compute_polynomial_coordinate(TEST_EXPONENT_MODIFIER, polynomial) @@ -48,6 +54,6 @@ def test_verify_polynomial_coordinate(self): # Assert self.assertTrue( verify_polynomial_coordinate( - value, TEST_EXPONENT_MODIFIER, polynomial.get_commitments + value, TEST_EXPONENT_MODIFIER, polynomial.get_commitments() ) ) From 229a71c6ebbf4d5890b2b736e5bf7aec7a13af3d Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:13:47 +0200 Subject: [PATCH 08/21] Update election_polynomial.py --- src/electionguard/election_polynomial.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 3d33a6c3..4006ea6d 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -74,9 +74,8 @@ def generate_polynomial( value = add_q(nonce, i) if nonce is not None else rand_q() commitment = g_pow_p(coefficient) # TODO Alternate schnoor proof method that doesn't need KeyPair - proof = make_schnorr_proof(ElGamalKeyPair(coefficient, commitment), rand_q()) + proof = make_schnorr_proof(ElGamalKeyPair(value, commitment), rand_q()) coefficient = Coefficient(value, commitment, proof) - coefficients.append(coefficient) return ElectionPolynomial(coefficient) From e41fe1f5822b18e6b22fdecd5cfac51ab638e4e1 Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:14:44 +0200 Subject: [PATCH 09/21] Update test_election_polynomial.py --- tests/unit/test_election_polynomial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 7b8bc2ad..3cdada05 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -26,7 +26,7 @@ def test_generate_polynomial(self): self.assertIsNotNone(polynomial) def test_compute_polynomial_coordinate(self): - # create proofs + # create proof proof = make_schnorr_proof(ElGamalKeyPair([ONE_MOD_Q, TWO_MOD_Q], [ONE_MOD_P, TWO_MOD_P]), rand_q()) From 45bcc6d856114ed53c4d6786accd70f7835ce22e Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:24:04 +0200 Subject: [PATCH 10/21] Update __init__.py --- src/electionguard/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/electionguard/__init__.py b/src/electionguard/__init__.py index 94e1adcd..6d1c954b 100644 --- a/src/electionguard/__init__.py +++ b/src/electionguard/__init__.py @@ -190,6 +190,7 @@ ) from electionguard.election_polynomial import ( ElectionPolynomial, + Coefficient LagrangeCoefficientsRecord, PUBLIC_COMMITMENT, SECRET_COEFFICIENT, From 9b52466b14d35800d522830c3386ec5a992b1295 Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:29:18 +0200 Subject: [PATCH 11/21] Update election_polynomial.py --- src/electionguard/election_polynomial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 4006ea6d..96a9c491 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -54,7 +54,7 @@ def get_commitments(self) -> List[PUBLIC_COMMITMENT]: def get_proofs(self) -> List[SchnorrProof]: """Access the list of proof of possesion of the private key for the secret coefficient""" - return return coefficient.proof for coefficient in self.coefficients + return coefficient.proof for coefficient in self.coefficients def generate_polynomial( number_of_coefficients: int, nonce: ElementModQ = None From da0217254a9e83a695ec917226332068846a0c04 Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:37:32 +0200 Subject: [PATCH 12/21] Update __init__.py --- src/electionguard/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electionguard/__init__.py b/src/electionguard/__init__.py index 6d1c954b..ef0c00a4 100644 --- a/src/electionguard/__init__.py +++ b/src/electionguard/__init__.py @@ -190,7 +190,7 @@ ) from electionguard.election_polynomial import ( ElectionPolynomial, - Coefficient + Coefficient, LagrangeCoefficientsRecord, PUBLIC_COMMITMENT, SECRET_COEFFICIENT, From 2d53e0bd60cf3dfd4ee579bb4b3de53b5253c4fc Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:48:45 +0200 Subject: [PATCH 13/21] Update election_polynomial.py --- src/electionguard/election_polynomial.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 96a9c491..4a403743 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -50,11 +50,11 @@ class ElectionPolynomial: def get_commitments(self) -> List[PUBLIC_COMMITMENT]: """Access the list of public keys generated from secret coefficient""" - return coefficient.commitment for coefficient in self.coefficients + return [coefficient.commitment for coefficient in self.coefficients] def get_proofs(self) -> List[SchnorrProof]: """Access the list of proof of possesion of the private key for the secret coefficient""" - return coefficient.proof for coefficient in self.coefficients + return [coefficient.proof for coefficient in self.coefficients] def generate_polynomial( number_of_coefficients: int, nonce: ElementModQ = None From b4578f9eaa973ad5d687087b6de87574c31d12bb Mon Sep 17 00:00:00 2001 From: Brites Date: Thu, 21 Oct 2021 23:51:52 +0200 Subject: [PATCH 14/21] refactor test_compute_polynomial_coordinate --- src/electionguard/election_polynomial.py | 12 +++++++----- tests/unit/test_election_polynomial.py | 12 ++++-------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 4006ea6d..20990eaa 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -72,11 +72,13 @@ def generate_polynomial( # Note: the nonce value is not safe. it is designed for testing only. # this method should be called without the nonce in production. value = add_q(nonce, i) if nonce is not None else rand_q() - commitment = g_pow_p(coefficient) - # TODO Alternate schnoor proof method that doesn't need KeyPair - proof = make_schnorr_proof(ElGamalKeyPair(value, commitment), rand_q()) + commitment = g_pow_p(value) + proof = make_schnorr_proof( + ElGamalKeyPair(value, commitment), rand_q() + ) # TODO Alternate schnoor proof method that doesn't need KeyPair coefficient = Coefficient(value, commitment, proof) - return ElectionPolynomial(coefficient) + coefficients.append(coefficient) + return ElectionPolynomial(coefficients) def compute_polynomial_coordinate( @@ -95,7 +97,7 @@ def compute_polynomial_coordinate( computed_value = ZERO_MOD_Q for (i, coefficient) in enumerate(polynomial.coefficients): exponent = pow_q(exponent_modifier, i) - factor = mult_q(coefficients.value, exponent) + factor = mult_q(coefficient.value, exponent) computed_value = add_q(computed_value, factor) return computed_value diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 7b8bc2ad..a91da181 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -27,16 +27,12 @@ def test_generate_polynomial(self): def test_compute_polynomial_coordinate(self): # create proofs - proof = make_schnorr_proof(ElGamalKeyPair([ONE_MOD_Q, TWO_MOD_Q], - [ONE_MOD_P, TWO_MOD_P]), - rand_q()) + proof_one = make_schnorr_proof(ElGamalKeyPair(ONE_MOD_Q, ONE_MOD_P), rand_q()) + proof_two = make_schnorr_proof(ElGamalKeyPair(TWO_MOD_Q, TWO_MOD_P), rand_q()) # Arrange - polynomial = ElectionPolynomial( - Coefficient([ONE_MOD_Q, TWO_MOD_Q], - [ONE_MOD_P, TWO_MOD_P], - proof) - ) + polynomial = ElectionPolynomial([Coefficient(ONE_MOD_Q, ONE_MOD_P, proof_one), + Coefficient(TWO_MOD_Q, TWO_MOD_P, proof_two)] # Act value = compute_polynomial_coordinate(TEST_EXPONENT_MODIFIER, polynomial) From a8bd0440ff2c60e66f3e63ae218cbaa1aa234a33 Mon Sep 17 00:00:00 2001 From: Brites Date: Thu, 21 Oct 2021 23:54:57 +0200 Subject: [PATCH 15/21] refactor test_compute_polynomial_coordinate --- tests/unit/test_election_polynomial.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 99ef4cd4..663aac75 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -29,6 +29,7 @@ def test_compute_polynomial_coordinate(self): # create proofs proof_one = make_schnorr_proof(ElGamalKeyPair(ONE_MOD_Q, ONE_MOD_P), rand_q()) proof_two = make_schnorr_proof(ElGamalKeyPair(TWO_MOD_Q, TWO_MOD_P), rand_q()) + # Arrange polynomial = ElectionPolynomial([Coefficient(ONE_MOD_Q, ONE_MOD_P, proof_one), Coefficient(TWO_MOD_Q, TWO_MOD_P, proof_two)] From 9e849fb6b0311bd42926a397013b99510188abdd Mon Sep 17 00:00:00 2001 From: Brites Date: Mon, 25 Oct 2021 14:20:42 +0200 Subject: [PATCH 16/21] removed trailing whitespaces --- src/electionguard/election_polynomial.py | 12 ++++++------ src/electionguard/key_ceremony.py | 2 +- tests/unit/test_election_polynomial.py | 6 ++---- tests/unit/test_key_ceremony.py | 10 ++++------ 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index fa6445c8..7f915cf3 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -24,12 +24,12 @@ @dataclass class Coefficient: """ - A set of coefficients that define an Election Polynomal + A set of coefficients that define an Election Polynomal """ - value: SECRET_COEFFICIENT + value: SECRET_COEFFICIENT """The secret coefficient `a_ij` """ - + commitment: PUBLIC_COMMITMENT """The public key `K_ij` generated from secret coefficient""" @@ -69,13 +69,13 @@ def generate_polynomial( coefficients: List[Coefficient] = [] for i in range(number_of_coefficients): - # Note: the nonce value is not safe. it is designed for testing only. + # Note: the nonce value is not safe. it is designed for testing only. # this method should be called without the nonce in production. value = add_q(nonce, i) if nonce is not None else rand_q() commitment = g_pow_p(value) proof = make_schnorr_proof( - ElGamalKeyPair(value, commitment), rand_q() - ) # TODO Alternate schnoor proof method that doesn't need KeyPair + ElGamalKeyPair(value, commitment), rand_q() + ) # TODO Alternate schnoor proof method that doesn't need KeyPair coefficient = Coefficient(value, commitment, proof) coefficients.append(coefficient) return ElectionPolynomial(coefficients) diff --git a/src/electionguard/key_ceremony.py b/src/electionguard/key_ceremony.py index 12215e98..55194c14 100644 --- a/src/electionguard/key_ceremony.py +++ b/src/electionguard/key_ceremony.py @@ -217,7 +217,7 @@ def generate_election_key_pair( """ polynomial = generate_polynomial(quorum, nonce) key_pair = ElGamalKeyPair( - polynomial.coefficients[0], polynomial.coefficient_commitments[0] + polynomial.coefficients[0].value, polynomial.coefficients[0].commitment ) return ElectionKeyPair(guardian_id, sequence_order, key_pair, polynomial) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 663aac75..06cd211c 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -3,7 +3,6 @@ from electionguard.election_polynomial import ( Coefficient, compute_polynomial_coordinate, - PolynomialCoefficients, ElectionPolynomial, generate_polynomial, verify_polynomial_coordinate, @@ -29,11 +28,10 @@ def test_compute_polynomial_coordinate(self): # create proofs proof_one = make_schnorr_proof(ElGamalKeyPair(ONE_MOD_Q, ONE_MOD_P), rand_q()) proof_two = make_schnorr_proof(ElGamalKeyPair(TWO_MOD_Q, TWO_MOD_P), rand_q()) - + # Arrange polynomial = ElectionPolynomial([Coefficient(ONE_MOD_Q, ONE_MOD_P, proof_one), - Coefficient(TWO_MOD_Q, TWO_MOD_P, proof_two)] - + Coefficient(TWO_MOD_Q, TWO_MOD_P, proof_two)]) # Act value = compute_polynomial_coordinate(TEST_EXPONENT_MODIFIER, polynomial) diff --git a/tests/unit/test_key_ceremony.py b/tests/unit/test_key_ceremony.py index 839c911e..8be1e576 100644 --- a/tests/unit/test_key_ceremony.py +++ b/tests/unit/test_key_ceremony.py @@ -50,12 +50,10 @@ def test_generate_election_key_pair(self): self.assertIsNotNone(election_key_pair.key_pair.public_key) self.assertIsNotNone(election_key_pair.key_pair.secret_key) self.assertIsNotNone(election_key_pair.polynomial) - self.assertEqual( - len(election_key_pair.polynomial.coefficient_commitments), QUORUM - ) - self.assertEqual(len(election_key_pair.polynomial.coefficient_proofs), QUORUM) - for proof in election_key_pair.polynomial.coefficient_proofs: - self.assertTrue(proof.is_valid()) + + self.assertEqual(len(election_key_pair.polynomial.coefficients), QUORUM) + for coefficient in election_key_pair.polynomial.coefficients: + self.assertTrue(coefficient.proof.is_valid()) def test_generate_election_partial_key_backup(self): # Arrange From 6a23f713959f851b9cd092457f27def387873edb Mon Sep 17 00:00:00 2001 From: Brites Date: Thu, 28 Oct 2021 22:48:15 +0200 Subject: [PATCH 17/21] addded missing imports --- tests/unit/test_election_polynomial.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 06cd211c..4a5b6b77 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -1,5 +1,7 @@ from tests.base_test_case import BaseTestCase - +from electionguard.schnorr import make_schnorr_proof +from electionguard.elgamal import ElGamalKeyPair +from electionguard.group import rand_q from electionguard.election_polynomial import ( Coefficient, compute_polynomial_coordinate, From c8d2feb4e488d188ab7797e6ba03c2bcc1460750 Mon Sep 17 00:00:00 2001 From: Brites Date: Sun, 31 Oct 2021 18:10:24 +0100 Subject: [PATCH 18/21] black formatting --- src/electionguard/election_polynomial.py | 5 ++++- tests/unit/test_election_polynomial.py | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 7f915cf3..657c61e2 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -21,6 +21,7 @@ SECRET_COEFFICIENT = ElementModQ # Secret coefficient of election polynomial PUBLIC_COMMITMENT = ElementModP # Public commitment of election polynomial + @dataclass class Coefficient: """ @@ -36,6 +37,7 @@ class Coefficient: proof: SchnorrProof """A proof of possession of the private key for the secret coefficient""" + @dataclass class ElectionPolynomial: """ @@ -56,6 +58,7 @@ def get_proofs(self) -> List[SchnorrProof]: """Access the list of proof of possesion of the private key for the secret coefficient""" return [coefficient.proof for coefficient in self.coefficients] + def generate_polynomial( number_of_coefficients: int, nonce: ElementModQ = None ) -> ElectionPolynomial: @@ -75,7 +78,7 @@ def generate_polynomial( commitment = g_pow_p(value) proof = make_schnorr_proof( ElGamalKeyPair(value, commitment), rand_q() - ) # TODO Alternate schnoor proof method that doesn't need KeyPair + ) # TODO Alternate schnoor proof method that doesn't need KeyPair coefficient = Coefficient(value, commitment, proof) coefficients.append(coefficient) return ElectionPolynomial(coefficients) diff --git a/tests/unit/test_election_polynomial.py b/tests/unit/test_election_polynomial.py index 4a5b6b77..428fc0fe 100644 --- a/tests/unit/test_election_polynomial.py +++ b/tests/unit/test_election_polynomial.py @@ -32,8 +32,12 @@ def test_compute_polynomial_coordinate(self): proof_two = make_schnorr_proof(ElGamalKeyPair(TWO_MOD_Q, TWO_MOD_P), rand_q()) # Arrange - polynomial = ElectionPolynomial([Coefficient(ONE_MOD_Q, ONE_MOD_P, proof_one), - Coefficient(TWO_MOD_Q, TWO_MOD_P, proof_two)]) + polynomial = ElectionPolynomial( + [ + Coefficient(ONE_MOD_Q, ONE_MOD_P, proof_one), + Coefficient(TWO_MOD_Q, TWO_MOD_P, proof_two), + ] + ) # Act value = compute_polynomial_coordinate(TEST_EXPONENT_MODIFIER, polynomial) From ea75e766191e4c5d3b1c3125201db98f0bdc2c3e Mon Sep 17 00:00:00 2001 From: Brites <50711059+Brites101@users.noreply.github.com> Date: Mon, 1 Nov 2021 19:31:08 +0100 Subject: [PATCH 19/21] Update election_polynomial.py --- src/electionguard/election_polynomial.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index eb6c90d3..dcf099bd 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -18,8 +18,8 @@ ) from .schnorr import make_schnorr_proof, SchnorrProof -SecretCoefficient = ElementModQ # Secret coefficient of election polynomial -PublicCommitment = ElementModP # Public commitment of election polynomial +SECRET_COEFFICIENT = ElementModQ # Secret coefficient of election polynomial +PUBLIC_COMMITMENT = ElementModP # Public commitment of election polynomial @dataclass From 74752a794147d505bf3af18393bf7b9711e16ebf Mon Sep 17 00:00:00 2001 From: Keith Fung Date: Wed, 3 Nov 2021 19:21:07 -0400 Subject: [PATCH 20/21] =?UTF-8?q?=E2=9C=85=20Fix=20linting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/electionguard/__init__.py | 4 ++-- src/electionguard/election_polynomial.py | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/electionguard/__init__.py b/src/electionguard/__init__.py index 147c4316..04cffe26 100644 --- a/src/electionguard/__init__.py +++ b/src/electionguard/__init__.py @@ -185,8 +185,8 @@ sequence_order_sort, ) from electionguard.election_polynomial import ( - ElectionPolynomial, Coefficient, + ElectionPolynomial, LagrangeCoefficientsRecord, PublicCommitment, SecretCoefficient, @@ -410,6 +410,7 @@ "CiphertextTally", "CiphertextTallyContest", "CiphertextTallySelection", + "Coefficient", "CompactPlaintextBallot", "CompactSubmittedBallot", "CompensatedDecryptionShare", @@ -443,7 +444,6 @@ "ElectionPartialKeyBackup", "ElectionPartialKeyChallenge", "ElectionPartialKeyVerification", - "Coefficient", "ElectionPolynomial", "ElectionPublicKey", "ElectionType", diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index dcf099bd..1675f229 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -18,8 +18,8 @@ ) from .schnorr import make_schnorr_proof, SchnorrProof -SECRET_COEFFICIENT = ElementModQ # Secret coefficient of election polynomial -PUBLIC_COMMITMENT = ElementModP # Public commitment of election polynomial +SecretCoefficient = ElementModQ # Secret coefficient of election polynomial +PublicCommitment = ElementModP # Public commitment of election polynomial @dataclass @@ -28,10 +28,10 @@ class Coefficient: A set of coefficients that define an Election Polynomal """ - value: SECRET_COEFFICIENT + value: SecretCoefficient """The secret coefficient `a_ij` """ - commitment: PUBLIC_COMMITMENT + commitment: PublicCommitment """The public key `K_ij` generated from secret coefficient""" proof: SchnorrProof @@ -50,7 +50,7 @@ class ElectionPolynomial: coefficients: List[Coefficient] """List of coefficient value, commitments and proofs""" - def get_commitments(self) -> List[PUBLIC_COMMITMENT]: + def get_commitments(self) -> List[PublicCommitment]: """Access the list of public keys generated from secret coefficient""" return [coefficient.commitment for coefficient in self.coefficients] @@ -71,7 +71,6 @@ def generate_polynomial( """ coefficients: List[Coefficient] = [] - for i in range(number_of_coefficients): # Note: the nonce value is not safe. it is designed for testing only. # this method should be called without the nonce in production. From ce88c010b0cc22c542659264ddd4dabd5144155d Mon Sep 17 00:00:00 2001 From: Keith Fung Date: Thu, 4 Nov 2021 10:02:39 -0400 Subject: [PATCH 21/21] =?UTF-8?q?=F0=9F=93=9D=20Fix=20plural=20to=20singul?= =?UTF-8?q?ar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/electionguard/election_polynomial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/electionguard/election_polynomial.py b/src/electionguard/election_polynomial.py index 1675f229..011f66e5 100644 --- a/src/electionguard/election_polynomial.py +++ b/src/electionguard/election_polynomial.py @@ -25,7 +25,7 @@ @dataclass class Coefficient: """ - A set of coefficients that define an Election Polynomal + A coefficient of an Election Polynomial """ value: SecretCoefficient