test: use Jacobian coordinates in FastGEMul

bitcoin/bitcoin: PR #36896

1 suggestion

Optional improvements

Link

[suggestion] · test_fast_mul computes the same generic reference twice

Reusing the generic reference removes duplicate work from the new framework unit test without reducing its comparisons.

Location: test/functional/test_framework/crypto/secp256k1.py:420 (head).

Details and suggested fix

For each of the 44 scalars, the first two assertions independently evaluate coords(GE.mul((a, G))). This reference is uncached and traverses all 256 bit positions, using FE fraction arithmetic and curve checks for finite additions and doublings. Compute ref = coords(GE.mul((a, G))) once per iteration and compare both FAST_G.mul(a) and mul_G(a % GE.ORDER) against it. This preserves the cached-entry-point check and the separate comparison for H, while removing 44 redundant reference multiplications. The runtime saving has not been measured.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The preceding FastGEMul change explicitly prioritized cheaper repeated fixed-base multiplication, accepting a larger precomputed table and additional initialization work.

PR #36327 explains the four-bit window table retained by this change: it reduces approximately 128 point additions to 64 lookups and about 60 nonzero additions. This documents a performance tradeoff within the test helper, but does not establish that retaining FE arithmetic was itself an intentional independence requirement.

Source 1

Ralph's inferences

The inspected implementation supports a simplicity interpretation more strongly than an arithmetic-independence interpretation, although the original author's specific intent remains unverified.

The module explicitly describes itself as designed for ease of understanding rather than performance. At the pinned base, FastGEMul constructs its table and accumulates results through ordinary GE addition, sharing the FE arithmetic used by generic GE.mul. Thus the old fast path was not an independent implementation of the underlying group arithmetic; it primarily supplied a different scalar-multiplication schedule using existing understandable primitives.

Retaining explicit generic GE.mul comparisons preserves an available simple reference path for checking the specialized accumulator, but does not establish complete arithmetic independence or preserve simplicity inside FastGEMul itself.

The inspected generic GE.mul performs high-to-low bitwise doubling and addition through GE/FE, without dispatching to FastGEMul. The proposed tests call that method explicitly, so their expected results do not reuse the Jacobian accumulation being compared. However, the optimized path still constructs its precomputed table through GE addition and shares field and group definitions with the reference. This supports describing GE.mul as a comparison oracle for the new accumulation path—not as a wholly independent cryptographic implementation. Keeping that oracle also does not remove the specialized formulas added to the shared helper.