Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: NAF and splitK in crypto.secp256k1.curve #74

Merged
merged 8 commits into from
Feb 14, 2020
Merged

tests: NAF and splitK in crypto.secp256k1.curve #74

merged 8 commits into from
Feb 14, 2020

Conversation

teknico
Copy link
Collaborator

@teknico teknico commented Feb 12, 2020

Part of #70.

Converted the crypto.secp256k1.curve unit tests to pytest format.

Added tests for NAF and splitK in crypto.secp256k1.curve, and fixed a double bug in splitK.

Moved randBytes to util.helpers and added a sign function (that Python unfortunately lacks).

Changed curve.lamda to curve.lambda_ (trailing underscores are the usual Python convention for avoiding name conflicts).

@teknico
Copy link
Collaborator Author

teknico commented Feb 12, 2020

Bah, dataclasses were introduced in Python 3.7 . Shall I use a standard class or a dictionary? It's just a few lines in a test.

EDIT: another possibility is to add the dataclasses backport package as a dependency. Since it only depends on Python 3.6, and excludes any later versions, it will not be installed on Python 3.7, 3.8 and later.

EDIT2: alas, Poetry won't install dataclasses, it's a known bug.

I'll use a dictionary.

@@ -210,10 +210,10 @@ def splitK(self, k):
# being a bottleneck.
# c1 = round(b2 * k / n) from step 4.
# Rounding isn't really necessary and costs too much, hence skipped
c1 = (self.b2 + k) // self.N
c1 = (self.b2 * k) // self.N
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This (and the similar one below) is a bugfix: the original Go code has a bigInt.Mul here, and tests fail without this change.

Copy link
Member

Choose a reason for hiding this comment

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

Weird that we never hit an issue with this with signature verification. Hmmm.

Copy link
Member

@JoeGruffins JoeGruffins left a comment

Choose a reason for hiding this comment

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

Wish I understood all this math.

decred/tests/unit/crypto/secp256k1/test_curve.py Outdated Show resolved Hide resolved
decred/decred/crypto/secp256k1/curve.py Show resolved Hide resolved
decred/tests/unit/crypto/secp256k1/test_curve.py Outdated Show resolved Hide resolved
@teknico
Copy link
Collaborator Author

teknico commented Feb 13, 2020

Wish I understood all this math.

I won't claim I understand it either. I did have a quick look at the algorithms in the book mentioned in the Go code, but most of the work is translating the Go code and tests anyway.

@@ -210,10 +210,10 @@ def splitK(self, k):
# being a bottleneck.
# c1 = round(b2 * k / n) from step 4.
# Rounding isn't really necessary and costs too much, hence skipped
c1 = (self.b2 + k) // self.N
c1 = (self.b2 * k) // self.N
Copy link
Member

Choose a reason for hiding this comment

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

Weird that we never hit an issue with this with signature verification. Hmmm.

return 0


random.seed(0)
Copy link
Member

Choose a reason for hiding this comment

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

Can't set the random seed in production code. I'm hesitant to add any functions here that are just for tests. How about a test utilities module somewhere?

Copy link
Collaborator Author

@teknico teknico Feb 13, 2020

Choose a reason for hiding this comment

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

Can't set the random seed in production code. I'm hesitant to add any functions here that are just for tests.

Makes sense.

How about a test utilities module somewhere?

Test code does not live in packages and modules. Test discovery is performed by pytest according to its own rules. For cases like this, pytest offers fixtures, I'll change randBytes and sign as such.

The remaining question is whether setting the seed should be done in the fixture or in the test that uses it: I lean towards the latter.

- test helpers "randBytes" and "sign" removed from util/helpers.py and moved
  to a conftest.py file in the tests/ root directory, as pytest fixtures;
- test_curve.py changed to use the new fixtures;
- unit/util/test_database.py restored to its original state.
Copy link
Member

@buck54321 buck54321 left a comment

Choose a reason for hiding this comment

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

Looks great.

@buck54321 buck54321 merged commit 507a5f5 into decred:master Feb 14, 2020
@teknico teknico deleted the more-curve-tests branch February 14, 2020 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants