11 Commits

Author SHA1 Message Date
Sebastian Falbesoner
5d89bc031b remove superfluous #ifdef VERIFY/#endif preprocessor conditions
Now that the `VERIFY_CHECK` compiles to empty in non-VERIFY mode, blocks
that only consist of these macros don't need surrounding `#ifdef VERIFY`
conditions anymore.

At some places intentional blank lines are inserted for grouping and
better readadbility.
2023-12-01 00:54:41 +01:00
Tim Ruffing
17fa21733a ct: Be cautious and use volatile trick in more "conditional" paths
- secp256k1_scalar_cadd_bit
 - secp256k1_modinvXX_normalize_YY
 - secp256k1_modinvXX_divsteps_ZZ
 - ECMULT_CONST_TABLE_GET_GE

Even though those code loations are not problematic right now
(with current compilers).
2023-05-11 16:32:07 +02:00
roconnor-blockstream
647f0a5cb1
Update comment for secp256k1_modinv32_inv256 2023-03-10 08:53:33 -05:00
Russell O'Connor
5d8f53e312 Remove redudent checks.
These abs checks are implied by the subsequent line, and with the subsequent line written as it is, no underflow is possible with signed integers.
2023-03-07 09:10:36 -05:00
Russell O'Connor
2ef1c9b387 Update overflow check
One does not simply check for integer overlow.
2023-03-06 18:13:47 -05:00
Pieter Wuille
1de2a01c2b Native jacobi symbol algorithm
This introduces variants of the divsteps-based GCD algorithm used for
modular inverses to compute Jacobi symbols. Changes compared to
the normal vartime divsteps:
* Only positive matrices are used, guaranteeing that f and g remain
  positive.
* An additional jac variable is updated to track sign changes during
  matrix computation.
* There is (so far) no proof that this algorithm terminates within
  reasonable amount of time for every input, but experimentally it
  appears to almost always need less than 900 iterations. To account
  for that, only a bounded number of iterations is performed (1500),
  after which failure is returned. In VERIFY mode a lower iteration
  count is used to make sure that callers exercise their fallback.
* The algorithm converges to f=g=gcd(f0,g0) rather than g=0. To keep
  this test simple, the end condition is f=1, which won't be reached
  if started with non-coprime or g=0 inputs. Because of that we only
  support coprime non-zero inputs.
2023-02-28 15:54:00 -05:00
Pieter Wuille
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
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
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
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