expMod function

int expMod(
  1. int g,
  2. int k,
  3. int n
)

Returns g^k mod n.

Implementation

int expMod(int g, int k, int n) {
  int y = 1;
  while (k > 0) {
    if (k & 0x1 != 0) {
      y = (y * g) % n;
    }
    k >>>= 1;
    g = (g * g) % n;
  }
  return y;
}