I've tried to write a function to find the private key for RSA using the Extended Euclidean algorithm, I can't find the error but I really don't want to start again from scratch! For certain values it is correct but for others it isn't and I can't work out why, I really appreciate the help and I'm sorry if this question is too vague (I know that tends to annoy people on here). Can anyone find the error? Thank you very much in advance:
unsigned long long int modinv(unsigned long long int u, unsigned long long int v)
{
unsigned long long int inv, u1, u3, v1, v3, t1, t3, q;
unsigned long long int iter;
u1 = 1;
u3 = u;
v1 = 0;
v3 = v;
iter = 1;
while (v3 != 0)
{
q = u3 / v3;
t3 = u3 % v3;
t1 = u1 + q * v1;
u1 = v1; v1 = t1; u3 = v3; v3 = t3;
iter = -iter;
}
if (u3 != 1)
return 0;
if (iter < 0)
inv = v - u1;
else
inv = u1;
return inv;
}