2020-12-03 21:21:19 +01:00
|
|
|
#!/usr/bin/ruby
|
2020-12-06 00:53:29 +01:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
#######################################
|
2020-12-03 21:21:19 +01:00
|
|
|
#
|
2020-12-06 00:53:29 +01:00
|
|
|
# Marcin Woźniak
|
|
|
|
# s434812
|
2020-12-03 21:21:19 +01:00
|
|
|
#
|
|
|
|
########################################
|
|
|
|
|
|
|
|
load '../module.rb'
|
2020-12-06 00:53:29 +01:00
|
|
|
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)
|
2020-12-12 01:14:07 +01:00
|
|
|
fx = ((betterExponentiation(x,3,p) + (a * x) % p + b % p) % p) % p
|
2020-12-06 00:53:29 +01:00
|
|
|
return fx
|
|
|
|
end
|
|
|
|
|
|
|
|
def generatorKrzywej(p)
|
|
|
|
a = 0
|
|
|
|
b = 0
|
|
|
|
|
|
|
|
while true
|
2020-12-19 22:23:54 +01:00
|
|
|
if primalityTest(p) && (p % 4 == 3)
|
2020-12-06 00:53:29 +01:00
|
|
|
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
|
2020-12-19 22:23:54 +01:00
|
|
|
else
|
|
|
|
puts "Liczba nie spełnia wymagań"
|
|
|
|
break
|
2020-12-06 00:53:29 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def punktNaKrzywej(a,b,p)
|
2020-12-12 20:08:47 +01:00
|
|
|
if (delta(a,b,p) != 0) && (p % 4 == 3)
|
2020-12-12 01:14:07 +01:00
|
|
|
while true
|
2020-12-12 20:03:49 +01:00
|
|
|
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
|
2020-12-12 01:14:07 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-06 00:53:29 +01:00
|
|
|
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)
|
2020-12-19 22:23:54 +01:00
|
|
|
# 0 - element neutrany --> P + 0 = P
|
|
|
|
if (x1 == "e" && y1 == "e" )
|
|
|
|
return x2,y2
|
|
|
|
elsif (x2 == "e" && y2 == "e")
|
|
|
|
return x1,y1
|
|
|
|
end
|
2020-12-08 13:23:33 +01:00
|
|
|
# P + Q = R
|
2020-12-15 22:20:26 +01:00
|
|
|
if (x1 != x2)
|
2020-12-12 20:03:49 +01:00
|
|
|
lambda = (((y2 - y1) % p) * reciprocal_Phi_p((x2 - x1),p)) % p
|
2020-12-12 20:08:47 +01:00
|
|
|
x3 = (betterExponentiation(lambda,2,p) - (x1 % p) - (x2 % p)) % p
|
2020-12-12 20:03:49 +01:00
|
|
|
y3 = (lambda * (x1 - x3) - y1) % p
|
2020-12-08 13:23:33 +01:00
|
|
|
return x3,y3
|
|
|
|
end
|
|
|
|
# P + -Q = 0 DZIALA
|
|
|
|
if (x1 == x2) && (y1 == -y2)
|
2020-12-12 20:08:47 +01:00
|
|
|
puts "0 - el.neutralny"
|
2020-12-19 22:23:54 +01:00
|
|
|
return e,e
|
2020-12-08 13:23:33 +01:00
|
|
|
end
|
|
|
|
# P + P = 2P DZIALA
|
|
|
|
if (x1 == x2) && (y1 == y2)
|
2020-12-12 20:03:49 +01:00
|
|
|
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
|
2020-12-08 13:23:33 +01:00
|
|
|
return x3,y3
|
|
|
|
end
|
2020-12-06 00:53:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
#puts generatorKrzywej(11).inspect
|
|
|
|
#puts punktNaKrzywej(2,7,11).inspect
|
|
|
|
#puts czyPunktNalezyDoKrzywej(2,7,11,7,1).inspect
|
2020-12-12 20:03:49 +01:00
|
|
|
#puts czyPunktNalezyDoKrzywej(2,7,11,10,10).inspect
|
2020-12-06 00:53:29 +01:00
|
|
|
#puts punktPrzeciwny(2,2).inspect
|
2020-12-08 13:23:33 +01:00
|
|
|
#puts sumaPunktow(2,2,17,5,1,5,1).inspect
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
# Zadania z konca wykladu suma punktow
|
|
|
|
#
|
|
|
|
# Niech E/F7 będzie postaci
|
|
|
|
# E : Y^2 = X^3 + 1
|
|
|
|
#
|
|
|
|
# Niech P=(1,3) Q=(2,4) R=(6,0)
|
|
|
|
#
|
|
|
|
# Obliczyc:
|
|
|
|
# 1) -Q
|
|
|
|
# 2) R + [0,0]
|
|
|
|
# 3) P + Q
|
|
|
|
# 4) 2R
|
|
|
|
#
|
|
|
|
# Rozwiązanie:
|
|
|
|
a = 0
|
|
|
|
b = 1
|
|
|
|
p = 7
|
|
|
|
|
|
|
|
xp = 1
|
|
|
|
yp = 3
|
|
|
|
|
|
|
|
xq = 2
|
|
|
|
yq = 4
|
|
|
|
|
|
|
|
xr = 6
|
|
|
|
yr = 0
|
|
|
|
|
2020-12-08 13:48:51 +01:00
|
|
|
# 1) -Q
|
2020-12-12 20:08:47 +01:00
|
|
|
puts punktPrzeciwny(xq,yq).inspect
|
2020-12-08 13:48:51 +01:00
|
|
|
|
|
|
|
# 2) R + [0,0]
|
2020-12-19 22:23:54 +01:00
|
|
|
puts sumaPunktow(a,b,p,xr,yr,"e","e").inspect
|
2020-12-08 13:48:51 +01:00
|
|
|
|
|
|
|
# 3) P + Q
|
2020-12-12 20:08:47 +01:00
|
|
|
puts sumaPunktow(a,b,p,xp,yp,xq,yq).inspect
|
2020-12-08 13:48:51 +01:00
|
|
|
|
|
|
|
# 4) 2R
|
2020-12-12 20:08:47 +01:00
|
|
|
puts sumaPunktow(a,b,p,xp,yp,xp,yp).inspect
|
2020-12-08 13:23:33 +01:00
|
|
|
##################################################
|
2020-12-12 01:14:07 +01:00
|
|
|
|
2020-12-12 20:08:47 +01:00
|
|
|
##################################################
|
|
|
|
# Zadanie od profersora
|
2020-12-12 01:14:07 +01:00
|
|
|
a=239614427021073265587611886177902927263167863041565491257781227550405368115731464059190159
|
|
|
|
b=447169285435982716467332439542997876345372330045685811964291613238129105735899852114277221
|
|
|
|
p=1183779584357076950937981497685946292711107412152534481102525547387604378262522402526266939
|
|
|
|
px=285113634279465403319996581740169338329454608669814309137990174814243655992779447106132850
|
|
|
|
py=598700530906084162596261101440667782569915319623798143751082061599951188013331503150304328
|
|
|
|
|
|
|
|
# Zadanie 1
|
2020-12-12 20:08:47 +01:00
|
|
|
puts
|
|
|
|
puts "Zadanie 1"
|
|
|
|
puts generatorKrzywej(p).inspect
|
2020-12-12 01:14:07 +01:00
|
|
|
|
|
|
|
# Zadanie 2
|
2020-12-12 20:08:47 +01:00
|
|
|
puts
|
|
|
|
puts "Zadanie 2"
|
|
|
|
puts punktNaKrzywej(a,b,p).inspect
|
2020-12-12 01:14:07 +01:00
|
|
|
|
|
|
|
# Zadanie 3
|
2020-12-12 20:08:47 +01:00
|
|
|
puts
|
2020-12-12 01:14:07 +01:00
|
|
|
puts "Zadanie 3"
|
|
|
|
puts czyPunktNalezyDoKrzywej(a,b,p,px,py).inspect
|
|
|
|
|
|
|
|
# Zadanie 4
|
2020-12-12 20:08:47 +01:00
|
|
|
puts
|
|
|
|
puts "Zadanie 4"
|
|
|
|
puts punktPrzeciwny(px,py).inspect
|
2020-12-12 01:14:07 +01:00
|
|
|
|
|
|
|
# Zadanie 5
|
|
|
|
puts
|
|
|
|
puts "Zadanie 5"
|
2020-12-12 20:03:49 +01:00
|
|
|
q=sumaPunktow(a,b,p,px,py,px,py)
|
2020-12-12 01:14:07 +01:00
|
|
|
puts "P+P=Q=" + q.inspect
|
|
|
|
|
|
|
|
qx=q[0].to_i
|
|
|
|
qy=q[1].to_i
|
2020-12-12 20:03:49 +01:00
|
|
|
r=sumaPunktow(a,b,p,px,py,qx,qy)
|
2020-12-12 01:14:07 +01:00
|
|
|
puts "P+Q=R=" + r.inspect
|
2020-12-19 22:23:54 +01:00
|
|
|
|
2020-12-12 20:08:47 +01:00
|
|
|
##################################################
|