99 Commits

Author SHA1 Message Date
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
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
Pieter Wuille
4232e5b7da Rip out non-endomorphism code 2020-10-13 11:31:13 -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
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
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
Tim Ruffing
875d68b95f
Merge #699: Initialize field elements when resulting in infinity
47a7b8382fd6f1458d859b315cf3bcd3b9790b68 Clear field elements when writing infinity (Elichai Turkel)
61d1ecb02847be9d65ffe9df2d2408d85f3a0711 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 47a7b8382fd6f1458d859b315cf3bcd3b9790b68

Tree-SHA512: cdc2efc242a1b04b4f081183c07d4b2602cdba705e6b30b548df4e115e54fb97691f4b1a28f142f02d5e523c020721337a297b17d732acde147b910f5c53bd0a
2020-09-09 16:04:08 +02:00
Pieter Wuille
18d36327fd secp256k1_gej_double_nonzero supports infinity 2020-07-28 18:12:30 -07: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
Elichai Turkel
47a7b8382f
Clear field elements when writing infinity 2019-12-10 18:10:18 +02: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
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
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
Russell O'Connor
496c5b43b8 Make constants static:
static const secp256k1_ge secp256k1_ge_const_g;
static const int CURVE_B;
2018-08-15 15:42:06 -04:00
Russell O'Connor
9bd89c836b 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.
2018-08-12 23:47:38 -04:00
Pieter Wuille
8c1c831bdb Generalize Strauss to support multiple points
API by Andrew Poelstra.
2017-12-07 20:13:04 +00:00
Dan Raviv
abe2d3e84b Fix header guards using reserved identifiers
Identifiers starting with an underscore and followed immediately by a capital letter are reserved by the C++ standard.

The only header guards not fixed are those in the headers auto-generated from java.
2017-08-26 18:44:21 +03:00
Andrew Poelstra
03ff8c2d0a group_impl.h: remove unused secp256k1_ge_set_infinity function
Also remove `secp256k1_fe_verify` from field_*_.impl.h when VERIFY is not defined
2016-11-28 03:29:01 +00:00
Andrew Poelstra
83836a9547 Add exhaustive tests for group arithmetic, signing, and ecmult on a small group
If you compile without ./configure --enable-exhaustive-tests=no,
this will create a binary ./exhaustive_tests which will execute
every function possible on a group of small order obtained by
moving to a twist of our curve and locating a generator of small
order.

Currently defaults to order 13, though by changing some #ifdefs
you can get a couple other ones. (Currently 199, which will take
forever to run, and 14, which won't work because it's composite.)

TODO exhaustive tests for the various modules
2016-11-26 00:35:00 +00:00
Andrew Poelstra
20b8877be1 Add exhaustive test for group functions on a low-order subgroup
We observe that when changing the b-value in the elliptic curve formula
`y^2 = x^3 + ax + b`, the group law is unchanged. Therefore our functions
for secp256k1 will be correct if and only if they are correct when applied
to the curve defined by `y^2 = x^3 + 4` defined over the same field. This
curve has a point P of order 199.

This commit adds a test which computes the subgroup generated by P and
exhaustively checks that addition of every pair of points gives the correct
result.

Unfortunately we cannot test const-time scalar multiplication by the same
mechanism. The reason is that these ecmult functions both compute a wNAF
representation of the scalar, and this representation is tied to the order
of the group.

Testing with the incomplete version of gej_add_ge (found in 5de4c5dff^)
shows that this detects the incompleteness when adding P - 106P, which
is exactly what we expected since 106 is a cube root of 1 mod 199.
2016-11-25 20:45:29 +00:00
llamasoft
353c1bf0d7 Fix secp256k1_ge_set_table_gej_var parameter order
Rearranged secp256k1_ge_set_table_gej_var parameters so length comes last (it modifies both *a and *zr).
2016-07-26 14:24:45 -05:00
llamasoft
541b783920 Fix secp256k1_ge_set_all_gej_var parameter order
Rearranged secp256k1_ge_set_all_gej_var parameters so length comes after *a.
2016-07-26 14:24:42 -05:00
llamasoft
7d893f4980 Fix secp256k1_fe_inv_all_var parameter order
Rearranged secp256k1_fe_inv_all_var parameters so length is after array.
Text editor removed some trailing whitespaces.
2016-07-26 12:15:19 -05:00
Pieter Wuille
926836ad36 Make secp256k1_fe_sqrt constant time 2016-07-09 14:23:44 +02:00
Pieter Wuille
e2a8e92125
Merge #404: Replace 3M + 4S doubling formula with 2M + 5S one
8ec49d8 Add note about 2M + 5S doubling formula (Andrew Poelstra)
2016-07-07 17:06:49 +02:00
Andrew Poelstra
8ec49d8a5a Add note about 2M + 5S doubling formula 2016-07-07 15:04:36 +00:00
Andrew Poelstra
a6c6f994b5 Remove a bunch of unused stdlib #includes 2016-07-06 11:40:37 +00:00
Pieter Wuille
e6e9805fc4 Add function for testing quadratic residue field/group elements. 2016-07-04 13:55:04 +02:00
Andrew Poelstra
e72e93ad9c Add note about y=0 being possible on one of the sextic twists 2016-01-10 08:35:59 +00:00
Pieter Wuille
646662517f Improvements for coordinate decompression 2015-11-05 00:04:39 +01:00
Gregory Maxwell
9f6993f370 Remove some dead code. 2015-09-28 05:43:51 +00:00
Gregory Maxwell
2b199de888 Use the explicit NULL macro for pointer comparisons.
This makes it more clear that a null check is intended. Avoiding the
 use of a pointer as a test condition alse increases the type-safety
 of the comparisons.

