Added function
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
ec4a4773cc
commit
47a90f46a9
@ -34,8 +34,9 @@ def main
|
||||
puts "Ciphers: #{c1} + #{c2}"
|
||||
|
||||
# ALICE
|
||||
xc1 = wielokrotnoscPunktu(a,b,p,x,c1[0],c1[1])
|
||||
pmd = sumaPunktow(a,b,p,c2[0],c2[1],xc1[0],-xc1[1])
|
||||
#xc1 = wielokrotnoscPunktu(a,b,p,x,c1[0],c1[1])
|
||||
#pmd = sumaPunktow(a,b,p,c2[0],c2[1],xc1[0],-xc1[1])
|
||||
pmd = deSzyfrowanieElGamala(a,b,p,c1[0],c1[1],c2[0],c2[1],x)
|
||||
d = algorytmDeKodowania(a,b,p,pmd[0],pmd[1],n,u)
|
||||
puts "Dekodowana wiadomość #{d.inspect}"
|
||||
end
|
||||
|
37
module.rb
37
module.rb
@ -445,37 +445,26 @@ end
|
||||
# Oblicza wartość pierwiastka z dużych
|
||||
# liczb
|
||||
#
|
||||
# Źródło: https://codegolf.stackexchange.com/questions/85555/the-fastest-square-root-calculator
|
||||
# Źródło: https://stackoverflow.com/questions/8226087/how-do-i-get-math-sqrt-to-return-a-bignum-and-not-a-float
|
||||
#################################################################################
|
||||
def mysqrt(n)
|
||||
highest = 1
|
||||
sqrt_highest = 1
|
||||
while highest < n
|
||||
highest <<= 2
|
||||
sqrt_highest <<= 1
|
||||
def mysqrt(x)
|
||||
return 0 if x==0
|
||||
m=x
|
||||
p=x
|
||||
loop do
|
||||
r=(m+p/m)/2
|
||||
return m if m<=r
|
||||
m=r
|
||||
end
|
||||
|
||||
n /= highest+0.0
|
||||
|
||||
result = (n/4) + 1
|
||||
result = (result/2) + (n/(result*2))
|
||||
result = (result/2) + (n/(result*2))
|
||||
|
||||
return result*sqrt_highest
|
||||
end
|
||||
|
||||
#################################################################################
|
||||
# Funkcja liczenieOrd(p)
|
||||
#
|
||||
# Oblicza wartość ord dla podanych p.
|
||||
# Wynik jest zaokrąglany do podłogi, jeżeli chodzi o `mysqrt(p).to_i`
|
||||
#
|
||||
# Np.
|
||||
# 2 * mysqrt(8) => 5.68756145526057
|
||||
# (2 * mysqrt(8)).to_i => 5
|
||||
#################################################################################
|
||||
def liczenieOrd(p)
|
||||
ord = (p + 1 - (2 * mysqrt(p)).to_i ) % p
|
||||
ord = (p + 1 - (2 * mysqrt(p))) % p
|
||||
return ord
|
||||
end
|
||||
|
||||
@ -551,3 +540,9 @@ def szyfrowanieElGamala(m,n,u,a,b,p,px,py,qx,qy)
|
||||
puts "Wiadomość na prostej jako punkt #{c.inspect}"
|
||||
return c
|
||||
end
|
||||
|
||||
def deSzyfrowanieElGamala(a,b,p,c1x,c1y,c2x,c2y,x)
|
||||
xc1 = wielokrotnoscPunktu(a,b,p,x,c1x,c1y)
|
||||
pmd = sumaPunktow(a,b,p,c2x,c2y,xc1[0],-xc1[1])
|
||||
return pmd
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user