389 Commits

Author SHA1 Message Date
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
5437e7bdfbffddf69fdf7b4af7e997c78f5dafbf.
2024-01-04 15:45:11 +00:00
Pieter Wuille
04af0ba162 Replace ge_equals_ge[,j] calls with group.h equality calls 2023-12-01 16:10:20 -05:00
Pieter Wuille
60525f6c14 Add unit tests for group.h equality functions 2023-12-01 16:10:15 -05:00
Pieter Wuille
21f49d9bec Remove unused secp256k1_scalar_shr_int 2023-11-04 15:55:12 -04:00
Pieter Wuille
115fdc7232 Remove unused secp256k1_wnaf_const 2023-11-04 15:55:12 -04:00
Jonas Nick
aa9f3a3c00 ecmult_const: add/improve tests
* add test case for a=infinity

  The corresponding ecmult_const branch was not tested before this commit.

* add test for edge cases
2023-11-04 15:55:12 -04:00
Pieter Wuille
2140da9cd5 Add secp256k1_scalar_half for halving scalars (+ tests/benchmarks).
Co-authored-by: Jonas Nick <jonasd.nick@gmail.com>
Co-authored-by: Tim Ruffing <crypto@timruffing.de>
2023-11-04 15:54:08 -04:00
Tim Ruffing
ee7aaf213e
Merge bitcoin-core/secp256k1#1395: tests: simplify random_fe_non_zero (remove loop limit and unneeded normalize)
c45b7c4fbbf41b011f138c465a58322a36664fd3 refactor: introduce testutil.h (deduplicate `random_fe_`, `ge_equals_` helpers) (Sebastian Falbesoner)
dc5514144fb9d412aa3845432b053ee06a27da37 tests: simplify `random_fe_non_zero` (remove loop limit and unneeded normalize) (Sebastian Falbesoner)

Pull request description:

  `random_fe_non_zero` contains a loop iteration limit that ensures that we abort if `random_fe` ever yielded zero more than ten times in a row. This construct was first introduced in PR #19 (commit 09ca4f32) for random non-square field elements and was later refactored into the non-zero helper in PR #25 (commit 6d6102fe). The copy-over to the exhaustive tests happened recently in PR #1118 (commit 0f864207).

  This case seems to be practically irrelevant and I'd argue for keeping things simple and removing it (which was already suggested in https://github.com/bitcoin-core/secp256k1/pull/1118#discussion_r1067259954); if there's really a worry that the test's random generator is heavily biased towards certain values or value ranges then there should consequently be checks at other places too (e.g. directly in `random_fe` for 256-bit values that repeatedly overflow, i.e. >= p).

  Also, the _fe_normalize call is not needed and can be removed, as the result of `random_fe` is already normalized.

ACKs for top commit:
  real-or-random:
    utACK c45b7c4fbbf41b011f138c465a58322a36664fd3
  siv2r:
    ACK `c45b7c4` (reviewed the changes and tests for both the commits passed locally).

Tree-SHA512: 4ffa66dd0b8392d7d0083a71e7b0682ad18f9261fd4ce8548c3059b497d3462db97e16114fded9787661ca447a877a27f5b996bd7d47e6f91c4454079d28a8ac
2023-09-14 15:06:41 +02:00
Jonas Nick
70303643cf
tests: add CHECK_ERROR_VOID and use it in scratch tests 2023-09-04 16:19:49 +00:00
Jonas Nick
f8d7ea68df
tests: Replace counting_illegal_callbacks with CHECK_ILLEGAL_VOID
This commit also explicitly initializes shortpubkey. For some reason, removing
surrounding, unrelated lines results in gcc warnings when configured with
--enable-ctime-tests=no --with-valgrind=no.
2023-09-04 16:19:40 +00:00
Jonas Nick
a1d52e3e12
tests: remove unnecessary test in run_ec_pubkey_parse_test
This test tested whether setting the callback works correctly which should be
tested in the context tests.
2023-09-04 12:52:19 +00:00
Jonas Nick
875b0ada25
tests: remove unnecessary set_illegal_callback 2023-09-04 12:50:32 +00:00
Tim Ruffing
0ba2b94551
Merge bitcoin-core/secp256k1#1373: Add invariant checking for scalars
d23da6d55714271c720fee58fbff5e5ef2fe193f use secp256k1_scalar_verify checks (stratospher)
c7d0454932b42a9728b55033c94e000b1dbbb6f2 add verification for scalars (stratospher)
ad152151b06a40aaf6cd90561356ff451996455d update max scalar in scalar_cmov_test and fix schnorrsig_verify exhaustive test (stratospher)

