Beruflich Dokumente
Kultur Dokumente
Preparations:
(1) visit
http://www.math.wvu.edu/~hjlai/Teaching/Math373-578/Math373_Matlab/
1
Part 1: Number Theory Basics
(1) Factor 123456 into primes.
factor(123456)
gcd(x,y)
[a,b,c]=gcd(x,y)
2
Part 2: Operations involving integers modulo m
(1) Given m > 1 and a ∈ Z, find b ∈ Z with 0 ≤ b < m such that a ≡ b (mod m).
mod (a,m)
(2) Given m > 1 and a, b ∈ Z, find c ∈ Z with 0 ≤ c < m such that a + b ≡ c (mod m). (Do
the same for subtractions and multiplications)
mod (a+b,m)
(3) Find multiplicative inverse of a (mod m) (assuming that we have already known that a and
m are relatively prime).
Example: Find the multiplicative inverse of 8787 (mod 91919).
(4) Find multiplicative inverse of a (mod m) (assuming that we do not know if a and m are
relatively prime).
Example: Determine if 23456 has an inverse mod 987654, if it does, find it.
This means the gcd(23456, 987654) = 2, and so the inverse does not exist.
Example: Determine if 23456 has an inverse mod 987651, if it does, find it.
This means the gcd(23456, 987651) = 1 = 256892 · 23456 + (−6101) · 987651, and so 23456−1 ≡
256892 (mod 987651).
3
>> powermod(234, 567, 9871)
ans =
5334
4
allshift(’wklvvkrxogehtxlwhhdvb’)
wklvvkrxogehtxlwhhdvb
xlmwwlsyphfiuymxiiewc
ymnxxmtzqigjvznyjjfxd
znoyynuarjhkwaozkkgye
aopzzovbskilxbpallhzf
bpqaapwctljmycqbmmiag
cqrbbqxdumknzdrcnnjbh
drsccryevnloaesdookci
estddszfwompbfteppldj
ftueetagxpnqcgufqqmek
guvffubhyqordhvgrrnfl
hvwggvcizrpseiwhssogm
iwxhhwdjasqtfjxittphn
jxyiixekbtrugkyjuuqio
kyzjjyflcusvhlzkvvrjp
lzakkzgmdvtwimalwwskq
mabllahnewuxjnbmxxtlr
nbcmmbiofxvykocnyyums
ocdnncjpgywzlpdozzvnt
pdeoodkqhzxamqepaawou
qefppelriaybnrfqbbxpv
rfgqqfmsjbzcosgrccyqw
sghrrgntkcadpthsddzrx
thisshouldbequiteeasy
uijttipvmecfrvjuffbtz
vjkuujqwnfdgswkvggcua
A study of the output indicates that the plain text should be ’thisshouldbequiteeasy’ or ’this
should be quite easy’
5
and =
9
>> mod(-9*7, 26)
ans =
15
(Step 2) Knowing that the decrypt function is x ≡ 9y + 15, we can decrypt the message by
6
Part 3: Numbers with different bases
(1) Converting and Base-b number to a base-10 number
To convert a base-b number n = (dk−1 dk−2 · · · d1 d0 )b to base-10, by definition, the answer is
Example Convert a number-26 number (HP AC)26 to base-10. We can first get the corre-
sponding numerical values H = 7, P = 15, A = 0 and C = 2, and get the answer by
>> n = 133174
n =
133174
>> d0 = mod(133174, 26)
d0 =
2
>> n1 = (n - 2)/26
n1 =
5122
>>d1 = mod(n1, 26)
d1 =
0
>> n2 = (n1 - 0)/26
n2 =
197
>>d2 = mod(n2, 26)
d2 =
15
>> n3 = (n2 - 15)/26
n3 =
7
>>d3 = mod(n3, 26)
d3 =
7
7
to first convert the base-b numbers to base-10, and use matlab to do the operations, then use
matlab to convert the answers back to base-b.
Example Multiply HE by IS in Z26 .
Step 1: Convert HE = 7(26) + 4 = 186 and IS = 8(28) + 18 = 226.
Step 2: Compute the base-10 multiplication.
Step 3: Convert the answer back to Base-26.
8
Part 4: Discrete Log and RSA
(1) Find a Primitive Root
>> primitiveroot(65537)
ans =
3
Thus 3 is a primitive root for 65537. (Remark: the function ”primitiveroot” finds the small-
est primitive root of the input number.)
For n = 1:12;
a = powermod(2,n,13);
if a == 7; disp(n);
end
end
n = 11
>> powermod(2,11,13)
ans =
7
>> d=201
d =
201
9
>> p=263
p =
263
>> c = [246 18 156 0 256 127 18 156 96 256 235 0 132 68]
c =
Columns 1 through 5
246 18 156 0 256
Columns 6 through 10
127 18 156 96 256
Columns 11 through 14
235 0 132 68
>> m = powermod(c, d, p)
m =
Columns 1 through 5
19 17 4 0 18
Columns 6 through 10
20 17 4 8 18
Columns 11 through 14
11 0 13 3
246201 ≡ 19; 18201 ≡ 17; 156201 ≡ 4; 0201 ≡ 0; 256201 ≡ 18; 127201 ≡ 20; 18201 ≡ 17;
156201 ≡ 4; 96201 ≡ 8; 256201 ≡ 18; 235201 ≡ 11; 0201 ≡ 0; 132201 ≡ 13; 68201 ≡ 3.
and so the cipher text is (19, 17, 4, 0, 18, 20, 17, 4, 8, 18, 11, 0, 13, 3), which means, with Z26 al-
phabet, treasure island.
10
yy =
633
Thus x ≡ 232 ≡ 311 and y ≡ 2153 ≡ 633 (mod 907), and so the common key can be computed
by k = xy (mod p)
>> k = powermod(xx,y,p)
k =
121
or by k = y x (mod p)
>> k = powermod(yy,x,p)
k =
121
(6) Example: RSA with a single letter plain text Let p = 167, q = 547, n = 91349,
e = 5 and cipher text c ≡ 88291 (mod n). To find plain text m, we first find φ(n) = 90636,
>> n = 91349
n =
91349
>> eulerphi(n)
ans =
90636
11
and compute (using Euclidean Algorithm)
>> [a,b,c] = gcd(5, 90636)
a =
1
b =
-18127
c =
1
12
Thus 1 = gcd(296123, 465336) = (11)(296123) + (−7)(465336), and so e ≡ 11. Then compute
ce (mod n),
>> c = 369510
c =
369510
>> e = 11
e =
11
>> powermod(c,e,n)
ans =
10101
(8) Example: (ElGamal Cipher) Suppose A and B are using the ElGamal public-key
cipher to communicate with p = 1213 and e = 15. Suppose A sends a cipher tex c = (661, 193)
to B. Find the plain text m.
Solution: Here t = 193 and r = 661. Compute
>> p = 1213
p =
1213
>> t=193
t =
193
>> r=661
r =
661
>> e = 15
e =
15
>> r1 = powermod(r, -e, p)
r1 =
924
>> mod(t*r1, p)
ans =
21
Therefore,
m =≡ tr−e ≡ 193 · 924 ≡ 21 (mod 1213).
(9) Example: (ElGamal signature scheme) Bob receives m = 121 from Alice, together
with
(i) sigk (m, r) = (h, g) = (480, 532), and
(ii) sigk (m, r) = (h, g) = (480, 21), Bob downloads Alice’s KE = (p, a, b) = (641, 3, 88). Which
signature should Bob accepts? which one he should reject?
13
Solution: (i) For sigk (m, r) = (h, g) = (480, 532), Bob recognizes that b = 88, h = 480, and
g = 532. He computes
>> p = 641
p =
641
>> a = 3
a =
3
>> b = 88
b =
88
>> h = 480
h =
480
>> g = 532
g =
532
>> d = mod(powermod(b, h, p) * powermod(h, g, p), p)
d =
191
>> m = 121
m =
121
>> s = powermod(a, m, p)
s =
300
>> g = 21
g =
21
>> d = mod(powermod(b, h, p) * powermod(h, g, p), p)
d =
300
>> s = powermod(a, m, p)
s =
300
14
Part 5: Chinese Remainder Theorem
(1) Chinese Remainder Theorem Applications
Example: Find a solution x for the system
(
x ≡ 3 (mod 5)
.
x ≡ 2 (mod 7)
15
Part 6: Operations involving matrices modulo m
(1) Creating a Matrix; Computing Inverse of a Matrix mod 26
>> M = [1 2 3; 4 5 6; 7 8 10]
M =
1 2 3
4 5 6
7 8 10
(B) Example Compute the inverse of M mod 26. To do that, we first compute the inverse
of M as it is a real number matrix. Then converted it to an integer valued matrix. In the last
step, we take mod 26 in every entry, as shown below.
Step 1: Compute the inverse of M . (We need the comment ”format rat;” to out put the
rational numbers. Without it, the output might be decimals, and the next step will not work
out easily.)
>> format rat;
>> Minv = inv(M)
Minv =
-2/3 -4/3 1
-2/3 11/3 -2
1 -2 1
Step 2: Need to rationalize this matrix before we take modulo m. Note that every fractional
entry of Minv has a denominator 3. Multiply everything by 3 to make it an integer valued
matrix. (You can also multiply 27 here as 27 ≡ 1 (mod 26)).
>> M1=(Minv*3)
M1 =
-2 -4 3
-2 11 -6
3 -6 3
Step 3: Find the inverse of M (mod 26) by modifying M 1. Note that 3−1 ≡ 9 (mod 26)
>> M2=round(mod(M1*9, 26))
M2 =
8 16 1
8 21 24
1 24 1
Then M −1 = M 2.
16
(2) Matrix Computations in matlab
Example 1 Create a matrix
1 13 2
A= 3 4 1 .
−2 16 1
>> A = [1 13 2; 3 4 1; -2 16 1]
A =
1 13 2
3 4 1
-2 16 1
Example 2 Define a row vector (1, 2, 4, 5, 6).
>> x = [1, 2, 4, 5, 6]
x =
1 2 4 5 6
Example 3 Define a column vector (1, 2, 4, 5, 6)T .
>> y = [1; 2; 4; 5; 6]
y =
1
2
4
5
6
Example 3 Compute the matrix multiplication
1 13 2 2 7 1
3 4 1 0 1 12 .
−2 16 1 −2 0 1
We can first define these matrix and then multiply them together.
>> A = [1 13 2; 3 4 1; -2 16 1]
A =
1 13 2
3 4 1
-2 16 1
>> B = [2 7 1; 0 1 12; -2 0 1]
B =
2 7 1
0 1 12
-2 0 1
>> C = A*B
If we want to compute the matrix multiplication in Zm , for example, computing AB in Z26 ,
then we use the following comment.
>> mod(A*B, 26)
17
Part 7: Ciphers using blocks of size larger than 1
(1) Example: (encryption using blocks of size 3, or trigraphs)
Choice of Parameters: Let p = 281, q = 167. Then n = 46927. Pick e = 39423. Thus
the enciphering key is (46927, 39423) and the deciphering key is (46927, 26767). In order to use
the English Alphabet in the messages, Bob also tells Alice to use base-N representation of the
numerics with N = 26. Alice can key in the encryption keys.
>> n = 46927
n =
46927
>> e = 39423
e =
39423
Bob needs to compute the deciphering key:
>> f = eulerphi(n)
f =
46480
>> d = powermod(e, -1, f)
d =
26767
Encoding Process: To send a message Y ES to Bob, Alice first finds the numerical equivalent
of Y ES = (24 4 18)26 7→ P = 24(26)2 + 4(26) + 18 = 16346 (in base-10).
>> m = 24*(26)^2 + 4*(26) + 18
m =
16346
Next, Alice computes C = P m = 1634639423 ≡ 21166 (mod 46927) in Zn :
>> c= powermod(m,e, n)
c =
21166
Alice then converts C to Base-26 numbers (You can use matlab to combine all of the
following steps. I write down these steps for you to see what we are actually doing).
Find the first digit:
>> mod(c, 26)
ans =
2
Find the second digit
>> c = (c-2)/26
c =
814
>> mod(c, 26)
ans =
8
18
Find the third digit
>> c = (c-8)/26
c =
31
>> mod(c, 26)
ans =
5
(As long as the current value of c = 31 > 26, Alice needs to continue) to find the four digit.
Find the third digit
>> c = (c-5)/26
c =
1
Alice now stops as the current value of c = 1 < 26. This is obtained:
c = 21166 = (1582)26 7→ BFIC,
and so Alice sends BFIC to Bob.
19
Part 8: Elliptic Curve Computations
(1) Graph the elliptic curve y 2 = x3 − x over the real number field R.
20
14 9
ans =
15 4
ans =
16 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 4 9 16 8 2 15 13 13 15 2 8 16 9 4 1
21
ans =
14 4
ans =
15 7
ans =
16 15
E = {(0, 0), (1, ±6), (3, ±8), (4, 0), (6, ±1), (11, ±4), (13, 0), (14, ±2), (16, ±7), O}.
(3) Add points (1, 3) + (3, 5) and (1, 3) + O on the curve y 2 = x3 + 24x + 13 (mod 29). (Recall
that O represent the infinity).
(4) Computing kP . For P = (1, 3) and an integer k > 0, we are to compute kP on the curve
y 2 = x3 + 24x + 13 (mod 29).
If we want to compute k · P for one value of k, say k = 7, then we can do the following.
Therefore,
2P = (11, 10)
3P = (23, 28)
22
4P = (0, 10)
5P = (19, 7)
6P = (18, 19)
7P = (15, 6)
8P = (20, 24)
Therefore, the answer is O = (inf, inf). Note that the 0 in the denominators is a 0 mod 29.
(For example, the denominator could have been 58, as an integer).
(6) Computing nP by the double-and-add algorithm for the elliptic curve E below over F =
Z1999 :
y 2 = x3 + 1828x + 1675,
with P = (1756, 348) and n = 11.
Iteration:
(Step 1) n = 11 is odd, R := R + Q = P + O = P = (1756, 348), Q := 2Q = (1526, 1612).
Update n := b11/2c = 5.
Update n := b5/2c = 2.
23
>> multell([1657,1579],2,1828, 1675, 1999)
ans =
1849 225
Update n := b2/2c = 1.
Update n := b1/2c = 0. (Since we know that n = 1 after the updating, we will stop at the next
step and so there is no need to actually compute 2Q.)
24