Skip to content

Conversation

@Brites101
Copy link
Contributor

@Brites101 Brites101 commented Oct 13, 2021

Issue

Fixes #473

Description

  • Refactored ElectionPolynomial to use a new PolynomialCoefficients dataclass.

  • Refactored references to ElectionPolynomial

  • Fixed a typo in the Build and Run guide.

Testing

Not sure if there was a better way to test these changes. I performed the test included in the Build and Run guide.

@ghost
Copy link

ghost commented Oct 13, 2021

CLA assistant check
All CLA requirements met.

@Brites101 Brites101 changed the title Refactor-polynomialcoefficients Refactor ElectionPolynomial to use a Coefficient class Oct 13, 2021
@Brites101 Brites101 marked this pull request as ready for review October 13, 2021 21:20
@Brites101 Brites101 changed the title Refactor ElectionPolynomial to use a Coefficient class Refactor ElectionPolynomial to use PolynomialCoefficients dataclass Oct 13, 2021
@lgtm-com
Copy link

lgtm-com bot commented Oct 13, 2021

This pull request introduces 1 alert when merging d74c8f9 into 82df51b - view on LGTM.com

new alerts:

  • 1 for Unused import

Removed unused PolynomialCoefficients import added in previous commit
@keithrfung keithrfung self-requested a review October 14, 2021 13:18
@keithrfung keithrfung added the hacktoberfest Issues for the Hacktoberfest label Oct 14, 2021

computed_value = ZERO_MOD_Q
for (i, coefficient) in enumerate(polynomial.coefficients):
for (i, coefficient) in enumerate(polynomial.coefficients.values):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

polynomial.coefficients should return an List of PolynomialCoefficient here so you shouldn't be able to get the property of it like this.

I believe it would be

for (i, coefficient) in enumerate(polynomial.coefficients):
      ...
      factor = mult_q(coefficient.value, exponent)
      ...

self.polynomial.coefficient_commitments,
self.polynomial.coefficient_proofs,
self.polynomial.coefficients.commitments,
self.polynomial.coefficients.proofs,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comments. These lines shouldn't function as expected.

Originally this was

self.polynomial.coefficient_commitments: List[ElementModP]

now it's

self.polynomial.coefficients: List[PolynomialCoefficient]

This could be addressed by adding a convenience method to the polynomial class such as

def get_commitments(self) -> List[ElementModP]

Copy link
Contributor Author

@Brites101 Brites101 Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking time to write this review. my initial solution was like this

self.polynomial.coefficients.commitments: PUBLIC_COMMITMENT

The issue is that it was not returning the expected list, but the constant directly?

I will add the suggested method. Shouldn't it access PUBLIC_COMMITMENT instead of ElementModP directly?

def get_commitments(self) -> List[PUBLIC_COMMITMENT]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It should access PUBLIC_COMMITMENT that would be ideal.

compute_polynomial_coordinate(backup.designated_sequence_order, polynomial),
polynomial.coefficient_commitments,
polynomial.coefficient_proofs,
polynomial.coefficients.commitments,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