Pull request description:

  From #1360. This PR:
  1. adds `secp256k1_scalar_verify` to make sure scalars are reduced mod the group order in VERIFY mode
  2. uses `secp256k1_scalar_verify` in all the scalar functions except `secp256k1_scalar_clear`, `secp256k1_scalar_reduce_512`, `secp256k1_scalar_mul_512` and `secp256k1_scalar_*_var` functions in `scalar_low_impl.h`

ACKs for top commit:
  real-or-random:
    utACK d23da6d55714271c720fee58fbff5e5ef2fe193f
  theStack:
    Code-review ACK d23da6d55714271c720fee58fbff5e5ef2fe193f

Tree-SHA512: a371b319d948198c4038d35c9ea58f4b94de4dc312215e2b78a323c2acd4ae1355d97935c558b388774832d6d0058b97ff8ca50c3aab40b9ede5307760d0a505
2023-08-18 11:44:17 +02:00
Sebastian Falbesoner
c45b7c4fbb refactor: introduce testutil.h (deduplicate random_fe_, ge_equals_ helpers) 2023-08-17 19:44:00 +02:00
Sebastian Falbesoner
dc5514144f tests: simplify random_fe_non_zero (remove loop limit and unneeded normalize)
`random_fe_non_zero` contains a loop iteration limit that ensures that
we abort if `random_fe` ever yielded zero more than ten times in a row.
This construct was first introduced in PR #19 (commit 09ca4f32) for
random non-square field elements and was later refactored into the
non-zero helper in PR #25 (commit 6d6102fe). The copy-over to the
exhaustive tests happened recently in PR #1118 (commit 0f864207).

This case seems to be practically irrelevant and I'd argue for keeping
things simple and removing it; if there's really a worry that the test's
random generator is heavily biased towards certain values or value
ranges then there should consequently be checks at other places too
(e.g. directly in `random_fe` for 256-bit values that repeatedly
overflow, i.e. >= p).

Also, the _fe_normalize call is not needed and can be removed, as the
result of `random_fe` is already normalized.
2023-08-17 19:40:49 +02:00
siv2r
54058d16fe field: remove secp256k1_fe_equal_var
`fe_equal_var` hits a fast path only when the inputs are unequal, which is
uncommon among its callers (public key parsing, ECDSA verify).
2023-08-16 17:39:25 +05:30
siv2r
bb4efd6404 tests: remove unwanted secp256k1_fe_normalize_weak call
It is not neccessary for the second argument in `secp256k1_fe_equal_var`
(or `secp256k1_fe_equal`) to have magnitude = 1.
Hence, removed the `secp256k1_fe_normalize_weak` call for those argument.
2023-08-16 17:38:04 +05:30
Tim Ruffing
eedd781085
Merge bitcoin-core/secp256k1#1348: tighten group magnitude limits, save normalize_weak calls in group add methods (revival of #1032)
b7c685e74adbd83937990e90f076600fabf8ccf0 Save _normalize_weak calls in group add methods (Peter Dettman)
c83afa66e0c324e42d13adff0e4f7db9b2868788 Tighten group magnitude limits (Peter Dettman)
173e8d061a8d1526f80d9ae79dd7f0371d38f7e0 Implement current magnitude assumptions (Peter Dettman)
49afd2f5d8c323d32a21f2fe182823b6d7704eb2 Take use of _fe_verify_magnitude in field_impl.h (Sebastian Falbesoner)
4e9661fc426c6068b2472f52a772c312bc26acc9 Add _fe_verify_magnitude (no-op unless VERIFY is enabled) (Peter Dettman)
690b0fc05abd76cb7f6bd87e88bf7b8b0fd1ab70 add missing group element invariant checks (Sebastian Falbesoner)

Pull request description:

  This PR picks up #1032 by peterdettman. It's essentially a rebase on master; the original first commit (09dbba561fdb9d57a2cc9842ce041d9ba29a6189) which introduced group verification methods has mostly been replaced by PR #1299 (commit f20266722ac93ca66d1beb0d2f2d2469b95aafea) and what remains now is only adding a few missing checks at some places. The remaining commits are unchanged, though some (easy-to-solve) conflicts appeared through cherry-picking. The last commit which actually removes the `normalize_weak` calls is obviously the critical one and needs the most attention for review.

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

