957 Commits

Author SHA1 Message Date
Andrew Poelstra
92a48a764d scratch space: use single allocation 2019-05-25 22:53:50 +00:00
Gregory Maxwell
40839e21b9
Merge #592: Use trivial algorithm in ecmult_multi if scratch space is small
9ab96f7 Use trivial algorithm in ecmult_multi if scratch space is small (Jonas Nick)

Pull request description:

  `ecmult_multi` already selects the trivial algorithm if the scratch space is NULL. With this PR the trivial algorithm is also selected if the scratch space is too small to use pippenger or strauss instead of returning 0. That makes it more easier to avoid consensus relevant inconsistencies just because scratch space construction was messed up.

ACKs for commit 9ab96f:
  real-or-random:
    utACK 9ab96f7

Tree-SHA512: aa451adf8880af15cf167a59cb07fc411edc43f26c8eb0873bdae2774382ba182e2a1c54487912f8f2999cb0402d554b9d293e2fb9483234471348a1f43c6653
2019-05-25 22:41:35 +00:00
Gregory Maxwell
dcf392027b Fix ability to compile tests without -DVERIFY.
Broken by 3f3964e4.

It's important that the tests are also run without -DVERIFY due to
 the possibility that side-effects of a VERIFY_CHECK fix a bug that
 would otherwise be detected.

Use of the verify_check macro in tests isn't sufficient.
2019-05-25 21:49:45 +00:00
Tim Ruffing
238305fdbb Move _preallocated functions to separate header 2019-05-25 14:01:09 +02:00
Tim Ruffing
814cc78d71 Add tests for contexts in preallocated memory 2019-05-25 14:01:09 +02:00
Tim Ruffing
ba12dd08da Check arguments of _preallocated functions 2019-05-25 14:01:09 +02:00
Tim Ruffing
5feadde462 Support cloning a context into preallocated memory 2019-05-25 14:01:09 +02:00
Tim Ruffing
c4fd5dab45 Switch to a single malloc call 2019-05-25 14:01:09 +02:00
Tim Ruffing
ef020de16f Add size constants for preallocated memory 2019-05-25 13:58:09 +02:00
Tim Ruffing
1bf7c056ba Prepare for manual memory management in preallocated memory
* Determine ALIGNMENT more cleverly and move it to util.h
 * Implement manual_malloc() helper function
2019-05-25 13:58:09 +02:00
Gregory Maxwell
248bffb052 Guard memcmp in tests against mixed size inputs.
Reported by real-or-random.

Fixes #623.
2019-05-25 11:54:08 +00:00
Tim Ruffing
2842dc523e Make WINDOW_G configurable
This makes WINDOW_G a configurable value in the range of [2..24].
The upper limit of 24 is a defensive choice. The code is probably
correct for values up to 27 but those larger values yield in huge
tables (>= 256MiB), which are i) unlikely to be really beneficial
in practice and ii) increasingly difficult to test.
2019-05-24 21:02:31 +02:00
Tim Ruffing
14c7dbd444 Simplify control flow in DER parsing 2019-05-23 15:22:29 +02:00
Tim Ruffing
ec8f20babd Avoid out-of-bound pointers and integer overflows in size comparisons
This changes pointer calculations in size comparions to a form that
ensures that no out-of-bound pointers are computed, because even their
computation yields undefined behavior.
Also, this changes size comparions to a form that ensures that neither
the left-hand side nor the right-hand side can overflow.
2019-05-23 15:22:29 +02:00
Tim Ruffing
01ee1b3b3c Parse DER-enconded length into a size_t instead of an int
This avoids a possibly implementation-defined signed (int) to unsigned
(size_t) conversion portably.
2019-05-23 15:22:29 +02:00
Gregory Maxwell
912680ed86
Merge #561: Respect LDFLAGS and #undef STATIC_PRECOMPUTATION if using basic config
dbed75d Undefine `STATIC_PRECOMPUTATION` if using the basic config (DesWurstes)
310111e Keep LDFLAGS if `--coverage` (DesWurstes)

Pull request description:

  Update: **This is a trimmed pull request with strong rationale.**

  - Adding `--coverage` shouldn't reset `LDFLAGS`, this is definitely a typo
  - The basic configuration should undefine `STATIC_PRECOMPUTATION`, as generating it is not supported and it complicates #549

