-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 382cdb2
Showing
7 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include<iostream> | ||
#include<list> | ||
using namespace std; | ||
class DLP | ||
{ | ||
static long long callsToAlgo; | ||
static long long callsToDiscreetLogBruteForce; | ||
|
||
long long dot(long long a, long long b, long long N) | ||
{ | ||
return (a*b)%N; | ||
} | ||
|
||
long long poweringAlgo(long long p, long long q, long long r, long long N, bool wantDetails) | ||
{ | ||
callsToAlgo++; | ||
if (r%2==1) | ||
q=dot(q,p,N); | ||
p=dot(p,p,N); | ||
r=r/2; | ||
if(wantDetails) | ||
{ | ||
cout<<"p="<<p<<endl; | ||
cout<<"q="<<q<<endl; | ||
cout<<"r="<<r<<endl; | ||
cout<<"------------------------"<<endl; | ||
} | ||
if (r>0) | ||
return poweringAlgo(p,q,r,N,wantDetails); | ||
else | ||
return q; | ||
} | ||
|
||
long long getPower(long long p, long long k, long long N, bool wantDetails=true) | ||
{ | ||
long long res = poweringAlgo(p,1,k,N,wantDetails); | ||
if(wantDetails) | ||
{ | ||
cout<<"Number of calls to poweringAlgo="<<callsToAlgo<<endl; | ||
} | ||
return res; | ||
} | ||
|
||
bool isGenerator(long long i, long long N) | ||
{ | ||
long long count = 1; | ||
long long a = i; | ||
while(true){ | ||
a=(a*i)%N; | ||
count++; | ||
if(count==N-1){ | ||
return true; | ||
} | ||
if(a==i){ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
list<long long> getGenerator(long long Z, int num) | ||
{ | ||
list <long long> generators; | ||
for(int i=Z-1; i>=2; i--) | ||
{ | ||
if(isGenerator(i,Z)) | ||
{ | ||
cout<<i<<endl; | ||
generators.push_back(i); | ||
if(num==1) | ||
return generators; | ||
} | ||
} | ||
return generators; | ||
} | ||
|
||
long long discreetLogBruteForce(long long base, long long num, long long N, bool wantDetails) | ||
{ | ||
long long a = 1; | ||
long long temp = base; | ||
while(true) | ||
{ | ||
callsToDiscreetLogBruteForce++; | ||
a++; | ||
temp = (temp*base)%N; | ||
if(temp == num) | ||
{ | ||
if(wantDetails) | ||
cout<<"Number of steps"<<callsToDiscreetLogBruteForce; | ||
return a; | ||
} | ||
} | ||
} | ||
|
||
}; | ||
long long DLP::callsToAlgo = 0; | ||
long long DLP::callsToDiscreetLogBruteForce = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include<iostream> | ||
#include<math.h> | ||
#include "Util.CPP" | ||
using namespace std; | ||
class Primality | ||
{ | ||
Util obj; | ||
public: | ||
bool isPrimeByMRPT(long long a, long long N, bool wantDetails) | ||
{ | ||
long long n = 0; | ||
long long temp = N-1; | ||
long long pw = 0; | ||
while(temp>0) | ||
{ | ||
temp=temp/2; | ||
pw++; | ||
} | ||
long long r = (N-1)/(pow(2,pw)); | ||
long long s = (r-1)/2; | ||
if(wantDetails) | ||
{ | ||
cout<<N<<"= 1 + (2^"<<pw<<")*(2*"<<s<<"+1)"<<endl; | ||
cout<<"k+1="<<pw<<endl; | ||
cout<<"r="<<r<<endl; | ||
cout<<"This completes step 1"<<endl; | ||
cout<<"-------------------------------"<<endl; | ||
long long h = obj.getPower(a,r,N); | ||
cout<<"h="<<h; | ||
cout<<"This completes step 2"<<endl; | ||
if(h==1) | ||
{ | ||
cout<<"N is probably prime. This completes step 3 where we have to check if h=1"<<endl; | ||
return true; | ||
} | ||
else | ||
{ | ||
cout<<"h is not equal to 1. This completes step 3 where we have to check if h=1"<<endl; | ||
step4AndBeyond(h,N,n,pw,wantDetails); | ||
} | ||
} | ||
} | ||
|
||
bool step4AndBeyond(long long h, long long N, long long n, long long pw, bool wantDetails) | ||
{ | ||
if(wantDetails) | ||
{ | ||
cout<<"In the beginning of step 4-"<<endl; | ||
cout<<"h="<<h<<endl; | ||
cout<<"n="<<n<<endl; | ||
} | ||
long long gcd = obj.gcd(h-1,N); | ||
if(wantDetails) | ||
cout<<"gcd="<<gcd; | ||
if(gcd!=1) | ||
{ | ||
cout<<"N is composite"<<endl; | ||
return false; | ||
} | ||
else if(h==N-1) | ||
{ | ||
cout<<"N is probably prime"<<endl; | ||
return true; | ||
} | ||
else | ||
{ | ||
h=obj.getPower(h,2,N); | ||
n+=1; | ||
if(wantDetails) | ||
{ | ||
cout<<"At the end of step 4"<<endl; | ||
cout<<"h="<<h<<endl; | ||
cout<<"n="<<n<<endl; | ||
} | ||
if(n==pw) | ||
{ | ||
cout<<"N is composite"<<endl; | ||
return false; | ||
} | ||
else | ||
{ | ||
cout<<"Will have to go back to step 4"<<endl; | ||
cout<<"-------------------------------"<<endl; | ||
step4AndBeyond(h,N,n,pw,wantDetails); | ||
} | ||
|
||
} | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include<iostream> | ||
#include<iterator> | ||
#include<tuple> | ||
#include "Util.CPP" | ||
class RSA | ||
{ | ||
Util obj; | ||
public: | ||
long long getDecryptionKey(long long N, long long e, bool wantDetails) | ||
{ | ||
list<tuple<long long, long long>> factors = obj.factorize(N); | ||
int flag = 1; | ||
long long facts[2]; | ||
if(factors.size()==2) | ||
{ | ||
list<tuple<long long, long long>> :: iterator it; | ||
int i=0; | ||
for(it=factors.begin(); it!=factors.end(); ++it) | ||
{ | ||
if(get<1>(*it)!=1) | ||
flag=0; | ||
else | ||
facts[i]=get<0>(*it); | ||
i++; | ||
} | ||
if(flag==1) | ||
{ | ||
long long LCM = obj.lcm(facts[0]-1,facts[1]-1); | ||
e = e % LCM; | ||
for(long long x = 1; x < LCM; x++) | ||
{ | ||
if((e * x) % LCM == 1) | ||
{ | ||
cout<<"Decryption Key="<<x<<endl; | ||
return x; | ||
} | ||
} | ||
cout<<"Decryption Key=1"<<endl; | ||
return 1; | ||
} | ||
else | ||
{ | ||
cout<<"Invalid input"<<endl; | ||
} | ||
|
||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include<iostream> | ||
#include<list> | ||
#include<tuple> | ||
#include<math.h> | ||
using namespace std; | ||
class Util | ||
{ | ||
public: | ||
bool isPrime(long long N) | ||
{ | ||
long long root = int(sqrt(N)) + 1; | ||
for(long long i = 0; i <= root; i++) | ||
{ | ||
if(N%i==0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
long long gcd(long long a, long long b) | ||
{ | ||
if(b==0) | ||
return a; | ||
return gcd(b,a%b); | ||
} | ||
|
||
long long lcm(long long a, long long b) | ||
{ | ||
return (a*b)/gcd(a,b); | ||
} | ||
|
||
long long getPower(long long p, long long r, long long N, long long q=1) | ||
{ | ||
if (r%2==1) | ||
q=(q*p)%N; | ||
p=(p*p)%N; | ||
r=r/2; | ||
if (r>0) | ||
return getPower(p,r,N,q); | ||
else | ||
return q; | ||
} | ||
|
||
list<tuple<long long, long long>> factorize(long long N) | ||
{ | ||
list<tuple<long long, long long>> factors; | ||
long long i = 2; | ||
while(N>0) | ||
{ | ||
long long power = 0; | ||
while(N%i==0) | ||
{ | ||
power++; | ||
N=N/i; | ||
} | ||
if(power!=0) | ||
factors.push_back(make_tuple(i, power)); | ||
} | ||
return factors; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include<iostream> | ||
using namespace std; | ||
void main() | ||
{ | ||
int choice=0; | ||
cout<<"This is a repository of several algorithms from IDC207 course."<<endl; | ||
cout<<"1.Discreet log and powers in multiplicative group"<<endl; | ||
cin>>choice; | ||
switch (choice) | ||
{ | ||
case 1: | ||
|
||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#IDC207 | ||
A repository of implementations of various algorithms taught in IDC207 course at IISERM. |