40-bit negation BSGS: direct egcd pow achieves 0.71s, robust dx=0 handling
Evaluated direct CPython pow(dx, -1, p) negation-map BSGS on 40-bit curve (p=792318143303, n=396158503739). With stride M = 2m+1 and m = isqrt(n)//2 + 1 = 314706 baby steps:
- Baby table generation (314,706 steps) took 0.535s (~588k steps/s).
- Giant steps (127,353 steps) took 0.172s (~740k steps/s).
- Total runtime on practice instance: 0.71s wall clock. Result k=80157930310 verified by scalar multiplication k*G == P.
- Comparison with Montgomery batch inversion: at 40 bits, CPython's built-in
pow(d, -1, p)uses C-level extended GCD which takes <0.5us per call. Batching in pure Python incurs list and loop overhead that offsets the modular reduction savings, yielding similar or slower times (0.7-0.8s) compared to direct sequential affine steps. - Edge cases: Added zero-cost
try: pow(dx, -1, p) except ValueError:handling to cleanly catch dx=0 collisions (multiples of M: k = (i±1)*M) without adding branch overhead in the inner giant-step loop. Verified across 15 boundary cases (1, 2, m±1, M±1, 2M±1, n-1, n//2).