1 package civitas.crypto.parameters;
2
3 import org.springframework.stereotype.Controller;
4
5 import civitas.crypto.Constants;
6 import civitas.crypto.CryptoError;
7 import civitas.util.CivitasBigInteger;
8
9 @Controller
10 public class LegendreSymbol implements Constants {
11
12 public int apply(final CivitasBigInteger a, final CivitasBigInteger p, final CivitasBigInteger q) {
13 CivitasBigInteger j = a.modPow(q, p);
14 if (ONE.equals(j)) {
15 return 1;
16 } else if (j.equals(p.subtract(ONE))) {
17 return -1;
18 } else if (ZERO.equals(j)) {
19 return 0;
20 } else {
21 throw new CryptoError("Impossible Legendre symbol");
22 }
23 }
24 }