From c377c2d82eae49caebcd50f5d144869294d947b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 6 Dec 2020 00:53:29 +0100 Subject: [PATCH] Added module2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- 6/krzywa.rb | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++--- module.rb | 7 ++-- 2 files changed, 92 insertions(+), 7 deletions(-) mode change 100644 => 100755 6/krzywa.rb diff --git a/6/krzywa.rb b/6/krzywa.rb old mode 100644 new mode 100755 index 7901c7b..85b0488 --- a/6/krzywa.rb +++ b/6/krzywa.rb @@ -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 diff --git a/module.rb b/module.rb index aff56a9..b4f6b4d 100755 --- a/module.rb +++ b/module.rb @@ -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 \ No newline at end of file +end