Tree-SHA512: f15167eff7ef6ed971c726a4d738de9a15be95b0c947d7e38329e7b16656202b7113497d36625304e784866349f2293f6f1d8cb97df35393af9ea465a4156da3
2023-08-16 13:51:11 +02:00
Sebastian Falbesoner
a1bd4971d6 refactor: take use of secp256k1_scalar_{zero,one} constants (part 2) 2023-08-01 02:40:21 +02:00
Peter Dettman
c83afa66e0 Tighten group magnitude limits
- adjust test methods that randomize magnitudes

Co-authored-by: Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
Co-authored-by: Jonas Nick <jonasd.nick@gmail.com>
2023-07-28 13:05:04 +02:00
stratospher
ad152151b0 update max scalar in scalar_cmov_test and fix schnorrsig_verify exhaustive test
- `secp256k1_scalar_set_int` in scalar_low uses input mod EXHAUSTIVE_TEST_ORDER
- directly store s in sig64 without reducing it mod the group order for testing
2023-07-27 11:59:33 +05:30
Jonas Nick
c545fdc374
Merge bitcoin-core/secp256k1#1298: Remove randomness tests
6ec3731e8c53658fcf68634c81bb1e47cad791ad Simplify test PRNG implementation (Pieter Wuille)
fb5bfa4eed834dcd58109525408a2d88dabc48c5 Add static test vector for Xoshiro256++ (Tim Ruffing)
723e8ca8f7ee75126bac4240feeac825c23a0d44 Remove randomness tests (Pieter Wuille)

Pull request description:

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

Tree-SHA512: 4cbbb9c42e31f067b17dd9169ae5d5e68bce77d1253452db9df523d3be2b5d61002d5a4203e5a153f257ec63c5ff2113555743eeb402d4b6c573069ea494d407
2023-07-18 14:05:46 +00:00
Tim Ruffing
cc55757552
Merge bitcoin-core/secp256k1#1340: clean up in-comment Sage code (refer to secp256k1_params.sage, update to Python3)
600c5adcd59240305e22918943f45dceeabb7e93 clean up in-comment Sage code (refer to secp256k1_params.sage, update to Python3) (Sebastian Falbesoner)

