Skip to content

Conversation

theStack
Copy link
Contributor

@theStack theStack commented May 14, 2023

This PR adds missing unit test coverage for the Python ECDSA implementation, which should be useful for detecting potential problems early whenever changes in the test framework's Python implementation of secp256k1 are made (e.g. #26222). Note that right now we don't call ECPubKey.verify_ecdsa anywhere in our tests, so we wouldn't notice if it is broken at some point.

To keep it simple, the already existing unit test for Schnorr signatures is extended to also check ECDSA signatures. For that purpose, the dictionary storing private-key/public-key entries use their legacy types ECKey/ECPubKey instead of bare byte-arrays, and for Schnorr signing/verification the necessary conversions (ECKey -> bare private key, ECPubKey -> x-only pubkey) is done later when needed. To avoid code duplication, a helper function random_bitflip for damaging signatures is introduced.

The unit test can be run by either calling it for this single module:
$ python3 -m unittest ./test/functional/test_framework/key.py
or simply running $ ./test/functional/test_runner.py which calls all test framework module's unit tests at the start (see TEST_FRAMEWORK_MODULES list).

@DrahtBot
Copy link
Contributor

DrahtBot commented May 14, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK sipa, stratospher, achow101
Concept ACK pinheadmz

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

No conflicts as of last run.

Copy link
Member

@pinheadmz pinheadmz left a comment

Choose a reason for hiding this comment

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

concept ACK

I also think it would be cool to see a set of ECDSA test vectors here. We already have a set of BIP340 vectors tested both in python and in the key_tests.cpp unit test, assuring that both implementations handle Schnorr (at least those cases) identically.

Comment on lines +528 to +306
if privkey.is_valid:
keys[privkey] = privkey.get_pubkey()
Copy link
Member

Choose a reason for hiding this comment

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

IIUC we don't assert that the keys we expect to be invalid are marked correctly. Seems like we mine as well ensure that generate_privkey() is always valid but certain edge cases are not (0, SECP256K1_ORDER, and 2**256 - 1)

@theStack theStack force-pushed the 202305-test-add_ecdsa_signing_unittest branch from 490874f to 96b3f2d Compare September 20, 2023 16:22
@theStack
Copy link
Contributor Author

Rebased on master.

Copy link
Member

@sipa sipa left a comment

Choose a reason for hiding this comment

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

utACK 96b3f2d

Copy link
Contributor

@stratospher stratospher left a comment

Choose a reason for hiding this comment

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

tested ACK 96b3f2d.

@achow101
Copy link
Member

ACK 96b3f2d

@achow101 achow101 merged commit 5bbf735 into bitcoin:master Sep 29, 2023
@theStack theStack deleted the 202305-test-add_ecdsa_signing_unittest branch September 29, 2023 18:50
Frank-GER pushed a commit to syscoin/syscoin that referenced this pull request Oct 5, 2023
…plementation

96b3f2d test: add unit test coverage for Python ECDSA implementation (Sebastian Falbesoner)

Pull request description:

  This PR adds missing unit test coverage for the Python ECDSA implementation, which should be useful for detecting potential problems early whenever changes in the test framework's Python implementation of secp256k1 are made (e.g. bitcoin#26222). Note that right now we don't call `ECPubKey.verify_ecdsa` anywhere in our tests, so we wouldn't notice if it is broken at some point.

  To keep it simple, the already existing unit test for Schnorr signatures is extended to also check ECDSA signatures. For that purpose, the dictionary storing private-key/public-key entries use their legacy types `ECKey/ECPubKey` instead of bare byte-arrays, and for Schnorr signing/verification the necessary conversions (ECKey -> bare private key, ECPubKey -> x-only pubkey) is done later when needed. To avoid code duplication, a helper function `random_bitflip` for damaging signatures is introduced.

  The unit test can be run by either calling it for this single module:
  `$ python3 -m unittest ./test/functional/test_framework/key.py`
  or simply running `$ ./test/functional/test_runner.py` which calls all test framework module's unit tests at the start (see TEST_FRAMEWORK_MODULES list).

ACKs for top commit:
  achow101:
    ACK 96b3f2d
  sipa:
    utACK 96b3f2d
  stratospher:
    tested ACK 96b3f2d.

Tree-SHA512: b993f25b843fa047376addda4ce4b0f15750ffba926528b5cca4c5f99b9af456206f4e8af885d25a017dddddf382ddebf38765819b3d16a3f28810d03b010808
@bitcoin bitcoin locked and limited conversation to collaborators Sep 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants