Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Demonstration of Pollard's p-1 factorization method

Views: 20
Image: ubuntu2204
Kernel: SageMath 9.8
def pollard_pm1(N,B=0): if not B: B=ceil(sqrt(N)) a = Integers(N).random_element() b = a for ell in primes(B): q = 1 while q < N: q *= ell b = b^q if b == 1: return 0 d = gcd(b.lift()-1,N) if d > 1: return d return 0 def random_unsafe_prime(bits): while true: a=randint(0,bits) b=randint(0,floor((bits-a)/log(3,2))) c=randint(0,floor((bits-a-floor(b*log(3,2)))/log(5,2))) d=floor((bits-a-floor(b*log(3,2))-floor(c*log(5,2)))/log(7,2)) p = 2^a*3^b*5^c*7^d+1 if is_prime(p): return p
factor(random_unsafe_prime(2048)-1)
2^1752 * 3^57 * 5^50 * 7^32
b = 4096 t=cputime() p1=random_unsafe_prime(b//2) p2=random_prime(2^(b//2),proof=True) print(p1,p2,p1*p2) print("Generated %d-bit RSA modulus in %.3fs\n"%(b,cputime()-t)) t=cputime() print(pollard_pm1(p1*p2)) print("Factored %d-bit RSA modulus in %.3fs\n"%(b,cputime()-t))
18411143311830315432564267847886589565734618380116581848893820212229360419528581191343171801379552263771044474311429771612474707649030763272195924791543542713817946176887439389805438767836083830713043052999945910591423970067784624816954072293922082591664289811369285501257210674881493653838694299492687322469131843269245801232544404206627932984793487139043940245504987447613092751496113212449079516427748432236364830824353976571881991707358446125182241285943855153152000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 28693049396770529016339561182013721252179845559834719112430638058544437865282819117087708280901957907329602555113565128160172564189660003937194567858392267454752553925532928232543343206131234887143670661720386464445199299671800716574877870607248091503018885627024840822113596352101168456536307177751831930803771863393737171034654933702565646548693860214711048576620137585185389660389031688187955606613484472347343072573503258539730390102415306624775473928439475841738594842196517633031106936086321884773778495389812442272127423870963440963967661149545484333134519623713094519145867775035287518622336778901887171785571 528271844497368592022578947998397008709209350542371987522980324922744036105445416345919135861778798846417289336024299522636681845859267601459360820396203966162856387863657758509618703851400231561681576081257087625794568813913738148979743220175173081528267944802490907810547480484759448081228379485860198382554304126935313631661485125932764473190301268379488947578164878334878869981055677620757919483274862305629235695189882199224319149381234488865236940089494647111944159399433810134752745276962960361236003098535456397298814411372352137285705697277726488468497988377093598600474619604483132087305863659116433589200614114564774546997013829958788630296365959496943130221641164931578408030772172385337751031625617186077436516486472058310426453507296685023142481538349125312126730138151804547914758903713515916105444803866816449379219590652749432642708723344607151487931050092733656943999351615935384561781181781913492611187086713459963616043983017677089007038471871273842072571288972890571595387506167266967482131544065491242089866113017424894570813947027210778781057880157688984611530594842196517633031106936086321884773778495389812442272127423870963440963967661149545484333134519623713094519145867775035287518622336778901887171785571 Generated 4096-bit RSA modulus in 23.009s 18411143311830315432564267847886589565734618380116581848893820212229360419528581191343171801379552263771044474311429771612474707649030763272195924791543542713817946176887439389805438767836083830713043052999945910591423970067784624816954072293922082591664289811369285501257210674881493653838694299492687322469131843269245801232544404206627932984793487139043940245504987447613092751496113212449079516427748432236364830824353976571881991707358446125182241285943855153152000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 Factored 4096-bit RAS modulus in 0.088s