Added module2

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-06 00:53:29 +01:00
parent 17237eab0a
commit c377c2d82e
Signed by: y0rune
GPG Key ID: F204C385F57EB348
2 changed files with 92 additions and 7 deletions

92
6/krzywa.rb Normal file → Executable file
View File

@ -1,10 +1,94 @@
#########################################
#!/usr/bin/ruby
# coding: utf-8
#######################################
#
#
#
#
# Marcin Woźniak
# s434812
#
########################################
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
return fx
end
def generatorKrzywej(p)
a = 0
b = 0
while true
#p = generate(300)
if primalityTest(p)
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
end
end
end
def punktNaKrzywej(a,b,p)
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)
y = Math.sqrt(fx).to_i #betterExponentiation(x,((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)
end
#puts generatorKrzywej(11).inspect
#puts punktNaKrzywej(2,7,11).inspect
#puts czyPunktNalezyDoKrzywej(2,7,11,7,1).inspect
#puts czyPunktNalezyDoKrzywej(2,7,11,2,2).inspect
#puts punktPrzeciwny(2,2).inspect

View File

@ -1,4 +1,5 @@
#!/usr/bin/ruby
# coding: utf-8
#####################################
#
@ -13,7 +14,7 @@ require 'prime'
require 'thread'
def nwd(a, b)
if a == 0
if a == 0
return false
end
b == 0 ? a : nwd(b, a.modulo(b))
@ -120,7 +121,7 @@ def primalityTest(n)
return true
end
counter = 20
counter = 10
while (counter != 0) do
b = SecureRandom.random_number(2..n-2) # Tez dziala n-1
if betterExponentiation(b,n-1,n) != 1
@ -168,4 +169,4 @@ end
def generate(n)
return `openssl prime -generate -bits '#{n}'`.gsub(/\n$/, '').to_i
end
end