Okay, now I got different code working:
function egcd(a, B)/>
if a == 0 then
return b, 0, 1
else
g, y, x = egcd(b % a, a)
return g, x - (math.ceil(b / a) - 1) * y, y
end
end
function modinv(a,m)
g, x, y = egcd(a, m)
if g ~= 1 then
error("modular inverse does not exist")
else
return x % m
end
end
It works very good and fast with generating up to 48bit RSA-Keys,
but it makes errors with 56-bit-Keys (one ore some digits are wrong calculated) and when I try to generate 128-bit keys, it crashes with the error message in the second function. Does someone know why this could happen?
Here are examples:
32-bit: modinv(65537,1792011760) - function: 1137216673 - correct: 1137216673 - Correct!
48-bit: modinv(65537,121444194924000) - function: 56953893693473 - correct: 56953893693473 - Correct!
56-bit: modinv(65537,32478974171940912) - function: 2916997146284453 - correct: 7176526618305329 - Wrong!
128-bit: modinv(65537,158237197426104200175876099804099312720) - "abc.lua: 13: modular inverse does not exist"
btw: that "/>" at the first function is a bug in the forum software, I can't delete it, so ignore it.