Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-11-08 23:33:06 +01:00
parent 63e97d3f52
commit d2816f13ea
Signed by: y0rune
GPG Key ID: F204C385F57EB348

View File

@ -27,22 +27,6 @@ end
## Zad. 1.1
def random_gen_Zn(n,k)
#b = k.to_s(2).count "[0-1]"
#r = SecureRandom.random_number(n)
#x = r.to_s(2).count "[0-1]"
#until x != b do
# #if r < n then
# r = SecureRandom.random_number(n)
# x = r.to_s(2).count "[0-1]"
# puts r
# puts x
# #end
#end
#return r
if 2**(k-1) < n && k > 0 then
if k == 1 then
min = 0
@ -52,10 +36,10 @@ def random_gen_Zn(n,k)
max = (2**k)-1
end
end
r = rand(min..max)
while true do
ra = rand(min..max)
if ra < n then
r = rand(min..max)
if r < n then
break
end
end
@ -65,30 +49,31 @@ def random_gen_Zn(n,k)
end
## Zad. 1.2
def reciprocal_Phi_n(n,b)
u = extended_euklides(n,b)[0]
v = extended_euklides(n,b)[1]
def reciprocal_Phi_p(n,p)
# n mod p
u = extended_euklides(n,p)[0]
v = extended_euklides(n,p)[1]
if v % n == 0 then
return v
if u * n % p == 1
puts u
else
return u
puts v
end
end
## Zad. 1.3
def betterExponentiation(x,k,n)
b = x.to_s(2)
b = k.to_s(2).reverse
l = b.count "[0-1]"
y = 1
i = l - 1
for j in 1..i
while i >= 0
y = y**2 % n
if b[-1*(j)]
if b[i]=="1" then
y = y * x % n
end
i = i - 1
end
return y
end
@ -97,7 +82,7 @@ end
def remSqEuler(a,p)
ans = betterExponentiation(a,(p-1)/2,p)
if ans > 0 && Prime.prime?(p) then
if ans == 1 && Prime.prime?(p) then
return true
else
return false
@ -105,18 +90,43 @@ def remSqEuler(a,p)
end
## Zad. 1.5
def squareRootFp(a)
p = 3 % 4
if remSqEuler(a,p) then
bp = betterExponentiation(a,(p+1)/4,4) % 4
bm = -1 * bp
return bp,bm
def squareRootFp(p,b)
if p % 4 == 3 && remSqEuler(p,b) == true then
for i in 1..p
a = betterExponentiation(i, (p+1)/4, p)
#puts i.inspect + " " + a.inspect + "=" + b.inspect
if a == b then
puts i
break
end
end
end
end
#puts extended_euklides(10,13)
puts random_gen_Zn(50,3)
#puts reciprocal_Phi_n(10,13)
#puts betterExponentiation(112218876,2,10)
#puts remSqEuler(3,13)
#puts squareRootFp(13)
## Zad. 1.6
def primalityTest(n)
if n == 1 then
return false
end
if n == 2 || n == 3 then
return true
end
counter = rand(n/2..n)
while (counter != 0) do
b = rand(2..n)
if betterExponentiation(b,n-1,n) != 1 then
return false
end
counter = counter + 1
end
return true
end
#1 #puts random_gen_Zn(50,3)
#2 #puts reciprocal_Phi_p(5,7)
#3 #puts betterExponentiation(24637864786278436723,10,3)
#4 #puts remSqEuler(5,13)
#5 #puts squareRootFp(7,2)
#6 #puts primalityTest(10)