1007 Commits

Author SHA1 Message Date
Pieter Wuille
6be01036c8 Add secp256k1_fe_is_square_var function
The implementation calls the secp256k1_modinvNN_jacobi_var code, falling back
to computing a square root in the (extremely rare) case it failed converge.
2023-02-28 15:57:32 -05:00
Pieter Wuille
1de2a01c2b Native jacobi symbol algorithm
This introduces variants of the divsteps-based GCD algorithm used for
modular inverses to compute Jacobi symbols. Changes compared to
the normal vartime divsteps:
* Only positive matrices are used, guaranteeing that f and g remain
  positive.
* An additional jac variable is updated to track sign changes during
  matrix computation.
* There is (so far) no proof that this algorithm terminates within
  reasonable amount of time for every input, but experimentally it
  appears to almost always need less than 900 iterations. To account
  for that, only a bounded number of iterations is performed (1500),
  after which failure is returned. In VERIFY mode a lower iteration
  count is used to make sure that callers exercise their fallback.
* The algorithm converges to f=g=gcd(f0,g0) rather than g=0. To keep
  this test simple, the end condition is f=1, which won't be reached
  if started with non-coprime or g=0 inputs. Because of that we only
  support coprime non-zero inputs.
2023-02-28 15:54:00 -05:00
Pieter Wuille
04c6c1b181 Make secp256k1_modinv64_det_check_pow2 support abs val 2023-02-27 15:38:05 -05:00
Pieter Wuille
5fffb2c7af Make secp256k1_i128_check_pow2 support -(2^n) 2023-02-27 15:38:05 -05:00
Pieter Wuille
1b21aa5175
Merge bitcoin-core/secp256k1#1078: group: Save a normalize_to_zero in gej_add_ge
e089eecc1e54551287b12539d2211da631a6ec5c group: Further simply gej_add_ge (Tim Ruffing)
ac71020ebe052901000e5efa7a59aad77ecfc1a0 group: Save a normalize_to_zero in gej_add_ge (Tim Ruffing)

Pull request description:

  As discovered  by sipa in #1033.

  See commit message for reasoning but note that the infinity handling will be replaced in the second commit again.

ACKs for top commit:
  sipa:
    ACK e089eecc1e54551287b12539d2211da631a6ec5c
  apoelstra:
    ACK e089eecc1e54551287b12539d2211da631a6ec5c

Tree-SHA512: fb1b5742e73dd8b2172b4d3e2852490cfd626e8673b72274d281fa34b04e9368a186895fb9cd232429c22b14011df136f4c09bdc7332beef2b3657f7f2798d66
2023-02-14 14:55:46 -05:00
Jonas Nick
eb6bebaee3
scalar: restrict split_lambda args, improve doc and VERIFY_CHECKs
VERIFY_CHECK(r1 != r2) is added because otherwise the verify_scalar_split fails.
2023-01-19 21:14:38 +00:00
Jonas Nick
620ba3d74b
benchmarks: fix bench_scalar_split
scalar_split_lambda requires that the input pointer is different to both output
pointers. Without this fix, the internal benchmarks crash when compiled with
-DVERIFY.

This was introduced in commit 362bb25608dbcd724a07dd5170c4ebe081c3dd84 (which
requires configuring with --enable-endomorphism to exhibit the crash).
2023-01-19 18:12:31 +00:00
Pieter Wuille
5fbff5d348
Merge bitcoin-core/secp256k1#1170: contexts: Forbid destroying, cloning and randomizing the static context
e39d954f118a29db2c33e9a9a507053fff5573ed tests: Add CHECK_ILLEGAL(_VOID) macros and use in static ctx tests (Tim Ruffing)
61841fc9ee5aa1ffde3ccd512660207034125ebd contexts: Forbid randomizing secp256k1_context_static (Tim Ruffing)
4b6df5e33e197a50fd7d9bc4c14b8ba8526013b9 contexts: Forbid cloning/destroying secp256k1_context_static (Tim Ruffing)

