114 lines
3.5 KiB
Ruby
114 lines
3.5 KiB
Ruby
|
#!/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 RecoverPrimeFactors(n,e,d)
|
||
|
k = d * e - 1
|
||
|
v = 0
|
||
|
v0 = 0
|
||
|
|
||
|
if primalityTest(k)
|
||
|
puts "Prime factors not found"
|
||
|
return false
|
||
|
end
|
||
|
|
||
|
t = divisors_of(k)
|
||
|
s = (k/t).to_s(2).length-1
|
||
|
|
||
|
a = SecureRandom.random_number(1..n)
|
||
|
|
||
|
if reciprocal_Phi_p(a,n) > 1
|
||
|
return a
|
||
|
end
|
||
|
|
||
|
v = betterExponentiation(a,t,n)
|
||
|
|
||
|
if v == 1 % n
|
||
|
return 0
|
||
|
end
|
||
|
|
||
|
while v != 1 % n
|
||
|
v0 = v % n
|
||
|
v = betterExponentiation(v,2,n)
|
||
|
end
|
||
|
|
||
|
if v == -1 % n
|
||
|
return 0
|
||
|
else
|
||
|
d = reciprocal_Phi_p(v0 + 1, n)
|
||
|
return d
|
||
|
end
|
||
|
end
|
||
|
|
||
|
#n=143
|
||
|
n=14205142842144491469901035779943007321473952670460614909740188710462796861921791780746014298824348546889748863603913825380912304112461129061114480661500416910991853573649055897001583708234998530660447745535711467407798340361335928981312718926721467943464464347521000503179497153112764130114342341251457556854374337702225661788558784747007799183865452550277915792606190524979919835785502848268656744723582283945123371679980696891117277548547543492116459573915049465031893477375432302554045103150951955486083526016584926750095118984741954481489582827589374811855794969993254570253121737541317841105374871
|
||
|
e=2219702669760051625529760071259189046161364151701596790770763259600544290997125107128138578832480323854037838605599695123440903054424577956799678397891626783444723950147784407335462559143107157658471735164714153971357443698994082727673072343180069044835094856719244582969485137575845153825021391095268519544748057926663150576101990156077844973202826679622719216615756960610764785110408304311098865781072786879379296360025429207038042833064515876868608188436266546466015175298619766069707237580766787423687287858279125035537409323009740621048068813783768774814593993312720811077575752373741693972477513
|
||
|
d=9738454175598488918517912045396815318351885031131011603301149540233201870415928124228184903947308481461717153640402767289853198952704967449300122329014740408508653613839688094250923162490670540988214688775753190900423588412005697560323304500348114898045236656807283167901253083798426709790746938525240264995502098847606530252043043212677911465343705421183831116604350283789270965024124861992541018116786274867535581082248878546385006259988838129620903989258127062367035340066868353921340378027331177496332241490297041686454303452932424111634076797215417394272455217584601075851777273706083879476230809
|
||
|
|
||
|
puts RecoverPrimeFactors(n,e,d).inspect
|