Pull request description:

  Some of the C source files contain contain in-comment Sage code calculating secp256k1 parameters that are already defined in the file secp256k1_params.sage.  Replace that by a corresponding load instruction and access the necessary variables. In ecdsa_impl.h, update the comment to use a one-line shell command calling sage to get the values.

  The remaining code (test `test_add_neg_y_diff_x` in tests.c) is updated to work with a current version based on Python3 (Sage 9.0+, see https://wiki.sagemath.org/Python3-Switch).

  The latter can be seen as a small follow-up to PR #849 (commit 13c88efed0005eb6745a222963ee74564054eafb).

ACKs for top commit:
  sipa:
    ACK 600c5adcd59240305e22918943f45dceeabb7e93
  real-or-random:
    ACK 600c5adcd59240305e22918943f45dceeabb7e93

Tree-SHA512: a9e52f6afbce65edd9ab14203612c3d423639f450fe8f0d269a3dda04bebefa95b607f7aa0faec864cb78b46d49f281632bb1277118749b7d8613e9f5dcc8f3d
2023-07-10 18:34:16 +02:00
Sebastian Falbesoner
600c5adcd5 clean up in-comment Sage code (refer to secp256k1_params.sage, update to Python3)
Some of the C source files contain contain in-comment Sage code
calculating secp256k1 parameters that are already defined in the file
secp256k1_params.sage.  Replace that by a corresponding load instruction
and access the necessary variables. In ecdsa_impl.h, update the comment
to use a one-line shell command calling sage to get the values.

The remaining code (test `test_add_neg_y_diff_x` in tests.c) is updated
to work with a current version based on Python3 (Sage 9.0+, see
https://wiki.sagemath.org/Python3-Switch).

The latter can be seen as a small follow-up to PR #849 (commit
13c88efed0005eb6745a222963ee74564054eafb).
2023-07-10 02:28:31 +02:00
Sebastian Falbesoner
5a95a268b9 tests: introduce helper for non-zero random_fe_test results
There are several instances in the tests where random non-zero field
elements are generated by calling `random_fe_test` in a do/while-loop.
This commit deduplicates all these by introducing a
`random_fe_non_zero_test` helper. Note that some instances checked the
is-zero condition via `secp256k1_fe_normalizes_to_zero_var`, which is
unnecessary, as the result of `random_fe_test` is already normalized (so
strictly speaking, this is not a pure refactor).
2023-06-27 10:31:24 +02:00
Sebastian Falbesoner
304421d57b tests: refactor: remove duplicate function random_field_element_test
There is a function `random_fe_test` which does exactly the
same, so use that instead. Note that it's also moved up before the
`random_group_element_test` function, in order to avoid needing a forward
declaration.
2023-06-27 10:31:22 +02:00
Tim Ruffing
3aef6ab8e1
Merge bitcoin-core/secp256k1#1345: field: Static-assert that int args affecting magnitude are constant
be8ff3a02aeff87c60d49883a1b2afa8b2999bbe field: Static-assert that int args affecting magnitude are constant (Tim Ruffing)

Pull request description:

  See #1001.

  Try to revert the lines in `tests.c` to see the error message in action.

ACKs for top commit:
  sipa:
    ACK be8ff3a02aeff87c60d49883a1b2afa8b2999bbe. Verified by introducing some non-constant expressions and seeing compilation fail.
  theStack:
    ACK be8ff3a02aeff87c60d49883a1b2afa8b2999bbe

Tree-SHA512: 8befec6ee64959cdc7f3e29b4b622410794cfaf69e9df8df17600390a93bc787dba5cf86239de6eb2e99c038b9aca5461e4b3c82f0e0c4cf066ad7c689941b19
2023-06-27 09:39:10 +02:00
Sebastian Falbesoner
7d8d5c86df tests: refactor: take use of secp256k1_ge_x_on_curve_var
The recently merged ellswift PR (#1129) introduced a helper
`secp256k1_ge_x_on_curve_var` to check if a given X coordinate is
valid (i.e. the expression x^3 + 7 is square, see commit
79e5b2a8b80f507e2c9936ff1c4e2fb39bc66a4e). This can be used for code
deduplication in the `ecmult_const_mult_xonly` test.
2023-06-25 22:26:20 +02:00
Pieter Wuille
9695deb351 Add tests for ellswift module
These include both test vectors taken from BIP324, as randomized unit tests.
2023-06-20 11:31:58 -04:00
Pieter Wuille
79e5b2a8b8 Add functions to test if X coordinate is valid 2023-06-20 11:05:32 -04:00
Sebastian Falbesoner
7067ee54b4 tests: add tests for secp256k1_{read,write}_be64
This can be reviewed with `--ignore-all-space` (or `-w`), to ignore
already existing code that was only indented.
2023-06-17 01:54:25 +02:00
Tim Ruffing
be8ff3a02a field: Static-assert that int args affecting magnitude are constant
See #1001.
2023-06-13 13:34:49 +02:00
Tim Ruffing
debf3e5c08
Merge bitcoin-core/secp256k1#1330: refactor: take use of secp256k1_scalar_{zero,one} constants
ade5b367018a624ff7ca1ecbb4a64889d47b0142 tests: add checks for scalar constants `secp256k1_scalar_{zero,one}` (Sebastian Falbesoner)
654246c63585422a184121a26d42dcae792e87c6 refactor: take use of `secp256k1_scalar_{zero,one}` constants (Sebastian Falbesoner)

Pull request description:

  Rather than allocating a (non-constant) scalar variable on the stack with the sole purpose of setting it to a constant value, the global constants `secp256k1_scalar_{zero,one}` (apparently introduced in 34a67c773b0871e5797c7ab506d004e80911f120, PR #710) can be directly used instead for the values 0 or 1. There is very likely not even a difference in run-time, but it leads to simpler and less code which might be nice.

ACKs for top commit:
  sipa:
    utACK ade5b367018a624ff7ca1ecbb4a64889d47b0142
  real-or-random:
    utACK ade5b367018a624ff7ca1ecbb4a64889d47b0142

Tree-SHA512: 0ff05a449c153f7117a4a56efef04b2087c2330f4692f3390a0b1d95573785ac7ae3fe689ed0ec2ecc64b575d2489d6e341d32567e75a1a4b4d458c3ecd406a1
2023-05-31 19:39:05 +02:00
Sebastian Falbesoner
ade5b36701 tests: add checks for scalar constants secp256k1_scalar_{zero,one} 2023-05-30 12:24:33 +02:00
Hennadii Stepanov
e83801f5db
test: Warn if both VERIFY and COVERAGE are defined 2023-05-30 11:17:20 +01:00
Sebastian Falbesoner
654246c635 refactor: take use of secp256k1_scalar_{zero,one} constants 2023-05-30 12:10:41 +02:00
Pieter Wuille
31b4bbee1e Make fe_cmov take max of magnitudes 2023-05-15 09:36:55 -04:00
Pieter Wuille
5b32602295 Split fe_set_b32 into reducing and normalizing variants 2023-05-11 13:49:33 -04:00
Tim Ruffing
1907f0f166 build: Make tests work with external default callbacks 2023-05-11 19:08:35 +02:00
Tim Ruffing
fb5bfa4eed Add static test vector for Xoshiro256++ 2023-05-09 18:11:29 +02:00
Pieter Wuille
723e8ca8f7 Remove randomness tests
Our RNG has been replaced with Xoshiro256++, a well-analyzed RNG. Our
unit tests should not be resposible for verifying its statistical
qualities.
2023-05-08 12:17:33 -04:00
Hennadii Stepanov
d1e48e5474
refactor: Make 64-bit shift explicit
This change fixes MSVC level-3 warning C4334.
See: https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4334

Required to enable level 3 warnings (/W3).
2023-05-02 13:35:15 +01:00
Jonas Nick
c4062d6b5d
debug: move helper for printing buffers into util.h 2023-04-25 16:07:10 +00:00
Jonas Nick
3858bad2c6
tests: remove extra semicolon in macro 2023-04-21 20:21:28 +00:00
Jonas Nick
1f33bb2b1c
Merge bitcoin-core/secp256k1#1205: field: Improve docs +tests of secp256k1_fe_set_b32
162da73e9a48875aab1ee6ca1c14f86ca4646946 tests: Add debug helper for printing buffers (Tim Ruffing)
e9fd3dff76e30fcd83d060ad9195cadae9cdc9a2 field: Improve docs and tests of secp256k1_fe_set_b32 (Tim Ruffing)
ca92a35d019730aec9d3ec8097dcbb9633a69874 field: Simplify code in secp256k1_fe_set_b32 (Tim Ruffing)
d93f62e3693d6763891edcad11472f9d475177e5 field: Verify field element even after secp256k1_fe_set_b32 fails (Tim Ruffing)

Pull request description:

ACKs for top commit:
  jonasnick:
    ACK 162da73e9a48875aab1ee6ca1c14f86ca4646946

Tree-SHA512: b3ed8e45c969d0420275ff154462f3820b72b57832ccba1f6f427e0cfd9cff3e27440c20994f69ea33a576b1903eb7f04a989f0dbd574bbd96ee56c6dd4500f7
2023-04-21 16:23:59 +00:00
Tim Ruffing
162da73e9a tests: Add debug helper for printing buffers 2023-04-21 17:20:37 +02:00
Tim Ruffing
e9fd3dff76 field: Improve docs and tests of secp256k1_fe_set_b32 2023-04-21 17:20:37 +02:00
Pieter Wuille
69e1ec0331 Get rid of secp256k1_fe_const_b 2023-04-20 16:07:56 -04:00
Tim Ruffing
9ce9984f32
Merge bitcoin-core/secp256k1#1265: Remove bits argument from secp256k1_wnaf_const{_xonly}
a575339c0282ba49a4f46c9c660a4cc3b6bfc703 Remove bits argument from secp256k1_wnaf_const (always 256) (Pieter Wuille)

Pull request description:

  There is little reason for having the number of bits in the scalar as a parameter, as I don't think there are any (current) use cases for non-256-bit scalars.

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

Tree-SHA512: 994b1f19b4c513f6d070ed259a5d6f221a0c2450271ec824c5eba1cd0ecace276de391c170285bfeae96aaf8f1e0f7fe6260966ded0336c75c522ab6c56d182c
2023-04-18 12:25:09 +02:00
Pieter Wuille
7b7503dac5
Merge bitcoin-core/secp256k1#1245: tests: Add Wycheproof ECDSA vectors
e5de45460953c8ae16521b1928ac14de218998a3 tests: Add Wycheproof ECDSA vectors (RandomLattice)

Pull request description:

  This PR adds a test using the Wycheproof vectors as outlined in #1106. We add all 463 ECDSA test vectors. These vectors cover:
  - edge cases in arithmetic operations
  - signatures with special values for (r,s) that should be rejected
  - special cases of public keys

  The vectors are pulled from the Wycheproof project using a python script to emit C code.

  All the new ECDSA Wycheproof vectors pass.

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

Tree-SHA512: e9684f14ff3f5225a4a4949b490e07527d559c28aa61ed03c03bc52ea64785f0b80b9e1b1628665eacf24006526271ea0fb108629c9c3c1d758e52d214a056f1
2023-04-10 09:29:02 -04:00