-
Notifications
You must be signed in to change notification settings - Fork 1.1k
tests: add exhaustive extrakeys tweak coverage #1836
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
Open
mangoostaa
wants to merge
1
commit into
bitcoin-core:master
Choose a base branch
from
mangoostaa:add-exhaustive-tweak-tests-extrakeys
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,76 @@ static void test_exhaustive_extrakeys(const secp256k1_context *ctx, const secp25 | |
| } | ||
| } | ||
|
|
||
| /* TODO: keypair/xonly_pubkey tweak tests */ | ||
| /* Check keypair/xonly_pubkey tweak behavior over all non-zero tweaks. */ | ||
| for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { | ||
| int j; | ||
| int xonly_scalar = parities[i - 1] ? EXHAUSTIVE_TEST_ORDER - i : i; | ||
| secp256k1_scalar scalar_i; | ||
| unsigned char sk32[32]; | ||
|
|
||
| secp256k1_scalar_set_int(&scalar_i, i); | ||
| secp256k1_scalar_get_b32(sk32, &scalar_i); | ||
|
|
||
| for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) { | ||
| secp256k1_scalar scalar_j; | ||
| unsigned char tweak32[32]; | ||
| int expected_scalar; | ||
|
|
||
| secp256k1_pubkey tweaked_pk; | ||
| secp256k1_xonly_pubkey tweaked_xonly_pk; | ||
| secp256k1_keypair tweaked_keypair; | ||
| unsigned char serialized_pk[33]; | ||
| unsigned char serialized_xonly_pk[32]; | ||
| unsigned char expected_x[32]; | ||
| size_t serialized_pklen = sizeof(serialized_pk); | ||
| int expected_ret; | ||
| int expected_pk_parity; | ||
| int pk_parity; | ||
|
|
||
| secp256k1_scalar_set_int(&scalar_j, j); | ||
| secp256k1_scalar_get_b32(tweak32, &scalar_j); | ||
|
|
||
| expected_scalar = (xonly_scalar + j) % EXHAUSTIVE_TEST_ORDER; | ||
| expected_ret = expected_scalar != 0; | ||
|
|
||
| CHECK(secp256k1_xonly_pubkey_tweak_add(ctx, &tweaked_pk, &xonly_pubkey[i - 1], tweak32) == expected_ret); | ||
| CHECK(secp256k1_keypair_create(ctx, &tweaked_keypair, sk32) == 1); | ||
| CHECK(secp256k1_keypair_xonly_tweak_add(ctx, &tweaked_keypair, tweak32) == expected_ret); | ||
|
|
||
| if (!expected_ret) { | ||
| continue; | ||
| } | ||
|
|
||
| { | ||
| secp256k1_fe y = group[expected_scalar].y; | ||
| secp256k1_fe_normalize_var(&y); | ||
| expected_pk_parity = secp256k1_fe_is_odd(&y); | ||
| } | ||
| { | ||
| secp256k1_fe x = group[expected_scalar].x; | ||
| secp256k1_fe_normalize_var(&x); | ||
| secp256k1_fe_get_b32(expected_x, &x); | ||
| } | ||
|
|
||
| CHECK(secp256k1_ec_pubkey_serialize(ctx, serialized_pk, &serialized_pklen, &tweaked_pk, SECP256K1_EC_COMPRESSED)); | ||
| CHECK(serialized_pklen == sizeof(serialized_pk)); | ||
| CHECK((serialized_pk[0] == SECP256K1_TAG_PUBKEY_EVEN) || (serialized_pk[0] == SECP256K1_TAG_PUBKEY_ODD)); | ||
| CHECK(serialized_pk[0] == (expected_pk_parity ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN)); | ||
| CHECK(secp256k1_memcmp_var(&serialized_pk[1], expected_x, 32) == 0); | ||
|
Comment on lines
+116
to
+120
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit (here and below): serializing is not strictly needed I think, one could alternatively do these checks on the internal types, i.e. by loading into a group element instance (via |
||
|
|
||
| CHECK(secp256k1_xonly_pubkey_from_pubkey(ctx, &tweaked_xonly_pk, &pk_parity, &tweaked_pk)); | ||
| CHECK(pk_parity == expected_pk_parity); | ||
| CHECK(secp256k1_xonly_pubkey_serialize(ctx, serialized_xonly_pk, &tweaked_xonly_pk)); | ||
| CHECK(secp256k1_memcmp_var(serialized_xonly_pk, expected_x, 32) == 0); | ||
| CHECK(secp256k1_xonly_pubkey_tweak_add_check(ctx, serialized_xonly_pk, pk_parity, &xonly_pubkey[i - 1], tweak32)); | ||
|
|
||
| CHECK(secp256k1_keypair_pub(ctx, &tweaked_pk, &tweaked_keypair)); | ||
| serialized_pklen = sizeof(serialized_pk); | ||
| CHECK(secp256k1_ec_pubkey_serialize(ctx, serialized_pk, &serialized_pklen, &tweaked_pk, SECP256K1_EC_COMPRESSED)); | ||
| CHECK(serialized_pk[0] == (expected_pk_parity ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN)); | ||
| CHECK(secp256k1_memcmp_var(&serialized_pk[1], expected_x, 32) == 0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whitespace nit: