Fix _ecmult_wnaf to not rely on int having at least 32 value bits
This PR addresses the issue where secp256k1_ecmult_wnaf relies on the unstated and unchecked assumption that int has at least 32 value bits.
Changes Made:
- Change
secp256k1_ecmult_wnafto useint32_tfor variables that need 32 value bits (word, carry, sign) - Change wnaf array parameters and declarations to use
int32_tinstead ofint - Keep bit position variables as
int(bit, len, last_set_bit, now, verify_bit) - Update all wnaf array declarations in structs (secp256k1_strauss_point_state, secp256k1_pippenger_state)
- Update all local wnaf array declarations in functions
- Update ecmult_table_get functions to accept int32_t for n parameter
- Update test files (tests.c, bench_internal.c)
- Update test helper functions and wnaf_expected arrays
- All tests passing
- CodeQL security checks passed - no alerts found
Summary:
All wnaf-related arrays, variables, and parameters that store values potentially requiring 32 value bits now use int32_t. Bit position variables remain as int as intended. This ensures the code works correctly on platforms where int has fewer than 32 value bits (e.g., 16-bit platforms).
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
This section details on the original issue you should resolve
<issue_title>
_ecmult_wnafrelies oninthaving at least 32 value bits</issue_title> <issue_description>The currentsecp256k1_ecmult_wnafneeds the unstated and unchecked assumption thatinthas at least 32 value bits when it VERIFY_CHECKs thatw <= 31. In practice, we call it only withWINDOW_A == 5andWINDOW_G == ECMULT_WINDOW_SIZEwhere the latter is configurable in the range2..24.A consequence of this "bug" is that the code fails on a 16-bit platform if you set
ECMULT_WINDOW_SIZE > 16. I don't think we need to support this, but code without unchecked assumptions is bad. So I suggest that we rewrite the function to useint32_tinstead ofinteven if we don't use my macro approach. (We can keep the "bit position" arguments and variablesint.)Alternatively, we could add the assumption that
INT_MAX >= INT32_MAXbut this forbids 16-bit platforms, and the code seems to work on them in principle; see #792 (comment).Originally posted by @real-or-random in #1761#pullrequestreview-3432695127 </issue_description>
<agent_instructions>Use the suggestion to use
int32_tinstead ofint. Keep the "bit positions" variables and argumentsint.</agent_instructions>Comments on the Issue (you are @copilot in this section)
<comments> <comment_new><author>@real-or-random</author><body> Let's try some AI stuff... 🤷 </body></comment_new> </comments>
</details>
- Fixes bitcoin-core/secp256k1#1769
<!-- START COPILOT CODING AGENT TIPS -->
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.