Pull request description:

  As discussed in #1126.

  For randomization, this has a history. Initially, this threw the illegal callback but then we changed it to be a no-op on non-signing contexts: 6198375218 But this was with (non-static) none/verification contexts in mind, not with the static context. If we anyway forbid cloning the static context, you should never a way to randomize a copy of the static context. (You need a copy because the static context itself is not writable. But you cannot obtain a copy except when using memcpy etc.)

ACKs for top commit:
  sipa:
    utACK e39d954f118a29db2c33e9a9a507053fff5573ed
  apoelstra:
    ACK e39d954f118a29db2c33e9a9a507053fff5573ed

Tree-SHA512: dc804b15652d536b5d67db7297ac0e65eab3a64cbb35a9856329cb87e7ea0fe8ea733108104b3bba580077fe03d6ad6b161c797cf866a74722bab7849f0bb60c
2023-01-19 13:04:18 -05:00
Pieter Wuille
233822d849
Merge bitcoin-core/secp256k1#1195: ctime_tests: improve output when CHECKMEM_RUNNING is not defined
8f51229e0348a1524fed541f334cd4f1726d2685 ctime_tests: improve output when CHECKMEM_RUNNING is not defined (Jonas Nick)

Pull request description:

  When seeing the output
  ```
  Unless compiled under msan, this test can only usefully be run inside valgrind.
  ```
  I thought that I would have to go back to the `configure` output to manually check if it was compiled under memsan to determine whether this test can be usefully run outside valgrind. But when we go into this branch then it was definitely not compiled under msan, which means that we can make the output clearer.

ACKs for top commit:
  sipa:
    utACK 8f51229e0348a1524fed541f334cd4f1726d2685
  real-or-random:
    utACK 8f51229e03

Tree-SHA512: a4953a158b1375d8fc3a2ee29e7014c5399becf5f75ffd3765c0141861e092fbc120003e00dfd25ec54b92a466e133377b96d5a9f4017c100aaf64fb9a045df1
2023-01-19 11:00:16 -05:00
Tim Ruffing
e39d954f11 tests: Add CHECK_ILLEGAL(_VOID) macros and use in static ctx tests 2023-01-19 13:36:40 +01:00
Tim Ruffing
61841fc9ee contexts: Forbid randomizing secp256k1_context_static 2023-01-18 16:47:31 +01:00
Tim Ruffing
4b6df5e33e contexts: Forbid cloning/destroying secp256k1_context_static 2023-01-18 16:39:31 +01:00
Jonas Nick
8f51229e03
ctime_tests: improve output when CHECKMEM_RUNNING is not defined 2023-01-18 09:02:47 +00:00
Russell O'Connor
d6ff738d5b Ensure safety of ctz_debruijn implementation.
Adding U to the magic constants ensures that we are not mixing unsigned and signed value during multiplication, and ensures that the multiplication will not be subject to integer promotion.

The (uint32_t)/(uint64_t) casts ensure the values are properly truncated no matter the size of an int.

Prior to this commit, if secp256k1_ctz32_var_debruijn were some how managed to be built on a platform with 64-bit ints, (though this function is specifically only intended to be used on 32-bit platforms) it would perform an out-of-bounds array access.
2023-01-16 22:23:57 -05:00
Tim Ruffing
a01a7d86dc
Merge bitcoin-core/secp256k1#1192: Switch to exhaustive groups with small B coefficient
ce60785b2654e60b43577dd75996b7020afbfec8 Introduce SECP256K1_B macro for curve b coefficient (Pieter Wuille)
4934aa79958b506a6e9cfcfe30a8f685db3f5f5f Switch to exhaustive groups with small B coefficient (Pieter Wuille)

Pull request description:

  This has the advantage that in the future, multiplication with B can be done using `secp256k1_fe_mul_int` rather than the slower `secp256k1_fe_mul`.

