Commit Graph

1273 Commits

Author SHA1 Message Date
Jonas Nick
8be3839fb2 Remove unused scratch space from API 2024-10-21 09:18:44 +00:00
merge-script
a88aa93506 Merge bitcoin-core/secp256k1#1603: f can never equal -m
ef7ff03407 f can never equal -m (Russell O'Connor)

Pull request description:

  In fact, before reaching this particular VERIFY_CHECK, we had already successfully passed through

      VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, -1) > 0); /* f > -modulus */

  ensuring that f is not -m.

ACKs for top commit:
  sipa:
    ACK ef7ff03407
  real-or-random:
    utACK ef7ff03407

Tree-SHA512: a8a8dcbad4dff36b9c49e40e07b212312cbf915132aea008eab6ea61b35bddb6d7782229c2cc528fb404d05132482c602cad768414d76153bb425a3d23714fff
2024-10-08 02:09:18 +02:00
Jonas Nick
168c92011f build: allow enabling the musig module in cmake 2024-10-07 14:03:42 +00:00
Jonas Nick
f411841a46 Add module "musig" that implements MuSig2 multi-signatures (BIP 327) 2024-10-07 14:03:42 +00:00
Jonas Nick
0be79660f3 util: add constant-time is_zero_array function 2024-10-07 14:03:42 +00:00
Jonas Nick
c8fbdb1b97 group: add ge_to_bytes_ext and ge_from_bytes_ext 2024-10-07 14:03:40 +00:00
Russell O'Connor
ef7ff03407 f can never equal -m
In fact, before reaching this particular VERIFY_CHECK, we had already successfully passed through

    VERIFY_CHECK(secp256k1_modinv64_mul_cmp_62(&f, len, &modinfo->modulus, -1) > 0); /* f > -modulus */

ensuring that f is not -m.
2024-09-25 11:05:23 -04:00
Jonas Nick
85e224dd97 group: add ge_to_bytes and ge_from_bytes 2024-09-02 19:05:47 +00:00
MarcoFalke
fa67b6752d refactor: Use array initialization for unterminated strings
The previous code is correct and harmless to initialize an array with a
non-terminated character sequence using a string literal.

However, it requires exactly specifying the array size, which can be
cumbersome.

Also, GCC-15 may issue the -Wunterminated-string-initialization warning.
[1]

Fix both issues by using array initialization. This refactoring commit
does not change behavior.

[1] Example warning:

src/modules/schnorrsig/main_impl.h:48:46: error: initializer-string for array of 'unsigned char' is too long [-Werror=unterminated-string-initialization]
   48 | static const unsigned char bip340_algo[13] = "BIP0340/nonce";
      |                                              ^~~~~~~~~~~~~~~
2024-08-15 17:58:14 +02:00
Jonas Nick
af551ab9db tests: do not use functions from extrakeys module
This fixes a bug introduced in 7d2591ce12 that
prevented compiling the library without enabling the extrakeys module.
2024-07-24 06:57:38 +00:00
Elliot Lee
0e2fadb20c fix: typos in secp256k1.c 2024-06-24 14:24:48 -07:00
Sebastian Falbesoner
e73f6f8fd9 tests: refactor: drop secp256k1_ prefix from testrand.h functions
The rename was done with the following command:

$ sed -i 's/secp256k1_testrand/testrand/g' $(git grep -l secp256k1_testrand)
2024-05-27 03:29:36 +02:00
Sebastian Falbesoner
0ee7453a99 tests: refactor: add testutil_ prefix to testutil.h functions 2024-05-27 03:29:32 +02:00
Sebastian Falbesoner
0c6bc76dcd tests: refactor: move random_ helpers from tests.c to testutil.h
Can be reviewed via `--color-moved=dimmed-zebra`.
2024-05-27 03:09:11 +02:00
Sebastian Falbesoner
0fef8479be tests: refactor: rename random_field_element_magnitude -> random_fe_magnitude
Note that the already existing function `random_fe_magnitude` is removed
and the call-sites are adapted to pass the magnitude range of 8
(the maximum for secp256k1_fe_mul and secp256k1_fe_sqr) explicitly.
2024-05-27 03:09:11 +02:00
Sebastian Falbesoner
59db007f0f tests: refactor: rename random_group_element_... -> random_ge_...
The rename was done with the following command:

$ sed -i 's/random_group_element_/random_ge_/g' $(git grep -l random_group_element_)
2024-05-27 03:09:04 +02:00
Nicolas Iooss
9554362b15 tests: call secp256k1_ecmult_multi_var with a non-NULL error callback
Function secp256k1_ecmult_multi_var expects to be called with a non-NULL
error_callback parameter. Fix the invocation in test_ecmult_accumulate
to do this.

While at it, wrap the call in a CHECK macro to ensure it succeeds.

Fixes: https://github.com/bitcoin-core/secp256k1/issues/1527
2024-05-08 19:13:35 +02:00
Jonas Nick
7d2591ce12 Add secp256k1_pubkey_sort
Co-authored-by: Tim Ruffing <crypto@timruffing.de>
Co-authored-by: Russell O'Connor <roconnor@blockstream.io>
2024-04-25 20:23:31 +00:00
Pieter Wuille
a043940253 Permit COMB_BITS < 256 for exhaustive tests 2024-04-19 11:43:46 -04:00
Pieter Wuille
39b2f2a321 Add test case for ecmult_gen recoded = {-1,0,1} 2024-04-19 11:43:46 -04:00
Pieter Wuille
644e86de9a Reintroduce projective blinding 2024-04-19 11:43:46 -04:00
Peter Dettman
07810d9abb Reduce side channels from single-bit reads
Co-authored-by: Tim Ruffing <crypto@timruffing.de>
2024-04-19 11:43:46 -04:00
Peter Dettman
a0d32b597d Optimization: use Nx32 representation for recoded bits
The existing code needs to deal with the edge case that bit_pos >= 256,
which would lead to an out-of-bounds read from secp256k1_scalar.

Instead, recode the scalar into an array of uint32_t with enough zero
padding at the end to alleviate the issue. This also simplifies the
code, and is necessary for a security improvement in a follow-up
commit.

Original code by Peter Dettman, with modifications by Pieter Wuille.
2024-04-19 11:43:46 -04:00
Pieter Wuille
e03dcc44b5 Make secp256k1_scalar_get_bits support 32-bit reads
The old code would trigger UB when count=32.
2024-04-19 11:43:46 -04:00
Pieter Wuille
5005abee60 Rename scalar_get_bits -> scalar_get_bits_limb32; return uint32_t 2024-04-19 11:43:46 -04:00
Peter Dettman
6247f485b6 Optimization: avoid unnecessary doublings in precomputation 2024-04-19 11:43:46 -04:00
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
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
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
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
51df2d9ab3 tests: Drop redundant _scalar_check_overflow calls
Redundant since d23da6d557.
2024-01-17 16:54:04 +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
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
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
shuoer86
d77170a88d Fix typos 2024-01-03 20:03:07 +08:00