def test_compute_polynomial_coordinate(self):
# Arrange
polynomial = ElectionPolynomial(
polynomial = ElectionPolynomial(PolynomialCoefficients(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line actually changes pretty significantly. I tried to break it up for you to showcase what is happening. This could all be one line.

coefficient_1 = PolynomialCoefficient(ONE_MOD_Q, ONE_MOD_P, Proof_1)
coefficient_2 = PolynomialCoefficient(TWO_MOD_Q, TWO_MOD_P, Proof_2)
polynomial = ElectionPolynomial([coefficient_1, coefficient_2])

self.assertTrue(
verify_polynomial_coordinate(
value, TEST_EXPONENT_MODIFIER, polynomial.coefficient_commitments
value, TEST_EXPONENT_MODIFIER, polynomial.coefficients.commitments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comments.

@keithrfung
Copy link
Contributor

Really good first attempt. The helper methods I suggested on the class may make this easier for you.

class ElectionPolynomial():
     ...
     def get_commitments(self)-> List[ElementModP]:
             return [coefficient.commitment for coefficient in self.coefficients]
    ...

@keithrfung
Copy link
Contributor

@Brites101 Following up, any progress?

Copy link
Contributor Author

@Brites101 Brites101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Brites101 Following up, any progress?

Yes! Just submitted some corrections. Thank you for the thorough review before. Please check if these fixes are properly implemented.

[ONE_MOD_P, TWO_MOD_P],
[],
)
Coefficient(ONE_MOD_Q, TWO_MOD_Q,),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keithrfung , i left the proof blank, as it was the case in the previous iteration of this test, but i am unsure if this is correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the typing will yell at you for this. Try constructing a proof instead.

proof = make_schnorr_proof(ElGamalKeyPair(coefficient, commitment), rand_q())

@lgtm-com
Copy link

lgtm-com bot commented Oct 20, 2021

This pull request introduces 1 alert when merging b9969aa into 3a56cee - view on LGTM.com

new alerts:

  • 1 for Explicit export is not defined

Copy link
Contributor

@keithrfung keithrfung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving in the right direction but still has a bit more work to do. This will likely fail the build and the linter.

I tried to be thorough on notes here to give you a hand. Don't get discouraged. You got this.

[ONE_MOD_P, TWO_MOD_P],
[],
)
Coefficient(ONE_MOD_Q, TWO_MOD_Q,),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the typing will yell at you for this. Try constructing a proof instead.

proof = make_schnorr_proof(ElGamalKeyPair(coefficient, commitment), rand_q())

@lgtm-com
Copy link

lgtm-com bot commented Oct 20, 2021

This pull request introduces 2 alerts when merging e41fe1f into 3a56cee - view on LGTM.com

new alerts:

  • 1 for Syntax error
  • 1 for Explicit export is not defined

@lgtm-com
Copy link

lgtm-com bot commented Oct 20, 2021

This pull request introduces 2 alerts when merging 9b52466 into 3a56cee - view on LGTM.com

new alerts:

  • 2 for Syntax error

@lgtm-com
Copy link

lgtm-com bot commented Oct 20, 2021

This pull request introduces 1 alert when merging da02172 into 3a56cee - view on LGTM.com

new alerts:

  • 1 for Syntax error

Copy link
Contributor

@keithrfung keithrfung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comments

@keithrfung
Copy link
Contributor

As a helpful suggestion, before committing code, be sure to run:

  1. make lint to lint the code
  2. make test to test the code

@Brites101
Copy link
Contributor Author

1. make lint

As a helpful suggestion, before committing code, be sure to run:

1. `make lint` to lint the code

2. `make test` to test the code

Thank you for this. I tought this was for Linux/MAC, but now i see how to install it on windows. i will run it in future commits

Copy link
Contributor

@keithrfung keithrfung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a quick white space fix in the class, but the base code is now working, but the dependencies need some fixing. See the files called out by the linter.

Run make lint to continue to track these down.

************* Module electionguard.election_polynomial
src/electionguard/election_polynomial.py:27:59: C0303: Trailing whitespace (trailing-whitespace)
src/electionguard/election_polynomial.py:30:29: C0303: Trailing whitespace (trailing-whitespace)
src/electionguard/election_polynomial.py:32:0: C0303: Trailing whitespace (trailing-whitespace)
************* Module electionguard.key_ceremony
src/electionguard/key_ceremony.py:220:36: E1101: Instance of 'ElectionPolynomial' has no 'coefficient_commitments' member (no-member)
************* Module tests.unit.test_election_polynomial
tests/unit/test_election_polynomial.py:38:9: E0001: invalid syntax (<unknown>, line 38) (syntax-error)
************* Module tests.unit.test_key_ceremony
tests/unit/test_key_ceremony.py:54:16: E1101: Instance of 'ElectionPolynomial' has no 'coefficient_commitments' member (no-member)
tests/unit/test_key_ceremony.py:56:29: E1101: Instance of 'ElectionPolynomial' has no 'coefficient_proofs' member (no-member)
tests/unit/test_key_ceremony.py:57:21: E1101: Instance of 'ElectionPolynomial' has no 'coefficient_proofs' member (no-member)

Here's some quick help

src/electionguard/key_ceremony.py
https://github.com/microsoft/electionguard-python/blob/3a56cee6ba9cb722ab02d7bf885c87f8d788b0ee/src/electionguard/key_ceremony.py#L219-L221

This should be referencing the first coefficient like so:

key_pair = ElGamalKeyPair(
      polynomial.coefficients[0].value, polynomial.coefficients[0].commitment
)

tests/unit/test_key_ceremony.py
https://github.com/microsoft/electionguard-python/blob/3a56cee6ba9cb722ab02d7bf885c87f8d788b0ee/tests/unit/test_key_ceremony.py#L53-L59

The tests can change to be a bit more simple. The number of coefficients should equal the quorum and the proof line just needs to be adjusted.

self.assertEqual(len(election_key_pair.polynomial.coefficients), QUORUM)
for coefficient in election_key_pair.polynomial.coefficients:
      self.assertTrue(coefficient.proof.is_valid())

@Brites101
Copy link
Contributor Author

I've quite a few issues attempting to run the tests properly during the weekend. i was able to remove whitespaces and unused imports, but i am still trying to properly run all tests.

Copy link
Contributor

@keithrfung keithrfung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment.

@Brites101 Brites101 requested a review from keithrfung October 31, 2021 17:17
@keithrfung keithrfung requested review from SteveMaier-IRT and keithrfung and removed request for keithrfung November 3, 2021 23:21
@keithrfung keithrfung merged commit dcdcdcc into Election-Tech-Initiative:main Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest Issues for the Hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

♻️. Refactor ElectionPolynomial to use a Coefficient class

3 participants