Tree-SHA512: 29f0dd4c870ec60d535346446b453da459ca843ed1265c2bc966bf0fcbdf3c5c79f9e48a419662e81d790a7003f8877a16e2a5a74aa5c0b79645e15ad56a0f66
2019-05-23 00:38:30 +00:00
Gregory Maxwell
5df77a0eda
Merge #533: Make sure we're not using an uninitialized variable in secp256k1_wnaf_const(...)
248f046 Make sure we're not using an uninitialized variable in secp256k1_wnaf_const(...) (practicalswift)

Pull request description:

  Make sure we're not using an uninitialized variable in `secp256k1_wnaf_const(...)`:

  ```
  In file included from src/secp256k1.c:15:0,
                   from src/tests.c:17:
  src/ecmult_const_impl.h: In function ‘secp256k1_wnaf_const’:
  src/ecmult_const_impl.h:117:20: warning: ‘u’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       wnaf[word] = u * global_sign;
                      ^
  ```

  **Note to reviewers:** Perhaps an `assert(…);` is a bit drastic. What would be a more graceful way to handle this? :-)

Tree-SHA512: 536cd7cc5b87a84fbaac578cecbba81b8d82e4672a30a2db9a674b82856132e79b0158a6a88609bc24942ebdbf1fcd2c4399a4c31ab0654b88ace9c0e6f1eaf3
2019-05-22 04:44:29 +00:00
Gregory Maxwell
975e51e0d9
Merge #617: Pass scalar by reference in secp256k1_wnaf_const()
8979ec0 Pass scalar by reference in secp256k1_wnaf_const() (Tim Ruffing)

Pull request description:

  After this change, no struct or union is passed by value in the
  entire codebase. This makes it easier to compile the library with
  CompCert.

Tree-SHA512: 6b23e2b39701c3eeb6ae8c8d660cabe8872ac8f13141504c1ec55c47f2009e206129b34b31796e618114b60350598187df6df4c2be0e5c1b138a6126ad6a7484
2019-05-22 04:40:08 +00:00
Seonpyo Kim
069870d92a Clear a copied secret key after negation 2019-05-15 15:55:01 +09:00
Tim Ruffing
8979ec0d9a Pass scalar by reference in secp256k1_wnaf_const()
After this change, no struct or union is passed by value in the
entire codebase. This makes it easier to compile the library with
CompCert.
2019-05-14 11:43:54 +02:00
Roman Zeyde
d4d270a59c
Allow field_10x26_arm.s to compile for ARMv7 architecture 2019-05-07 22:37:35 +03:00
Pieter Wuille
e6d01e9347 Use size_t shifts when computing a size_t 2019-03-30 16:09:17 -07:00
practicalswift
248f046611 Make sure we're not using an uninitialized variable in secp256k1_wnaf_const(...) 2019-03-21 19:06:08 +01:00
Jonas Nick
9ab96f7b12 Use trivial algorithm in ecmult_multi if scratch space is small 2019-03-18 15:15:35 +00:00
Gregory Maxwell
d58bc93f2c Switch x86_64 asm to use "i" instead of "n" for immediate values.
The constraint "n" is a more obscure setting which is needed for
 special cases that don't apply to our use.

Use of "n" exposed some bugs in pre-release versions of clang.
This fixes #594
2019-03-10 22:52:12 +00:00
Jonas Nick
2277af5ff0 Fix integer overflow in ecmult_multi_var when n is large 2019-02-25 16:13:17 +00:00
DesWurstes
dbed75d969 Undefine STATIC_PRECOMPUTATION if using the basic config 2019-02-25 11:36:39 +03:00
Gregory Maxwell
85d0e1bcce
Merge #591: Make bench_internal obey secp256k1_fe_sqrt's contract wrt aliasing.
b76e45d Make bench_internal obey secp256k1_fe_sqrt's contract wrt aliasing. (Gregory Maxwell)

Pull request description:

  Bench_internal was previously incorrect but wasn't detected by -DVERIFY until PR #551.

