Skip to content

Commit

Permalink
Merge bitcoin#26222: Introduce secp256k1 module with field and group …
Browse files Browse the repository at this point in the history
…classes to test framework

d4fb58a test: EC: optimize scalar multiplication of G by using lookup table (Sebastian Falbesoner)
1830dd8 test: add secp256k1 module with FE (field element) and GE (group element) classes (Pieter Wuille)

Pull request description:

  This PR rewrites a portion of `test_framework/key.py`, in a compatible way, by introducing classes that encapsulate field element and group element logic, in an attempt to be more readable and reusable.

  To maximize readability, the group element logic does not use Jacobian coordinates. Instead, group elements just store (affine) X and Y coordinates directly. To compensate for the performance loss this causes, field elements are represented as fractions. This undoes most, but not all, of the performance loss, and there is a few % slowdown (as measured in `feature_taproot.py`, which heavily uses this).

  The upside is that the implementation for group laws (point doubling, addition, subtraction, ...) is very close to the mathematical description of elliptic curves, and this extends to potential future extensions (e.g. ElligatorSwift as needed by bitcoin#27479).

ACKs for top commit:
  achow101:
    ACK d4fb58a
  theStack:
    re-ACK d4fb58a
  stratospher:
    tested ACK d4fb58a. really liked how this PR makes the secp256k1 code in the tests more intuitive and easier to follow!

Tree-SHA512: 9e0d65d7de0d4fb35ad19a1c19da7f41e5e1db33631df898c6d18ea227258a8ba80c893dab862b0fa9b0fb2efd0406ad4a72229ee26d7d8d733dee1d56947f18
  • Loading branch information
achow101 committed Jun 28, 2023
2 parents 7952a59 + d4fb58a commit 626d346
Show file tree
Hide file tree
Showing 3 changed files with 409 additions and 286 deletions.
4 changes: 2 additions & 2 deletions test/functional/feature_taproot.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
sign_schnorr,
tweak_add_privkey,
ECKey,
SECP256K1
)
from test_framework import secp256k1
from test_framework.address import (
hash160,
program_to_witness,
Expand Down Expand Up @@ -695,7 +695,7 @@ def spenders_taproot_active():
# Generate an invalid public key
while True:
invalid_pub = random_bytes(32)
if not SECP256K1.is_x_coord(int.from_bytes(invalid_pub, 'big')):
if not secp256k1.GE.is_valid_x(int.from_bytes(invalid_pub, 'big')):
break

# Implement a test case that detects validation logic which maps invalid public keys to the
Expand Down
Loading

0 comments on commit 626d346

Please sign in to comment.