Answer sheet of Mathematics for Cryptography
Class date | November 16-27, 2019 |
---|---|
Signature | |
Exam given by | Prof. Z¨ulf¨ukar SAYGI |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Total |
---|---|---|---|---|---|---|---|---|---|---|
Instructions
- As this is an exam, you are supposed to complete the problems by yourself.
- You can use all kind of materials and you are welcome to ask me questions.
- The deadline is Wednesday 10 am (November 27).
- Show all your work to get complete score.
- Please send your exam paper by an email to sor give it directly to me
at the end of the class. - Good Luck...
Questions and Answers
1. Use the Euclidean algorithm to find the greatest common divisor of 2613 and 2171.
Code for solving the greatest common factor 1
int gcd(int a,int b)//Recursive
{
return b==0?a:gcd(b,a%b);
}
Code for solving the greatest common factor 2
int gcd(int a,int b) //Euclidean algorithm
{ //cycle
int t=a%b;
while(t!=0)
{
a=b;
b=t;
t=a%b;
}
return b;
}
Full version of the code to solve the greatest common factor
#include<iostream>
#include<cstdio>
using namespace std;
int gcd(int a,int b);
int gcd(int a,int b) //Euclidean algorithm
{
int t=a%b;
while(t!=0)
{
a=b;
b=t;
t=a%b;
}
return b;
}
int main(){
int m, n;
scanf("%d %d", &m, &n);
int gcd_m_n;
gcd_m_n = gcd(m, n);
printf("The greatest common divisor of %d and %d is %d", m, n, gcd_m_n);
getchar();
return 0;
}
The greatest common divisor of 2613 and 2171 is 13.
2.Use the extended Euclidean algorithm to find integers (x) and (y) satisfying that
inline void exgcd(int a,int b)
{
if (b)
{
exgcd(b,a%b);
int k=x;
x=y;
y=k-a/b*y;
}
else y=(x=1)-1;
}
#include<iostream>
#include<cstdio>
using namespace std;
int x, y;
inline void exgcd(int a,int b)
{
if (b)
{
exgcd(b,a%b);
int k=x;
x=y;
y=k-a/b*y;
}
else y=(x=1)-1;
}
int main(){
int m, n;
scanf("%d %d", &m, &n);
exgcd(m, n);
printf("gcd(a,b)=ax+by, a= %d, b= %d, x= %d, y=%d", m, n, x, y);
getchar();
return 0;
}
(gcd(a,b)=ax+by, a= 2613, b= 2171, x= -54, y=65.)
3. Is there any integers (x) and (y) satisfying the equation (1 = x · 14651 + y · 30758)
#include<iostream>
#include<cstdio>
using namespace std;
int x, y;
int gcd(int a,int b);
int gcd(int a,int b) //Euclidean algorithm
{
int t=a%b;
while(t!=0)
{
a=b;
b=t;
t=a%b;
}
return b;
}
inline void exgcd(int a,int b)
{
if (b)
{
exgcd(b,a%b);
int k=x;
x=y;
y=k-a/b*y;
}
else y=(x=1)-1;
}
int main(){
int a, b, c;
cin>>a>>b>>c;
int r = gcd(a,b);
if( c%r != 0){
printf("No integer solution.");
return 0;
}
exgcd(a, b);
x = x*c/r;
y = y*c/r;// x=x1+b/r*t , y=y1-a/r*t , t is an integer.
cout<<"General solution:"<<x<<"+"<<b/r<<"*t;"<<" "<<y<<"-"<<a/r<<"*t"<<" t is an integer."
getchar();
return 0;
}
No integer solution.
4. Determine all integers x such that
Easily accessible from the procedure in question 1. 121 144 169 Prime to each other can use Chinese Remainder Theorem.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
int a1,a2,a3,b1,b2,b3;
int m;
int exgcd(int a,int b,int &x,int &y){
if(!b){
x=1;y=0;
return a;
}
int t=exgcd(b,a%b,y,x);
y-=a/b*x;
return t;
}
int inv(int a,int b){//Solving Inverse Elements
int x=0,y=0;
int t=exgcd(a,b,x,y);
if(t!=1) return -1;
else return((x+b)%b);
}
int main(){
cin>>a1>>b1>>a2>>b2>>a3>>b3;//a 是'mod'字符后的数字,是121 144 169 b是 'mod'字符前的数字 是1 2 3
m=a1*a2*a3;
int x_min = (m/a1*inv(m/a1,a1)*b1+m/a2*inv(m/a2,a2)*b2+m/a3*inv(m/a3,a3)*b3)%m;
cout<<"The above algorithm can find the smallest integer solution x is "<<x_min<<"."<<endl;
cout<<"General solutions x are "<<x_min<<" + z * "<<m<<".";
getchar();
return 0;
}
The above algorithm can find the smallest integer solution x is 368930.
General solutions are (368930+z*2944656) z is an integer.
5. Compute (2^{1000000} mod 11) by using Fermat’s Little Theorem.
Fermat’s Little Theorem. :
If (p) is a prime and (gcd(a, p) = 1), then (a^{p-1} ≡ 1 mod p).
(11) is a prime and (gcd(2,11) = 1), then (2^{11-1}=2^{10} ≡ 1 mod 11).
(11) is a prime and (gcd(2^{10} ,11) = 1), then (2^{10*(11-1)}=2^{100} ≡ 1 mod 11).
(11) is a prime and (gcd(2^{100} ,11) = 1), then (2^{100*(11-1)}=2^{1000} ≡ 1 mod 11).
(11) is a prime and (gcd(2^{1000} ,11) = 1), then (2^{1000*(11-1)}=2^{10000} ≡ 1 mod 11).
(11) is a prime and (gcd(2^{10000} ,11) = 1), then (2^{10000*(11-1)}=2^{100000} ≡ 1 mod 11).
(11) is a prime and (gcd(2^{100000} ,11) = 1), then (2^{100000*(11-1)}=2^{1000000} ≡ 1 mod 11).
(2^{1000000} mod 11=1)
6. Compute (2^{1000000} mod 77) by using Euler’s Theorem.
$77=7*11,7 and 11 $ are prime.
(phi(77)=phi(7)*phi(11)=60)
(ecause) Euler’s Theorem:
(gcd(a,m)=1)(Rightarrow) (a^{phi(m)}≡1 mod m)
(Rightarrow) (a^x≡a^{x \% phi(m)} mod m)
( herefore)(2^{1000000} ≡ 2^{1000000 \% 60=40} mod 77)
( herefore) (2^{1000000} mod 77=2^{40})
7. Compute (2^{1000000} mod 154) by using Chinese Remainder Theorem.
Least common multiple 2, 7, 11
(154=2*7*11)
(2^{1000000} mod 154=100)
8. Find $5^{1234}mod 1453 $ using square-and-multiply algorithm.
typedef long long ll;
ll mod;
ll qpow(ll a, ll n)//a^n % mod
{
ll re = 1;
while(n)
{
if(n & 1)
re = (re * a) % mod;
n >>= 1;
a = (a * a) % mod;
}
return re % mod;
}
#include<iostream>
using namespace std;
typedef long long ll;
ll mod;
ll qpow(ll a, ll n)// a^n % mod
{
ll re = 1;
while(n)
{
if(n & 1)//Determine if the last bit of n is 1
re = (re * a) % mod;
n >>= 1;
a = (a * a) % mod;
}
return re % mod;
}
int main(){
int a, n;
cin>>a>>n>>mod;
cout<<qpow(a,n);
getchar();
return 0;
}
(5^{1234}mod 1453 = 1342)
9. Find the complete factorization of (x^9 - x in mathbb{F} _3[x]).
(x^9 - x =x(x+1)(x-1)(x^2+1)(x^4+1))