Added krzywa.rb and module
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
parent
37b5b68451
commit
dcd6ab195d
77
6/krzywa.rb
77
6/krzywa.rb
@ -24,7 +24,7 @@ def delta(a,b,p)
|
||||
end
|
||||
|
||||
def rownanieKrzywej(a,b,p,x)
|
||||
fx = (betterExponentiation(x,3,p) + (a * x) % p + b % p) % p
|
||||
fx = ((betterExponentiation(x,3,p) + (a * x) % p + b % p) % p) % p
|
||||
return fx
|
||||
end
|
||||
|
||||
@ -55,23 +55,19 @@ def generatorKrzywej(p)
|
||||
end
|
||||
|
||||
def punktNaKrzywej(a,b,p)
|
||||
if delta(a,b,p) != 0
|
||||
while true
|
||||
x = SecureRandom.random_number(1..p-1)
|
||||
if delta(a,b,p) != 0
|
||||
while true
|
||||
x = SecureRandom.random_number(1..p-1)
|
||||
fx = rownanieKrzywej(a,b,p,x)
|
||||
if remSqEuler(fx,p)
|
||||
if a == 0
|
||||
y = 0
|
||||
elsif p == 2
|
||||
y = p
|
||||
elsif p % 4 == 3
|
||||
y = Math.sqrt(fx).to_i % p
|
||||
#y = betterExponentiation(a,(p+1/4),p)
|
||||
if p % 4 == 3
|
||||
#y = Math.sqrt(fx).to_i % p
|
||||
y = betterExponentiation(x,(p+1/4),p)
|
||||
end
|
||||
return x,y
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def czyPunktNalezyDoKrzywej(a,b,p,x,y)
|
||||
@ -80,6 +76,7 @@ def czyPunktNalezyDoKrzywej(a,b,p,x,y)
|
||||
if fx == betterExponentiation(y,2,p)
|
||||
return true
|
||||
else
|
||||
puts rownanieKrzywej(a,b,p,x).inspect
|
||||
return false
|
||||
end
|
||||
end
|
||||
@ -98,12 +95,13 @@ def sumaPunktow(a,b,p,x1,y1,x2,y2)
|
||||
end
|
||||
# P + -Q = 0 DZIALA
|
||||
if (x1 == x2) && (y1 == -y2)
|
||||
puts "0 - el.neutralny"
|
||||
return "0"
|
||||
end
|
||||
# P + P = 2P DZIALA
|
||||
if (x1 == x2) && (y1 == y2)
|
||||
alpha = (((3 * betterExponentiation(x1,2,p) % p + a) % p) * reciprocal_Phi_p(2 * y1,p)) % p
|
||||
x3 = betterExponentiation(alpha,2,p) - (x1 % p) - (x2 % p)
|
||||
x3 = (betterExponentiation(alpha,2,p) - (x1 % p) - (x2 % p)) % p
|
||||
y3 = (alpha * (x1 - x3) - y1) % p
|
||||
return x3,y3
|
||||
end
|
||||
@ -112,8 +110,7 @@ def sumaPunktow(a,b,p,x1,y1,x2,y2)
|
||||
return x2,y2
|
||||
elsif (x2 == 0 && y2 == 0)
|
||||
return x1,y1
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
#puts generatorKrzywej(11).inspect
|
||||
@ -164,3 +161,51 @@ puts sumaPunktow(a,b,p,xp,yp,xq,yq).inspect
|
||||
# 4) 2R
|
||||
puts sumaPunktow(a,b,p,xp,yp,xp,yp).inspect
|
||||
##################################################
|
||||
|
||||
#puts generatorKrzywej(1183779584357076950937981497685946292711107412152534481102525547387604378262522402526266939).inspect
|
||||
#puts punktNaKrzywej(239614427021073265587611886177902927263167863041565491257781227550405368115731464059190159,447169285435982716467332439542997876345372330045685811964291613238129105735899852114277221,1183779584357076950937981497685946292711107412152534481102525547387604378262522402526266939).inspect
|
||||
#puts czyPunktNalezyDoKrzywej(2,7,11,7,1).inspect
|
||||
#puts czyPunktNalezyDoKrzywej(2,7,11,2,2).inspect
|
||||
#puts punktPrzeciwny(2,2).inspect
|
||||
#puts sumaPunktow(2,2,17,5,1,5,1).inspect
|
||||
|
||||
|
||||
|
||||
# Zadanie od profersora
|
||||
a=239614427021073265587611886177902927263167863041565491257781227550405368115731464059190159
|
||||
b=447169285435982716467332439542997876345372330045685811964291613238129105735899852114277221
|
||||
p=1183779584357076950937981497685946292711107412152534481102525547387604378262522402526266939
|
||||
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 5"
|
||||
q=sumaPunktow(a,b,p,px,py,px,py).inspect
|
||||
puts "P+P=Q=" + q.inspect
|
||||
|
||||
qx=q[0].to_i
|
||||
qy=q[1].to_i
|
||||
|
||||
r=sumaPunktow(a,b,p,px,py,qx,qy).inspect
|
||||
puts "P+Q=R=" + r.inspect
|
||||
|
@ -114,6 +114,10 @@ def betterExponentiation(x,k,n)
|
||||
return false
|
||||
end
|
||||
|
||||
if x == 0
|
||||
return 0
|
||||
end
|
||||
|
||||
if x < n && x > 0
|
||||
b = k.to_s(2).reverse
|
||||
l = b.count "[0-1]"
|
||||
@ -140,8 +144,7 @@ end
|
||||
####################################
|
||||
def remSqEuler(a,p)
|
||||
ans = betterExponentiation(a,(p-1)/2,p)
|
||||
|
||||
if ans == 1 && Prime.prime?(p)
|
||||
if ans == 1 && primalityTest(p)
|
||||
return true
|
||||
else
|
||||
return false
|
||||
|
Loading…
Reference in New Issue
Block a user