The extended GCD can be used to calculate mutual modular inverses of twocoprime numbers. Internally gmp_invert uses this extended GCD routine, but effectively throws away one of the inverses.If gcd(a,b)=1, then r.a+s.b=1Therefore r.a == 1 (mod s) and s.b == 1 (mod r)Note that one of r and s will be negative, and so you'll want tocanonicalise it.