Commit Graph

943 Commits

Author SHA1 Message Date
Jonas Nick a0c3fc177f schnorrsig: allow signing and verification of variable length msgs
Varlen message support for the default sign function comes from recommending
tagged_sha256. sign_custom on the other hand gets the ability to directly sign
message of any length. This also implies signing and verification support for
the empty message (NULL) with msglen 0.

Tests for variable lengths follow in a later commit.
2021-06-27 20:26:15 +00:00
Jonas Nick 5a8e4991ad Add secp256k1_tagged_sha256 as defined in BIP-340
Gives users the ability to hash messages to 32 byte before they are signed while
allowing efficient domain separation through the tag.
2021-06-27 20:26:15 +00:00
Jonas Nick b6c0b72fb0 schnorrsig: remove noncefp args from sign; add sign_custom function
This makes the default sign function easier to use while allowing more granular
control through sign_custom.

Tests for sign_custom follow in a later commit.
2021-06-27 20:26:15 +00:00
Pieter Wuille bdf19f105c Add random field multiply/square tests 2021-06-21 16:34:33 -07:00
Tim Ruffing 9be7b0f083 Avoid computing out-of-bounds pointer.
This is a pedantic case of UB.
2021-06-16 10:33:41 +02:00
Tim Ruffing 8ae56e33e7
Merge #879: Avoid passing out-of-bound pointers to 0-size memcpy
9570f674cc Avoid passing out-of-bound pointers to 0-size memcpy (Pieter Wuille)

Pull request description:

  Doing so could be considered UB in a pedantic interpretation of the standard. Avoid it.

  Closes #876.

ACKs for top commit:
  practicalswift:
    cr ACK 9570f674cc729cafcba65f4cce03552d9a6108f4: patch looks correct
  real-or-random:
    ACK 9570f674cc

Tree-SHA512: f991462d72e39f14e609021b8427c2e6756009bc8cd21efca2da46ec9410250725a4fed662df20fcdcfd10a4dc59038f13e8c166362b2eadde4366586b9ca72b
2021-06-16 10:22:03 +02:00
Jonas Nick 8f879c2887 Fix array size in bench_ecmult 2021-05-31 20:46:04 +00:00
Jonas Nick 2fe1b50df1 Add ecmult_gen, ecmult_const and ecmult to benchmark 2021-05-31 20:46:04 +00:00
Jonas Nick 593e6bad9c Clean up ecmult_bench to make space for more benchmarks 2021-05-31 20:46:04 +00:00
Jonas Nick 442cee5baf schnorrsig: add algolen argument to nonce_function_hardened
This avoids having to remove trailing NUL bytes in the nonce function
2021-05-28 11:40:52 +00:00
Tim Ruffing 489ff5c20a tests: Treat empty SECP2561_TEST_ITERS as if it was unset 2021-05-21 20:46:48 +02:00
Jonas Nick 202a030f7d
Merge #850: add `secp256k1_ec_pubkey_cmp` method
6eceec6d56 add `secp256k1_xonly_pubkey_cmp` method (Andrew Poelstra)
0d9561ae87 add `secp256k1_ec_pubkey_cmp` method (Andrew Poelstra)

Pull request description:

ACKs for top commit:
  elichai:
    Code review ACK 6eceec6d56
  jonasnick:
    ACK 6eceec6d56
  real-or-random:
    ACK 6eceec6d56

