mirror of
https://github.com/zach-sb/SENG2250_Assignment3.git
synced 2025-11-09 04:47:36 +11:00
Bigint
This commit is contained in:
parent
a0ee0db03b
commit
e5967e6e7a
43
Q2Tools.java
43
Q2Tools.java
@ -26,36 +26,33 @@ public class Q2Tools {
|
||||
* @param B
|
||||
* @param C
|
||||
*/
|
||||
public void fastModularExpon(int A, int B, int C)
|
||||
public BigInteger fastModularExpon(String base, String exponent, String modulus)
|
||||
{
|
||||
String bInBinary = Integer.toBinaryString(B);
|
||||
|
||||
double test = 1;
|
||||
// BigInteger baseVal = new BigInteger(base);
|
||||
// BigInteger exponentVal = new BigInteger(exponent);
|
||||
// BigInteger modulusVal = new BigInteger(modulus);
|
||||
|
||||
//modulusVal.equals(BigInteger.valueOf(1));
|
||||
if(modulusVal == 1)
|
||||
{
|
||||
int k = 0;
|
||||
//Loop through the Binary String, starting from the right
|
||||
for(int i = bInBinary.length()-1; i >= 0; i--)
|
||||
return BigInteger.valueOf(0);
|
||||
}
|
||||
|
||||
BigInteger temp = new BigInteger("1");
|
||||
|
||||
while(exponentVal>0)
|
||||
{
|
||||
|
||||
if(bInBinary.charAt(i)=='1')
|
||||
if((exponentVal & 1)==1)
|
||||
{
|
||||
System.out.println("Loop: "+k);
|
||||
double step1 = Math.pow(2, k);
|
||||
System.out.println("STEP1: "+step1);
|
||||
|
||||
double step2 = Math.pow(A, step1)%C;
|
||||
System.out.println("STEP2: "+step2);
|
||||
test= test*step2;
|
||||
// temp = (temp * baseVal) % modulusVal;
|
||||
temp = temp.multiply(BigInteger.valueOf(baseVal));
|
||||
temp = temp.mod(BigInteger.valueOf(modulusVal));
|
||||
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
System.out.println(test);
|
||||
|
||||
System.out.println(Math.pow(5, 64)%19);
|
||||
exponentVal = exponentVal >> 1;
|
||||
baseVal = (baseVal*baseVal) % modulusVal;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user