2020-11-29 16:10:15 +01:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
######################################
|
|
|
|
#
|
|
|
|
# Marcin Woźniak
|
|
|
|
# s434812
|
|
|
|
#
|
|
|
|
#####################################
|
|
|
|
|
|
|
|
load 'modul1.rb'
|
|
|
|
|
|
|
|
def factorial(n)
|
|
|
|
if n == 0
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return n * factorial(n-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mysqrt(x)
|
|
|
|
return 0 if x==0
|
|
|
|
m=x
|
|
|
|
p=x
|
|
|
|
loop do
|
|
|
|
r=(m+p/m)/2
|
|
|
|
return m if m<=r
|
|
|
|
m=r
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def secondSqrt(n)
|
|
|
|
return n.to_s(2).length-1
|
|
|
|
end
|
|
|
|
|
|
|
|
def divisors_of(n)
|
|
|
|
result = []
|
|
|
|
arr = []
|
|
|
|
|
|
|
|
1.times do |i|
|
|
|
|
arr[i] = Thread.new {
|
|
|
|
counter = 100
|
|
|
|
a = 2
|
|
|
|
w = nwd(a,n)
|
|
|
|
if w > 1 && w != n
|
|
|
|
result << w
|
|
|
|
end
|
|
|
|
|
|
|
|
for r in 2..100
|
|
|
|
d = nwd(betterExponentiation(a,factorial(r),n)-1,n)
|
|
|
|
if d == n
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
if d != n && d > 1 && d.odd?
|
|
|
|
result << d
|
|
|
|
end
|
|
|
|
|
|
|
|
if d == 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
r = r + 1
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
arr.each {|t| t.join}
|
|
|
|
return result.max
|
|
|
|
end
|
|
|
|
|
|
|
|
def outputPrimes(a, n)
|
|
|
|
p = a.gcd(n)
|
|
|
|
q = n / p
|
|
|
|
if p > q
|
2020-12-01 15:28:18 +01:00
|
|
|
p, q = q, p
|
|
|
|
return p,q
|
2020-11-29 16:10:15 +01:00
|
|
|
end
|
2020-12-01 15:28:18 +01:00
|
|
|
return p,q
|
2020-11-29 16:10:15 +01:00
|
|
|
end
|
|
|
|
|
2020-12-01 15:28:18 +01:00
|
|
|
def RecoverPrimeFactors(n,e,d)
|
2020-11-29 16:10:15 +01:00
|
|
|
k = d * e - 1
|
|
|
|
|
|
|
|
if primalityTest(k)
|
|
|
|
puts "Prime factors not found"
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-12-01 15:28:18 +01:00
|
|
|
#o = divisors_of(k)
|
|
|
|
#k = (k/o).to_s(2).length-1
|
|
|
|
|
2020-11-29 16:10:15 +01:00
|
|
|
t = 0
|
|
|
|
r = k
|
|
|
|
while(r % 2 == 0)
|
|
|
|
r = r / 2
|
|
|
|
t += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
for i in 1..100
|
|
|
|
g = SecureRandom.random_number(0..n)
|
|
|
|
y = betterExponentiation(g,r,n)
|
|
|
|
|
|
|
|
if y == 1 or y == (n - 1)
|
|
|
|
next
|
|
|
|
else
|
|
|
|
for j in 1..t-1
|
|
|
|
x = betterExponentiation(y,2,n)
|
|
|
|
if x == 1
|
|
|
|
p, q = outputPrimes(y - 1, n)
|
|
|
|
return p, q
|
|
|
|
elsif x == n - 1
|
|
|
|
next
|
|
|
|
end
|
|
|
|
y = x
|
|
|
|
x = betterExponentiation(y,2,n)
|
|
|
|
|
|
|
|
if x == 1
|
|
|
|
p, q = outputPrimes(y - 1, n)
|
|
|
|
return p, q
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#n=143
|
|
|
|
#e=7
|
|
|
|
#d=103
|
|
|
|
|
2020-12-03 21:21:19 +01:00
|
|
|
n=14205142842144491469901035779943007321473952670460614909740188710462796861921791780746014298824348546889748863603913825380912304112461129061114480661500416910991853573649055897001583708234998530660447745535711467407798340361335928981312718926721467943464464347521000503179497153112764130114342341251457556854374337702225661788558784747007799183865452550277915792606190524979919835785502848268656744723582283945123371679980696891117277548547543492116459573915049465031893477375432302554045103150951955486083526016584926750095118984741954481489582827589374811855794969993254570253121737541317841105374871
|
|
|
|
e=2219702669760051625529760071259189046161364151701596790770763259600544290997125107128138578832480323854037838605599695123440903054424577956799678397891626783444723950147784407335462559143107157658471735164714153971357443698994082727673072343180069044835094856719244582969485137575845153825021391095268519544748057926663150576101990156077844973202826679622719216615756960610764785110408304311098865781072786879379296360025429207038042833064515876868608188436266546466015175298619766069707237580766787423687287858279125035537409323009740621048068813783768774814593993312720811077575752373741693972477513
|
|
|
|
d=9738454175598488918517912045396815318351885031131011603301149540233201870415928124228184903947308481461717153640402767289853198952704967449300122329014740408508653613839688094250923162490670540988214688775753190900423588412005697560323304500348114898045236656807283167901253083798426709790746938525240264995502098847606530252043043212677911465343705421183831116604350283789270965024124861992541018116786274867535581082248878546385006259988838129620903989258127062367035340066868353921340378027331177496332241490297041686454303452932424111634076797215417394272455217584601075851777273706083879476230809
|
2020-11-29 16:10:15 +01:00
|
|
|
|
2020-12-01 15:28:18 +01:00
|
|
|
puts RecoverPrimeFactors(n,e,d).inspect
|