Tree-SHA512: f95cbf65f16c88a4adfa1ea7cc6ddabab14baa3b68fa069e78e6faad4852cdbfaea42ee72590d2e0b8f3159cf9b37969511550eb6b2d256b101e2147711cc817
2021-05-13 19:17:53 +00:00
Andrew Poelstra 6eceec6d56 add `secp256k1_xonly_pubkey_cmp` method 2021-05-06 18:36:44 +00:00
Andrew Poelstra 0d9561ae87 add `secp256k1_ec_pubkey_cmp` method 2021-05-06 18:36:41 +00:00
Tim Ruffing 6c52ae8724
Merge #937: Have ge_set_gej_var, gej_double_var and ge_set_all_gej_var initialize all fields of their outputs.
14c9739a1f tests: Improve secp256k1_ge_set_all_gej_var for some infinity inputs (Tim Ruffing)
4a19668c37 tests: Test secp256k1_ge_set_all_gej_var for all infinity inputs (Tim Ruffing)
45b6468d7e Have secp256k1_ge_set_all_gej_var initialize all fields. Previous behaviour would not initialize r->y values in the case where infinity is passed in. Furthermore, the previous behaviour wouldn't initialize anything in the case where all inputs were infinity. (Russell O'Connor)
31c0f6de41 Have secp256k1_gej_double_var initialize all fields. Previous behaviour would not initialize r->x and r->y values in the case where infinity is passed in. (Russell O'Connor)
dd6c3de322 Have secp256k1_ge_set_gej_var initialize all fields. Previous behaviour would not initialize r->x and r->y values in the case where infinity is passed in. (Russell O'Connor)

Pull request description:

  Previous behaviour would not initialize `r->x` and `r->y` values in the case where infinity is passed in.

ACKs for top commit:
  gmaxwell:
    ACK 14c9739a1f
  sipa:
    utACK 14c9739a1f
  real-or-random:
    ACK 14c9739a1f

Tree-SHA512: 2e779b767f02e348af4bbc62aa9871c3d1d29e61a6c643c879c49f2de27556a3588850acd2f7c7483790677597d01064025e14befdbf29e783f57996fe4430f9
2021-05-06 09:39:36 +02:00
Tim Ruffing 14c9739a1f tests: Improve secp256k1_ge_set_all_gej_var for some infinity inputs 2021-05-05 13:07:25 -04:00
Tim Ruffing 4a19668c37 tests: Test secp256k1_ge_set_all_gej_var for all infinity inputs 2021-05-05 13:07:25 -04:00
William Bright 3c90bdda95 change local lib headers to be relative for those pointing at "include/" dir
added relative paths to header files imported from src directory

added include guards for contrib/ files when referring to secp256k1.h
2021-05-05 09:24:05 -04:00
Russell O'Connor 45b6468d7e Have secp256k1_ge_set_all_gej_var initialize all fields.
Previous behaviour would not initialize r->y values in the case where infinity is passed in.
Furthermore, the previous behaviour wouldn't initialize anything in the case where all inputs were infinity.
2021-05-04 16:17:00 -04:00
Russell O'Connor 31c0f6de41 Have secp256k1_gej_double_var initialize all fields.
Previous behaviour would not initialize r->x and r->y values in the case where infinity is passed in.
2021-05-04 15:49:48 -04:00
Russell O'Connor dd6c3de322 Have secp256k1_ge_set_gej_var initialize all fields.
Previous behaviour would not initialize r->x and r->y values in the case where infinity is passed in.
2021-05-04 14:59:47 -04:00
Tim Ruffing 99f47c20ec gen_context: Don't use external ASM because it complicates the build
Fixes #931.
2021-05-03 15:05:38 +02:00
Gregory Maxwell 99e2d5be0d Avoids a missing brace warning in schnorrsig/tests_impl.h on old compilers.
GCC 4.9.2, at least, emits "warning: missing braces around initializer"
 without this.
2021-05-02 20:02:12 +00:00
Jonas Nick 34388af6b6
Merge #922: Add mingw32-w64/wine CI build
ed5a199bed tests: fopen /dev/urandom in binary mode (Tim Ruffing)
4dc37bf81b Add mingw32-w64/wine CI build (Pieter Wuille)

Pull request description:

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

Tree-SHA512: 45afc394e3a200f7c020426a66f78df8d12827b9dc91bb04dc1708c3ad5cdc4e7d20554d6d5c046d288552f4e722d4fe8a0f3234b662e7351a4d27aaaeb0d5c0
2021-05-02 12:57:40 +00:00
Tim Ruffing ed5a199bed tests: fopen /dev/urandom in binary mode
This makes a difference with mingw builds on Wine, where the subsequent
fread() may abort early in the default text mode.

The Microsoft C docs say:
"In text mode, CTRL+Z is interpreted as an EOF character on input."
2021-05-01 17:05:15 -07:00
Gregory Maxwell ae9e648526 Define SECP256K1_BUILD in secp256k1.c directly.
This avoids building without it and makes it safer to use a custom
 building environment.  Test harnesses need to #include secp256k1.c
 first now.
2021-05-01 19:27:27 +00:00
Tim Ruffing efad3506a8
Merge #906: Use modified divsteps with initial delta=1/2 for constant-time
be0609fd54 Add unit tests for edge cases with delta=1/2 variant of divsteps (Pieter Wuille)
cd393ce228 Optimization: only do 59 hddivsteps per iteration instead of 62 (Pieter Wuille)
277b224b6a Use modified divsteps with initial delta=1/2 for constant-time (Pieter Wuille)
376ca366db Fix typo in explanation (Pieter Wuille)

Pull request description:

  This updates the divsteps-based modular inverse code to use the modified version which starts with delta=1/2. For variable time, the delta=1 variant is still used as it appears to be faster.

  See https://github.com/sipa/safegcd-bounds/tree/master/coq and https://medium.com/blockstream/a-formal-proof-of-safegcd-bounds-695e1735a348 for a proof of correctness of this variant.

  TODO:
  * [x] Update unit tests to include edge cases specific to this variant

  I'm still running the Coq proof verification for the 590 bound in non-native mode. It's unclear how long this will take.

ACKs for top commit:
  gmaxwell:
    ACK be0609fd54
  sanket1729:
    crACK be0609fd54
  real-or-random:
    ACK be0609fd54 careful code review and some testing

Tree-SHA512: 2f8f400ba3ac8dbd08622d564c3b3e5ff30768bd0eb559f2c4279c6c813e17cdde71b1c16f05742c5657b5238b4d592b48306f9f47d7dbdb57907e58dd99b47a
2021-04-22 20:18:52 +02:00
Aaron Voisine 07067967ee add ECMULT_GEN_PREC_BITS to basic_config.h
set ECMULT_GEN_PREC_BITS to the "auto" value of 4 in basic_config.h, so libsecp can be used without autoconf
2021-04-15 17:18:03 +02:00
Tim Ruffing a3aa2628c7 gen_context: Don't include basic-config.h
Before this commit, gen_context.c both included libsecp256k1-config.h
and basic-config.h: The former only to obtain ECMULT_GEN_PREC_BITS
and the latter to obtain a basic working configuration to be able to
use the library.

This was inelegant and confusing: It meant that basic-config.h needs
to #undef all the macros defined in libsecp256k1-config.h. Moreover,
it meant that basic-config.h cannot define ECMULT_GEN_PREC_BITS,
essentially making this file specific for use in gen_context.c.

After this commit, gen_context.c include only libsecp256k1-config.h.
basic-config.h is not necessary anymore for the modules used in
gen_context.c because 79f1f7a made the preprocessor detect all the
relevant config options.

On the way, we remove an unused #define in basic-config.h.
2021-04-15 17:18:03 +02:00
Pieter Wuille be0609fd54 Add unit tests for edge cases with delta=1/2 variant of divsteps 2021-04-13 11:59:14 -07:00
Pieter Wuille cd393ce228 Optimization: only do 59 hddivsteps per iteration instead of 62 2021-04-13 11:59:14 -07:00
Pieter Wuille 277b224b6a Use modified divsteps with initial delta=1/2 for constant-time
Instead of using eta=-delta, use zeta=-(delta+1/2) to represent
delta. This variant only needs at most 590 iterations for 256-bit
inputs rather than 724 (by convex hull bounds analysis).
2021-04-13 11:59:11 -07:00
Jonas Nick 1e5d50fa93
Merge #889: fix uninitialized read in tests
99a1cfec17 print warnings for conditional-uninitialized (PiRK)
3d2cf6c5bd initialize variable in tests (PiRK)

Pull request description:

ACKs for top commit:
  real-or-random:
    ACK 99a1cfec17 code inspection
  jonasnick:
    ACK 99a1cfec17

Tree-SHA512: 72f92f51c44210ab54f166920f540525db0e3d1f19a2fa56e4a6d157a38a582f9dc649d919cf3278482c9fd723021b07759284a8fccbc574b62a22aac0facf51
2021-04-07 12:53:09 +00:00
Jonas Nick c083cc6e52
Merge #903: Make argument of fe_normalizes_to_zero{_var} const
23c3fb629b Make argument of fe_normalizes_to_zero{_var} const (Pieter Wuille)

Pull request description:

ACKs for top commit:
  real-or-random:
    ACK 23c3fb629b diff looks good
  jonasnick:
    ACK 23c3fb629b

Tree-SHA512: a51894a9e59851dc4854e92e4200ef6d12a11f6785b903c23585cfff5ef8d369216f4121260fe8789d46d3e215f3c2baa42decae99ab9328e8081f5274e67fab
2021-03-26 14:57:01 +00:00
William Bright 4504472269 changed import to use brackets <> for openssl as they are not local to the project 2021-03-20 19:59:51 -04:00
Pieter Wuille 23c3fb629b Make argument of fe_normalizes_to_zero{_var} const 2021-03-15 16:01:57 -07:00
Pieter Wuille 24ad04fc06 Make scalar_inverse{,_var} benchmark scale with SECP256K1_BENCH_ITERS 2021-03-15 13:01:56 -07:00
Peter Dettman ebc1af700f Optimization: track f,g limb count and pass to new variable-time update_fg_var
The magnitude of the f and g variables generally goes down as the algorithm
progresses. Make use of this by keeping tracking how many limbs are used, and
when the number becomes small enough, make use of this to reduce the complexity
of arithmetic on them.

Refactored by: Pieter Wuille <pieter@wuille.net>
2021-03-15 13:01:56 -07:00
Peter Dettman b306935ac1 Optimization: use formulas instead of lookup tables for cancelling g bits
This only seems to be a win on 64-bit platforms, so only do it there.

Refactored by: Pieter Wuille <pieter@wuille.net>
2021-03-15 13:01:56 -07:00
Pieter Wuille 9164a1b658 Optimization: special-case zero modulus limbs in modinv64
Both the field and scalar modulus can be written in signed{30,62} notation
with one or more zero limbs. Make use of this in the update_de function to
avoid a few wide multiplications when that is the case.

This doesn't appear to be a win in the 32-bit implementation, so only
do it for the 64-bit one.
2021-03-15 13:01:56 -07:00
Pieter Wuille 1f233b3fa0 Remove num/gmp support
The whole "num" API and its libgmp-based implementation are now unused. Remove them.
2021-03-15 13:01:52 -07:00
Pieter Wuille 20448b8d09 Remove unused Jacobi symbol support
No exposed functions rely on Jacobi symbol computation anymore. Remove it; it can always
be brough back later if needed.
2021-03-12 10:06:18 -08:00
Pieter Wuille 5437e7bdfb Remove unused scalar_sqr 2021-03-12 10:06:18 -08:00
Pieter Wuille aa9cc52180 Improve field/scalar inverse tests
Add a new run_inverse_tests that replaces all existing field/scalar inverse tests,
and tests a few identities for fixed inputs, small numbers (-999...999), random
inputs (structured and unstructured), as well as comparing with the output of
secp256k1_fe_inv_all_var.
2021-03-12 10:06:18 -08:00
Pieter Wuille 1e0e885c8a Make field/scalar code use the new modinv modules for inverses 2021-03-12 10:06:14 -08:00
Pieter Wuille 436281afdc Move secp256k1_fe_inverse{_var} to per-impl files
This temporarily duplicates the inversion code across the 5x52 and 10x26
implementations. Those implementations will be replaced in a next commit.
2021-03-11 10:25:26 -08:00
Pieter Wuille aa404d53be Move secp256k1_scalar_{inverse{_var},is_even} to per-impl files
This temporarily duplicates the inversion code across the 4x64 and 8x32
implementations. Those implementations will be replaced in a later commit.
2021-03-11 10:25:26 -08:00
Pieter Wuille 08d54964e5 Improve bounds checks in modinv modules
This commit adds functions to verify and compare numbers in signed{30,62} notation,
and uses that to do more extensive bounds checking on various variables in the modinv
code.
2021-03-11 10:25:22 -08:00
Pieter Wuille 151aac00d3 Add tests for modinv modules
This adds tests for the modinv{32,64}_impl.h directly (before the functions are used
inside the field/scalar code). It uses a naive implementation of modular multiplication
and gcds in order to verify the modular inverses themselves.
2021-03-08 09:56:07 -08:00
Pieter Wuille d8a92fcc4c Add extensive comments on the safegcd algorithm and implementation
This adds a long comment explaining the algorithm and implementation choices by building
it up step by step in Python.

Comments in the code are also reworked/added, with references to the long explanation.
2021-03-08 09:56:07 -08:00
Peter Dettman 8e415acba2 Add safegcd based modular inverse modules
Refactored by: Pieter Wuille <pieter@wuille.net>
2021-03-08 09:56:07 -08:00
Pieter Wuille de0a643c3d Add secp256k1_ctz{32,64}_var functions
These functions count the number of trailing zeroes in non-zero integers.
2021-03-08 09:56:07 -08:00
Jonas Nick 7d3497cdc4 ctime_test: move context randomization test to the end 2021-02-05 14:38:55 +00:00
PiRK 3d2cf6c5bd initialize variable in tests
This was detected while running the tests with the `-Wconditional-uninitialized` flag

```
./autogen.sh
CC=clang CFLAGS="-Wconditional-uninitialized" ./configure
make check
```

The resulting warning is a false positive, but setting the value to -1
ensures that the CHECK below will fail if recid is never written to.
2021-02-04 09:52:10 +01:00
Tim Ruffing e491d06b98 Use bit ops instead of int mult for constant-time logic in gej_add_ge 2021-01-30 19:38:24 +01:00
Tim Ruffing 659d0d4798
Merge #880: Add parens around ROUND_TO_ALIGN's parameter.
b6f649889a Add parens around ROUND_TO_ALIGN's parameter. This makes the macro robust against a hypothetical ROUND_TO_ALIGN(foo ? sizeA : size B) invocation. (Russell O'Connor)

Pull request description:

  This makes the macro robust against a hypothetical `ROUND_TO_ALIGN(foo ? sizeA : size B)` invocation.

  See also <https://wiki.sei.cmu.edu/confluence/display/c/PRE01-C.+Use+parentheses+within+macros+around+parameter+names>.

ACKs for top commit:
  sipa:
    ACK b6f649889a. This is the way.
  jonasnick:
    utACK b6f649889a
  real-or-random:
    utACK b6f649889a

Tree-SHA512: 6a2685f959e8ae472259e5ea75fe12e8e6213f56f5aec7603a896c294e6a8833caae25c412607d9c9a3125370a7765a3e506127b101a1b87203f95e326f6c6c6
2021-01-26 09:39:00 +01:00
Russell O'Connor b6f649889a Add parens around ROUND_TO_ALIGN's parameter.
This makes the macro robust against a hypothetical ROUND_TO_ALIGN(foo ? sizeA : size B) invocation.
2021-01-25 11:43:45 -05:00
Jonas Nick a4abaab793
Merge #877: Add missing secp256k1_ge_set_gej_var decl.
482e4a9cfc Add missing secp256k1_ge_set_gej_var decl. (Russell O'Connor)

Pull request description:

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

Tree-SHA512: 02195390fb79f08bcfd655dc56115ea37df42c1ad8f1123b26e7426e387d9658a3bb18fe9951140fc4dd78ce222b84d8b75ce77aec884675e0c26a2005dd2ddc
2021-01-25 14:00:47 +00:00
Jonas Nick 5671e5f3fd
Merge #874: Remove underscores from header defs.
fb390c5299 Remove underscores from header defs. This makes them consistent with other files and avoids reserved identifiers. (Russell O'Connor)

Pull request description:

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

Tree-SHA512: f49da79c0a90d1e82494821e7cf6f61c66bc377a3f37b2d4787ef19d2126e000627bfe4a76aa1c5bfffeb1382054aa824a7e9ab5d73c19d876b0828722c73854
2021-01-25 13:57:43 +00:00
Jonas Nick db726782fa
Merge #878: Remove unused secp256k1_fe_inv_all_var
75d2ae149e Remove unused secp256k1_fe_inv_all_var (Pieter Wuille)

Pull request description:

ACKs for top commit:
  practicalswift:
    cr ACK 75d2ae149ef37d3aa42fdefd1529aad89859816c: patch looks correct
  real-or-random:
    utACK 75d2ae149e
  jonasnick:
    utACK 75d2ae149e

Tree-SHA512: 6f548a436c6dcb275493e73e6afa23fd1b79392cc3071878f98735732ac9c93971e5c92736c3fe50eaae90a200e1a435e9be9f14d1a69251c83876a6e3c46d41
2021-01-25 13:40:00 +00:00
Pieter Wuille 9570f674cc Avoid passing out-of-bound pointers to 0-size memcpy
Doing so could be considered UB in a strict reading of the standard.
Avoid it.
2021-01-23 21:56:43 -08:00
Pieter Wuille 75d2ae149e Remove unused secp256k1_fe_inv_all_var 2021-01-23 20:16:51 -08:00
Russell O'Connor 482e4a9cfc Add missing secp256k1_ge_set_gej_var decl. 2021-01-23 19:12:19 -05:00
Russell O'Connor 2730618604 Avoid casting (void**) values.
Replaced with an expression that only casts (void*) values.
2021-01-23 15:22:54 -05:00
Russell O'Connor fb390c5299 Remove underscores from header defs.
This makes them consistent with other files and avoids reserved identifiers.
2021-01-23 14:48:35 -05:00
Tim Ruffing f2d9aeae6d
Merge #862: Autoconf improvements
3c15130709 Improve CC_FOR_BUILD detection (Tim Ruffing)
47802a4762 Restructure and tidy configure.ac (Tim Ruffing)
252c19dfc6 Ask brew for valgrind include path (Tim Ruffing)

Pull request description:

  See individual commit messages. These are improvements in preparation of the switch to Cirrus CI. (Maybe I'll just open a PR on top of this one.)

  The first commit made the difference between successful build https://cirrus-ci.com/task/6740575057608704 and unsuccessful build https://cirrus-ci.com/task/4909571074424832.

  I've tested the second commit without cross-compilation and with cross-compilation for android (https://github.com/bitcoin-core/secp256k1/issues/621#issuecomment-495703399)

  When working on the autoconf stuff, I noticed two things that I just want to write down here:
   - At some point we should update [build-aux/m4/ax_prog_cc_for_build.m4](https://www.gnu.org/software/autoconf-archive/ax_prog_cc_for_build.html). This is outdated, and [there have been a lot of fixes](https://github.com/autoconf-archive/autoconf-archive/pull/207) But the latest version is [broken](https://lists.gnu.org/archive/html/autoconf-archive-maintainers/2020-06/msg00002.html), so now is probably not the time.
   - The latest autoconf 2.70 deprecates `AC_PROG_CC_C89`. It's not needed anymore because `AC_PROG_CC` cares about testing for version support. This makes autoconf 2.70 output a warning that we should probably just ignore. We don't want to force users onto 2.70...

ACKs for top commit:
  sipa:
    utACK 3c15130709
  jonasnick:
    utACK 3c15130 makes sense (with my very basic understanding of autoconf)

Tree-SHA512: 595b9de316374c2213f1340cddaa22eb3190b01fa99aa6ae26e77804df41e7ecf96a09e03c28e8f8b9fd04e211e4ee2f78f1e5a7995143c84f99d2e16d4f0260
2021-01-12 15:38:52 +01:00
Tim Ruffing 328aaef22a
Merge #845: Extract the secret key from a keypair
33cb3c2b1f Add secret key extraction from keypair to constant time tests (Elichai Turkel)
36d9dc1e8e Add seckey extraction from keypair to the extrakeys tests (Elichai Turkel)
fc96aa73f5 Add a function to extract the secretkey from a keypair (Elichai Turkel)

Pull request description:

  With schnorrsig if you need to tweak the secret key (for BIP32) you must use the keypair API to get compatible secret/public keys which you do by calling `secp256k1_keypair_xonly_tweak_add()`, but after that there's no currently a way to extract the secret key back for storage.
  so I added a `secp256k1_keypair_seckey` function to extract the key

ACKs for top commit:
  jonasnick:
    ACK 33cb3c2b1f
  real-or-random:
    ACK 33cb3c2b1f code inspection, tests pass

Tree-SHA512: 11212db38c8b87a87e2dc35c4d6993716867b45215b94b20522b1b3164ca63d4c6bf5192a6bff0e9267b333779cc8164844c56669a94e9be72df9ef025ffcfd4
2021-01-12 10:56:14 +01:00
Tim Ruffing 3c15130709 Improve CC_FOR_BUILD detection
This commits simply uses CC as CC_FOR_BUILD and the same for
corresponding flags if we're not cross-compiling. This has a number of
benefits in this common case:
 - It avoids strange cases where very old compilers are used (#768).
 - Flags are consistently set for CC and CC_FOR_BUILD.
 - ./configure is faster.
 - You get compiler x consistently if you set CC=x; we got this wrong
   in CI in the past.

./configure warns if a _FOR_BUILD variable is set but ignored because
we're not cross-compiling.

The change exposed that //-style comments are used in gen_context.c,
which is also fixed by this commit.

This commit also reorganizes code in configure.ac to have a cleaner
separation of sections.
2021-01-08 16:09:04 +01:00
Elichai Turkel 33cb3c2b1f
Add secret key extraction from keypair to constant time tests 2020-12-19 11:01:36 +02:00
Elichai Turkel 36d9dc1e8e
Add seckey extraction from keypair to the extrakeys tests 2020-12-19 11:00:25 +02:00
Elichai Turkel fc96aa73f5
Add a function to extract the secretkey from a keypair 2020-12-19 11:00:25 +02:00
Dimitris Apostolou 07aa4c70ff
Fix insecure links 2020-12-18 00:24:22 +02:00
Tim Gates 18aadf9d28
docs: fix simple typo, dependecy -> dependency
There is a small typo in src/group_impl.h.

Should read `dependency` rather than `dependecy`.
2020-12-08 21:45:13 +11:00
Jonas Nick 6e85d675aa Rename tweak to tweak32 in public API 2020-12-04 14:16:43 +00:00
Jonas Nick f587f04e35 Rename msg32 to msghash32 in ecdsa_sign/verify and add explanation 2020-12-04 14:12:38 +00:00
Andrew Poelstra f4fa8d226a forbid a test iteration of 0 or less 2020-11-30 18:12:31 +00:00
Andrew Poelstra 0ce4554881 make test count iteration configurable by environment variable 2020-11-23 01:05:31 +00:00
Pieter Wuille 9e5939d284
Merge #835: Don't use reserved identifiers memczero and benchmark_verify_t
1f4dd03838 Typedef (u)int128_t only when they're not provided by the compiler (Tim Ruffing)
e89278f211 Don't use reserved identifiers memczero and benchmark_verify_t (Tim Ruffing)

Pull request description:

  As identified in #829 and #833. Fixes #829.

  Since we touch this anyway, this commit additionally makes the
  identifiers in the benchmark files a little bit more consistent.

  This is necessary before we can merge #833. I preferred a separate PR because it makes it easier to see the results of Travis in #833.

ACKs for top commit:
  sipa:
    utACK 1f4dd03838
  jonasnick:
    ACK 1f4dd03838

Tree-SHA512: c0ec92798f3c94f3ef6ac69b3f0f39a39257a32be9d9a068832cece1ebe64c89848b70e44652fc397004b8b240883ac4bc0c8f95abbe4ba4b028de120e6734bf
2020-11-04 15:29:25 -08:00
Jonas Nick d0a83f7328
Merge #839: Prevent arithmetic on NULL pointer if the scratch space is too small
29a299e373 Run the undefined behaviour sanitizer on Travis (Fabien)
7506e064d7 Prevent arithmetic on NULL pointer if the scratch space is too small (Fabien)

Pull request description:

ACKs for top commit:
  sipa:
    ACK 29a299e373. Reviewed the code changes and verified that building with these sanitizer flags catches the existing error, as well as a signed integer overflow if introduced.
  real-or-random:
    ACK 29a299e373 code inspection
  jonasnick:
    utACK 29a299e373

Tree-SHA512: 4d788f12f3d7b48018e884910adb9b530a05d88f504de83dadeab8a22d75da83c05a1518f7317de5f536c4dd243ea7b347b1eaddb2ca1d804c663e41b85db69d
2020-11-04 14:53:04 +00:00
Jonas Nick 903b16aa6c
Merge #840: Return NULL early in context_preallocated_create if flags invalid
ebfa2058e9 Return NULL early in context_preallocated_create if flags invalid (Tim Ruffing)

Pull request description:

ACKs for top commit:
  sipa:
    ACK ebfa2058e9
  jonasnick:
    ACK ebfa2058e9

Tree-SHA512: 61310539046e015e5c9e6b5702ac7b542fda854a23915ef5dd549361c8ec6e70aa6d509e02b30fd859b24dfaf2250721bb55270767323d6e94854067b7d7e9a6
2020-10-30 17:48:46 +00:00
Tim Ruffing 1f4dd03838 Typedef (u)int128_t only when they're not provided by the compiler 2020-10-27 20:18:18 +01:00
Tim Ruffing ebfa2058e9 Return NULL early in context_preallocated_create if flags invalid
If the user passes invalid flags to _context_create, and the default
illegal callback does not abort the program (which is possible), then we
work with the result of malloc(0), which may be undefined behavior. This
violates the promise that a library function won't crash after the
illegal callback has been called.

This commit fixes this issue by returning NULL early in _context_create
in that case.
2020-10-27 15:03:21 +01:00
Fabien 7506e064d7 Prevent arithmetic on NULL pointer if the scratch space is too small
If the scratch space is too small when calling
`secp256k1_ecmult_strauss_batch()`, the `state.pre_a` allocation will
fail and the pointer will be `NULL`. This causes `state.pre_a_lam` to be
computed from the `NULL` pointer.

It is also possible that the first allocation to fail is for `state.ps`,
which will cause the failure to occur when in
`secp256k1_ecmult_strauss_wnaf()`.

The issue has been detected by UBSAN using Clang 10:
```
CC=clang \
CFLAGS="-fsanitize=undefined -fno-omit-frame-pointer" \
LDFLAGS="-fsanitize=undefined -fno-omit-frame-pointer" \
../configure

UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 make check
```
2020-10-27 08:41:14 +01:00
Tim Ruffing 8893f42438 Avoids a potentially shortening size_t to int cast in strauss_wnaf_
Fixes #834.
2020-10-27 02:17:23 +01:00
Tim Ruffing e89278f211 Don't use reserved identifiers memczero and benchmark_verify_t
As identified in #829 and #833. Fixes #829.

Since we touch this anyway, this commit additionally makes the
identifiers in the benchmark files a little bit more consistent.
2020-10-20 15:00:19 +02:00
Pieter Wuille c582abade1 Consistency improvements to the comments 2020-10-13 13:21:50 -07:00
Pieter Wuille 63c6b71616 Reorder comments/function around scalar_split_lambda 2020-10-13 13:21:45 -07:00
Pieter Wuille 2edc514c90 WNAF of lambda_split output has max size 129 2020-10-13 11:31:13 -07:00
Pieter Wuille 4232e5b7da Rip out non-endomorphism code 2020-10-13 11:31:13 -07:00
Gregory Maxwell ebad8414b0 Check correctness of lambda split without -DVERIFY
The VERIFY macro turns on various paranoid consistency checks, but
 the complete functionality should still be tested without it.

This also adds a couple of static test points for extremely small
 split inputs/outputs.  The existing bounds vectors already check
 extremely large outputs.
2020-10-13 11:31:13 -07:00
Pieter Wuille fe7fc1fda8 Make lambda constant accessible 2020-10-13 11:31:13 -07:00
Pieter Wuille 9d2f2b44d8 Add tests to exercise lambda split near bounds 2020-10-13 11:31:13 -07:00
Russell O'Connor 9aca2f7f07 Add secp256k1_split_lambda_verify 2020-10-13 11:31:13 -07:00
Russell O'Connor acab934d24 Detailed comments for secp256k1_scalar_split_lambda 2020-10-13 11:31:13 -07:00
Russell O'Connor 76ed922a5f Increase precision of g1 and g2
This allows us to shift by 256+128 = 384 bits, which is a multiple of the limb size of
the scalar representation. This also happens to be the most precision possible for g2
that still fits into a 256-bit value.
2020-10-11 10:39:59 -07:00
Tim Ruffing 6173839c90 Switch to our own memcmp function
Fixes #823.
2020-10-11 10:39:20 -07:00
Tim Ruffing 63150ab4da
Merge #827: Rename testrand functions to have test in name
a45c1fa63c Rename testrand functions to have test in name (Pieter Wuille)

Pull request description:

  Suggested here: https://github.com/bitcoin-core/secp256k1/pull/808#discussion_r488871913

ACKs for top commit:
  real-or-random:
    ACK a45c1fa63c diff looks good
  elichai:
    utACK a45c1fa63c

Tree-SHA512: a15c29b88877e0f1a099acab90cbfa1e70420527e07348a69c8a5b539319a3131b771b86852e772a669a1eb3475d508d0f7e10f37eec363dc6640d4eaf967536
2020-09-27 11:15:14 +02:00
Tim Ruffing bb1f54280f
Merge #818: Add static assertion that uint32_t is unsigned int or wider
c0041b5cfc Add static assertion that uint32_t is unsigned int or wider (Tim Ruffing)

Pull request description:

  Solves one item in #792 .

ACKs for top commit:
  sipa:
    utACK c0041b5cfc
  elichai:
    ACK c0041b5cfc

Tree-SHA512: 9f700e89be39e15983260da94642593d16b9c437171e10377837ac73731ca7ba5dd7e328b3d93d0a24d143fb9e73abd11c578f6b58e2f94c82b783e977173b0c
2020-09-26 10:22:32 +02:00
Pieter Wuille a45c1fa63c Rename testrand functions to have test in name 2020-09-25 20:50:43 -07:00
Pieter Wuille 5006895bd6
Merge #808: Exhaustive test improvements + exhaustive schnorrsig tests
8b7dcdd955 Add exhaustive test for extrakeys and schnorrsig (Pieter Wuille)
08d7d89299 Make pubkey parsing test whether points are in the correct subgroup (Pieter Wuille)
87af00b511 Abstract out challenge computation in schnorrsig (Pieter Wuille)
63e1b2aa7d Disable output buffering in tests_exhaustive.c (Pieter Wuille)
39f67dd072 Support splitting exhaustive tests across cores (Pieter Wuille)
e99b26fcd5 Give exhaustive_tests count and seed cmdline inputs (Pieter Wuille)
49e6630bca refactor: move RNG seeding to testrand (Pieter Wuille)
b110c106fa Change exhaustive test groups so they have a point with X=1 (Pieter Wuille)
cec7b18a34 Select exhaustive lambda in function of order (Pieter Wuille)
78f6cdfaae Make the curve B constant a secp256k1_fe (Pieter Wuille)
d7f39ae4b6 Delete gej_is_valid_var: unused outside tests (Pieter Wuille)
8bcd78cd79 Make secp256k1_scalar_b32 detect overflow in scalar_low (Pieter Wuille)
c498366e5b Move exhaustive tests for recovery to module (Pieter Wuille)
be31791543 Make group order purely compile-time in exhaustive tests (Pieter Wuille)

Pull request description:

  A few miscellaneous improvements:
  * Just use EXHAUSTIVE_TEST_ORDER as order everywhere, rather than a variable
  * Move exhaustive tests for recovery module to the recovery module directory
  * Make `secp256k1_scalar_set_b32` detect overflow correctly for scalar_low (a comment in the recovery exhaustive test indicated why this was the case, but this looks incorrect).
  * Change the small test groups so that they include a point with X coordinate 1.
  * Initialize the RNG seed, allowing configurating from the cmdline, and report it.
  * Permit changing the number of iterations (re-randomizing for each).
  * Support splitting the work across cores from the cmdline.

  And a big one:
  * Add exhaustive tests for schnorrsig module (and limited ones for extrakeys).

ACKs for top commit:
  real-or-random:
    ACK 8b7dcdd955
  jonasnick:
    ACK 8b7dcdd955

Tree-SHA512: 18d7f362402085238faaced164c0ca34079717a477001fc0b13448b3529ea2ad705793a13b7a36f34bf12e9231fee11070f88cc51bfc2a83ca82aa13f7aaae71
2020-09-25 20:44:03 -07:00
Pieter Wuille 8b7dcdd955 Add exhaustive test for extrakeys and schnorrsig 2020-09-18 14:11:38 -07:00
Pieter Wuille 08d7d89299 Make pubkey parsing test whether points are in the correct subgroup 2020-09-18 14:11:38 -07:00
Pieter Wuille 87af00b511 Abstract out challenge computation in schnorrsig 2020-09-18 14:11:38 -07:00
Pieter Wuille 63e1b2aa7d Disable output buffering in tests_exhaustive.c 2020-09-18 14:11:37 -07:00
Pieter Wuille 39f67dd072 Support splitting exhaustive tests across cores 2020-09-18 14:11:02 -07:00
Pieter Wuille e99b26fcd5 Give exhaustive_tests count and seed cmdline inputs 2020-09-18 14:10:59 -07:00
Pieter Wuille 49e6630bca refactor: move RNG seeding to testrand 2020-09-18 14:01:15 -07:00
Pieter Wuille b110c106fa Change exhaustive test groups so they have a point with X=1
This enables testing overflow is correctly encoded in the recid, and
likely triggers more edge cases.

Also introduce a Sage script to generate the parameters.
2020-09-18 14:01:15 -07:00
Pieter Wuille cec7b18a34 Select exhaustive lambda in function of order 2020-09-18 14:01:15 -07:00
Pieter Wuille 78f6cdfaae Make the curve B constant a secp256k1_fe 2020-09-18 14:01:15 -07:00
Pieter Wuille d7f39ae4b6 Delete gej_is_valid_var: unused outside tests 2020-09-18 14:01:15 -07:00
Pieter Wuille 8bcd78cd79 Make secp256k1_scalar_b32 detect overflow in scalar_low 2020-09-18 14:01:15 -07:00
Pieter Wuille c498366e5b Move exhaustive tests for recovery to module 2020-09-18 14:01:15 -07:00
Pieter Wuille be31791543 Make group order purely compile-time in exhaustive tests 2020-09-18 14:01:15 -07:00
Tim Ruffing e73ff30922 Enable -Wundef warning 2020-09-18 13:39:17 +02:00
Tim Ruffing c0041b5cfc Add static assertion that uint32_t is unsigned int or wider 2020-09-17 13:35:37 +02:00
Tim Ruffing 5738e8622d tests: Initialize random group elements fully
Also fix add a missing comment.
2020-09-13 11:35:09 +02:00
Tim Ruffing 8ab24e8dad
Merge #558: Add schnorrsig module which implements BIP-340 compliant signatures
f431b3f28a valgrind_ctime_test: Add schnorrsig_sign (Jonas Nick)
16ffa9d97c schnorrsig: Add taproot test case (Jonas Nick)
8dfd53ee3f schnorrsig: Add benchmark for sign and verify (Jonas Nick)
4e43520026 schnorrsig: Add BIP-340 compatible signing and verification (Jonas Nick)
7332d2db6b schnorrsig: Add BIP-340 nonce function (Jonas Nick)
7a703fd97d schnorrsig: Init empty experimental module (Jonas Nick)
eabd9bc46a Allow initializing tagged sha256 (Jonas Nick)
6fcb5b845d extrakeys: Add keypair_xonly_tweak_add (Jonas Nick)
58254463f9 extrakeys: Add keypair struct with create, pub and pub_xonly (Jonas Nick)
f0010349b8 Separate helper functions for pubkey_create and seckey_tweak_add (Jonas Nick)
910d9c284c extrakeys: Add xonly_pubkey_tweak_add & xonly_pubkey_tweak_add_test (Jonas Nick)
176bfb1110 Separate helper function for ec_pubkey_tweak_add (Jonas Nick)
4cd2ee474d extrakeys: Add xonly_pubkey with serialize, parse and from_pubkey (Jonas Nick)
47e6618e11 extrakeys: Init empty experimental module (Jonas Nick)
3e08b02e2a Make the secp256k1_declassify argument constant (Jonas Nick)

Pull request description:

  This PR implements signing, verification and batch verification as described in [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) in an experimental module named `schnorrsig`. It includes the test vectors and a benchmarking tool.
  This PR also adds a module `extrakeys` that allows [BIP-341](https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki)-style key tweaking.

  (Adding ChaCha20 as a CSPRNG and batch verification was moved to PR #760).

  In order to enable the module run `./configure` with `--enable-experimental --enable-module-schnorrsig`.

  Based on apoelstra's work.

ACKs for top commit:
  gmaxwell:
    ACK f431b3f28a  (exactly matches the previous post-fixup version which I have already reviewed and tested)
  sipa:
    ACK f431b3f28a
  real-or-random:
    ACK f431b3f28a careful code review

Tree-SHA512: e15e849c7bb65cdc5d7b1d6874678e275a71e4514de9d5432ec1700de3ba92aa9f381915813f4729057af152d90eea26aabb976ed297019c5767e59cf0bbc693
2020-09-11 21:25:34 +02:00
Peter Dettman b53e0cd61f Avoid overly-wide multiplications 2020-09-10 15:19:15 +07:00
Pieter Wuille cb5524adc5 Add benchmark for secp256k1_ge_set_gej_var 2020-09-09 18:40:23 -07:00
Pieter Wuille 5c6af60ec5 Make jacobi benchmarks vary inputs
Also make the num_jacobi benchmark use the scalar order as modulus,
instead of a random number.
2020-09-09 18:40:16 -07:00
Pieter Wuille d0fdd5f009 Randomize the Z coordinates in bench_internal
Also increase the number of fe inputs.
2020-09-09 18:30:05 -07:00
Pieter Wuille c7a3424c5f Rename bench_internal variables
The _x and _y suffices are confusing; they don't actually correspond
to X and Y coordinates. Instead replace them with arrays.
2020-09-09 18:30:05 -07:00
Tim Ruffing 875d68b95f
Merge #699: Initialize field elements when resulting in infinity
47a7b8382f Clear field elements when writing infinity (Elichai Turkel)
61d1ecb028 Added test with additions resulting in infinity (Elichai Turkel)

Pull request description:

  Currently if `secp256k1_gej_add_var` / `secp256k1_gej_add_ge_var` /` secp256k1_gej_add_zinv_var` receive `P + (-P)` it will set `gej->infinity = 1` but doesn't call initialize the field elements.
  Notice that this is the only branch in the function that results in an uninitialized output.

  By using `secp256k1_gej_set_infinity()` it will set the field elements to zero while also setting the infinity flag.

  I also added a test that fails with valgrind on current master but passes with the fix.

  EDIT: This isn't a bug or something necessary, I just personally found this helpful.

ACKs for top commit:
  real-or-random:
    ACK 47a7b8382f

Tree-SHA512: cdc2efc242a1b04b4f081183c07d4b2602cdba705e6b30b548df4e115e54fb97691f4b1a28f142f02d5e523c020721337a297b17d732acde147b910f5c53bd0a
2020-09-09 16:04:08 +02:00
Tim Ruffing 54caf2e74f
Merge #799: Add fallback LE/BE for architectures with known endianness + SHA256 selftest
8bc6aeffa9 Add SHA256 selftest (Pieter Wuille)
5e5fb28b4a Use additional system macros to figure out endianness (Pieter Wuille)

Pull request description:

  These are all the architecture macros I could find with known endianness. Use those as a fallback when __BYTE_ORDER__ isn't available.

  See https://github.com/bitcoin-core/secp256k1/pull/787#issuecomment-673764652

  It also adds a SHA256 selftest, so that improperly overriding the endianness detection will be detected at runtime.

ACKs for top commit:
  real-or-random:
    ACK 8bc6aeffa9 I read the diff, and tested that the self-test passes/fails with/without the correct endianness setting
  gmaxwell:
    ACK 8bc6aeffa9  looks good and I also ran the tests on MIPS-BE and verified that forcing it to LE makes the runtime test  fail.

Tree-SHA512: aca4cebcd0715dcf5b58f5763cb4283af238987f43bd83a650e38e127f348131692b2eed7ae5b2ae96046d9b971fc77c6ab44467689399fe470a605c3458ecc5
2020-09-09 15:57:45 +02:00
Jonas Nick f431b3f28a valgrind_ctime_test: Add schnorrsig_sign 2020-09-06 19:00:08 +00:00
Jonas Nick 16ffa9d97c schnorrsig: Add taproot test case 2020-09-06 19:00:08 +00:00
Jonas Nick 8dfd53ee3f schnorrsig: Add benchmark for sign and verify 2020-09-06 19:00:08 +00:00
Jonas Nick 4e43520026 schnorrsig: Add BIP-340 compatible signing and verification 2020-09-06 19:00:03 +00:00
Jonas Nick 7332d2db6b schnorrsig: Add BIP-340 nonce function 2020-09-06 19:00:03 +00:00
Jonas Nick 7a703fd97d schnorrsig: Init empty experimental module 2020-09-06 19:00:03 +00:00
Jonas Nick eabd9bc46a Allow initializing tagged sha256
This will be used by the schnorrsig module
2020-09-06 19:00:03 +00:00
Jonas Nick 6fcb5b845d extrakeys: Add keypair_xonly_tweak_add 2020-09-06 19:00:00 +00:00
Jonas Nick 58254463f9 extrakeys: Add keypair struct with create, pub and pub_xonly 2020-09-06 18:59:57 +00:00
Jonas Nick f0010349b8 Separate helper functions for pubkey_create and seckey_tweak_add
This is in preparation for allowing code reuse by keypair functions
2020-09-06 18:59:57 +00:00
Jonas Nick 910d9c284c extrakeys: Add xonly_pubkey_tweak_add & xonly_pubkey_tweak_add_test 2020-09-06 18:59:57 +00:00
Jonas Nick 176bfb1110 Separate helper function for ec_pubkey_tweak_add
This is in preparation for allowing code reuse by xonly tweak add functions
2020-09-06 18:59:57 +00:00
Jonas Nick 4cd2ee474d extrakeys: Add xonly_pubkey with serialize, parse and from_pubkey 2020-09-06 18:59:50 +00:00
Tim Ruffing aabf00c155
Merge #648: Prevent ints from wrapping around in scratch space functions
60f7f2de5d Don't assume that ALIGNMENT > 1 in tests (Tim Ruffing)
ada6361dec Use ROUND_TO_ALIGN in scratch_create (Jonas Nick)
8ecc6ce50e Add check preventing rounding to alignment from wrapping around in scratch_alloc (Jonas Nick)
4edaf06fb0 Add check preventing integer multiplication wrapping around in scratch_max_allocation (Jonas Nick)

Pull request description:

  This PR increases the general robustness of scratch spaces. It does not fix an existing vulnerability because scratch spaces aren't used anywhere in master. Additionally,  it must be prevented anyway that an attacker has (indirect) control over the arguments touched in this PR.

ACKs for top commit:
  sipa:
    ACK 60f7f2de5d

Tree-SHA512: ecdd794b55a01d1d6d24098f3abff34cb8bb6f33737ec4ec93714aa631c9d397b213cc3603a916ad79f4b09d6b2f8973bf87fc07b81b25a530cc72c4dbafaba9
2020-09-02 02:20:32 +02:00
Jonas Nick 47e6618e11 extrakeys: Init empty experimental module
This is to prepare for xonly_pubkeys and keypairs.
2020-08-26 19:52:55 +00:00
Jonas Nick 3e08b02e2a Make the secp256k1_declassify argument constant
This is required to declassify pointers to constant memory. Declassify should
never modify its argument.
2020-08-26 19:52:03 +00:00
Pieter Wuille 8bc6aeffa9 Add SHA256 selftest 2020-08-22 16:35:47 -07:00
Pieter Wuille 5e5fb28b4a Use additional system macros to figure out endianness
Also permit it being overridden by explicitly passing SECP256K1_{BIG,LITTLE}_ENDIAN
2020-08-15 20:31:51 -07:00
Pieter Wuille 7c068998ba Compile-time check assumptions on integer types 2020-08-14 16:12:49 -07:00
Pieter Wuille 02b6c87b52 Add support for (signed) __int128 2020-08-13 11:46:34 -07:00
Tim Ruffing 979961c506
Merge #787: Use preprocessor macros instead of autoconf to detect endianness
0dccf98a21 Use preprocessor macros instead of autoconf to detect endianness (Tim Ruffing)

Pull request description:

  This does not fix any particular issue but it's preferable to not
  rely on autoconf. This avoids endianness mess for users on BE hosts
  if they use their build without autoconf.

  The macros are carefully written to err on the side of the caution,
  e.g., we #error if the user manually configures a different endianness
  than what we detect.

  Supersedes #770 .

ACKs for top commit:
  sipa:
    ACK 0dccf98a21
  gmaxwell:
    ACK 0dccf98a21

Tree-SHA512: 6779458de5cb6eaef2ac37f9d4b8fa6c9b299f58f6e5b72f2b0d7e36c12ea06074e483acfb85085a147e0f4b51cd67d897f61a67250ec1cea284a0f7680eb2e8
2020-08-13 12:36:53 +02:00
Tim Ruffing 0dccf98a21 Use preprocessor macros instead of autoconf to detect endianness
This does not fix any particular issue but it's preferable to not
rely on autoconf. This avoids endianness mess for users on BE hosts
if they use their build without autoconf.

The macros are carefully written to err on the side of the caution,
e.g., we #error if the user manually configures a different endianness
than what we detect.
2020-08-11 11:25:39 +02:00
Pieter Wuille 79f1f7a4f1 Autodetect __int128 availability on the C side
Instead of supporting configuration of the field and scalar size independently,
both are now controlled by the availability of a 64x64->128 bit multiplication
(currently only through __int128). This is autodetected from the C code through
__SIZEOF_INT128__, but can be overridden using configure's
--with-test-override-wide-multiply, or by defining
USE_FORCE_WIDEMUL_{INT64,INT128} manually.
2020-08-10 14:56:39 -07:00
Pieter Wuille 0d7727f95e Add SECP256K1_FE_STORAGE_CONST_GET to 5x52 field
So far this has not been needed, as it's only used by the static precomputation
which always builds with 32-bit fields.

This prepares for the ability to have __int128 detected on the C side, breaking
that restriction.
2020-08-10 14:34:01 -07:00
Pieter Wuille 18d36327fd secp256k1_gej_double_nonzero supports infinity 2020-07-28 18:12:30 -07:00
Tim Ruffing 214cb3c321
Merge #772: Improve constant-timeness on PowerPC
67a429f31f Suppress a harmless variable-time optimization by clang in _int_cmov (Tim Ruffing)
5b196338f0 Remove redundant "? 1 : 0" after comparisons in scalar code (Tim Ruffing)

Pull request description:

  Attempt at resolving #771 .

  This surprisingly seems to improve the situation at least for the compilers available on godbolt.

ACKs for top commit:
  gmaxwell:
    ACK 67a429f31f
  elichai:
    tACK 67a429f31f

Tree-SHA512: ee8b0c86831ec8c3d5a9abcad773ed8a0f267e5c47012e4e1423b10a64c26b4cf6e3c466c3df765ba7e636787a3fe134d633926d67b599287f12c51be924f478
2020-07-28 16:12:24 +02:00
Tim Ruffing 2e1b9e0458 tests: Abort if malloc() fails during context cloning tests
Found by the clang static analyzer.

This is the worst true positive that it found. I feel somewhat proud.
2020-07-28 10:24:44 +02:00
Tim Ruffing 67a429f31f Suppress a harmless variable-time optimization by clang in _int_cmov
Follow up on 52a03512c1
2020-07-27 14:35:05 +02:00
Tim Ruffing 5b196338f0 Remove redundant "? 1 : 0" after comparisons in scalar code
This prevents GCC from generating branches on PowerPC in certain
cases.

Fixes #771.
2020-07-26 14:59:56 +02:00
Tim Ruffing 3e5cfc5c73
Merge #741: Remove unnecessary sign variable from wnaf_const
37dba329c6 Remove unnecessary sign variable from wnaf_const (Jonas Nick)
6bb0b77e15 Fix test_constant_wnaf for -1 and add a test for it. (Jonas Nick)

Pull request description:

  There currently is a single branch in the `ecmul_const` function that is not being exercised by the tests. This branch is unreachable and therefore I'm suggesting to remove it.

  For your convenience the paper the wnaf algorithm can be found [here (The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar Multiplications Secure against Side Channel Attacks)](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.563.1267&rep=rep1&type=pdf). Similarly, unless I'm missing something important, I don't see how their algorithm needs to consider `sign(u[i-1])` unless `d` can be negative - which doesn't make much sense to me either.

ACKs for top commit:
  real-or-random:
    ACK 37dba329c6 I verified the correctness of the change and claimed invariant by manual inspection. I tested the code, both with 32bit and 64bit scalars.

Tree-SHA512: 9db45f76bd881d00a81923b6d2ae1c3e0f49a82a5d55347f01e1ce4e924d9a3bf55483a0697f25039c327e33edca6796ba3205c068d9f2f99aa5d655e46b15be
2020-07-26 12:21:14 +02:00
Gregory Maxwell 1309c03c45 Fix some compile problems on weird/old compilers.
The visibility attribute is a GCC 4+ feature.
GCC 2.95 also warns about the unsigned/signed comparision.
2020-07-26 05:26:56 +00:00
Tim Ruffing 22e578bb11 Undef HAVE___INT128 in basic-config.h to fix gen_context compilation
Fixes #768.
2020-07-21 11:09:23 +02:00
Jonas Nick 2e7fc5b537 Fix uninitialized variables in ecmult_multi test 2020-06-15 09:02:54 +00:00
Elichai Turkel 28609507e7
Add tests for the cmov implementations 2020-06-03 13:19:12 +03:00
Elichai Turkel 73596a85a2
Add ecdsa_sign_recoverable to the ctime tests 2020-06-03 13:19:11 +03:00
Elichai Turkel 2876af4f8d
Split ecdsa_sign logic into a new function and use it from ecdsa_sign and recovery 2020-06-03 13:19:09 +03:00
Elichai Turkel f79a7adcf5
Add valgrind uninit check to cmovs output 2020-05-26 23:30:56 +03:00
Elichai Turkel a39c2b09de
Fixed UB(arithmetics on uninit values) in cmovs 2020-05-22 13:25:26 +03:00
Tim Ruffing f39f99be0e
Merge #701: Make ec_ arithmetic more consistent and add documentation
7e3952ae82 Clarify documentation of tweak functions. (Jonas Nick)
89853a0f2e Make tweak function documentation more consistent. (Jonas Nick)
41fc785602 Make ec_privkey functions aliases for ec_seckey_negate, ec_seckey_tweak_add and ec_seckey_mul (Jonas Nick)
22911ee6da Rename private key to secret key in public API (with the exception of function names) (Jonas Nick)
5a73f14d6c Mention that value is unspecified for In/Out parameters if the function returns 0 (Jonas Nick)
f03df0e6d7 Define valid ECDSA keys in the documentation of seckey_verify (Jonas Nick)
5894e1f1df Return 0 if the given seckey is invalid in privkey_negate, privkey_tweak_add and privkey_tweak_mul (Jonas Nick)
8f814cddb9 Add test for boundary conditions of scalar_set_b32 with respect to overflows (Jonas Nick)
3fec982608 Use scalar_set_b32_seckey in ecdsa_sign, pubkey_create and seckey_verify (Jonas Nick)
9ab2cbe0eb Add scalar_set_b32_seckey which does the same as scalar_set_b32 and also returns whether it's a valid secret key (Jonas Nick)

Pull request description:

  Fixes #671. Supersedes #668.

  This PR unifies handling of invalid secret keys by introducing a new function `scalar_set_b32_secret` which returns false if the b32 overflows or is 0. By using this in `privkey_{negate, tweak_add, tweak_mul}` these function will now return 0 if the secret key is invalid which matches the behavior of `ecdsa_sign` and `pubkey_create`.

  Instead of deciding whether to zeroize the secret key on failure, I only added documentation for now that the value is undefined on failure.

ACKs for top commit:
  real-or-random:
    ACK 7e3952ae82 I read the diff carefully and tested the changes
  apoelstra:
    ACK 7e3952ae82

Tree-SHA512: 8e9a66799cd3b6ec1c3acb731d6778035417e3dca9300d840e2437346ff0ac94f0c9be4de20aa2fac9bb4ae2f8a36d4e6a34795a640b9cfbfee8311decb102f0
2020-04-30 18:13:55 +02:00
Jonas Nick 37dba329c6 Remove unnecessary sign variable from wnaf_const 2020-04-29 12:38:23 +00:00
Jonas Nick 6bb0b77e15 Fix test_constant_wnaf for -1 and add a test for it.
Before, test_constant_wnaf used scalar_cadd_bit to correct for the skew. But
this function does not correctly deal with overflows which is why num = -1
couldn't be tested.

This commit also adds tests for 0, 1/2 and 1/2-1 as they are corner cases
in constant_wnaf.
2020-04-29 12:38:23 +00:00
Jonas Nick 39198a03ea
Merge #732: Retry if r is zero during signing
37ed51a7ea Make ecdsa_sig_sign constant-time again after reverting 25e3cfb (Tim Ruffing)
93d343bfc5 Revert "ecdsa_impl: replace scalar if-checks with VERIFY_CHECKs in ecdsa_sig_sign" (Tim Ruffing)

Pull request description:

ACKs for top commit:
  elichai:
    ACK 37ed51a7ea makes sense.
  jonasnick:
    ACK 37ed51a7ea

Tree-SHA512: 82b5b8e29f48e84fd7a0681b62923d3bd87d724b38ef18e8c7969b0dcc5a405ebb26c14b5c5f4c7ba0ccabd152d1531d217809d1daf40872fe0c1e079b55c64b
2020-04-18 12:23:05 +00:00
f-daniel 4e284655d9
Fix typo in ecmult_const_impl.h
Fix small typo in the reference given for the wNAF method
2020-04-18 12:53:06 +02:00
Elichai Turkel ffef45c98a
Make recovery/main_impl.h non-executable 2020-04-15 23:14:06 +03:00
Tim Ruffing 37ed51a7ea Make ecdsa_sig_sign constant-time again after reverting 25e3cfb 2020-03-31 15:03:58 +02:00
Tim Ruffing 93d343bfc5 Revert "ecdsa_impl: replace scalar if-checks with VERIFY_CHECKs in ecdsa_sig_sign"
This reverts commit 25e3cfbf9b. The reverted
commit was probably based on the assumption that this is about the touched
checks cover the secret nonce k instead of r, which is the x-coord of the public
nonce. A signature with a zero r is invalid by the spec, so we should return 0
to make the caller retry with a different nonce. Overflow is not an issue.

Fixes #720.
2020-03-31 14:58:58 +02:00
Jonas Nick 41fc785602 Make ec_privkey functions aliases for ec_seckey_negate, ec_seckey_tweak_add and ec_seckey_mul 2020-03-30 20:51:47 +00:00
Jonas Nick 5894e1f1df Return 0 if the given seckey is invalid in privkey_negate, privkey_tweak_add and privkey_tweak_mul 2020-03-30 20:51:47 +00:00
Jonas Nick 8f814cddb9 Add test for boundary conditions of scalar_set_b32 with respect to overflows 2020-03-30 20:51:47 +00:00
Jonas Nick 3fec982608 Use scalar_set_b32_seckey in ecdsa_sign, pubkey_create and seckey_verify 2020-03-30 20:51:47 +00:00
Jonas Nick 9ab2cbe0eb Add scalar_set_b32_seckey which does the same as scalar_set_b32 and also returns whether it's a valid secret key 2020-03-30 20:51:47 +00:00
Tim Ruffing 01993878bb Add test for memczero() 2020-03-27 11:07:10 +01:00
Tim Ruffing 52a03512c1 Suppress a harmless variable-time optimization by clang in memczero
This has been not been caught by the new constant-time tests because
valgrind currently gives us a zero exit code even if finds errors, see
https://github.com/bitcoin-core/secp256k1/pull/723#discussion_r388246806 .

This commit also simplifies the arithmetic in memczero.

Note that the timing leak here was the bit whether a secret key was
out of range. This leak is harmless and not exploitable. It is just
our overcautious practice to prefer constant-time code even here.
2020-03-27 10:23:45 +01:00
Elichai Turkel ca4906b02e
Pass num of iters to benchmarks as variable, and define envvar 2020-03-13 11:48:01 +02:00
Elichai Turkel 02dd5f1bbb
free the ctx at the end of bench_ecdh 2020-03-04 14:14:51 +02:00
Gregory Maxwell 3d2302257f Constant-time behaviour test using valgrind memtest.
Valgrind does bit-level tracking of the "uninitialized" status of memory,
 property tracks memory which is tainted by any uninitialized memory, and
 warns if any branch or array access depends on an uninitialized bit.

That is exactly the verification we need on secret data to test for
 constant-time behaviour. All we need to do is tell valgrind our
 secret key is actually uninitialized memory.

This adds a valgrind_ctime_test which is compiled if valgrind is installed:

Run it with libtool --mode=execute:
$ libtool --mode=execute valgrind ./valgrind_ctime_test
2020-02-24 18:59:30 +00:00
Tim Ruffing 96d8ccbd16
Merge #710: Eliminate harmless non-constant time operations on secret data.
7b50483ad7 Adds a declassify operation to aid constant-time analysis. (Gregory Maxwell)
34a67c773b Eliminate harmless non-constant time operations on secret data. (Gregory Maxwell)

Pull request description:

  There were several places where the code was non-constant time
   for invalid secret inputs.  These are harmless under sane use
   but get in the way of automatic const-time validation.

  (Nonce overflow in signing is not addressed, nor is s==0 in signing)

ACKs for top commit:
  sipa:
    utACK 7b50483ad7
  real-or-random:
    ACK 7b50483ad7 I read the code carefully and tested it
  jonasnick:
    reACK 7b50483ad7

Tree-SHA512: 0776c3a86e723d2f97b9b9cb31d0d0e59dfcf308093b3f46fbc859f73f9957f3fa977d03b57727232040368d058701ef107838f9b1ec98f925ec78ddad495c4e
2020-02-24 14:04:36 +01:00
Gregory Maxwell 7b50483ad7 Adds a declassify operation to aid constant-time analysis.
ECDSA signing has a retry loop for the exceptionally unlikely case
 that S==0.  S is not a secret at this point and this case is so
 rare that it will never be observed but branching on it will trip
 up tools analysing if the code is constant time with respect to
 secrets.

Derandomized ECDSA can also loop on k being zero or overflowing,
 and while k is a secret these cases are too rare (1:2^255) to
 ever observe and are also of no concern.

This adds a function for marking memory as no-longer-secret and
 sets it up for use with the valgrind memcheck constant-time
 test.
2020-02-20 17:27:26 +00:00
Gregory Maxwell 34a67c773b Eliminate harmless non-constant time operations on secret data.
There were several places where the code was non-constant time
 for invalid secret inputs.  These are harmless under sane use
 but get in the way of automatic const-time validation.

(Nonce overflow in signing is not addressed, nor is s==0 in
 signing)
2020-02-20 17:27:03 +00:00
Tim Ruffing eb45ef3384 Clarify that a secp256k1_ecdh_hash_function must return 0 or 1
and improve style of the ECDH docs.
2020-02-14 16:18:00 +01:00
Tim Ruffing d72b9e2483
Merge #682: Remove Java Native Interface
642cd062bd Remove Java Native Interface (Jonas Nick)

Pull request description:

  This was discussed in #508. The main reasons are that the existing Java Native Interface (JNI) bindings would need way more work to remain useful to Java developers but the maintainers and regular contributors of libsecp are not very familiar with Java (and evidently are motivated enough to improve the situation). We don't know who relies on these bindings with the exception of ACINQ who have their own fork at https://github.com/ACINQ/secp256k1/tree/jni-embed/src/java (@sstone). Bitcoinj can optionally use the libsecp bindings.

  Ideally, there would be a separate repository owned by Java developers with just the bindings. Until this exists, Java developers relying on libsecp can use ACINQs fork or an older commit of libsecp.

ACKs for top commit:
  real-or-random:
    ACK 642cd062bd I read the diff
  real-or-random:
    ACK 642cd062bd I read the diff, and I verified that the diff to 7d9066a66c0f13cabb0c4f71aca30edd3494f0d5, which has been ACKed by sipa, is only the additonal removal of ax_jni_include_dir.m4

Tree-SHA512: 9e573f2b01897bd5f301707062b41de53424517b537ce0834d9049d003cfd73fa1bcc024b543256016e4c9a1126f7c7fef559b84dc4914083b5a2d0ad5e57ea8
2020-02-10 12:00:35 +01:00
Marko Bencun dabfea7e21
field: extend docstring of secp256k1_fe_normalize 2020-01-17 12:41:35 +01:00
Marko Bencun dc7d8fd9e2
scalar: extend docstring of secp256k1_scalar_set_b32 2020-01-17 12:41:35 +01:00
Gregory Maxwell d567b779fe Clarify comments about use of rzr on ge functions and abs function. 2020-01-11 00:54:52 +00:00
Gregory Maxwell 2241ae6d14 Remove secret-dependant non-constant time operation in ecmult_const.
ECMULT_CONST_TABLE_GET_GE was branching on its secret input.

Also makes secp256k1_gej_double_var implemented as a wrapper
 on secp256k1_gej_double_nonzero instead of the other way
 around.  This wasn't a constant time bug but it was fragile
 and could easily become one in the future if the double_var
 algorithm is changed.
2020-01-09 12:21:28 +00:00
Jonas Nick 642cd062bd Remove Java Native Interface 2020-01-08 09:13:09 +00:00
Tim Ruffing d644dda5c9
Merge #689: Remove "except in benchmarks" exception for fp math
bde2a32286 Convert bench.h to fixed-point math (Wladimir J. van der Laan)

Pull request description:

  Convert `bench.h` to fixed-point math, removing all use of float math from the repository:

  - Use 64-bit integer microsecond timestamps
  - Use decimal fixed-point math for formatting numbers

  It turned out to be a little trickier than I expected because of formatting and rounding. But, output should be the same before and after.

  I used the following to test the number formatting: https://gist.github.com/laanwj/f971bfbe018e39c19677a21ff954d0c7

ACKs for top commit:
  real-or-random:
    ACK bde2a32286 I've read the code in detail and I've tested it. I haven't explicitly tested the formatting function with known/hardcoded inputs.

Tree-SHA512: 41ab6024b88c65a4b194272097c70d527bedb396dc7ab9d3d93165f1a19d31092798370f66399443a8d5393d0a6dcf5825679de5a325550865cfdef3586bf64c
2019-12-13 13:17:10 +01:00
Wladimir J. van der Laan bde2a32286 Convert bench.h to fixed-point math
- Use 64-bit integer microsecond timestamps
- Use fixed-point math for formatting numbers

Then, remove "except in benchmarks" exception from `README.md`.
2019-12-13 11:23:39 +01:00
Elichai Turkel 47a7b8382f
Clear field elements when writing infinity 2019-12-10 18:10:18 +02:00
Elichai Turkel 61d1ecb028
Added test with additions resulting in infinity 2019-12-10 18:08:25 +02:00
Jonas Nick a0771d15e6 Explicitly disable buffering for stderr in tests 2019-11-25 10:23:21 +00:00
Jonas Nick fb424fbba2 Make travis show the ./tests seed by removing stdout buffering and always cat tests.log after a travis run. 2019-11-25 10:23:21 +00:00
Elichai Turkel 362bb25608
Modified bench_scalar_split so it won't get optimized out 2019-10-28 16:50:09 +02:00
Elichai Turkel 73a30c6b58
Added accumulators and checks on benchmarks so they won't get optimized out 2019-10-28 16:50:07 +02:00
Jonas Nick b76142ff25 Remove note about heap allocation in secp256k1_ecmult_odd_multiples_table_storage_var which was removed in 47045270fa 2019-10-28 12:21:36 +00:00