Fixpoint gcdGI_rec (n : nat) (xx  yy : GI) {struct n} :=
let rr := modGI xx yy in
if rr == 0 then yy else
if n is n1.+1 then gcdGI_rec n1 yy rr else rr.
Definition gcdGI x y :=
let: (x1, y1) := if ('N x < 'N y)%N then (y, x) else (x, y) in
if x1 == 0 then y1 else
gcdGI_rec ('N x1) x1 y1.

Lemma eqGI_mul2r (r p q : GI) : r != 0 -> (p * r %= q * r) = (p %= q).
Fixpoint egcdGI_rec n (x y : GI) :=
if y == 0 then (1, 0) else
if n is n1.+1 then
let: (u, v) := egcdGI_rec n1 y (modGI x y) in
(v, u - v * (x %/ y)%GI)
else (1, 0).