Skip to content

fast modular arithmetic (montgomery multiplication) - #3

Merged
grenade merged 12 commits into
mainfrom
feat/montgomery
Sep 11, 2025
Merged

grenade merged 12 commits into
mainfrom
feat/montgomery

Conversation

@grenade

@grenade grenade commented Sep 11, 2025

Copy link
Copy Markdown
Contributor
  • Rationale: Modular multiplication is now the hot path. Fixed modulus n per job allows Montgomery form.
  • Tasks:
    • Switch big-integer backend for 512-bit ops to a fixed-width limb implementation (e.g., crypto-bigint) or implement custom 8×64-bit Montgomery mul/reduction.
    • Precompute Montgomery parameters once per job:
      • R = 2^(word_size*limbs) mod n, R^2 mod n, and n' (Montgomery inverse).
    • Keep y, m in Montgomery domain:
      • Initial transforms: m_hat = m * R^2 mod n, y0_hat = mod_pow(m, h + start_nonce, n) in Montgomery or transform after standard pow.
      • Per nonce: y_hat = montgomery_mul(y_hat, m_hat, n, n').
      • Before SHA3, either leave in normal domain (one extra Montgomery reduction per iteration) or compute SHA3 over the Montgomery representation if the consensus math requires the normal domain only (likely the latter).
  • Acceptance:
    • 0.25x speedup over Phase 1 on the same hardware (target depends on implementation).
    • No correctness regressions.

- introduce new `engine-montgomery` crate implementing a 512-bit
fixed-width montgomery multiplication backend for qpow mining. - mirror
`cpu-fast` engine behavior and metrics for direct comparison. - add
runtime backend selection with portable and x86_64 bmi2/adx kernels. -
expose `--engine cpu-montgomery` cli option in miner-cli. - integrate
engine and metrics support in miner-service with feature gating. - add
engine backend info metric for observability and a/b testing.
Implement CIOS multiplication using MULX, ADCX, and ADOX instructions
for x86_64 with BMI2 and ADX features detected at runtime. Fall back to
other implementations when features are unavailable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant