Karim Belabas on Tue, 13 Jun 2023 11:09:32 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Why is "x*Mod(1/y, p)" 28.7% faster than "Mod(x/y, p)" ?


* hermann@stamm-wilbrandt.de [2023-06-13 10:53]:
> "##" measures time in milliseconds.
> 
> So I use a very big prime (1million decimal digits) and its sqrtm1 =
> sqrt(-1) (mod p) to see 3-digit millisecond runtimes, from this Pari/GP
> script:
> https://github.com/Hermann-SW/RSA_numbers_factored/blob/main/pari/sqrtm1.smallest_known_1million_digit_prime.gp
> 
> 323ms is 28.7% faster than 453ms, what is the reason that "x*Mod(1/y, p)"
> outperforms "Mod(x/y, p)"?

x/y starts by computing the GCD of two large numbers, to reduce the
fraction to irreducible represention. This step is trivial in 1/y which
is obviously reduced.

This GCD step is unnecessary but must be performed since x/y requests it
(PARI doesn't support reducible fractions: they are reduced on the spot).
It is of the same order of difficulty as inverting the denominator mod p
[extended GCD].

Cheers,

    K.B.
-- 
Pr. Karim Belabas, U. Bordeaux, Vice-président en charge du Numérique
Institut de Mathématiques de Bordeaux UMR 5251 - (+33) 05 40 00 29 77
http://www.math.u-bordeaux.fr/~kbelabas/