Help with pow() function

Hey i am attempting to make a RSA encryption but there is a problem with the encryption. i have figured out it is my pow() function that is not working correctly

I have based my RSA encryption off of this YouTube video (made in python, attempting to port in LUA)

The problem with the pow function, in python you could do:

pow(101, 13, 143)
# returns 140

I have attempted to implement the pow function as such:

local function pow(x, y, z)
	return (x ^ y) % z
end
pow(101, 13, 143)

when calling it with the arguments: 101, 13, 143 it just returns 0
image

Running the code in python it works fine

def custompow(x, y, z):
	return (x ** y) % z # python ** is the same as  ^ in LUA

image

Is there anything I have done wrong (e.g used wrong operator)? Any help is appreciated

Screenshot 2020-10-07 at 1.04.16 AM
maybe lua just can’t handle this big number

1 Like

guess this is an error with LUA? since running the same command in python it returns correct answer (140)
image

RSA requires you to use integers which lua doesn’t use. Use another algorithm which works with doubles or use a BigNumInt library (very slow)

1 Like

Lua 5.1 from the manual 2.5.1, modulus is defined as: a % b == a - math.floor(a/b)*b, I think the problem comes from this algorithm.
This is almost certainly a bug and it returns the correct result in Lua 5.3.

(You can use math.fmod which doesn’t have this problem)

1 Like

I didnt actually know this, guess i will have to do some more researching. Got any other algorithms? Preferably one that works similar to RSA (Public-key cryptography)