Election-Tech-Initiative / electionguard-python

A python module implementing the ElectionGuard specification. This implementation can be used to conduct End-to-End Verifiable Elections as well as privacy-enhanced risk-limiting audits.
https://www.electionguard.vote/
MIT License
162 stars 96 forks source link

Deserialization issue with Proof Usage #132

Closed danwallach closed 4 years ago

danwallach commented 4 years ago

Here's the code to deserialize a ProofUsage enum:

    set_deserializer(
        lambda usage_string, cls, **_: ProofUsage[usage_string], ProofUsage
    )

You can't just use ProofUsage[usage_string]. That crashes, as below:

>>> from electionguard.proof import ProofUsage
>>> ProofUsage["SecretValue"]
<ProofUsage.SecretValue: 'Prove knowledge of secret value'>
>>> ProofUsage["Prove knowledge of secret value"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python@3.8/3.8.2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 344, in __getitem__
    return cls._member_map_[name]
KeyError: 'Prove knowledge of secret value'

You need to convert from the string (“Prove knowledge of secret value”) back to the name of the enum (“SecretValue”) before you can do the lookup like this.

To try to catch these and other issues, I wrote a new Hypothesis unit test in test_serialization.py and found several other issues along these lines, where deserialization was not the inverse of serialization.

keithrfung commented 4 years ago

When writing issues, there needs to be a singular issue being addressed. The description of this issue lists one problem where the title lists the overarching issue.

keithrfung commented 4 years ago

Also there was an existing issue on this fact so this collides with existing issue #120 . This includes these problems as the Core class is the Ballot and all the underlying information underneath that particular class. I've reopened this and I'll edit this description to be more accurate.

danwallach commented 4 years ago

The problem started with ProofUsage, then as I tried to fix it the problem ballooned into a larger problem with serialization as I investigated.