commands → invmod

invmod(A,B)
invmod(A,B,X)
invmod(A,B,X,P)

The invmod command computes the inverse of A modulo B where A and B are integers or polynomials. If the inverse does not exist then null is returned. In the case of polynomials, the variable X must be specified as the third argument. An optional fourth argument P specifies a modulus for polynomial computations.

Examples

# integers
invmod(2,5);
	3
(2*3) % 5;
	1

# polynomials
invmod(x+1,x^2+x+1,x);
	-x

# polynomials mod p
invmod(x-1,x^2+x+1,x,11);
	7*x+3
remainder((x-1)*(7*x+3),x^2+x+1,x,11);
	1

Algorithm

The extended Euclidean algorithm is used for both integers and polynomials.

References

Link to Handbook of Applied Cryptography