-
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
Showing
6 changed files
with
323 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,36 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
|
||
def nwd_expanded(j, k): | ||
assert 0 <= j < k | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
|
||
def zad1(): | ||
j, k = 57, 93 | ||
result = nwd(57, 93) | ||
print(f"nwd({j}, {k}) = {result}") | ||
d, s, t = nwd_expanded(57, 93) | ||
assert (j * s + k * t) == result | ||
print(f"{s=}, {t=}") | ||
|
||
|
||
def main(): | ||
zad1() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,47 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
|
||
def nwd_expanded(j, k): | ||
assert 0 <= j < k | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def zad2(): | ||
exercises = { | ||
"a": [17, 101], | ||
"b": [357, 1234], | ||
"c": [3125, 9987], | ||
} | ||
|
||
for exercise, values in exercises.items(): | ||
a, n = values[0], values[1] | ||
reverse_a = multiplicative_inverse(a, n) | ||
print(f"{exercise}: {a=:5}, {n=:5}, result={reverse_a:5}") | ||
|
||
|
||
def main(): | ||
zad2() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,50 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
|
||
def nwd_expanded(j, k): | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def chi(a1, a2, a3, m1, m2, m3): | ||
M = m1 * m2 * m3 | ||
M1 = M // m1 | ||
M2 = M // m2 | ||
M3 = M // m3 | ||
print(f"{M=}, {M1=}, {M2=}, {M3=}") | ||
y1 = multiplicative_inverse(M1, m1) | ||
y2 = multiplicative_inverse(M2, m2) | ||
y3 = multiplicative_inverse(M3, m3) | ||
print(f"chi^(-1)(x) = {a1}*{y1}*{M1}, {a2}*{y2}*{M2}, {a3}*{y3}*{M3} % {M}") | ||
return ((a1 * M1 * y1) + (a2 * M2 * y2) + (a3 * M3 * y3)) % M | ||
|
||
|
||
def zad3(): | ||
print(chi(2, 2, 3, 3, 5, 7)) | ||
|
||
|
||
def main(): | ||
zad3() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,50 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
|
||
def nwd_expanded(j, k): | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def chi(a1, a2, a3, m1, m2, m3): | ||
M = m1 * m2 * m3 | ||
M1 = M // m1 | ||
M2 = M // m2 | ||
M3 = M // m3 | ||
print(f"{M=}, {M1=}, {M2=}, {M3=}") | ||
y1 = multiplicative_inverse(M1, m1) | ||
y2 = multiplicative_inverse(M2, m2) | ||
y3 = multiplicative_inverse(M3, m3) | ||
print(f"chi^(-1)(x) = {y1}*{a1}, {y2}*{a2}, {y3}*{a3}") | ||
return ((a1 * M1 * y1) + (a2 * M2 * y2) + (a3 * M3 * y3)) % M | ||
|
||
|
||
def zad4(): | ||
print(chi(12, 9, 23, 25, 26, 27)) | ||
|
||
|
||
def main(): | ||
zad4() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,50 @@ | ||
# Aleksander Kluczka | ||
|
||
|
||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
|
||
def nwd_expanded(j, k): | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def chi(a1, a2, m1, m2): | ||
M = m1 * m2 | ||
M1 = M // m1 | ||
M2 = M // m2 | ||
y1 = multiplicative_inverse(M1, m1) | ||
y2 = multiplicative_inverse(M2, m2) | ||
return ((a1 * M1 * y1) + (a2 * M2 * y2)) % M | ||
|
||
|
||
def zad5(): | ||
a = multiplicative_inverse(13, 99) | ||
b = multiplicative_inverse(15, 101) | ||
print(f"{a=}, {b=}") | ||
result = chi(4 * a, 56 * b, 99, 101) | ||
print(f"x = {result}") | ||
|
||
|
||
def main(): | ||
zad5() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 @@ | ||
# Aleksander Kluczka | ||
|
||
import math | ||
|
||
|
||
def nwd(j, k): | ||
if j == 0: | ||
return k | ||
r = k % j | ||
return nwd(r, j) | ||
|
||
|
||
def nwd_expanded(j, k): | ||
if j == 0: | ||
return k, 0, 1 | ||
r = k % j | ||
d, xp, yp = nwd_expanded(r, j) | ||
x = yp - (k // j) * xp | ||
y = xp | ||
return d, x, y | ||
|
||
|
||
def multiplicative_inverse(a, n): | ||
d, x, y = nwd_expanded(a, n) | ||
reverse_a = x % n | ||
assert (a * reverse_a) % n == 1 | ||
return reverse_a | ||
|
||
|
||
def chi(a1, a2, m1, m2): | ||
M = m1 * m2 | ||
M1 = M // m1 | ||
M2 = M // m2 | ||
y1 = multiplicative_inverse(M1, m1) | ||
y2 = multiplicative_inverse(M2, m2) | ||
return ((a1 * M1 * y1) + (a2 * M2 * y2)) % M | ||
|
||
|
||
def square_power(a, e, n): | ||
d = 1 | ||
e_bin = f"{e:b}" | ||
s = len(e_bin) - 1 | ||
assert s <= math.log2(n) | ||
while s >= 0: | ||
d = (d * d) % n | ||
if e_bin[s - 1] == "1": | ||
d = (d * a) % n | ||
s -= 1 | ||
return d | ||
|
||
|
||
def zad6(): | ||
def lecture(): | ||
p = 23 | ||
g = 5 | ||
|
||
a = 6 | ||
A = square_power(g, a, p) | ||
|
||
b = 15 | ||
B = square_power(g, b, p) | ||
print(f"{A=}, {B=}") | ||
alicja_s = square_power(B, a, p) | ||
bob_s = square_power(A, b, p) | ||
print(f"{alicja_s=}, {bob_s=}\n") | ||
|
||
def next(): | ||
p = 12987461 | ||
g = 3606738 | ||
|
||
a = 357 | ||
b = 199 | ||
|
||
A = square_power(g, a, p) | ||
B = square_power(g, b, p) | ||
print(f"{A=}, {B=}") | ||
alicja_s = square_power(B, a, p) | ||
bob_s = square_power(A, b, p) | ||
print(f"{alicja_s=}, {bob_s=}") | ||
|
||
lecture() | ||
next() | ||
|
||
|
||
def main(): | ||
zad6() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |