-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.lua
94 lines (86 loc) · 2.69 KB
/
crypto.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function euclidean_algorithm(num1,num2)
if b>0 then
-- local remainder= num1 % num2
return euclidean_algorithm(b, num1 % num2)
end
return num1
end
gcd=euclidean_algorithm
function least_common_multiple(a,b)
local min= math.min(a,b) --(a > b) and b or a; -- a > b ? b : a
local max= math.max(a,b) --(a > b) and a or b; -- a > b ? a : b
return (max / gcd(min,max)) * min
end
lcm=least_common_multiple
function modular_extended_euclidean_algorithm(a,b)
local min=math.min(a,b)
local max=math.max(a,b)
local function inner_extended_euclidean_algorithm(c,d,e,f)
local coefficient=math.floor(d/c)
if e==nil or f==nil then
e=0;
f=1;
end
if d%c~=0 and (( e - (f * coefficient) ) % max)~=0 then
print("Original: "..tostring(d)..
" = "..tostring(coefficient)..
"("..tostring(c)..") + "..
tostring(d%c)
)
print("2 Answers Ago: "..tostring(e))
print("1 Answer Ago: "..tostring(f))
print("Coefficient: floor("..tostring(d)..
"/"..tostring(c)..
") = "..tostring(coefficient)
)
print("Original Max Number: "..tostring(max))
print("Inverse: "..tostring(e)..
" - "..tostring(f)..
"("..tostring(coefficient)..") mod "..tostring(max)..
" = "..tostring(e-(f*coefficient))..
" mod "..tostring(max).." = "..tostring(( e - (f * coefficient) ) % max).."\n"
)
return inner_extended_euclidean_algorithm(d%c,c,f,( e - (f * coefficient) ) % max)
else
return f
end
end
return inner_extended_euclidean_algorithm(min,max)
end
emodgcd=modular_extended_euclidean_algorithm
function extended_euclidean_algorithm(a,b)
local min=math.min(a,b)
local max=math.max(a,b)
local function inner_extended_euclidean_algorithm(c,d,e,f)
local coefficient=math.floor(d/c)
if e==nil or f==nil then
e=0;
f=1;
end
if d%c~=0 and ( e - (f * coefficient) )~=0 then
print("Original: "..tostring(d)..
" = "..tostring(coefficient)..
"("..tostring(c)..") + "..
tostring(d%c)
)
print("2 Answers Ago: "..tostring(e))
print("1 Answer Ago: "..tostring(f))
print("Coefficient: floor("..tostring(d)..
"/"..tostring(c)..
") = "..tostring(coefficient)
)
print("Inverse: "..tostring(e)..
" - "..tostring(f)..
"("..tostring(coefficient)..")"..
" = "..tostring(e-(f*coefficient)).."\n"
)
return inner_extended_euclidean_algorithm(d%c,c,f,( e - (f * coefficient) ))
else
return f
end
end
return inner_extended_euclidean_algorithm(min,max)
end
egcd=extended_euclidean_algorithm
print("Modular Inverse Euclidean Algorithm answer: "..tostring(emodgcd(26,15)))
print("Inverse Euclidean Algorithm answer: "..tostring(egcd(26,15)))