Updated
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
f2aae14003
commit
8b15630942
216
6/krzywa.rb
216
6/krzywa.rb
@ -9,114 +9,6 @@
|
||||
########################################
|
||||
|
||||
load '../module.rb'
|
||||
require 'thread'
|
||||
require 'thwait'
|
||||
|
||||
def returnRownanie(a,b,p)
|
||||
puts
|
||||
puts "Równanie krzywej jest równe: " + "Y^2 = X^3+" + a.inspect + "X+" + b.inspect + " mod "+ p.inspect
|
||||
puts
|
||||
end
|
||||
|
||||
def delta(a,b,p)
|
||||
d = ((4 * betterExponentiation(a,3,p) % p) + (27 * betterExponentiation(b,2,p) % p)) % p
|
||||
return d
|
||||
end
|
||||
|
||||
def rownanieKrzywej(a,b,p,x)
|
||||
fx = ((betterExponentiation(x,3,p) + (a * x) % p + b % p) % p) % p
|
||||
return fx
|
||||
end
|
||||
|
||||
def generatorKrzywej(p)
|
||||
a = 0
|
||||
b = 0
|
||||
|
||||
while true
|
||||
if primalityTest(p) && (p % 4 == 3)
|
||||
threads = []
|
||||
|
||||
threads << Thread.new {
|
||||
a = SecureRandom.random_number(1..p-1)
|
||||
}
|
||||
|
||||
threads << Thread.new {
|
||||
b = SecureRandom.random_number(1..p-1)
|
||||
}
|
||||
|
||||
ThreadsWait.all_waits(*threads)
|
||||
if delta(a,b,p) != 0
|
||||
returnRownanie(a,b,p)
|
||||
return a,b
|
||||
end
|
||||
else
|
||||
puts "Liczba nie spełnia wymagań"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def punktNaKrzywej(a,b,p)
|
||||
if (delta(a,b,p) != 0) && (p % 4 == 3)
|
||||
while true
|
||||
x = SecureRandom.random_number(0..p-1)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
if remSqEuler(fx,p)
|
||||
y = betterExponentiation(fx,((p+1)/4),p)
|
||||
return x,y
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def czyPunktNalezyDoKrzywej(a,b,p,x,y)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
|
||||
if fx == betterExponentiation(y,2,p)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def punktPrzeciwny(x,y)
|
||||
return x,-y
|
||||
end
|
||||
|
||||
def sumaPunktow(a,b,p,x1,y1,x2,y2)
|
||||
# 0 - element neutrany --> P + 0 = P
|
||||
if (x1 == "e" && y1 == "e" )
|
||||
return x2,y2
|
||||
elsif (x2 == "e" && y2 == "e")
|
||||
return x1,y1
|
||||
end
|
||||
# P + Q = R
|
||||
if (x1 != x2)
|
||||
lambda = (((y2 - y1) % p) * reciprocal_Phi_p((x2 - x1),p)) % p
|
||||
x3 = (betterExponentiation(lambda,2,p) - (x1 % p) - (x2 % p)) % p
|
||||
y3 = (lambda * (x1 - x3) - y1) % p
|
||||
return x3,y3
|
||||
end
|
||||
# P + -Q = 0 DZIALA
|
||||
if (x1 == x2) && (y1 == -y2)
|
||||
puts "0 - el.neutralny"
|
||||
return e,e
|
||||
end
|
||||
# P + P = 2P DZIALA
|
||||
if (x1 == x2) && (y1 == y2)
|
||||
lambda = (((3 * betterExponentiation(x1,2,p) % p + a) % p) * reciprocal_Phi_p(2 * y1,p)) % p
|
||||
x3 = (betterExponentiation(lambda,2,p) - (x1 % p) - (x2 % p)) % p
|
||||
y3 = (lambda * (x1 - x3) - y1) % p
|
||||
return x3,y3
|
||||
end
|
||||
end
|
||||
|
||||
#puts generatorKrzywej(11).inspect
|
||||
#puts punktNaKrzywej(2,7,11).inspect
|
||||
#puts czyPunktNalezyDoKrzywej(2,7,11,7,1).inspect
|
||||
#puts czyPunktNalezyDoKrzywej(2,7,11,10,10).inspect
|
||||
#puts punktPrzeciwny(2,2).inspect
|
||||
#puts sumaPunktow(2,2,17,5,1,5,1).inspect
|
||||
|
||||
##################################################
|
||||
# Zadania z konca wykladu suma punktow
|
||||
@ -133,31 +25,31 @@ end
|
||||
# 4) 2R
|
||||
#
|
||||
# Rozwiązanie:
|
||||
a = 0
|
||||
b = 1
|
||||
p = 7
|
||||
|
||||
xp = 1
|
||||
yp = 3
|
||||
|
||||
xq = 2
|
||||
yq = 4
|
||||
|
||||
xr = 6
|
||||
yr = 0
|
||||
|
||||
# 1) -Q
|
||||
puts punktPrzeciwny(xq,yq).inspect
|
||||
|
||||
# 2) R + [0,0]
|
||||
puts sumaPunktow(a,b,p,xr,yr,"e","e").inspect
|
||||
|
||||
# 3) P + Q
|
||||
puts sumaPunktow(a,b,p,xp,yp,xq,yq).inspect
|
||||
|
||||
# 4) 2R
|
||||
puts sumaPunktow(a,b,p,xp,yp,xp,yp).inspect
|
||||
##################################################
|
||||
#a = 0
|
||||
#b = 1
|
||||
#p = 7
|
||||
#
|
||||
#xp = 1
|
||||
#yp = 3
|
||||
#
|
||||
#xq = 2
|
||||
#yq = 4
|
||||
#
|
||||
#xr = 6
|
||||
#yr = 0
|
||||
#
|
||||
## 1) -Q
|
||||
#puts punktPrzeciwny(xq,yq).inspect
|
||||
#
|
||||
## 2) R + [0,0]
|
||||
#puts sumaPunktow(a,b,p,xr,yr,"e","e").inspect
|
||||
#
|
||||
## 3) P + Q
|
||||
#puts sumaPunktow(a,b,p,xp,yp,xq,yq).inspect
|
||||
#
|
||||
## 4) 2R
|
||||
#puts sumaPunktow(a,b,p,xp,yp,xp,yp).inspect
|
||||
###################################################
|
||||
|
||||
##################################################
|
||||
# Zadanie od profersora
|
||||
@ -167,35 +59,39 @@ p=118377958435707695093798149768594629271110741215253448110252554738760437826252
|
||||
px=285113634279465403319996581740169338329454608669814309137990174814243655992779447106132850
|
||||
py=598700530906084162596261101440667782569915319623798143751082061599951188013331503150304328
|
||||
|
||||
# Zadanie 1
|
||||
puts
|
||||
puts "Zadanie 1"
|
||||
puts generatorKrzywej(p).inspect
|
||||
|
||||
# Zadanie 2
|
||||
puts
|
||||
puts "Zadanie 2"
|
||||
puts punktNaKrzywej(a,b,p).inspect
|
||||
|
||||
# Zadanie 3
|
||||
puts
|
||||
puts "Zadanie 3"
|
||||
puts czyPunktNalezyDoKrzywej(a,b,p,px,py).inspect
|
||||
|
||||
# Zadanie 4
|
||||
puts
|
||||
puts "Zadanie 4"
|
||||
puts punktPrzeciwny(px,py).inspect
|
||||
|
||||
# Zadanie 5
|
||||
puts
|
||||
puts "Zadanie 1 #{generatorKrzywej(p).inspect}"
|
||||
puts "Zadanie 2 #{punktNaKrzywej(a,b,p).inspect}"
|
||||
puts "Zadanie 3 #{czyPunktNalezyDoKrzywej(a,b,p,px,py).inspect}"
|
||||
puts "Zadanie 4 #{punktPrzeciwny(px,py).inspect}"
|
||||
puts "Zadanie 5"
|
||||
q=sumaPunktow(a,b,p,px,py,px,py)
|
||||
puts "P+P=Q=" + q.inspect
|
||||
|
||||
qx=q[0].to_i
|
||||
qy=q[1].to_i
|
||||
pp=sumaPunktow(a,b,p,px,py,px,py)
|
||||
qx,qy=pp[0],pp[1]
|
||||
r=sumaPunktow(a,b,p,px,py,qx,qy)
|
||||
puts "P+Q=R=" + r.inspect
|
||||
puts "P+P=Q = #{pp.inspect}"
|
||||
puts "P+Q=R = #{r.inspect}"
|
||||
|
||||
##################################################
|
||||
#Zadania od Marcina
|
||||
puts `clear`
|
||||
p = 489198001832658669053371291694019055502521600295245000665130252689633682157211441103746103
|
||||
a = 143655542375421547495538657783935617582804745476111687935604061101439410572992748159327949
|
||||
b = 317483121238416221164186798475625852751072649807905602729473072669192329094502393489452530
|
||||
px = 214920231222257412002472184644105947527864300425093926130634470978902408198296382507987372
|
||||
py = 213782952948563488188771820496583435437623918801486982688994669228366002105132455299788553
|
||||
|
||||
puts "Zadanie1: #{generatorKrzywej(p).inspect}"
|
||||
puts "Zadanie2: #{punktNaKrzywej(a,b,p).inspect}"
|
||||
puts "Zadanie3: #{czyPunktNalezyDoKrzywej(a,b,p,px,py).inspect}"
|
||||
puts "Zadanie4: #{punktPrzeciwny(px,py).inspect}"
|
||||
puts "Zadanie5:"
|
||||
pe = sumaPunktow(a,b,p,px,py,"e","e")
|
||||
pmp = sumaPunktow(a,b,p,px,py,px,-py)
|
||||
pp = sumaPunktow(a,b,p,px,py,px,py)
|
||||
puts "P + e = #{pe.inspect}"
|
||||
puts "P + (-P) = #{pmp.inspect}"
|
||||
puts "P + P = #{pp.inspect}"
|
||||
|
||||
qx = 642259555221384310824793582418757985600466641224722883964129011723625337870528779957486982
|
||||
qy = 217544632016156259911730007881694454319320868952565819223745628076715981593127804307986145
|
||||
puts "P + Q = #{sumaPunktow(a,b,p,px,py,qx,qy)}"
|
||||
|
@ -9,127 +9,6 @@
|
||||
###################################
|
||||
|
||||
load '../module.rb'
|
||||
load '../module2.rb'
|
||||
|
||||
puts `clear`
|
||||
|
||||
def wielokrotnoscPunktu2(a,b,p,n,x,y)
|
||||
counter = n - 1
|
||||
punktQ = []
|
||||
punktP = [x,y]
|
||||
temp = [x,y]
|
||||
while counter > 0
|
||||
punktQ = sumaPunktow(a,b,p,temp[0],temp[1],punktP[0],punktP[1])
|
||||
temp = punktQ
|
||||
counter = counter - 1
|
||||
end
|
||||
return punktQ
|
||||
end
|
||||
|
||||
def wielokrotnoscPunktu(a,b,p,n,x,y)
|
||||
punktQ = [x,y]
|
||||
punktR = ["e","e"]
|
||||
|
||||
while n > 0
|
||||
if n % 2 == 1
|
||||
punktR = sumaPunktow(a,b,p,punktR[0],punktR[1],punktQ[0],punktQ[1])
|
||||
n = n - 1
|
||||
end
|
||||
punktQ = sumaPunktow(a,b,p,punktQ[0],punktQ[1],punktQ[0],punktQ[1])
|
||||
n = n / 2
|
||||
end
|
||||
return punktR
|
||||
end
|
||||
|
||||
def liczenieOrd(a,b,p)
|
||||
threads = []
|
||||
punktyNaKrzywej = Array.new
|
||||
unikalnePunktyNaKrzywej = Array.new
|
||||
|
||||
threads << Thread.new {
|
||||
counter = 10
|
||||
while counter > 0
|
||||
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
|
||||
counter = counter -1
|
||||
end
|
||||
}
|
||||
|
||||
threads << Thread.new {
|
||||
counter = 10
|
||||
while counter > 0
|
||||
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
|
||||
counter = counter -1
|
||||
end
|
||||
}
|
||||
|
||||
threads << Thread.new {
|
||||
counter = 10
|
||||
while counter > 0
|
||||
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
|
||||
counter = counter -1
|
||||
end
|
||||
}
|
||||
|
||||
ThreadsWait.all_waits(*threads)
|
||||
|
||||
punktyNaKrzywej.sort.each { |e|
|
||||
unikalnePunktyNaKrzywej.push(e) if e != unikalnePunktyNaKrzywej[-1]
|
||||
}
|
||||
|
||||
return unikalnePunktyNaKrzywej.count
|
||||
end
|
||||
|
||||
def generowanieKluczyElGamalKrzywaEliptyczna(k)
|
||||
while true
|
||||
p = generate(k)
|
||||
if (primalityTest(p)) && (p % 4 == 3)
|
||||
krzywa = generatorKrzywej(p)
|
||||
a = krzywa[0].to_i
|
||||
b = krzywa[1].to_i
|
||||
|
||||
punktyNaKrzywej = Array.new
|
||||
|
||||
punktP = punktNaKrzywej(a,b,p)
|
||||
ord = liczenieOrd(a,b,p)
|
||||
|
||||
while true
|
||||
x = SecureRandom.random_number(1..ord)
|
||||
if x < ord
|
||||
punktQ = wielokrotnoscPunktu(a,b,p,x,punktP[0],punktP[1])
|
||||
pubKey = [a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1]]
|
||||
privKey = [a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1],x]
|
||||
return a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1],x
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def algorytmKodowania(a,b,p,m,n,u)
|
||||
if (m < n) && (p > n*u)
|
||||
for i in 1..u
|
||||
x = (m * u % p) + (i % p)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
if remSqEuler(fx,p)
|
||||
y = betterExponentiation(fx,((p+1)/4),p)
|
||||
end
|
||||
end
|
||||
else
|
||||
puts "Nieprawidołowe dane"
|
||||
end
|
||||
return [x,y]
|
||||
end
|
||||
|
||||
def algorytmDeKodowania(a,b,p,x,y,n,u)
|
||||
m = (x - 1) / u
|
||||
return m
|
||||
end
|
||||
|
||||
def szyfrowanieElGamala(m,n,u,a,b,p,px,py,qx,qy)
|
||||
c = algorytmKodowania(a,b,p,m,n,u)
|
||||
puts "Wiadomość na prostej jako punkt #{c.inspect}"
|
||||
return c
|
||||
end
|
||||
|
||||
def main
|
||||
k = 30
|
||||
|
209
module.rb
209
module.rb
@ -12,6 +12,7 @@ require 'openssl'
|
||||
require 'securerandom'
|
||||
require 'prime'
|
||||
require 'thread'
|
||||
require 'thwait'
|
||||
|
||||
####################################
|
||||
# Funkcja nwd(a,b)
|
||||
@ -117,7 +118,7 @@ def betterExponentiation(x,k,n)
|
||||
if x == 0
|
||||
return 0
|
||||
end
|
||||
|
||||
|
||||
if x < n && x > 0
|
||||
b = k.to_s(2).reverse
|
||||
l = b.count "[0-1]"
|
||||
@ -251,3 +252,209 @@ end
|
||||
def generate(n)
|
||||
return `openssl prime -generate -bits '#{n}'`.gsub(/\n$/, '').to_i
|
||||
end
|
||||
|
||||
#### MODULE 2 ####
|
||||
def returnRownanie(a,b,p)
|
||||
puts
|
||||
puts "Równanie krzywej jest równe: " + "Y^2 = X^3+" + a.inspect + "X+" + b.inspect + " mod "+ p.inspect
|
||||
puts
|
||||
end
|
||||
|
||||
def delta(a,b,p)
|
||||
d = ((4 * betterExponentiation(a,3,p) % p) + (27 * betterExponentiation(b,2,p) % p)) % p
|
||||
return d
|
||||
end
|
||||
|
||||
def rownanieKrzywej(a,b,p,x)
|
||||
fx = ((betterExponentiation(x,3,p) + (a * x) % p + b % p) % p) % p
|
||||
return fx
|
||||
end
|
||||
|
||||
def generatorKrzywej(p)
|
||||
a = 0
|
||||
b = 0
|
||||
|
||||
while true
|
||||
if primalityTest(p) && (p % 4 == 3)
|
||||
threads = []
|
||||
|
||||
threads << Thread.new {
|
||||
a = SecureRandom.random_number(1..p-1)
|
||||
}
|
||||
|
||||
threads << Thread.new {
|
||||
b = SecureRandom.random_number(1..p-1)
|
||||
}
|
||||
|
||||
ThreadsWait.all_waits(*threads)
|
||||
if delta(a,b,p) != 0
|
||||
#returnRownanie(a,b,p)
|
||||
return a,b
|
||||
end
|
||||
else
|
||||
puts "Liczba nie spełnia wymagań"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def punktNaKrzywej(a,b,p)
|
||||
if (delta(a,b,p) != 0) && (p % 4 == 3)
|
||||
while true
|
||||
x = SecureRandom.random_number(0..p-1)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
if remSqEuler(fx,p)
|
||||
y = betterExponentiation(fx,((p+1)/4),p)
|
||||
return x,y
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def czyPunktNalezyDoKrzywej(a,b,p,x,y)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
|
||||
if fx == betterExponentiation(y,2,p)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def punktPrzeciwny(x,y)
|
||||
return x,-y
|
||||
end
|
||||
|
||||
def sumaPunktow(a,b,p,x1,y1,x2,y2)
|
||||
# 0 - element neutrany --> P + 0 = P
|
||||
if (x1 == "e" && y1 == "e" )
|
||||
return x2,y2
|
||||
elsif (x2 == "e" && y2 == "e")
|
||||
return x1,y1
|
||||
end
|
||||
# P + Q = R
|
||||
if (x1 != x2)
|
||||
lambda = (((y2 - y1) % p) * reciprocal_Phi_p((x2 - x1),p)) % p
|
||||
x3 = (betterExponentiation(lambda,2,p) - (x1 % p) - (x2 % p)) % p
|
||||
y3 = (lambda * (x1 - x3) - y1) % p
|
||||
return x3,y3
|
||||
end
|
||||
# P + -Q = 0 DZIALA
|
||||
if (x1 == x2) && (y1 == -y2)
|
||||
#puts "0 - el.neutralny"
|
||||
return "e","e"
|
||||
end
|
||||
# P + P = 2P DZIALA
|
||||
if (x1 == x2) && (y1 == y2)
|
||||
lambda = (((3 * betterExponentiation(x1,2,p) % p + a) % p) * reciprocal_Phi_p(2 * y1,p)) % p
|
||||
x3 = (betterExponentiation(lambda,2,p) - (x1 % p) - (x2 % p)) % p
|
||||
y3 = (lambda * (x1 - x3) - y1) % p
|
||||
return x3,y3
|
||||
end
|
||||
end
|
||||
|
||||
#### MODULE 3 ####
|
||||
def wielokrotnoscPunktu(a,b,p,n,x,y)
|
||||
punktQ = [x,y]
|
||||
punktR = ["e","e"]
|
||||
|
||||
while n > 0
|
||||
if n % 2 == 1
|
||||
punktR = sumaPunktow(a,b,p,punktR[0],punktR[1],punktQ[0],punktQ[1])
|
||||
n = n - 1
|
||||
end
|
||||
punktQ = sumaPunktow(a,b,p,punktQ[0],punktQ[1],punktQ[0],punktQ[1])
|
||||
n = n / 2
|
||||
end
|
||||
return punktR
|
||||
end
|
||||
|
||||
def liczenieOrd(a,b,p)
|
||||
threads = []
|
||||
punktyNaKrzywej = Array.new
|
||||
unikalnePunktyNaKrzywej = Array.new
|
||||
|
||||
threads << Thread.new {
|
||||
counter = 10
|
||||
while counter > 0
|
||||
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
|
||||
counter = counter -1
|
||||
end
|
||||
}
|
||||
|
||||
threads << Thread.new {
|
||||
counter = 10
|
||||
while counter > 0
|
||||
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
|
||||
counter = counter -1
|
||||
end
|
||||
}
|
||||
|
||||
threads << Thread.new {
|
||||
counter = 10
|
||||
while counter > 0
|
||||
punktyNaKrzywej << punktNaKrzywej(a,b,p)[0]
|
||||
counter = counter -1
|
||||
end
|
||||
}
|
||||
|
||||
ThreadsWait.all_waits(*threads)
|
||||
|
||||
punktyNaKrzywej.sort.each { |e|
|
||||
unikalnePunktyNaKrzywej.push(e) if e != unikalnePunktyNaKrzywej[-1]
|
||||
}
|
||||
|
||||
return unikalnePunktyNaKrzywej.count
|
||||
end
|
||||
|
||||
def generowanieKluczyElGamalKrzywaEliptyczna(k)
|
||||
while true
|
||||
p = generate(k)
|
||||
if (primalityTest(p)) && (p % 4 == 3)
|
||||
krzywa = generatorKrzywej(p)
|
||||
a = krzywa[0].to_i
|
||||
b = krzywa[1].to_i
|
||||
|
||||
punktyNaKrzywej = Array.new
|
||||
|
||||
punktP = punktNaKrzywej(a,b,p)
|
||||
ord = liczenieOrd(a,b,p)
|
||||
|
||||
while true
|
||||
x = SecureRandom.random_number(1..ord)
|
||||
if x < ord
|
||||
punktQ = wielokrotnoscPunktu(a,b,p,x,punktP[0],punktP[1])
|
||||
pubKey = [a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1]]
|
||||
privKey = [a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1],x]
|
||||
return a,b,p,punktP[0],punktP[1],punktQ[0],punktQ[1],x
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def algorytmKodowania(a,b,p,m,n,u)
|
||||
if (m < n) && (p > n*u)
|
||||
for i in 1..u
|
||||
x = (m * u % p) + (i % p)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
if remSqEuler(fx,p)
|
||||
y = betterExponentiation(fx,((p+1)/4),p)
|
||||
end
|
||||
end
|
||||
else
|
||||
puts "Nieprawidołowe dane"
|
||||
end
|
||||
return [x,y]
|
||||
end
|
||||
|
||||
def algorytmDeKodowania(a,b,p,x,y,n,u)
|
||||
m = (x - 1) / u
|
||||
return m
|
||||
end
|
||||
|
||||
def szyfrowanieElGamala(m,n,u,a,b,p,px,py,qx,qy)
|
||||
c = algorytmKodowania(a,b,p,m,n,u)
|
||||
puts "Wiadomość na prostej jako punkt #{c.inspect}"
|
||||
return c
|
||||
end
|
||||
|
@ -1 +0,0 @@
|
||||
6/krzywa.rb
|
@ -1 +0,0 @@
|
||||
8-lab/szyfrowanie-krzywe.rb
|
Loading…
Reference in New Issue
Block a user