Tree-SHA512: 5ecb9e3ba63ddf28cde4012154dbb1dda9b7178016d970dba58a4af20c31aa1ec3458ba2623541233fb16fceff7b3053054d6eee0229d0bfb6281d1a10554e9f
2019-02-24 19:20:54 +00:00
Jonas Nick
a697d82da9 Add trivial ecmult_multi to the benchmark tool 2019-02-23 20:30:31 +00:00
Jonas Nick
bade617417 Add trivial ecmult_multi algorithm. It is selected when no scratch space is given and just multiplies and adds the points. 2019-02-23 20:24:28 +00:00
Gregory Maxwell
20c5869df2
Merge #516: improvements to random seed in src/tests.c
be40c4d Fixup for C90 mixed declarations. (Gregory Maxwell)
8b3841c fix bug in fread() failure check (Don Viszneki)
cddef0c tests: add warning message when /dev/urandom fails (Don Viszneki)

Pull request description:

  I've made two small changes to `src/tests.c` circa random seed generation.

  Added a warning when `/dev/urandom` fails, mostly to defend against the case that someone should use the code verbatim, but also to enhance its illustrative power.

  Also I fixed a bug with how the return value of `fread()` was being evaluated. In fact, `/dev/urandom` was never being applied before as the check on the return value of `fread()` always failed!

Tree-SHA512: 239dbe8316220c2f0e5b370bf9a18f78196e96cc4a7edea58cf2521b2c9cbc8da065be96aa859f90324d57e388d30f7670ce6bc1cca52e5162e5ca66b1a55b34
2019-02-21 11:46:13 +00:00
Gregory Maxwell
b76e45d5d6 Make bench_internal obey secp256k1_fe_sqrt's contract wrt aliasing.
Bench_internal was previously incorrect but wasn't detected by
 -DVERIFY until PR #551.
2019-02-21 05:58:33 +00:00
Gregory Maxwell
870a977644
Merge #562: Make use of TAG_PUBKEY constants in secp256k1_eckey_pubkey_parse
d3cb1f9 Make use of TAG_PUBKEY constants in secp256k1_eckey_pubkey_parse (Ben Woosley)

Pull request description:

  This results in more self-documenting code.

  Constants defined here:
  1e6f1f5ad5/include/secp256k1.h (L175-L180)

Tree-SHA512: 2026103c487a5ebdea9a2f5ec2be96d74e38d5b2269b4df11e354bb54aba2925b47c3185f530205019840b00fc3501121acfa5234faf1d095d71777826414f69
2019-02-21 05:35:10 +00:00
Gregory Maxwell
be40c4d0b5 Fixup for C90 mixed declarations.
Reported-by: Jonas Nick <jonasd.nick@gmail.com>
2019-02-21 05:00:29 +00:00
Gregory Maxwell
c71dd2c08f
Merge #509: Fix algorithm selection in bench_ecmult
0f05173 Fix algorithm selection in bench_ecmult (Jonas Nick)

Pull request description:

  Without this commit using an unknown argument results in a segmentation fault.

Tree-SHA512: 85b8f977e62fa360ff6dc923bd86f08725c30e178d58f43d33cbd8eb69e54fa4ec76e4407624611a80554cf9961689bce6b216da735ac95b256d0d2f60fe3328
2019-02-21 04:47:12 +00:00
Gregory Maxwell
e96901a4b9
Merge #587: Make randomization of a non-signing context a noop
6198375 Make randomization of a non-signing context a noop (Tim Ruffing)

Pull request description:

  Before this commit secp256k1_context_randomize called illegal_callback
  when called on a context not initialized for signing. This is not
  documented. Moreover, it is not desirable because non-signing contexts
  may use randomization in the future.

  This commit makes secp256k1_context_randomize a noop in this case. This
  is safe because the context cannot be used for signing anyway.

  This fixes #573 and it fixes rust-bitcoin/rust-secp256k1#82.

Tree-SHA512: 34ddfeb004d9da8f4a77c739fa2110544c28939378e779226da52f410a0e36b3aacb3ebd2e3f3918832a9027684c161789cfdc27a133f2f0e0f1c47e8363029c
2019-02-21 04:33:59 +00:00
Gregory Maxwell
2ebdad772a
Merge #552: Make constants static:
496c5b4 Make constants static: static const secp256k1_ge secp256k1_ge_const_g; static const int CURVE_B; (Russell O'Connor)

Pull request description:

  static const secp256k1_ge secp256k1_ge_const_g;
  static const int CURVE_B;

Tree-SHA512: df8d34777d3b1b90eef875d50c2870e2480f08685b399a22eaf584ce294766e0de3a41523757a23e311e633886c89400313421282beca791326a06695f348940
2019-02-21 04:26:40 +00:00
Gregory Maxwell
1c131affd3
Merge #551: secp256k1_fe_sqrt: Verify that the arguments don't alias.
bf8b86c secp256k1_fe_sqrt: Verify that the arguments don't alias. (Russell O'Connor)

Pull request description:

Tree-SHA512: 4e9128625817be83a1259e7dbdb82c4be8ee0174af7d5e76eeac3245d23d6bf01378d9697dcb870eb61577afd7c0556cb3c6633e3b27504add14c10b18f7c607
2019-02-21 04:23:50 +00:00
Gregory Maxwell
ba698f883b
Merge #539: Assorted minor corrections
52ab96f clean dependendies in field_*_impl.h (Russell O'Connor)
deff5ed Correct math typos in field_*.h (Russell O'Connor)
4efb3f8 Add check that restrict pointers don't alias with all parameters. (Russell O'Connor)

Pull request description:

  * add more checks for restrict pointers.
  * correct math typos.
  * refine dependencies on "num.h"

Tree-SHA512: c368f577927db2ace3e7f46850cb2fdf9d7d169b698a9697767e1f82e9e7091f2b2fea0f7cf173048eb4c1bb56824c884fa849c04c595ee97766c01f346a54ec
2019-02-21 04:20:35 +00:00
Gregory Maxwell
949e85b009
Merge #550: Optimize secp256k1_fe_normalize_weak calls.
9bd89c8 Optimize secp256k1_fe_normalize_weak calls. Move secp256k1_fe_normalize_weak calls out of ECMULT_TABLE_GET_GE and ECMULT_TABLE_GET_GE_STORAGE and into secp256k1_ge_globalz_set_table_gej instead. (Russell O'Connor)

Pull request description:

  Move secp256k1_fe_normalize_weak calls out of ECMULT_TABLE_GET_GE and ECMULT_TABLE_GET_GE_STORAGE and into secp256k1_ge_globalz_set_table_gej instead.

Tree-SHA512: 7bbb1aca8e37a268a26d7061bd1f390db129e697792f1d5ddd10ea34927616edc26ef118b500c3e5e14d1d463196033ef64e4d34b765380325c24835458b7a9b
2019-02-21 04:14:40 +00:00
Tim Ruffing
6198375218 Make randomization of a non-signing context a noop
Before this commit secp256k1_context_randomize called illegal_callback
when called on a context not initialized for signing. This is not
documented. Moreover, it is not desirable because non-signing contexts
may use randomization in the future.

This commit makes secp256k1_context_randomize a noop in this case. This
is safe because the context cannot be used for signing anyway.

This fixes #573 and it fixes rust-bitcoin/rust-secp256k1#82.
2019-01-27 13:33:52 +01:00
liuyujun
74e2dbd68e JNI: fix use sig array 2019-01-03 09:08:58 +08:00
Tim Ruffing
c663397f46 Use __GNUC_PREREQ for detecting __builtin_expect 2018-12-17 11:47:25 +01:00
Tim Ruffing
3cb057f842 Fix possible integer overflow in DER parsing
If we’re in the last loop iteration, then `lenleft == 1` and it could
be the case that `ret == MAX_SIZE`, and so `ret +  lenleft` will
overflow to 0 and the sanity check will not catch it. Then we will
return `(int) MAX_SIZE`, which should be avoided because this value is
implementation-defined. (However, this is harmless because
`(int) MAX_SIZE == -1` on all supported platforms.)
2018-12-14 11:43:47 +01:00
Andrew Poelstra
b3bf5f99a3 ecmult_impl: expand comment to explain how effective affine interacts with everything 2018-11-10 14:01:13 +00:00
Peter Dettman
efa783f8f0 Store z-ratios in the 'x' coord they'll recover 2018-11-10 13:14:43 +00:00
Andrew Poelstra
ffd3b346fe add secp256k1_ge_set_all_gej_var test which deals with many infinite points 2018-11-09 00:20:53 +00:00
Andrew Poelstra
84740acd2a ecmult_impl: save one fe_inv_var 2018-11-09 00:20:53 +00:00
Andrew Poelstra
47045270fa ecmult_impl: eliminate scratch memory used when generating context 2018-11-09 00:20:51 +00:00
Andrew Poelstra
7f7a2ed3a8 ecmult_gen_impl: eliminate scratch memory used when generating context 2018-11-09 00:16:06 +00:00