(This is also MISRA C 2012 rules 14.4 and 11.9)
2015-09-23 22:00:43 +00:00
Pieter Wuille
dd891e0ed5 Get rid of _t as it is POSIX reserved 2015-09-21 21:03:37 +02:00
Gregory Maxwell
912f203fc5 Eliminate a few unbraced statements that crept into the code.
Also avoids some easily avoided multiple-returns.
2015-09-21 17:21:35 +00:00
GSongHashrate
81e45ff9d1 Update group_impl.h 2015-09-17 22:38:21 +01:00
Andrew Poelstra
4401500060 Add constant-time multiply secp256k1_ecmult_const for ECDH
Designed with clear separation of the wNAF conversion, precomputation
and exponentiation (since the precomp at least we will probably want
to separate in the API for users who reuse points a lot.

Future work:
  - actually separate precomp in the API
  - do multiexp rather than single exponentiation
2015-07-31 12:39:09 -05:00
Pieter Wuille
995c548771 Introduce callback functions for dealing with errors. 2015-07-26 18:08:38 +02:00
Peter Dettman
5a43124c69 Save 1 _fe_negate since s1 == -s2 2015-07-07 22:30:00 +10:00
Peter Dettman
a5d796e0b1 Update code comments 2015-07-07 09:16:15 +09:30
Peter Dettman
7d054cd030 Refactor to save a _fe_negate 2015-07-04 16:38:46 +09:30
Peter Dettman
b28d02a5d5 Refactor to remove a local var 2015-07-04 16:30:56 +09:30
Peter Dettman
55e7fc32cb Perf. improvement in _gej_add_ge
- Avoid one weak normalization
- Change one full normalization to weak
- Avoid unnecessary fe assignment
- Update magnitude annotations
2015-07-04 16:21:35 +09:30
Andrew Poelstra
5de4c5dffd
gej_add_ge: fix degenerate case when computing P + (-lambda)P
If two points (x1, y1) and (x2, y2) are given to gej_add_ge with
x1 != x2 but y1 = -y2, the function gives a wrong answer since
this causes it to compute "lambda = 0/0" during an intermediate
step. (Here lambda refers to an auxiallary variable in the point
addition formula, not the cube-root of 1 used by the endomorphism
optimization.)

This commit catches the 0/0 and replaces it with an alternate
expression for lambda, cmov'ing it in place if necessary.
2015-06-29 08:21:58 -07:00
Andrew Poelstra
bcf2fcfd3a
gej_add_ge: rearrange algebra
There is zero functionality or opcount changes here; I need to do
this to make sure both R and M are computed before they are used,
since a future patch will replace either none or both of them.

Also compute r->y directly in terms of r->x, which again will be
used in a future patch.
2015-06-23 12:44:15 -07:00
Pieter Wuille
a1d5ae1527 Tiny optimization 2015-05-05 20:40:24 +02:00
Peter Dettman
2d5a186cee Apply effective-affine trick to precomp 2015-04-30 09:25:44 -07:00
Peter Dettman
4f9791abba Effective affine addition in EC multiplication
* Make secp256k1_gej_add_var and secp256k1_gej_double return the
  Z ratio to go from a.z to r.z.
* Use these Z ratios to speed up batch point conversion to affine
  coordinates, and to speed up batch conversion of points to a
  common Z coordinate.
* Add a point addition function that takes a point with a known
  Z inverse.
* Due to secp256k1's endomorphism, all additions in the EC
  multiplication code can work on affine coordinate (with an
  implicit common Z coordinate), correcting the Z coordinate of
  the result afterwards.

Refactoring by Pieter Wuille:
* Move more global-z logic into the group code.
* Separate code for computing the odd multiples from the code to bring it
  to either storage or globalz format.
* Rename functions.
* Make all addition operations return Z ratios, and test them.
* Make the zr table format compatible with future batch chaining
  (the first entry in zr becomes the ratio between the input and the
  first output).

Original idea and code by Peter Dettman.
2015-04-30 09:23:21 -07:00