ACKs for top commit:
  real-or-random:
    ACK ce60785b2654e60b43577dd75996b7020afbfec8 also ran the exhaustive tests with the group of size 7
  apoelstra:
    ACK ce60785b2654e60b43577dd75996b7020afbfec8

Tree-SHA512: 006041189d18319ddb9c0ed54e479f393b83ab2a368d198bd24860d1d2574c0c1a311aea24fbef2e74bb7859a687dfc803b9e963e6dc5c61cb707e20f52b5a70
2023-01-16 22:36:15 +01:00
Tim Ruffing
a7a7bfaf3d
Merge bitcoin-core/secp256k1#1190: Make all non-API functions (except main) static
e03ef8655933d3e2b4830e7f8fe86deba820a073 Make all non-API functions (except main) static (Pieter Wuille)

Pull request description:

ACKs for top commit:
  real-or-random:
    utACK e03ef8655933d3e2b4830e7f8fe86deba820a073
  hebasto:
    ACK e03ef8655933d3e2b4830e7f8fe86deba820a073.

Tree-SHA512: 36a35d9a8da05411c88644aec81e79883febce3e08c9eb6b0ec95cfc3705fd6abfd66f7ee10dfa081ca20786d26b0a060ead7f5c8109bf02a73dde7ef811144b
2023-01-16 18:46:37 +01:00
Pieter Wuille
ce60785b26 Introduce SECP256K1_B macro for curve b coefficient 2023-01-13 17:05:39 -05:00
Pieter Wuille
4934aa7995 Switch to exhaustive groups with small B coefficient 2023-01-13 17:05:35 -05:00
Pieter Wuille
74b026f05d Add runtime checking for DECLASSIFY flag 2023-01-11 16:07:37 -05:00
Pieter Wuille
5048be17e9 Rename valgrind_ctime_test -> ctime_tests 2023-01-11 16:07:37 -05:00
Pieter Wuille
6eed6c18de Update error messages to suggest msan as well 2023-01-11 16:07:37 -05:00
Pieter Wuille
8e11f89a68 Add support for msan integration to checkmem.h 2023-01-11 16:07:37 -05:00
Pieter Wuille
8dc64079eb Add compile-time error to valgrind_ctime_test 2023-01-11 16:07:37 -05:00
Pieter Wuille
0db05a770e Abstract interactions with valgrind behind new checkmem.h 2023-01-11 16:07:35 -05:00
Tim Ruffing
9a93f48f50 refactor: Rename STTC to STATIC_CTX in tests 2023-01-10 18:43:09 +01:00
Tim Ruffing
3385a2648d refactor: Rename global variables to uppercase in tests 2023-01-10 18:43:09 +01:00
Pieter Wuille
e03ef86559 Make all non-API functions (except main) static 2023-01-09 12:02:27 -05:00
Tim Ruffing
39e8f0e3d7 refactor: Separate run_context_tests into static vs proper contexts 2023-01-05 10:28:10 +01:00
Tim Ruffing
a4a09379b1 tests: Clean up and improve run_context_tests() further 2023-01-05 10:28:10 +01:00
Tim Ruffing
fc90bb5695 refactor: Tidy up main() 2023-01-05 10:28:10 +01:00
Tim Ruffing
f32a36f620 tests: Don't use global context for context tests 2023-01-05 10:28:10 +01:00
Tim Ruffing
ce4f936c4f tests: Tidy run_context_tests() by extracting functions 2023-01-05 10:28:04 +01:00
Tim Ruffing
18e0db30cb tests: Don't recreate global context in scratch space test 2023-01-04 16:52:36 +01:00
Tim Ruffing
b19806122e tests: Use global copy of secp256k1_context_static instead of clone 2023-01-04 16:39:50 +01:00
Tim Ruffing
01b819a8c7
Merge bitcoin-core/secp256k1#1158: Add a secp256k1_i128_to_u64 function.
d21647520532957a78027be1ab606b814a2ec720 test secp256k1_i128_to_i64 (Russell O'Connor)
4bc429019dc4bff6af0f9824ad6ab6745f09f8ba Add a secp256k1_i128_to_u64 function. (Russell O'Connor)

Pull request description:

  I wanted to experiment with what would be required to split up `secp256k1_i128_to_i64` between those cases when a signed 64 bit value is being demoted, versus an unsigned 64 bit value is being extracted from the lower bits, and this is the result.

  I'm not sure this is a useful PR, so feel free to close it.  However, since it is already written, I figured it is worth at least discussing.

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

Tree-SHA512: 41dbb1d33b3078bee8e71a838cfad6f1859c0bba602ae061259add8e9e8ea5aa482daa41de79dbd7433ddbef4a0bc52757f3c45d63acc9c0eb05aa3ca891b922
2022-12-21 17:25:07 +01:00
Jonas Nick
eacad90f69
Merge bitcoin-core/secp256k1#1171: Change ARG_CHECK_NO_RETURN to ARG_CHECK_VOID which returns (void)
a49e0940ad671f96533d5a79f2ca1fa4020abc0a docs: Fix typo (Tim Ruffing)
2551cdac903937c112357d4eb43bc194072a6cc2 tests: Fix code formatting (Tim Ruffing)
c635c1bfd54417487745bbbf518114a962a47bcc Change ARG_CHECK_NO_RETURN to ARG_CHECK_VOID which returns (void) (Tim Ruffing)
cf66f2357c6ad8c5fe219577ad56e6f51301ca5a refactor: Add helper function secp256k1_context_is_proper() (Tim Ruffing)

Pull request description:

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

Tree-SHA512: 0fd4ee88510f2de0de96378ae69ce6e610a446000bb78597026c5924803e1ce5a4f76303fc6446233a6129f9c42dce1b1549f93bef935131101e47b5a69cdf2f
2022-12-21 15:28:10 +00:00
Hennadii Stepanov
b627ba7050
Remove dependency on src/libsecp256k1-config.h
This change eases the use of alternate build systems by moving
the variables in `src/libsecp256k1-config.h` to compiler macros
for each invocation, preventing duplication of these variables
for each build system.

Co-authored-by: Ali Sherief <ali@notatether.com>
2022-12-15 10:56:16 +00:00
Tim Ruffing
2551cdac90 tests: Fix code formatting 2022-12-08 16:30:26 +01:00
Tim Ruffing
c635c1bfd5 Change ARG_CHECK_NO_RETURN to ARG_CHECK_VOID which returns (void) 2022-12-08 16:30:26 +01:00
Tim Ruffing
cf66f2357c refactor: Add helper function secp256k1_context_is_proper() 2022-12-08 16:30:26 +01:00
Jonas Nick
d6dc0f4ae3
tests: Switch to NONE contexts in module tests 2022-12-07 10:54:50 +00:00
Jonas Nick
0c8a5caddd
tests: Switch to NONE contexts in tests.c 2022-12-07 10:54:50 +00:00
Jonas Nick
86540e9e1f
tests: add test for deprecated flags and rm them from run_context 2022-12-07 10:54:50 +00:00
Jonas Nick
caa0ad631e
group: add gej_eq_var 2022-12-07 10:54:50 +00:00
Jonas Nick
37ba744f5b
tests: Switch to NONE contexts in exhaustive and ctime tests 2022-12-07 10:54:50 +00:00
Jonas Nick
8d7a9a8eda
benchmarks: Switch to NONE contexts 2022-12-07 10:54:43 +00:00
Tim Ruffing
e02d6862bd selftest: Expose in public API 2022-12-05 11:26:44 +01:00
Tim Ruffing
e383fbfa66 selftest: Rename internal function to make name available for API 2022-12-05 11:26:44 +01:00
Tim Ruffing
d2c6d48de3 tests: Use new name of static context 2022-12-05 11:26:44 +01:00
Tim Ruffing
53796d2b24 contexts: Rename static context 2022-12-05 11:26:44 +01:00