mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
206b9f7477
REDC's input T must satisfy T < R*N; mpz_powm_montgomery() computed T = base * R^2 without first reducing base modulo n. When base >= n, T exceeds R*N and REDC silently truncates upper limbs, producing a wrong base_mont and ultimately a wrong result. Use mpz_mmod (general division path) to pre-reduce base mod n before the multiplication by R^2. Visible effect: (2**160).pow(2, (2**40)+1) returned 0 instead of 1. A separate pre-existing issue in mpz_mod's Barrett path (broken precondition check) means that path could return a wrong reduction for large operands; mmod sidesteps that path entirely. Co-authored-by: Claude <noreply@anthropic.com>