-
Notifications
You must be signed in to change notification settings - Fork 1.1k
tests: refactor tagged hash verification #1725
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: refactor tagged hash verification #1725
Conversation
src/modules/ellswift/tests_impl.h
Outdated
test_sha256_eq(&sha, &sha_optimized); | ||
secp256k1_sha256 sha_optimized; | ||
{ | ||
unsigned char tag[] = "secp256k1_ellswift_encode"; |
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.
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.
Nice find! The commit message states "However, it requires exactly specifying the array size, which can be
cumbersome," but I don't think this is true.
Using the test program:
// repro.c
#include <stdio.h>
int main() {
char str[] = "hello world"; // This should trigger the warning
printf("%s\n", str);
return 0;
}
I am able to compile with gcc14:
nix-shell --expr 'with import <nixpkgs> {}; mkShell.override { stdenv = overrideCC stdenv gcc14; }'
gcc -v
gcc -Wall -Wextra -Wpedantic -Werror repro.c -o out
and able to compile with gcc15:
nix-shell --expr 'with import <nixpkgs> {}; mkShell.override { stdenv = overrideCC stdenv gcc15; }'
gcc -v
gcc -Wall -Wextra -Wpedantic -Werror repro.c -o out
However, if I specify the array size, I can reproduce the error:
// repro.c
#include <stdio.h>
int main() {
char str[11] = "hello world"; // This should trigger the warning
printf("%s\n", str);
return 0;
}
No error with:
nix-shell --expr 'with import <nixpkgs> {}; mkShell.override { stdenv = overrideCC stdenv gcc14; }'
gcc -Wall -Wextra -Wpedantic -Werror repro.c -o out
And an error with:
nix-shell --expr 'with import <nixpkgs> {}; mkShell.override { stdenv = overrideCC stdenv gcc15; }'
gcc -Wall -Wextra -Wpedantic -Werror repro.c -o out
repro.c: In function ‘main’:
repro.c:4:20: error: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (12 chars into 11 available) [-Werror=unterminated-string-initialization]
4 | char str[11] = "hello world"; // This should trigger the warning
| ^~~~~~~~~~~~~
cc1: all warnings being treated as errors
Based on the above, I'd recommend we prefer the approach in this PR of not specifying the array size and perhaps document it as the preferred convention going forward? I find being able to specify the tag as a string to be much more reviewable than specifying the tag as an array of characters.
That being said, also happy to go the other way and update the musig tests to match the other modules if thats the preferred convention, as I think the main benefit is to have all of the modules follow the same convention.
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.
To convince myself, I also verified with a few versions of clang, e.g.,:
nix-shell --expr 'with import <nixpkgs> {}; mkShell.override { stdenv = llvmPackages_16.stdenv; }'
clang -Wall -Wextra -Wpedantic -Werror -Wmost repro.c
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.
@josibake The NUL byte resulting from char str[] = "hello world"
does not hurt per se, but there are two minor issues with this: First, it's conceptually the wrong thing: If we want a char array, the simplest thing to do is to define a char array instead of a NUL-terminated string. Second and probably more relevant, it changes sizeof(str)
to be 12 instead of 11. (See https://godbolt.org/z/da6PExKTh for demonstration. godbolt.org is the easiest way to test toy examples on many compilers.) We could, of course, accept this and always use sizeof(str) - 1
, but it's easy to miss this.
edit: Sorry, I now saw that you're aware of the - 1
thing. And I agree, the ability to grep for the string is a good argument for the NUL-terminated string. If you ask me, I prefer to forego the grepability and define the right kind of object and have sizeof
correct. But there's no definitive answer in the end.
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.
@real-or-random thanks for the context! That explains the sizeof(str) - 1
for the musig examples. So it seems the choices are:
- Do something conceptually wrong for something that is slightly easier to review
- Do the conceptually correct thing for something that is slightly harder to review
"Slightly harder/easier" is a bit hand-wavy, but the fact that we used to specify the tags as strings (and the recently added musig also adopted this convention vs staying consistent with the existing modules) indicates option 1 is the more natural option. However, it likely needs an explainer, especially for why we are using sizeof(tag) - 1
. On the flipside, I'm guessing option 2 feels more natural for reviewers who review/write a majority of the time in C?
Regardless of which convention is chosen, I do think its worth documenting in CONTRIBUTING.md
. I'll add a commit for that once reviewers have weighed in on which convention they prefer.
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.
Maybe godbolt.org/z/eKbT6sha4?
That still generates a warning if I add -Wextra
.
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.
Maybe godbolt.org/z/eKbT6sha4?
That still generates a warning if I add
-Wextra
.
Right.
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.
Oh, interesting, I wasn't aware of nonstring
. That's another neat way.
Though when I think about it, I still prefer {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
. Code is read much more often than it's written, so it makes sense to optimize reader (or reviewer) burden, and {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
is immediately clear to a reviewer familiar with C. It's just a bit hard on the eyes, but there will be no need to look up macros or GNU extension attributes, etc.
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.
Though when I think about it, I still prefer
{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
. Code is read much more often than it's written, so it makes sense to optimize reader (or reviewer) burden, and{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
is immediately clear to a reviewer familiar with C. It's just a bit hard on the eyes, but there will be no need to look up macros or GNU extension attributes, etc.
Agreed. That's why I raised this point in the first place.
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.
Sounds like 2 votes for keeping it as is, vs one vote to change it 😅 I'll update this PR tomorrow to instead convert the musig module to the existing convention, and add a note documenting the convention.
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.
Concept ACK it's a good idea to make this consistent
Concept ACK In the risk of sounding heretic, wouldn't it also be an option to let |
Hehe, I think that's also a good approach. It increases legibility at the cost of introducing the assumption that there are NUL bytes (which is most likely true even for future tags, yes). If I had to pick, I'd still pack the array initializer simply because the tag is conceptually an array. I think we have reached a point where @josibake should just pick one of the many good options, and we'll move on with that one. 😄 |
6424805
to
17af09d
Compare
Thanks everyone for chiming in! I reworked this to update the musig tests to use
Given that this library is written in C, it seems best to write code that is familiar to reviewers and is idiomatic C.
Agree. Though we can represent tags as strings, ultimately they are character arrays. Creating them as char arrays seems to have the least surprises, e.g., Lastly, I decided against adding a blurb to |
Agreed, this is too much of a niche thing to bother with in this file. Of course, it won't hurt if it's documented there, but then we could also document hundreds of other things in CONTRIBUTING.md. |
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.
ACK mod nit, you could also squash these commits
Move the sha256_tag_test_internal function out of the musig module into tests.c. This makes it available to other modules wishing to verify tagged hashes without needing to duplicate the function. Change the function signature to expect a const unsigned char and update the tagged hash tests to use static const unsigned char character arrays (where necessary). Add a comment for each tag. This is done as a convenience for checking the strings against the protocol specifications, where the tags are normally specified as strings. Update tests in the ellswift and schnorrsig modules to use the sha256_tag_test_internal helper function.
17af09d
to
5153cf1
Compare
Renamed helper function to |
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.
utACK 5153cf1 assuming CI passes
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.
Code-review ACK 5153cf1
aa85bfb530 docs: update README 9f42a30b82 ci: enable silentpayments module d504e48145 tests: add sha256 tag test 124750d580 tests: add constant time tests b35ffa2e30 tests: add BIP-352 test vectors 038c5b9c9d silentpayments: add benchmarks for scanning 88eb3d4545 silentpayments: add examples/silentpayments.c 22b20fd617 silentpayments: receiving df1de93765 silentpayments: recipient label support 76a0451c76 silentpayments: sending 3cd3a93bff build: add skeleton for new silentpayments (BIP352) module f36afb8b3d Merge bitcoin-core/secp256k1#1725: tests: refactor tagged hash verification 5153cf1c91 tests: refactor tagged hash tests d2dcf52091 Merge bitcoin-core/secp256k1#1726: docs: fix broken link to Tromer's cache.pdf paper 489a43d1bf docs: fix broken link to eprint cache.pdf paper d599714147 Merge bitcoin-core/secp256k1#1722: docs: Exclude modules' `bench_impl.h` headers from coverage report 0458def51e doc: Add `--gcov-ignore-parse-errors=all` option to `gcovr` invocations 1aecce5936 doc: Add `--merge-mode-functions=separate` option to `gcovr` invocations 106a7cbf41 doc: Exclude modules' `bench_impl.h` headers from coverage report a9e955d3ea autotools, docs: Adjust help string for `--enable-coverage` option e523e4f90e Merge bitcoin-core/secp256k1#1720: chore(ci): Fix typo in Dockerfile comment 24ba8ff168 chore(ci): Fix typo in Dockerfile comment 74b8068c5d Merge bitcoin-core/secp256k1#1717: test: update wycheproof test vectors c25c3c8a88 test: update wycheproof test vectors 20e3b44746 Merge bitcoin-core/secp256k1#1688: cmake: Avoid contaminating parent project's cache with `BUILD_SHARED_LIBS` 2c076d907a Merge bitcoin-core/secp256k1#1711: tests: update Wycheproof 7b07b22957 cmake: Avoid contaminating parent project's cache with BUILD_SHARED_LIBS 5433648ca0 Fix typos and spellings 9ea54c69b7 tests: update Wycheproof files git-subtree-dir: src/secp256k1 git-subtree-split: aa85bfb530b9ffc3dde6eaa7a976e129b8bd2f58
36e76952c Merge bitcoin-core/secp256k1#1738: check-abi: remove support for obsolete CMake library output location (src/libsecp256k1.so) 4985ac0f8 Merge bitcoin-core/secp256k1#1737: doc: mention ctx requirement for `_ellswift_create` (not secp256k1_context_static) 7ebaa134a check-abi: remove support for obsolete CMake library output location (src/libsecp256k1.so) 806de38bf doc: mention ctx requirement for `_ellswift_create` (not secp256k1_context_static) 03fb60ad2 Merge bitcoin-core/secp256k1#1681: doc: Recommend clang-cl when building on Windows d93380fb3 Merge bitcoin-core/secp256k1#1731: schnorrsig: Securely clear buf containing k or its negation 8113671f8 Merge bitcoin-core/secp256k1#1729: hash: Use size_t instead of int for RFC6979 outlen copy 325d65a8c Rename and clear var containing k or -k 960ba5f9c Use size_t instead of int for RFC6979 outlen copy 737912430 ci: Add more tests for clang-cl 7379a5bed doc: Recommend clang-cl when building on Windows f36afb8b3 Merge bitcoin-core/secp256k1#1725: tests: refactor tagged hash verification 5153cf1c9 tests: refactor tagged hash tests d2dcf5209 Merge bitcoin-core/secp256k1#1726: docs: fix broken link to Tromer's cache.pdf paper 489a43d1b docs: fix broken link to eprint cache.pdf paper d59971414 Merge bitcoin-core/secp256k1#1722: docs: Exclude modules' `bench_impl.h` headers from coverage report 0458def51 doc: Add `--gcov-ignore-parse-errors=all` option to `gcovr` invocations 1aecce593 doc: Add `--merge-mode-functions=separate` option to `gcovr` invocations 106a7cbf4 doc: Exclude modules' `bench_impl.h` headers from coverage report a9e955d3e autotools, docs: Adjust help string for `--enable-coverage` option e523e4f90 Merge bitcoin-core/secp256k1#1720: chore(ci): Fix typo in Dockerfile comment 24ba8ff16 chore(ci): Fix typo in Dockerfile comment 74b8068c5 Merge bitcoin-core/secp256k1#1717: test: update wycheproof test vectors c25c3c8a8 test: update wycheproof test vectors 20e3b4474 Merge bitcoin-core/secp256k1#1688: cmake: Avoid contaminating parent project's cache with `BUILD_SHARED_LIBS` 2c076d907 Merge bitcoin-core/secp256k1#1711: tests: update Wycheproof 7b07b2295 cmake: Avoid contaminating parent project's cache with BUILD_SHARED_LIBS 5433648ca Fix typos and spellings 9ea54c69b tests: update Wycheproof files b9313c6e1 Merge bitcoin-core/secp256k1#1708: release cleanup: bump version after 0.7.0 a660a4976 Merge bitcoin-core/secp256k1#1707: release: Prepare for 0.7.0 7ab8b0cc0 release cleanup: bump version after 0.7.0 a3e742d94 release: Prepare for 0.7.0 f67b0ac1a ci: Don't hardcode ABI version 020ee6049 Merge bitcoin-core/secp256k1#1706: musig/tests: initialize keypair cde413089 musig/tests: initialize keypair 6037833c9 Merge bitcoin-core/secp256k1#1702: changelog: update 40b4a0652 changelog: update 5e74086dc Merge bitcoin-core/secp256k1#1705: musig/test: Remove dead code 7c3380423 Merge bitcoin-core/secp256k1#1696: build: Refactor visibility logic and add override 8d967a602 musig/test: Remove dead code 983711cd6 musig/tests: Refactor vectors_signverify 73a695958 Merge bitcoin-core/secp256k1#1704: cmake: Make `secp256k1_objs` inherit interface defines from `secp256k1` bf082221f cmake: Make `secp256k1_objs` inherit interface defines from `secp256k1` c82d84bb8 build: add CMake option for disabling symbol visibility attributes ce7923874 build: Add SECP256K1_NO_API_VISIBILITY_ATTRIBUTES e5297f6d7 build: Refactor visibility logic cbbbf3bd6 Merge bitcoin-core/secp256k1#1699: ci: enable musig module for native macOS arm64 job 943479a7a Merge bitcoin-core/secp256k1#1694: Revert "cmake: configure libsecp256k1.pc during install" 3352f9d66 ci: enable musig module for native macOS arm64 job ad60ef7ea Merge bitcoin-core/secp256k1#1689: ci: Convert `arm64` Cirrus tasks to GHA jobs c49877909 Merge bitcoin-core/secp256k1#1687: cmake: support the use of launchers in ctest -S scripts 44b205e9e Revert "cmake: configure libsecp256k1.pc during install" 0dfe387db cmake: support the use of launchers in ctest -S scripts 89096c234 Merge bitcoin-core/secp256k1#1692: cmake: configure libsecp256k1.pc during install 7106dce6f cmake: configure libsecp256k1.pc during install 29e73f4ba Merge bitcoin-core/secp256k1#1685: cmake: Emulate Libtool's behavior on FreeBSD 746e36b14 Merge bitcoin-core/secp256k1#1678: cmake: add a helper for linking into static libs a28c2ffa5 Merge bitcoin-core/secp256k1#1683: README: add link to musig example 2a9d37473 Merge bitcoin-core/secp256k1#1690: ci: Bump GCC snapshot major version to 16 add146e10 ci: Bump GCC snapshot major version to 16 004f57fcd ci: Move Valgrind build for `arm64` from Cirrus to GHA 5fafdfc30 ci: Move `gcc-snapshot` build for `arm64` from Cirrus to GHA e814b79a8 ci: Switch `arm64_debian` from QEMU to native `arm64` Docker image bcf77346b ci: Add `arm64` architecture to `docker_cache` job b77aae922 ci: Rename Docker image tag to reflect architecture 145ae3e28 cmake: add a helper for linking into static libs 819210974 README: add link to musig example, generalize module enabling hint 95db29b14 Merge bitcoin-core/secp256k1#1679: cmake: Use `PUBLIC_HEADER` target property in installation logic 37dd422b5 cmake: Emulate Libtool's behavior on FreeBSD f24b838be Merge bitcoin-core/secp256k1#1680: doc: Promote "Building with CMake" to standard procedure 3f31ac43e doc: Promote "Building with CMake" to standard procedure 6f67151ee cmake: Use `PUBLIC_HEADER` target property c32715b2a cmake, move-only: Move module option processing to `src/CMakeLists.txt` 201b2b8f0 Merge bitcoin-core/secp256k1#1675: cmake: Bump minimum required CMake version to 3.22 3af71987a cmake: Bump minimum required CMake version to 3.22 92394476e Merge bitcoin-core/secp256k1#1673: Assert field magnitude at control-flow join 3a4f448cb Assert field magnitude at control-flow join git-subtree-dir: src/secp256k1 git-subtree-split: 36e76952cbf1cf54ddd2d8756cc31a486e2ba1d9
Opened in response to #1698 (comment)
We use tagged hashes in
modules/musig
,modules/schnorrsig
,modules/ellswift
, and the proposedmodules/silentpayments
. In looking for inspiration on how to add tagged hash midstate verification for #1698, it seemed like a good opportunity to DRY up the code across all of the modules.I chose the convention used in the ellswift module as this seems the most idiomatic C. Since the tags are normally specified as strings in the BIPs, I also added a comment above each char array for convenience.
If its deemed too invasive to refactor the existing modules in this PR, I'm happy to drop the refactor commits for the ellswift and schnorrsig modules. All I need for #1698 is the first commit which moves the utility function out of the musig module to make it available to use in the silent payments module.