-
Notifications
You must be signed in to change notification settings - Fork 0
/
FME.java
52 lines (40 loc) · 1.36 KB
/
FME.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package rsa;
import java.math.BigInteger;
/**
* Class with the implementation of
* Fast Modular Exponentiation.
*/
public class FME {
/**
* Returns the mod using FME test.
* @param a is the base of BigInteger type
* @param b is the exponent of BigInteger type
* @param value is modulo (m) of BigInteger type
* @return a^b (mod m)
*/
static BigInteger test(BigInteger a, BigInteger b, BigInteger value){
BigInteger finalNumber = BigInteger.ONE;
BigInteger remainder = a.mod(value);
String binaryOfb = getBinary(b);
int len = binaryOfb.length() - 1;
for(int i=0; i <= len; i++){
if(binaryOfb.charAt(len - i) == '1')
finalNumber = finalNumber.multiply(remainder);
remainder = remainder.multiply(remainder).mod(value);
}
return finalNumber.mod(value);
}
/**
* Converts decimal number to binary number.
* @param number is decimal value which will be converted.
* @return Binary Number in the form of String
*/
private static String getBinary(BigInteger number){
String binaryNumber = "";
while(!number.equals(BigInteger.ZERO)){
binaryNumber = number.mod(BigInteger.TWO) + binaryNumber;
number = number.divide(BigInteger.TWO);
}
return binaryNumber;
}
}