Commit Graph

2337 Commits

Author SHA1 Message Date
Pieter Wuille
15d0cca2a6 Optimization: first table lookup needs no point addition 2024-04-19 11:43:46 -04:00
Pieter Wuille
7a33db35cd Optimization: move (2^COMB_BITS-1)/2 term into ctx->scalar_offset
It is unnecessary to recompute this term needed by the SDMC algorithm
for every multiplication; move it into the context scalar_offset value
instead.
2024-04-19 11:43:45 -04:00
Pieter Wuille
ed2a056f3d Provide 3 configurations accessible through ./configure 2024-04-19 11:43:26 -04:00
Pieter Wuille
5f7be9f6a5 Always generate tables for current (blocks,teeth) config 2024-04-19 11:43:26 -04:00
Peter Dettman
fde1dfcd8d Signed-digit multi-comb ecmult_gen algorithm
This introduces the signed-digit multi-comb multiplication algorithm
for constant-time G multiplications (ecmult_gen). It is based on
section 3.3 of "Fast and compact elliptic-curve cryptography" by
Mike Hamburg (see https://eprint.iacr.org/2012/309).

Original implementation by Peter Dettman, with changes by Pieter Wuille
to use scalars for recoding, and additional comments.
2024-04-19 11:43:22 -04:00
Pieter Wuille
486518b350 Make exhaustive tests's scalar_inverse(&x,&x) work
The old code overwrote the input at the start of the function,
making a call like secp256k1_scalar_inverse(&x,&x) always fail.
2024-04-19 10:27:15 -04:00
Pieter Wuille
ab45c3e089 Initial gej blinding -> final ge blinding
Instead of having the starting point of the ecmult_gen computation be
offset, do it with the final point. This enables reasoning over the
set of points reachable in intermediary computations, which can be
leveraged by potential future optimization.

Because the final point is in affine coordinates, its projective
blinding is no longer possible. It will be reintroduced again in
a different way, in a later commit.

Also introduce some more comments and more descriptive names.
2024-04-19 10:27:12 -04:00
Tim Ruffing
aa00a6b892 Introduce CEIL_DIV macro and use it 2024-04-15 13:18:27 -04:00
Tim Ruffing
d8311688bd Merge bitcoin-core/secp256k1#1515: ci: Note affected clangs in comment on ASLR quirk
a85e2233e7 ci: Note affected clangs in comment on ASLR quirk (Tim Ruffing)

Pull request description:

  Resolves #1506.

ACKs for top commit:
  fanquake:
    ACK a85e2233e7

Tree-SHA512: a82d6f5e57175434c1f66c7b01c90eb2877db794d7f5213b3652ecb51ebb7c03ff13ab13424b3e6d41b9eaa504af330f16855e32e63d1805c1a475f462c6da66
2024-04-04 11:52:27 +02:00
Tim Ruffing
a85e2233e7 ci: Note affected clangs in comment on ASLR quirk
Resolves #1506.
2024-04-04 11:49:00 +02:00
Tim Ruffing
4b77fec67a Merge bitcoin-core/secp256k1#1512: msan: notate more variable assignments from assembly code
f7f0184ba1 msan: notate more variable assignments from assembly code (Cory Fields)
a61339149f change inconsistent array param to pointer (Cory Fields)

Pull request description:

  This was missed in 31ba404944 because older versions of clang did not complain about it. But clang-17, at least, does.

  The array-as-a-param makes this annoying because `sizeof(l)` is not helpful. I'd be happy to change the size calculation if there are any better suggestions or strong preferences.

ACKs for top commit:
  sipa:
    utACK f7f0184ba1
  real-or-random:
    ACK f7f0184ba1 tests work fine with clang 17 and `./configure CFLAGS="-fsanitize=memory -fsanitize-memory-track-origins=2 -fno-omit-frame-pointer -g -O1 -fno-optimize-sibling-calls" CC=clang`

Tree-SHA512: 8ab22209ef322a10f500b123c82ae5e7141ae1da0e7a890cbf90bd7d2eb11f397db4ccfe15a1666f2f49228585cccbf5bec741effebd1e2c6012cb7ea1689675
2024-04-03 19:06:22 +02:00
Cory Fields
f7f0184ba1 msan: notate more variable assignments from assembly code
This was missed in 31ba404944 because older
versions of clang did not complain about it. But clang-17, at least, does.
2024-04-03 16:03:19 +00:00
Cory Fields
a61339149f change inconsistent array param to pointer
The behavior is identical, but the former syntax suggests guarantees that
don't actually exist.
2024-04-03 16:03:19 +00:00
Tim Ruffing
05bfab69ae Merge bitcoin-core/secp256k1#1507: ci: Add workaround for ASLR bug in sanitizers
a5e8ab2484 ci: Add sanitizer env variables to debug output (Tim Ruffing)
84a93de4d2 ci: Add workaround for ASLR bug in sanitizers (Tim Ruffing)

Pull request description:

  Fixes #1506.

  This also adds the sanitizer env variables to our debug output as suggested in the same issue.

ACKs for top commit:
  sipa:
    utACK a5e8ab2484
  jonasnick:
    ACK a5e8ab2484

Tree-SHA512: 5162d14eeec01e088c600ed77e21c5ffd4dec23327b7e81b5ecac59b7c535cac97cd7b7b744c767766036dfc6d9152a9933eb326cf4065d56c46e2ee858da662
2024-03-20 13:25:32 +01:00
Tim Ruffing
a5e8ab2484 ci: Add sanitizer env variables to debug output 2024-03-19 23:52:15 +01:00
Tim Ruffing
84a93de4d2 ci: Add workaround for ASLR bug in sanitizers
Fixes #1506.
2024-03-19 23:52:15 +01:00
Jonas Nick
427e86b9ed Merge bitcoin-core/secp256k1#1490: tests: improve fe_sqr test (issue #1472)
2028069df2 doc: clarify input requirements for secp256k1_fe_mul (Sebastian Falbesoner)
11420a7a28 tests: improve fe_sqr test (Sebastian Falbesoner)

Pull request description:

ACKs for top commit:
  real-or-random:
    utACK 2028069df2
  jonasnick:
    ACK 2028069df2

Tree-SHA512: bb01bf6ceb34f0475a60b8dcb0cec000859a0c20f1009426bd8cab609f1941f44f84802f1565a719f7d2a55466076fb1591a353b1b75e6c0ceac44806d908176
2024-02-27 17:17:00 +00:00
Sebastian Falbesoner
2028069df2 doc: clarify input requirements for secp256k1_fe_mul
"... neither can be equal to b." could suggest that the values are not
allowed to be identical, but what is meant here is that the mentioned
inputs shouldn't point to the same object.
2024-02-27 16:32:49 +01:00
Sebastian Falbesoner
11420a7a28 tests: improve fe_sqr test
Currently the `run_sqr` test doesn't do anything with the
result of the `fe_sqr` call. Improve that by checking that
the equation `(x+y)*(x-y) = x^2 - y^2` holds for some random
values y, as suggested in issue #1471 by real-or-random.
The existing loop for generating the x values is kept as-is.
2024-02-27 16:32:45 +01:00
Jonas Nick
cdc9a6258e Merge bitcoin-core/secp256k1#1489: tests: add missing fe comparison checks for inverse field test cases
e7bdddd9c9 refactor: rename `check_fe_equal` -> `fe_equal` (Sebastian Falbesoner)
00111c9c56 tests: add missing fe comparison checks for inverse field test cases (Sebastian Falbesoner)

Pull request description:

ACKs for top commit:
  real-or-random:
    utACK e7bdddd9c9
  jonasnick:
    ACK e7bdddd9c9

Tree-SHA512: 1d14cb87bf3d190be6e11ae205ed25090758aae589f50793d9bcbdb3c04378ca08f6a3d41567fdf472786ea3234cf1f3b9c95ece8b605b4a7667a81a27b249e2
2024-02-27 15:18:20 +00:00
Tim Ruffing
d926510cf7 Merge bitcoin-core/secp256k1#1496: msan: notate variable assignments from assembly code
31ba404944 msan: notate variable assignments from assembly code (Cory Fields)
e7ea32e30a msan: Add SECP256K1_CHECKMEM_MSAN_DEFINE which applies to memory sanitizer and not valgrind (Cory Fields)

Pull request description:

  msan isn't smart enough to see that these are set without some help.

  This was pointed out here: https://github.com/bitcoin-core/secp256k1/pull/1169#issuecomment-1370003449

  With this commit, msan output is clean even with x86 asm turned on.

ACKs for top commit:
  real-or-random:
    utACK 31ba404944
  hebasto:
    re-ACK 31ba404944.

Tree-SHA512: c9c51fe542247e1e0a93f6d0063d119cf777ca8c1b7e9c8e45e168a2020dc503872eb2a78004725de81267a3ce78c923be1f8546fb92a3e95fc7ef034e5ba932
2024-02-27 14:47:18 +01:00
Cory Fields
31ba404944 msan: notate variable assignments from assembly code
msan isn't smart enough to see that these are set without some help.
2024-02-23 17:30:39 +00:00
Cory Fields
e7ea32e30a msan: Add SECP256K1_CHECKMEM_MSAN_DEFINE which applies to memory sanitizer and not valgrind 2024-02-23 17:30:39 +00:00
Sebastian Falbesoner
e7bdddd9c9 refactor: rename check_fe_equal -> fe_equal
As this function doesn't do any checking, it's better to rename it,
so that it's less likely to miss the needed `CHECK`.
2024-02-01 15:34:40 +01:00
Sebastian Falbesoner
00111c9c56 tests: add missing fe comparison checks for inverse field test cases
`check_fe_equal` is a wrapper around `secp256k1_fe_equal` that takes
care of normalization. Since it doesn't check anything itself, the
CHECK macro is needed at the call-sites to actually ensure equality.
2024-02-01 00:49:25 +01:00
Tim Ruffing
0653a25d50 Merge bitcoin-core/secp256k1#1486: ci: Update cache action
94a14d5290 ci: Update cache action (Hennadii Stepanov)

Pull request description:

  This PR fixes deprecation warnings for Node.js 16 actions in the GHA CI.

  See:
  - https://github.com/marketplace/actions/cache
  - https://github.com/actions/cache/releases/tag/v4.0.0

ACKs for top commit:
  real-or-random:
    ACK 94a14d5290 thanks!

Tree-SHA512: 6f520908aaadf179955255a2b3a93c2cb96f23cf3a9d00a53b3ae635007983337f1a768cb6039e0e0b1bc9630930b143dd1c650f366185fd20727ab97221519b
2024-01-25 14:56:56 +01:00
Hennadii Stepanov
94a14d5290 ci: Update cache action
This change fixes deprecation warnings for Node.js 16 actions in the GHA
CI.

See:
- https://github.com/marketplace/actions/cache
- https://github.com/actions/cache/releases/tag/v4.0.0
2024-01-25 12:09:47 +00:00
Jonas Nick
2483627299 Merge bitcoin-core/secp256k1#1483: cmake: Recommend native CMake commands in README
3777e3f36a cmake: Recommend native CMake commands in README (Tim Ruffing)

Pull request description:

ACKs for top commit:
  hebasto:
    ACK 3777e3f36a
  jonasnick:
    ACK 3777e3f36a

Tree-SHA512: 884e54ee3ec9617edbb98d439ccd3fa8b3d9448969a4f5a88d22d034329ec5024238d6f91e28160f82f77eed678100266ac8b5495b6072b48caa0514a9cec881
2024-01-23 19:41:01 +00:00
Jonas Nick
5ad3aa3dcd Merge bitcoin-core/secp256k1#1484: tests: Drop redundant _scalar_check_overflow calls
51df2d9ab3 tests: Drop redundant _scalar_check_overflow calls (Tim Ruffing)

Pull request description:

ACKs for top commit:
  stratospher:
    ACK 51df2d9.
  jonasnick:
    ACK 51df2d9ab3

Tree-SHA512: 52caff34b0cbb8570b6aa962c86c249e216d3a78661715c6adf6804379c60be049e36fcb714cd562d350787949dfccf95d0b9a885480e08513664864abd36928
2024-01-23 19:30:00 +00:00
Tim Ruffing
51df2d9ab3 tests: Drop redundant _scalar_check_overflow calls
Redundant since d23da6d557.
2024-01-17 16:54:04 +01:00
Tim Ruffing
3777e3f36a cmake: Recommend native CMake commands in README
Resolves one item in #1235. Closes #1294.
2024-01-17 15:19:53 +01:00
Tim Ruffing
e4af41c61b Merge bitcoin-core/secp256k1#1249: cmake: Add SECP256K1_LATE_CFLAGS configure option
42f8c51402 cmake: Add `SECP256K1_LATE_CFLAGS` configure option (Hennadii Stepanov)

Pull request description:

  This PR enables users to override compiler flags that have been set by the CMake-based build system, such as warning flags.

  The Autotools-based build system has the same feature out-of-the-box.

  See more details [here](https://github.com/bitcoin-core/secp256k1/issues/1235#issuecomment-1465330925).

  Here are some examples of the new option usage:
  ```
  cmake -S . -B build -DSECP256K1_LATE_CFLAGS="-Wno-extra -Wlong-long"
  ```

  ```
  cmake -S . -B build -DSECP256K1_BUILD_EXAMPLES=ON -DSECP256K1_LATE_CFLAGS=-O1
  cmake --build build
  ...
  In function ‘secp256k1_ecmult_strauss_wnaf’,
      inlined from ‘secp256k1_ecmult’ at /home/hebasto/git/secp256k1/src/ecmult_impl.h:353:5:
  /home/hebasto/git/secp256k1/src/ecmult_impl.h:291:5: warning: ‘aux’ may be used uninitialized [-Wmaybe-uninitialized]
    291 |     secp256k1_ge_table_set_globalz(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->aux);
        |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  In file included from /home/hebasto/git/secp256k1/src/secp256k1.c:29:
  /home/hebasto/git/secp256k1/src/ecmult_impl.h: In function ‘secp256k1_ecmult’:
  /home/hebasto/git/secp256k1/src/group_impl.h:174:13: note: by argument 3 of type ‘const secp256k1_fe *’ to ‘secp256k1_ge_table_set_globalz’ declared here
    174 | static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr) {
        |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  In file included from /home/hebasto/git/secp256k1/src/secp256k1.c:30:
  /home/hebasto/git/secp256k1/src/ecmult_impl.h:345:18: note: ‘aux’ declared here
    345 |     secp256k1_fe aux[ECMULT_TABLE_SIZE(WINDOW_A)];
        |                  ^~~
  ...
  ```

  Please note that in the last case providing `env CFLAGS=-O1` or `-DCMAKE_C_FLAGS=-O1` won't work.

ACKs for top commit:
  real-or-random:
    ACK 42f8c51402

Tree-SHA512: 2b152e420a4a8ffd5f67857de03ae5ba9f2223e535ac01a867c1025e0619180d8255fdd1e5fb8279b290f0a1c96bcc874043ef968fcd99b1ff4e13041a91b1e1
2024-01-17 13:20:50 +01:00
Tim Ruffing
3bf4d68fc0 Merge bitcoin-core/secp256k1#1482: build: Clean up handling of module dependencies
e6822678ea build: Error if required module explicitly off (Tim Ruffing)
89ec583ccf build: Clean up handling of module dependencies (Tim Ruffing)

Pull request description:

  This is a cleanup which makes it easier to add further modules with dependencies, e.g., in #1452. The diff looks larger than it is because I also reordered the modules and made the order consistent between CMake and autotools.

  (We noticed that the current logic could be improved in https://github.com/BlockstreamResearch/secp256k1-zkp/pull/275.)

ACKs for top commit:
  jonasnick:
    ACK e6822678ea
  hebasto:
    ACK e6822678ea.

Tree-SHA512: 040e791e5b5b9b8845a39632633a45ca759391455910bdefba2b7b77c6340e65df6eda18199ae2ad65c30ee2fc6630471437aec143c26fe09ae4c11409a37622
2024-01-17 13:20:19 +01:00
Tim Ruffing
e6822678ea build: Error if required module explicitly off 2024-01-16 22:58:15 +01:00
Tim Ruffing
89ec583ccf build: Clean up handling of module dependencies
This also makes the order in which module options are processed
consistent between CMake and autotools (the reverse order of the listing
printed to stdout).
2024-01-16 22:36:50 +01:00
Jonas Nick
44378867a0 Merge bitcoin-core/secp256k1#1468: v0.4.1 release aftermath
b37fdb28ce check-abi: Minor UI improvements (Tim Ruffing)
ad5f589a94 check-abi: Default to HEAD for new version (Tim Ruffing)
9fb7e2f156 release process: Style and formatting nits (Tim Ruffing)
e7053d065b release process: Add email step (Tim Ruffing)
429d21dc79 release process: Run sanity checks on release PR (Tim Ruffing)

Pull request description:

ACKs for top commit:
  hebasto:
    ACK b37fdb28ce.
  jonasnick:
    ACK b37fdb28ce

Tree-SHA512: 6e18a5b897d29a3dd3a73ba81623dd91c04fa6730fb56374b924dc84baaec8c55d0c689ee1a41dab9a03ccd566082fc59ffb5d68cafd536a136fc7aaac2d8ef5
2024-01-16 20:01:44 +00:00
Tim Ruffing
a9db9f2d75 Merge bitcoin-core/secp256k1#1480: Get rid of untested sizeof(secp256k1_ge_storage) == 64 code path
ba5d72d626 assumptions: Use new STATIC_ASSERT macro (Tim Ruffing)
e53c2d9ffc Require that sizeof(secp256k1_ge_storage) == 64 (Tim Ruffing)
d0ba2abbff util: Add STATIC_ASSERT macro (Tim Ruffing)

Pull request description:

  This gets rid of an untested code path. Resolves https://github.com/bitcoin-core/secp256k1/issues/1352.

  This is a bit opinionated in the sense that it adds a static assertion where it's needed in `secp256k1_pubkey_save` and `secp256k1_pubkey_load`. I think this is justified in this case. It helps the reviewer verify that these functions are correct.

  See individual commit messages.

ACKs for top commit:
  sipa:
    utACK ba5d72d626
  jonasnick:
    ACK ba5d72d626

Tree-SHA512: 2553c0610b62bcda6d4ef26eb26b5b2e07acf723bcd299691a2d02da57af22b8763f63c2d4adb17d30de8825b6157be6e4f0398147854fbabdf8b865fb0b5c88
2024-01-09 18:59:27 +01:00
Jonas Nick
74b7c3b53e Merge bitcoin-core/secp256k1#1476: include: make docs more consistent
da7bc1b803 include: in doc, remove article in front of "pointer" (Jonas Nick)
aa3dd5280b include: make doc about ctx more consistent (Jonas Nick)
e3f690015a include: remove obvious "cannot be NULL" doc (Jonas Nick)

Pull request description:

ACKs for top commit:
  sipa:
    ACK da7bc1b803
  real-or-random:
    ACK da7bc1b803

Tree-SHA512: 809f312fa0cd1e9502ac79b8a1c502b87e6dfc2db8ad6bbd96d7ddbdaadad0c3b6110fa704b770c353cd34d5bf5547541cbb5f2779425d7419b584e721c854c2
2024-01-09 16:52:00 +00:00
Tim Ruffing
b37fdb28ce check-abi: Minor UI improvements 2024-01-09 01:05:09 +01:00
Tim Ruffing
ad5f589a94 check-abi: Default to HEAD for new version 2024-01-09 01:00:02 +01:00
Tim Ruffing
9fb7e2f156 release process: Style and formatting nits 2024-01-09 00:59:24 +01:00
Tim Ruffing
ba5d72d626 assumptions: Use new STATIC_ASSERT macro
This also splits the big "&&" expression into separate expressions. If
we ever see an assertion fail, the error message will tell it precisely
which one failed.
2024-01-08 16:10:55 +01:00
Tim Ruffing
e53c2d9ffc Require that sizeof(secp256k1_ge_storage) == 64
This gets rid of an untested code path. Resolves #1352.

secp256k1_ge_storage is a struct with two secp256k1_fe_storage fields.
The C standard allows the compiler to add padding between the fields and
at the end of the struct, but no sane compiler in the end would do this:
The only reason to add padding is to ensure alignment, but such padding
is never necessary between two fields of the same type.

Similarly, secp256k1_fe_storage is a struct with a single array of
uintXX_t. No padding is allowed between array elements. Again, C allows
the compiler to insert padding at the end of the struct, but there's no
absolute reason to do so in this case.

For the uintXX_t itself, this guaranteed to have no padding bits, i.e.,
it's guaranteed to have exactly XX bits.

So I claim that for any existing compiler in the real world,
sizeof(secp256k1_ge_storage) == 64.
2024-01-08 16:08:42 +01:00
Tim Ruffing
d0ba2abbff util: Add STATIC_ASSERT macro 2024-01-08 16:08:42 +01:00
Jonas Nick
da7bc1b803 include: in doc, remove article in front of "pointer" 2024-01-05 13:06:50 +00:00
Jonas Nick
aa3dd5280b include: make doc about ctx more consistent
Replaces "ctx: a secp256k1 context object" with "ctx: pointer to a context
object". Also removes the word "existing".
2024-01-04 17:15:03 +00:00
Jonas Nick
e3f690015a include: remove obvious "cannot be NULL" doc 2024-01-04 17:15:01 +00:00
Tim Ruffing
d373bf6d08 Merge bitcoin-core/secp256k1#1474: tests: restore scalar_mul test
3dbfb48946 tests: restore scalar_mul test (Jonas Nick)

Pull request description:

  Without this commit, the res[i][1] test vectors are unused. They were introduced to test the correctness of scalar_sqr(x) and scalar_mul(x, x). These tests were deleted as part of removing scalar_sqr in commit
  [5437e7bdfb](5437e7bdfb (diff-c2d5f1f7616875ab71cd41b053cfb428696988ff89642b931a0963d50f34f7e8L2195)).

  Discovered in https://github.com/bitcoin-core/secp256k1/discussions/1463 by Coding-Enthusiast (thanks!).

ACKs for top commit:
  real-or-random:
    utACK 3dbfb48946

Tree-SHA512: 914e08db3efaa1cef546a9730096e740478c422d41fedb2b71ec3a7ea962f81740a05dc7e7c1fb191088f6d38b5690479c7d0864ca8abf2b2e9c4334f03ca605
2024-01-04 17:48:36 +01:00
Tim Ruffing
79e094517c Merge bitcoin-core/secp256k1#1473: Fix typos
d77170a88d Fix typos (shuoer86)

Pull request description:

  Fix some typos caught by spell checker

ACKs for top commit:
  real-or-random:
    utACK d77170a88d

Tree-SHA512: 18722459b0b8d906ad93dd0f159b0a70a338d08c121ce6523bb6be70be33febdffa5241efc000acf18c70a845795b0582599a71d6dd25b663fee1358c8d38c85
2024-01-04 17:39:19 +01:00
Jonas Nick
3dbfb48946 tests: restore scalar_mul test
Without this commit, the res[i][1] test vectors are unused. They were introduced
to test the correctness of scalar_sqr(x) and scalar_mul(x, x). These tests were
deleted as part of removing scalar_sqr in commit
5437e7bdfb.
2024-01-04 15:45:11 +00:00