<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Paolo Ardoino - ideas, programming, web and more... &#187; Maths</title>
	<atom:link href="http://ardoino.com/category/maths/feed/" rel="self" type="application/rss+xml" />
	<link>http://ardoino.com</link>
	<description>Homo quisque faber ipse fortunae suae</description>
	<lastBuildDate>Tue, 27 Oct 2009 22:42:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Large integers factorization</title>
		<link>http://ardoino.com/42-large-integers-factorization/</link>
		<comments>http://ardoino.com/42-large-integers-factorization/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 07:00:34 +0000</pubDate>
		<dc:creator>Paolo Ardoino</dc:creator>
				<category><![CDATA[Crypto/Security]]></category>
		<category><![CDATA[Maths]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[factorization]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[rsa]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://ardoino.com/42-large-integers-factorization/</guid>
		<description><![CDATA[Summarizing post of large integers factorization methods I have exposed in my articles.]]></description>
			<content:encoded><![CDATA[<p>This is a summarizing post of large integers factorization methods I have exposed in my articles.</p>
<ul>
<li> Classical method
<ul>
<li><a href="/pub/maths/classical/classical.txt" title="Classical method for large integers factorization" target="_blank">Source code</a></li>
<li><a href="/pub/maths/classical/REPORT_CLASSICAL.txt" title="Classical method for large integers factorization: results" target="_blank">Results</a></li>
</ul>
</li>
<li>Pollard P-1
<ul>
<li><a href="/pub/maths/pollard/pollard.txt" title="Pollard p-1 method for large integers factorization" target="_blank">Source code</a></li>
<li><a href="/pub/maths/pollard/REPORT_SPMI.txt" title="Pollard p-1 method for large integers factorization : results" target="_blank">Results</a></li>
</ul>
</li>
<li>Elliptic curve method
<ul>
<li><a href="/pub/maths/ecm/ecm.txt" title="Elliptic curve method for large integers factorization" target="_blank">Source code</a> , <a href="/pub/maths/ecm/factor_base.txt" title="Elliptic curve method for large integers factorization: factor base" target="_blank">Factor base</a></li>
<li><a href="/pub/maths/ecm/REPORT_SECMI.txt" title="Elliptic curve method for large integers factorization : results" target="_blank">Results</a></li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ardoino.com/42-large-integers-factorization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Basic mathematical background and Lenstra factoring algorithm</title>
		<link>http://ardoino.com/22-maths-factoring-lenstra/</link>
		<comments>http://ardoino.com/22-maths-factoring-lenstra/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 07:00:47 +0000</pubDate>
		<dc:creator>Paolo Ardoino</dc:creator>
				<category><![CDATA[Crypto/Security]]></category>
		<category><![CDATA[Italian]]></category>
		<category><![CDATA[Maths]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[factoring]]></category>
		<category><![CDATA[gmp]]></category>
		<category><![CDATA[lenstra]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[rsa]]></category>

		<guid isPermaLink="false">http://ardoino.com/22-maths-factoring-lenstra/</guid>
		<description><![CDATA[

[This article appeared on OndaQuadra0A Elettronic Magazine]
Fondamenti di Matematica - Algoritmi Fondamentali
Metodo delle curve ellittiche per la fattorizzazione
di numeri interi.
by Paolo Ardoino AKA binduck &#60; paolo.ardoino@gmail.com &#62;
&#60; http://ardoino.com &#62;

Prima di passare al prossimo algoritmo per la riduzione in fattori
primi di un numero, conviene spiegare alcuni concetti matematici
e alcuni algoritmi che stanno alla base delle operazioni [...]]]></description>
			<content:encoded><![CDATA[<pre>

[This article appeared on OndaQuadra0A Elettronic Magazine]
Fondamenti di Matematica - Algoritmi Fondamentali
Metodo delle curve ellittiche per la fattorizzazione
di numeri interi.
by Paolo Ardoino AKA binduck &lt; paolo.ardoino@gmail.com &gt;
&lt; http://ardoino.com &gt;

Prima di passare al prossimo algoritmo per la riduzione in fattori
primi di un numero, conviene spiegare alcuni concetti matematici
e alcuni algoritmi che stanno alla base delle operazioni tra numeri
interi composti da molte cifre.
N.B. Questi sono solo riassunti basilari e molto semplici, che potete
consultare per avere un idea delle nozioni che sono richieste negli
articoli matematico-informatici. Per una completa visione di questi
leggete testi di matematica e algoritmi.
Mi raccomando ricordate che la matematica e' molto importante se
volete tuffarvi nel campo della crittografia.
Le implementazioni degli algoritmi presentati in questo articolo
possono essere facilmente trovate in ogni libreria matematica per
qualsiasi linguaggio di programmazione.

0] Notazione usata

1] Concetti fondamentali

1.1] Matematica
1.1.1] Strutture algebriche
1.1.2] Gruppo
1.1.3] Anello
1.1.4] Campo
1.1.5] Aritmetica modulare
1.1.6] Piccolo teorema di Fermat

1.2] Informatica
1.2.1] Calcolabilita' e complessita' di un algoritmo
1.2.2] Rappresentazione numerica di un messaggio
1.2.3] Rappresentazione di un messaggio in Zn

2] Algoritmi fondamentali
2.1] Test di primalita' e teorema di Fermat
2.1.1] Probabilistic primality test
2.2] Numeri random
2.2.1] Linear congruential method
2.3] Massimo comun divisore GCD

3] Elliptic Curve Method
<span id="more-22"></span>

0]
+ Somma
- Sottrazione
/ Divisione
* Moltiplicazione
^ Elevamento a potenza
% Resto della divisione
mod Modulo
= Assegnamento
== Uguaglianza
&gt; Maggiore
&lt; Minore
&gt;= Maggiore uguale

&lt;= Minore uguale
/= Diverso
|x| Valore assoluto di x
° Operatore di composizione
[A1..Ax] Elementi da 1 a x
=&gt; Allora
&lt;=&gt; Se e solo se
@ Per ogni
E Appartenenza
t.c. Tale che
-] Esiste
idA Identitita' in A
log(n) Logaritmo in base due di n
Log(n) Logaritmo in base dieci di n
lim(x -&gt; +inf) f(x)
  si legge: limite per x che tende a piu' infinito di f(x)
GCD(a, b) Massimo comun divisore tra a e b
LCM(a, b) Minimo comune multiplo tra a e b

1] Concetti fondamentali
In questa sezione verranno spiegati in modo molto basilare alcuni
concetti matematici necessari.

1.1.1] Strutture algebriche
Dato un insieme A una operazione binaria interna e' una legge che ad ogni
coppia del prodotto cartesiano AxA fa corrispondere univocamente un
elemento di A.

1.1.2] Gruppi
Un gruppo (G, *) ha un'operazione interna * ed e' un insieme che
soddisfa le proprieta':
-proprieta' associativa P @ a,b,c E G, (a*b)*c == a * (b * c)
-ha l'elemento neutro @ a E G, uG * a == a * uG == a [dove uG e'
l'elemento neutro]
-ha l'inverso @ a E G, -] b E G t.c. a * b == b * a = uG

Dato un gruppo (G, *) se a * b == b * a , @ a, b E G allora G e' un
gruppo commutativo o gruppo abeliano.

1.1.3] Anelli
Un anello (A, +, *) ha due operazioni interne una denotata additivamente
e l'altra moltiplicativamente; gli anelli sono:
-un gruppo abeliano rispetto alla somma [prima operazione]
-il prodotto [seconda operazione] gode della proprieta' associativa
-proprieta' distributiva a * (b + c) == a * b + a * c ,
(a + b) * c == a * c + b * c

Un anello si dice commutativo se il prodotto [seconda operazione] gode
della proprieta' commutativa. Se esiste un elemento neutro rispetto al
prodotto allora l'anello ha un'identita'.

1.1.4] Campi
Un campo (C, +, *) ha due operazione interne, come nel caso dell'anello
una denotata additivamente e una denotata moltiplicamente.
-C e' un gruppo moltiplicativo.
-la seconda operazione e' distributiva rispetto alla prima.

1.1.5] Artimetica modulare
L'aritmetica modulare studia i resti delle divisioni aritmetiche.
X e' il divisendo, m e' il divisore, Q e' il quoziente e R e' il resto.
(X mod m) = R si legge X modulo m e' uguale a R.
Esempi:
5 mod 3 = 2 infatti m * Q + R == X -&gt; 3 * 1 + 2 == 5
31 mod 43 = 31 infatti 43 * 0 + 31 == 31

Possiamo dire che R &lt; m, infatti 0 &lt; R &lt; m - 1

(X mod 1) == 0 -&gt; un qualsiasi numero modulo 1 e' sempre uguale a 0.
(0 mod m) == 0 -&gt; 0 modulo qualsiasi numero e' sempre uguale a 0.
(X + Y) (mod m) == X(mod m) + Y(mod m) -&gt; il resto di una somma e'
uguale alla somma dei resti.
(X * Y) (mod m) == X(mod m) * Y(mod m) -&gt; il resto di un prodotto e'
uguale al prodotto dei resti. Quest'ultima equivalenza ci sara' utile
nella determinazione dei resti in divisioni tra numeri composti da
molte cifre poiche' ci dice che:
(X^2)(mod m) == X(mod m) * X(mod m) == R^2

1.1.6] Piccolo teorema di Fermat
Cosa ci dice il piccolo teorema di Fermat?
p divide a^(p - 1) - 1, quando p e' primo e a e' primo con p [non hanno
divisori comuni].
Una generalizzazione che puo' seguire da questa forma e': presi due
numeri interi positivi m, n con m == n allora a^n == a^m (mod p).
[Utilizzato per la dimostrazione dell'algoritmo RSA].

1.2] Informatica

1.2.1] Calcolabilita' e complessita' di un algoritmo
Calcolabilita': e' possibile scrivere un algoritmo per risolvere il
problema?

Complessita': sapendo che il problema e' calcolabile, quanto e'
complesso? Per valutare la complessita' ci interessiamo del tempo
di calcolo (tralaciamo quindi lo spazio di memoria occupato).
La complessita' viene calcolata tenendo conto di tutte le operazioni
algebriche e logiche, accesso in lettura e scrittura, etc...
Nelle nostre valutazioni, comunque, partiremo dal presupposto che la
macchina che eseguira' l'algoritmo sara' una macchina ideale, un
calcolatore astratto che non tenga conto delle prestazioni hardware.
Per poter descrivere la complessita' di un algoritmo e' necessario
conoscere gli ordini di grandezza: teta, omega, O.
Prendiamo ora due funzioni f e g, diremo che:
f e' O(g) se f cresce al piu' come g, quindi g e' il limite superiore.
f e' omega(g) se f cresce almeno come g, quindi g e' il limite
inferiore.
f e' teta(g) se f cresce come g, quindi f ha lo stesso ordine di
grandezza di g.
Per il concetto di limite, lim (x -&gt; +inf) f(x)/g(x) == c
Se c /= 0 =&gt; f e' teta(g) e quindi g e' teta(f)
Se c == 0 =&gt; f e' O(g)
Un esempio potrebbe essere: questo algoritmo ha complessita' O(log n),
questo vuol dire che ha complessita' log in base 2 di n.

Negli algoritmi ci puo' essere un'istruzione dominante allora accade che
riducendo la complessita' di questa, la complessita' dell'intero
algoritmo cali vertiginosamente.

Il metodo migliore per valutare la complessita' di un codice e' quello
di spezzarlo in blocchi e analizzare l'ordine di grandezza di ogni
singolo blocco.

Esempio di calcolo della complessita':
{
int n, i, p;
scanf("%d", &amp;n);
for(i = 0, p = 0; i &lt; n; i++) { p++; }
}
La complessita' di questo blocco e' O(n). Questa potrebbe crescere
inserendo una nuova operazione di complessita' maggiore.

1.2.2] Rappresentazione numerica di un messaggio
N.B QUESTO PARAGRAFO E QUELLO SULLA RAPPRESENTAZIONE DEI NUMERI IN Zn
sono fondamentali per la comprensione della maggior parte degli
algoritmi di crittografia.

Supponiamo per esempio che un messaggio sia composto solo dalle 21
lettere dell'alfabeto italiano piu' lo spazio. Quindi con i numeri
compresi tra 0 e 21 possiamo indicare ogni carattere.
Dati m blocchi, quanti blocchi possiamo codificare col nostro
insieme di 22 caratteri? 22^m; quindi possiamo indicare ogni blocco
identificandolo tra 0 e (22^m) - 1.
Prendiamo per esempio un blocco [A1..Am], indichiamo con i numeri
[X1..Xm] corrispondenti alle lettere A1..Am e indichiamo il blocco
col numero risultante.
Piu' precisamente con il metodo illustrato qui sotto otteniamo che ogni
blocco sia indicato univocamente:
x = 22^(m - 1) * X1 + 22^(m - 2) * X2 + ... + 22^1 * X(m - 1) + Xm
Il processo e' ovviamente invertibile, infatti possiamo ricavare
[X1..Xm] e quindi [A1..Am]. Prendiamo il nostro x e dividiamolo per 22,
il resto ottenuto da questa divisione sara' Xm. Ora dividiamo il
quoziente (Q) della divisione per 22 e otteniamo X(m - 1) e cosi' via.

1.2.3] Rappresentazione di un messaggio in Zn
N.B QUESTO PARAGRAFO E' FONDAMENTALE PER LA COMPRENSIONE DELLA
MAGGIOR PARTE DEGLI ALGORITMI DI CRITTOGRAFIA.
[Questo paragrafo e' in parte un riassunto, semplificato della Ref.2]
[In questo paragrafo e' necessaria la parte di aritmetica modulare]
Una volta trovato il numero di simboli del nostro alfabeto (22 nel
paragrafo precente), genericamente n, possiamo indicare con Zn
l'insieme dei numeri compresi tra 0 e n - 1. Di qui possiamo arrivare
a scrivere una funzione f:Zn -&gt; Zn che ad ogni blocco di m caratteri di
Zn associa un nuovo blocco in Zn; questo procedimento rende possibile
l'operazione inversa (descrittazione) f^-1.
Una condizione necessaria su f perche' questa sia invertibile e' che
sia iniettiva, cioe' che a ogni elemento del dominio ne associ uno e
uno solo dell'immagine; se la condizione non fosse necessaria allora
non potremmo scrivere una funzione che decritta univocamente i blocchi
crittati.
Matematicamente una funzione si dice iniettiva se:
f(x) == f(x') &lt;=&gt; x == x'

Una funzione f e' invertibile se e solo se e' iniettiva.

La condizione di invertibilita' si esprime in questo modo:
Presi due elementi x E X, y E Y sia f una funzione da X a Y,
allora esiste una funzione f^-1 da Y a X se e solo se
f^-1(f(x)) = x  e f(f^-1(y)) = y
L'invertibilita' puo' essere scritta anche cosi':
g ° f = idA e f ° g = idB
[In generale: f ° g == f(g)]

**EQUIVALENZA**
Definiamo ora l'operatore di equivalenza o conguenza in Zn
a == b (mod n).
Detto piu' semplicemente se pensiamo a Zn come ad un insieme di
numeri consecutivi, prendiamo due numeri a, b E Zn.
**SOMMA**
Se a + b &lt; n allora possiamo prendere come risultato della somma
a + b.
Se a + b &gt;= n allora dobbiamo sottrarre n poiche' sforeremo
dall'insieme Zn dato che dovremmo considerare numeri piu' grandi di n
che non possono essere dentro l'insieme.
Quindi se ripensiamo all'equivalenza un n + 1 == 1 in Zn, n + 2 == 2
in Zn, etc...
L'operazione di riduzione si effettua per passare da un generico numero
al suo corrispondente in Zn.
Esempio:
3 + 5 == 2 in Z6, poiche' 8 (mod 6) == 2
**PRODOTTO**
Ovviamente tutto cio' che abbiamo detto finora per la somma vale allo
stesso modo per il prodotto.
a * b = c (mod n)
**OPPOSTO**
Ogni x E Zn ammette opposto, che e' semplicemente il numero congruente
a -x in Zn cosicche' x + (-x) == 0, poiche' 0 e' l'elemento neutro della
somma.
Esempio:
In Z8, l'inverso di 5 e' 3, infatti 5 + 3 == 8 == 0
**INVERSO**
Per quanto riguarda l'inverso dobbiamo riferirci all'elemento neutro del
prodotto che e' 1, infatti x * x^-1 == 1.
Non tutti gli elementi di Zn quindi ammettono inverso. Prendiamo ad
esempio in Z5 il numero 2; bene il suo inverso sara' 3, poiche'
2 * 3 == 6 == 1.
Mentre 2 non ha inverso in Z6, dato che ogni numero moltiplicato per 2
da' come risultato un numero pari, mentre l'inverso dovrebbe essere
dispari.
In generale possiamo riassumere che un numero ha inverso in Zn se e solo
se GCD(x, n) == 1.
Di conseguenza se n e' primo (cioe' divisibile solo per uno e per se
stesso) allora ogni numero tranne 0 ammette inverso, e Zn definito
queste operazioni di somma e prodotto e' un campo; al contrario se
n non e' primo esiste almeno un numero in Zn che non ha inverso e
Zn e' un anello.

2] Algoritmi fondamentali
Quali caratteristiche deve avere un algoritmo?
a) Finito: deve terminare dopo un numero finito di passi.
b) Definito: essendo i computer delle macchine deterministiche, ogni
passo dell'algoritmo deve essere definito precisamente.
c) Input: 0 o piu' valori in input.
d) Output: 1 o piu' output.
e) Realizzabilita': tutte le operazioni usate nell'algoritmo devono
poter essere fattibili anche da un uomo su carta.

Un algoritmo si dice computazionalmente trattabile se esiste un
algoritmo efficiente che lo risolva.
Un algoritmo si dice efficiente se esiste una funzione che lo limita
superiormente.

2.1] Test di primalita' e teorema di Fermat
Secondo quanto ci dice il teorema di Fermat x^(p-1) mod p == 1
se p e' primo e x non e' multiplo di p; quando questa relazione non e'
verificata allora p e' composto.
Servono quindi solo O(log n) moltiplicazioni mod n per verificare il
teorema di Fermat. Comunque per n molto grandi i calcoli diventano
molto costosi in termini di tempo e risorse.

2.1.1] Test probailistico di primalita'
Il test probabilistico di primalita' in p e' fondamentale per
controllare in modo veloce, e comunque affidabile, se un intero e'
primo oppure composto (e quindi riducibile in fattori primi :)).
0] Prendiamo un numero intero n dispari
(ovviamente se e' pari avra' almeno un fattore, 2);
1] Sia n = 1 + (2^k) * q
(q sara' dispari)
2] Scegliamo un x casuale tale che 1 &lt; x &lt; n
3] Sia j = 0 e y = (x^q) mod n
(questo calcolo richiede O(log q) passi)
4] Se j == 0 e y == 1, oppure y == n - 1 allora n e' probabilemte primo
Se j &gt; 0 and y == 1 saltiamo al passo 6.
Altrimenti proseguiamo.
5] j = j + 1
Se j &lt; k allora y = (y^2) mod n e ritorniamo al passo 4.
Altrimenti proseguiamo.
6] n e' sicuramente composto.

L'algoritmo in se, computato una singola volta ha una probabilita' pari
a 1/4 di fallire; per ottenere una maggiore sicurezza e' possibile
ripetere l'algoritmo r volte, cosi' che la probabilita' di fallimento
sia di (1/4)^r. Pensiamo quindi di ripetere l'algoritmo un numero
finito di volte, ad esempio 100, la probabilita' che il nostro
algoritmo fallisca sara' di (1/4)^100, praticamente 0.

[Questo test e' il piu' gettonato ed e' implementato in tutte le
librerie matematiche (vedi ad esempio GNU Multi Precision e la libreria
matematica di OpenSSL) per il fatto che e' veloce ed affidabile.]

2.2] Numeri random
Un numero random, e' un numero scelto a caso; scrivere un algoritmo
che fornisca una buona fonte di numeri primi non e' affatto semplice.
Algoritmi come il supe-rrandom number generator sono obsoleti e
convergono in modo molto veloce e questo ci insegna che per generare
numeri random non si dovrebbe usare metodi casuali, ma invece bisogna
basarsi sulla teoria matematica.
Una fonte di numeri casuali nei sistemi GNU/Linux e' /dev/random.
In C possiamo per ottenere numeri random e' sufficiente appoggiarsi
a due funzioni contenute nella stdlib, che sono la srand(), che permette
di settare il random seed e la rand() che ci restituisce un intero
casuale compreso tra 0 e RAND_MAX.
#define    RAND_MAX        2147483647
Ovviamente la sequenza di numeri casuali e' riottenibile reinserendo
il lo stesso seed in srand().
Un esempio di inizializzazione potrebbe essere srand(time(NULL));.

2.2.1] Linear congruential method
Per generare numeri casuali uniformemente distribuiti tra 0 e 1 si
utilizza prevaletentemente il linear conguential method.
In questo documento mostro solo l'idea alla base dell'algoritmo.
Si scelgano 4 numeri:
m modulo t.c m &gt; 0
a moltiplicatore t.c 0 &lt;= a &lt; m
c incrementatore t.c 0 &lt;= c &lt; m
Xo valore di partenza t.c 0 &lt;= Xo &lt; m

La sequenza di numeri casuali Xn seguira da:
X(n + 1) = (a*Xn + c) mod m
(n &gt;= 0)
Ovviamente per scegliere i 4 numeri di partenza ci son dei metodi che
ci permettono di scegliere dei buoni valori.

2.3] Massimo comun divisore GCD
[GCD == Great Commin Divisor]
In questa sezione vedremo oltre l'aspetto matematico del massimo comun
divisore anche l'algoritmo per il calcolo.
Dati due numeri a, b il massimo comun divisore GCD(a, b) e' numero piu'
grande che li divide entrambe. Vediamo ora alcune proprieta' del GCD.
GCD(0, 0) == 0
GCD(u, v) == GCD(v, u)
GCD(u, v) == GCD(-u, v)
GCD(u, 0) == |u|

L'algoritmo di Euclide ci permette di trovare il massimo comun divisore
senza prima trovare i fattori primi di u e v.
Poniamo sempre a sinistra l'intero maggiore.

Vediamo prima l'algoritmo originale:
0] Siano A e C due interi &gt; 1 calcolarne il GCD
1] Se A &gt; C e C divide A =&gt; C e' il GCD(A, C)
2] (A mod C) == 1 allora A e C son primi tra loro, quindi
   l'algoritmo termina. Altrimenti (A mod C) &gt; 1, calcoliamo il
   GCD(C, A mod C).
Questo algoritmo da vita quindi a una procedura ricorsiva.

Ora invece vediamo un'implementazione moderna dell'algoritmo.
0] Siano u e v due interi &gt;= 0 calcolare GCD(u, v)
1] Se v == 0 allora GCD(u, v) == u
2] r = u mod v, u = v, v = r; ritorniamo al punto 1].

Esistono altri algoritmi per il calcolo del GCD [come il binary gcd
algorithm], e quindi si potrebbe continuare a parlare del massimo
comun divisore per pagine e pagine, ma direi che per rendere l'idea
i due descritti son piu' che sufficienti.

3] Elliptic Curve Method

L'idea che sta alla base dell'algoritmo di Lenstra e' quella di
sfruttare delle curve ellittiche, scelte casualmente, per svolgere
dei tentativi di fattorizzazione, e ognuno di questi ha una probabilita'
non nulla di trovare un fattore primo di N.
Inanzitutto vediamo come e' fatta una curva ellittica, la cui equazione
e': y^2 = x^3 + a*x + b
Da questo possiamo dedurre che una curve ellittica e' un grafico di
una cubica (terzo grado) [non bisogna confondersi con l'ellisse].
Le curve ellittiche son funzioni continue il che ci permette di
costruire operazioni binarie tra i suoi vari punti in un modello
geometrico naturale, il che trasforma l'insieme di punti in gruppo
abeliano.
Le CE possono essere definite su qualsiasi campo k.
L'algoritmo e' un miglioramento dell'algoritmo Pollard p-1 ed era il
metodo piu' veloce per trovare i fattori primi di un intero prima
del Generalized Number Field Sieve. Comunque e' ancora l'algoritmo
piu' veloce per interi inferiore a 64 bits [20 cifre].
Il miglioramento consiste nel fatto che l'algoritmo di Lenstra
considera il gruppo di una curva ellittica casuale su un campo finito
Zp [con p primo], il quale ha sempre ordine p - 1. Invece l'ordine
del gruppo della CE su Zp varia casualmente tra  p e 2p.

0]
Sia n il nostro intero da ridurre in fattori primi.

1]
Scegliamo una curva ellittica C: y^2 = x^3 + a*x + b, tale che a e b
appartengano a Z (insieme dei numeri interi).
Scegliamo poi un punto P(x, y).
Sia la scelta di C che la scelta di P dovranno essere pseudo-casuali.
[Se noi fallissimo il tentativo di fattorizzazione con la coppia (C,P)
scelta ora dovremmo sceglierne un'altra a caso.]

2]
Verifichiamo ora che il massimo comun divisore GCD(4a^3 + 27b^2, n) == 1
Se questa condizione e' vera abbiamo la conferma che la curve da noi
scelta e' riducibile mod p. Questo vuol dire che, preso un primo p,
possiamo considerare i coefficienti dell'equazione della curva modulo p
se e solo se questi sono primi con p.
[GCD(K,Z) leggasi massimo comun divisore tra K e Z]
Se 1 &lt; GCD(4a^3 + 27b^2, n) &lt; n allora abbiamo trovato un divisore non
banale di n, quindi abbiamo trovato un fattore primo di n. Ogni volta
che si verifica questa condizione possiamo dividere n per il fattore
trovato e continuare la scomposizione in fattori primi.
Se, invece, troviamo che il risultato del massimo comun divisore e'
uguale ad n allora dobbiamo generare una nuova coppia (C,P).

3]
Prendiamo un intero k tale che questo sia il prodotto di tutti i
numeri primi minori di un certo b scelto a caso.
Assumiamo per facilitare le operazioni di calcolo che b sia inferiore
a un intero a 4 byte senza segno.
Ora e' sufficiente trovare tutti i primi inferiori di questo b e
moltiplicarli, cosicche' k sia multiplo di ognuno di loro.
Si calcoli kP nel gruppo, con il metodo delle potenze veloci, modulo n.
kP e' uno zero della curva ellittica nel gruppo Zp [dove p e' un
divisore primo di n], ma non e' uno zero nel gruppo Zq [dove q e' un
altro divisore di n, con q /= p]. A questo punto possiamo trovare un
fattore di n computando il GCD(xP, n) [dove xP e' la prima coordinata
del punto P].

4] Se il procedimento fallisce e' necessario ripartire con una nuova
coppia (C, P).

Un'implementazione decente dell'algoritmo e' gmp-ecm di Paul Zimmermann
and Alexander Kruppa.
[Il codice dell'SECMI (Simple Elliptic Curve Method Implementation) che
trovate sul mio sito e' una semplice reimplementazione di quello scritto
da Rihard Brent, che dava dei problemi con alcuni numeri e poteva essere
velocizzato].

Per qualsiasi chiarimento scrivetemi a paolo.ardoino@gmail.com
Ciao

Riferimenti:
*1 - The art of computer programming Vol. 1 - 2
*2 - Appunti di crittografia di Giovanni Alberti
*3 - Libri e appunti vari di matematica</pre>
]]></content:encoded>
			<wfw:commentRss>http://ardoino.com/22-maths-factoring-lenstra/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Random prime numbers using OpenSSL bignum</title>
		<link>http://ardoino.com/7-maths-openssl-primes-random/</link>
		<comments>http://ardoino.com/7-maths-openssl-primes-random/#comments</comments>
		<pubDate>Tue, 01 Nov 2005 07:00:00 +0000</pubDate>
		<dc:creator>Paolo Ardoino</dc:creator>
				<category><![CDATA[Crypto/Security]]></category>
		<category><![CDATA[Maths]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[openssl]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[rsa]]></category>

		<guid isPermaLink="false">http://ardoino.com/index.php/1970/01/01/generate-random-prime-numbers-using-openssl-bignum/</guid>
		<description><![CDATA[This simple C program shows how to generate random prime numbers using openssl bignum libraries; it takes as argument the length of the primes in bits.Here&#8217;s the source
Version: 0.1
/**********************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either [...]]]></description>
			<content:encoded><![CDATA[<p>This simple C program shows how to generate random prime numbers using openssl bignum libraries; it takes as argument the length of the primes in bits.Here&#8217;s the source<span id="more-7"></span></p>
<p><strong>Version</strong>: 0.1</p>
<p><span style="color: #808080; font-style: italic;">/**********************************************************************<br />
This program is free software; you can redistribute it and/or modify<br />
it under the terms of the GNU General Public License as published by<br />
the Free Software Foundation; either version 2 of the License, or<br />
(at your option) any later version.<br />
This program is distributed in the hope that it will be useful,<br />
but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. &nbsp;See the<br />
GNU General Public License for more details.</p>
<p>You should have received a copy of the GNU General Public License<br />
along with this program; if not, write to the Free Software<br />
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA &nbsp;02111-1307 USA<br />
************************************************************************<br />
&nbsp; &nbsp; &nbsp; &nbsp; (c) 2004 by Paolo Ardoino &nbsp;&lt; paolo.ardoino@gmail.com &gt;<br />
***********************************************************************/</span></p>
<p><span style="color: #339933;">#define _GNU_SOURCE</span><br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;unistd.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<span style="color: #339933;">#include &lt;string.h&gt;</span><br />
<span style="color: #339933;">#include &lt;openssl/bn.h&gt;</span></p>
<p><span style="color: #993333;">void</span> status<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></p>
<p><span style="color: #993333;">void</span> print_prime<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>prime<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> strlen<span style="color: #009900;">&#40;</span>prime<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> prime<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">&#8216;0&#8242;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> strlen<span style="color: #009900;">&#40;</span>prime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%c&quot;</span><span style="color: #339933;">,</span> prime<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>prime<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; BIGNUM <span style="color: #339933;">*</span>num_tmp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">long</span> <span style="color: #993333;">int</span> num_bits <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argc <span style="color: #339933;">&gt;=</span> 2 <span style="color: #339933;">&amp;&amp;</span> argv<span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_bits <span style="color: #339933;">=</span> atol<span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_bits <span style="color: #339933;">=</span> <span style="color: #0000dd;">1024</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Prime generator by (c) 2004 Paolo Ardoino &lt; paolo.ardoino@gmail.com &gt;<span style="color: #000099; font-weight: bold;">\n</span> usage: ./genprimes [num_bits]<span style="color: #000099; font-weight: bold;">\n</span>Generating %ld bits primes.<span style="color: #000099; font-weight: bold;">\n</span>Wait&#8230;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>num_bits<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; num_tmp <span style="color: #339933;">=</span> BN_new<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">;;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BN_generate_prime<span style="color: #009900;">&#40;</span>num_tmp<span style="color: #339933;">,</span>num_bits<span style="color: #339933;">,</span>1<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>NULL<span style="color: #339933;">,</span>status<span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prime <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span>BN_num_bytes<span style="color: #009900;">&#40;</span>num_tmp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prime <span style="color: #339933;">=</span> BN_bn2dec<span style="color: #009900;">&#40;</span>num_tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print_prime<span style="color: #009900;">&#40;</span>prime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free<span style="color: #009900;">&#40;</span>prime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; BN_free<span style="color: #009900;">&#40;</span>num_tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></p>
<p>&nbsp;</pre>
<p><strong>Download this code:</strong> <a href="http://ardoino.com/pub/maths/openssl_primes_random.txt">openssl_primes_random.txt</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ardoino.com/7-maths-openssl-primes-random/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Factoring large integers: pollard p-1 method</title>
		<link>http://ardoino.com/9-maths-factoring-pollard/</link>
		<comments>http://ardoino.com/9-maths-factoring-pollard/#comments</comments>
		<pubDate>Tue, 09 Mar 2004 07:00:39 +0000</pubDate>
		<dc:creator>Paolo Ardoino</dc:creator>
				<category><![CDATA[Crypto/Security]]></category>
		<category><![CDATA[Maths]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[factorization]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[pollard]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://ardoino.com/maths-factoring-pollard/</guid>
		<description><![CDATA[It is a number theoretic integer factorization algorithm, invented by John Pollard in 1974. It is a special-purpose algorithm, meaning that it is only suitable for integers with specific types of factors; it is the simplest example of an algebraic-group factorisation algorithm]]></description>
			<content:encoded><![CDATA[<p><em>It is a number theoretic integer factorization algorithm, invented by John Pollard in 1974. It is a special-purpose algorithm, meaning that it is only suitable for integers with specific types of factors; it is the simplest example of an algebraic-group factorisation algorithm.</em></p>
<p align="right">From <a href="http://en.wikipedia.org/wiki/Pollard's_p_-_1_algorithm" target="_blank">Wikipedia</a></p>
<p><strong>Pollard p-1 algorithm </strong></p>
<ol>
<li>  N &gt; 2</li>
<li><em>b</em> = ( a finite integer )</li>
<li>Let <em>k</em> be a multiple of all (or nearly all) integers &lt;= <em>b</em>     ( i.e  <em>k</em> = <em>b</em> )</li>
<li>2 &lt;= <em>a</em> &lt;= N &#8211; 2   ( with <em>a</em> random )</li>
<li>Compute Greatest Common Divisor (GCD)  between <em>a</em>^<em>k</em>-1(mod <em>N</em>)  and <em>N</em>
<ul>
<li>So we need to comput  <em>a</em>^<em>k</em>-1  in ZN   and to find the Greatest Common Divisor between this value and <em>N</em> ( i.e : use the Euclid&#8217;s algorithm )</li>
</ul>
</li>
<li>If GCD = 1 then return to 4. ; else if GCD &gt; 1 then  GCD is a prime number and we found a factor of N</li>
</ol>
<p><span id="more-9"></span><br />
<strong>Pollard p-1 method implementation</strong></p>
<p>The following program need GMP (GNU Multi Precision)  library<br />
Compile:<em> gcc spmi.c -o spmi -lgmp -lm</em><br />
<span style="color: #808080; font-style: italic;">/*****************************************************************************/</span><br />
<span style="color: #808080; font-style: italic;">/* This program is free software; you can redistribute it and/or modify &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* it under the terms of the GNU General Public License as published by &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* the Free Software Foundation; either version 2 of the License, or&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* (at your option) any later version.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* This program is distributed in the hope that it will be useful,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* but WITHOUT ANY WARRANTY; without even the implied warranty of &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. &nbsp;See the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* GNU General Public License for more details. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* You should have received a copy of the GNU General Public License&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* along with this program; if not, write to the Free Software&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA &nbsp;02111-1307 &nbsp;USA */</span><br />
<span style="color: #808080; font-style: italic;">/*****************************************************************************/</span><br />
<span style="color: #808080; font-style: italic;">/*&nbsp; &nbsp; &nbsp; (c) 2003 by Paolo Ardoino &nbsp;&lt;paolo.ardoino@gmail.com&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span><br />
<span style="color: #808080; font-style: italic;">/*****************************************************************************/</span></p>
<p><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<span style="color: #339933;">#include &lt;time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;gmp.h&gt;</span></p>
<p><span style="color: #339933;">#define MAX_B 1000L &nbsp;/* MAX b */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">float</span> b <span style="color: #339933;">=</span> <span style="color:#800080;">0.</span><span style="color: #339933;">;</span><br />
&nbsp; mpz_t N<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> GCD<span style="color: #339933;">,</span> tmp<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">struct</span> timeval tm0<span style="color: #339933;">,</span> tm1<span style="color: #339933;">;</span><br />
&nbsp; gmp_randstate_t state<span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;SPMI &#8211; Simple Pollard p-1 Method Implementaion.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;(c) 2003 by Paolo Ardoino &nbsp;&lt;paolo.ardoino@gmail.com&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc <span style="color: #339933;">!=</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Usage: %s &lt;N&gt; &lt;b&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;N&gt;: integer to factorize.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;b&gt;: small integer for computation of k.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>argv <span style="color: #339933;">+</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>argv <span style="color: #339933;">+</span> 2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; mpz_init_set_str<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>argv <span style="color: #339933;">+</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 10<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Errot: Please insert N &gt;= 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;N = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; b <span style="color: #339933;">=</span> atof<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>argv <span style="color: #339933;">+</span> 2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;</span> MAX_B<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Warning: b too large. Setting to %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> MAX_B<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span>MAX_B<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;b = %.0f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mpz_init<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm0<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Tries to compute m = N mod 2 */</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* if m == 0 =&gt; 2|N [2 is a factor of N] */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>mpz_mod_ui<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = 2<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_div_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Checks if N == 1 */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Checks if N is prime */</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Uses a probility primality test that has */</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* probabity of failure == 0.25 ^ x [here x == 10] */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_probab_prime_p<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_perfect_power_p<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;N is a perfect root.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; mpz_init<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mpz_init<span style="color: #009900;">&#40;</span>GCD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mpz_sub_ui<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* tmp = N &#8211; 1 */</span><br />
&nbsp; &nbsp; mpz_init<span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mpz_fac_ui<span style="color: #009900;">&#40;</span>k<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* k = b! */</span><br />
&nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;k = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gmp_randinit_default<span style="color: #009900;">&#40;</span>state<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; mpz_sub_ui<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_urandomm<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> state<span style="color: #339933;">,</span> tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* 0 &lt; a &lt; N &#8211; 2 */</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> 0<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_set_ui<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_powm<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> a<span style="color: #339933;">,</span> k<span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* computes a^k (mod(N)) */</span><br />
&nbsp; &nbsp; &nbsp; mpz_sub_ui<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> tmp<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* a^k &#8211; 1 (mod(N)) */</span><br />
&nbsp; &nbsp; &nbsp; mpz_abs<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_gcd<span style="color: #009900;">&#40;</span>GCD<span style="color: #339933;">,</span> tmp<span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* GCD(a^k &#8211; 1 (mod(N)), N) */</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>GCD<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #808080; font-style: italic;">/* GCD &gt; 1 */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_probab_prime_p<span style="color: #009900;">&#40;</span>GCD<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #808080; font-style: italic;">/* GCD is prime */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> GCD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* GCD is a factor of N */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mpz_div<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> GCD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>GCD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_probab_prime_p<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>GCD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;</pre>
<p><strong>Download this code:</strong> <a href="http://ardoino.com/pub/maths/pollard/pollard.txt">pollard.txt</a><br />
<strong>Results of the Pollard p-1</strong></p>
<p>HARDWARE :<br />
CPU model name	: AMD Athlon(TM) XP 2000+<br />
CPU MHz		: 1666.240<br />
CPU cache size	: 256 KB<br />
CPU bogomips		: 3322.67<br />
RAM MB		: 512 MB<br />
RAM MHz		: 266 MHz</p>
<p>SOFTWARE :<br />
Operative System  : 	Gentoo GNU/Linux [kernel v2.6.2]<br />
spmi.c            : 	my implementation of the Pollard p-1 Method</p>
<p>RESULTS  :<br />
N[integer to factorize]: 3369738766071892021  [2^64]<br />
b = 10<br />
Factor: 204518747<br />
Factor: 16476429743<br />
Factorization has been completed in 352-355 seconds.</p>
<p>N[integer to factorize]: 15236506168104630133 [2^64]<br />
b = 10<br />
Factor: 993427735919<br />
Factor: 15337307<br />
Factorization has been completed in 22 seconds.</p>
<p>69319838167205913708637865377<br />
1026727872351803 * 67515297903059</p>
]]></content:encoded>
			<wfw:commentRss>http://ardoino.com/9-maths-factoring-pollard/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Factoring large integers: classical method</title>
		<link>http://ardoino.com/8-maths-factoring-classical/</link>
		<comments>http://ardoino.com/8-maths-factoring-classical/#comments</comments>
		<pubDate>Mon, 08 Mar 2004 07:00:57 +0000</pubDate>
		<dc:creator>Paolo Ardoino</dc:creator>
				<category><![CDATA[Crypto/Security]]></category>
		<category><![CDATA[Maths]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[classical]]></category>
		<category><![CDATA[factoring]]></category>
		<category><![CDATA[gmp]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[rsa]]></category>

		<guid isPermaLink="false">http://www.ardoino.com/index.php/2004/03/08/factoring-large-integers-classical-and-pollard-p-1-methods/</guid>
		<description><![CDATA[
RSA strength depends on the difficulty to find prime factors of large integers and this is why these kind of algorithms gain a lot of attention.
Take an RSA key of 512 bits [ RSA-512 ]&#8230;.

The GNFS (Generalize Number Field Sieve)  algorithm needed 2 months and 10 days to be completed:

2 months for the sieving [...]]]></description>
			<content:encoded><![CDATA[<p class="adsquare"><!--adsense#Square250x250--></p>
<p>RSA strength depends on the difficulty to find prime factors of large integers and this is why these kind of algorithms gain a lot of attention.<br />
Take an RSA key of 512 bits [ RSA-512 ]&#8230;.<br />
<span id="more-8"></span><br />
The <strong>GNFS</strong> (Generalize Number Field Sieve)  algorithm needed 2 months and 10 days to be completed:</p>
<ul>
<li>2 months for the sieving part on 300 PC 400 MHz with 64Mb di RAM ( for an equivalent of 8000<br />
MIPS-Year )</li>
<li>10 days on a Cray C90 to compute the matrix</li>
</ul>
<p>Having t = ( time to compute 2^512 )<br />
A simple calculus shows that we neeed:</p>
<ul>
<li>LOG(2^576) / LOG(2^512) = 10.9 -&gt; 10.9 * t   for a 576 bits RSA key</li>
<li>LOG(2^1024) / LOG(2^512) = 7 * 10^6 * t   for a 1024 bits RSA key</li>
<li>LOG(2^2048) / LOG(2^512) = 9 * 10^15 * t   for a 2048 bits RSA key</li>
</ul>
<p>As we can see from this results, time required for a 2048 bits key is near the eternity;<br />
even if we have 10^6 computers time required to compute a 2048 bits key is 9 * 10^9 bigger<br />
than time required for a 512 bits key&#8230; so it is out of our possibilities.</p>
<p>Note that the best algorithm for integer factorization is NFS ( Number Field Sieve ).</p>
<p>Now we&#8217;ll take a look to the classical ( and slowest ) method for integers factorization to understand the complexity of this problem.</p>
<p><strong>Classical method algorithm</strong></p>
<ol>
<li>N &gt;= 2</li>
<li>S = square root of N ( we take only the integer part )</li>
<li>Divide N / K (with K odd , K &gt; 1 and K &lt; S)
<ul>
<li>If K is a divisor of N we have found a prime factor of N and then N = N / K Restart from 2.</li>
</ul>
</li>
</ol>
<p><strong>Classical method implementation</strong></p>
<p>The following program need GMP (GNU Multi Precision)  library<br />
Compile: <em>gcc cfmi.c -o cfmi -lgmp</em><br />
<span style="color: #808080; font-style: italic;">/*****************************************************************************/</span><br />
<span style="color: #808080; font-style: italic;">/* This program is free software; you can redistribute it and/or modify &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* it under the terms of the GNU General Public License as published by &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* the Free Software Foundation; either version 2 of the License, or&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* (at your option) any later version.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* This program is distributed in the hope that it will be useful,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* but WITHOUT ANY WARRANTY; without even the implied warranty of &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. &nbsp;See the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* GNU General Public License for more details. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* You should have received a copy of the GNU General Public License&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* along with this program; if not, write to the Free Software&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
<span style="color: #808080; font-style: italic;">/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA &nbsp;02111-1307 &nbsp;USA */</span><br />
<span style="color: #808080; font-style: italic;">/*****************************************************************************/</span><br />
<span style="color: #808080; font-style: italic;">/*&nbsp; &nbsp; &nbsp; (c) 2003 by Paolo Ardoino &nbsp;&lt;paolo.ardoino@gmail.com&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; */</span><br />
<span style="color: #808080; font-style: italic;">/*****************************************************************************/</span></p>
<p>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<span style="color: #339933;">#include &lt;time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;gmp.h&gt;</span></p>
<p><span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #993333;">long</span> <span style="color: #993333;">int</span> tmpui <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; mpz_t N<span style="color: #339933;">,</span> sqrt<span style="color: #339933;">,</span> tmp<span style="color: #339933;">,</span> ctr<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">struct</span> timeval tm0<span style="color: #339933;">,</span> tm1<span style="color: #339933;">;</span><br />
&nbsp;<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>argc <span style="color: #339933;">!=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;CFMI &#8211; Classical Factorization Method <span style="color: #000099; font-weight: bold;">\<br />
</span> &nbsp; &nbsp;Implementaion.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Usage: %s &lt;N&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&lt;N&gt;: integer to factorize.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>argv <span style="color: #339933;">+</span> 1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; mpz_init_set_str<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>argv <span style="color: #339933;">+</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 10<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; fprintf<span style="color: #009900;">&#40;</span>stderr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Errot: Please insert N &gt;= 2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;N = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mpz_init<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm0<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Tries to compute m = N mod 2 */</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* if m == 0 =&gt; 2|N [2 is a factor of N] */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>mpz_mod_ui<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = 2<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_div_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Checks if N == 1 */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Checks if N is prime */</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Uses a probility primality test that has */</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* probabity of failure == 0.25 ^ x [here x == 10] */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_probab_prime_p<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; mpz_init<span style="color: #009900;">&#40;</span>sqrt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; mpz_init_set_ui<span style="color: #009900;">&#40;</span>ctr<span style="color: #339933;">,</span> 3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* sets ctr = 3 */</span><br />
&nbsp; &nbsp; mpz_sqrt<span style="color: #009900;">&#40;</span>sqrt<span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_odd_p<span style="color: #009900;">&#40;</span>sqrt<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; mpz_add_ui<span style="color: #009900;">&#40;</span>sqrt<span style="color: #339933;">,</span> sqrt<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></p>
<p>&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* while ctr &lt; sqrt(N) */</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>mpz_cmp<span style="color: #009900;">&#40;</span>ctr<span style="color: #339933;">,</span> sqrt<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_mod<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> ctr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> ctr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mpz_div<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> ctr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mpz_sqrt<span style="color: #009900;">&#40;</span>sqrt<span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_odd_p<span style="color: #009900;">&#40;</span>sqrt<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mpz_add_ui<span style="color: #009900;">&#40;</span>sqrt<span style="color: #339933;">,</span> sqrt<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; mpz_add_ui<span style="color: #009900;">&#40;</span>ctr<span style="color: #339933;">,</span> ctr<span style="color: #339933;">,</span> 2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_cmp_ui<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> 1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>ctr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>sqrt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>mpz_probab_prime_p<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gmp_printf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factor = %Zd<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>ctr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mpz_clear<span style="color: #009900;">&#40;</span>sqrt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gettimeofday<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>tm1<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Factorization has been completed in %ld seconds.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>\<br />
&nbsp; &nbsp; &nbsp; &nbsp; tm1.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> tm0.<span style="color: #202020;">tv_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; exit<span style="color: #009900;">&#40;</span>EXIT_SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span></p>
<p>&nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
&nbsp;</pre>
<p><strong>Download this code:</strong> <a href="http://ardoino.com/pub/maths/classical/classical.txt">classical.txt</a></p>
<p><strong>Results of classical method</strong></p>
<p>HARDWARE :<br />
CPU model name	: AMD Athlon(TM) XP 2000+<br />
CPU MHz		: 1666.240<br />
CPU cache size	: 256 KB<br />
CPU bogomips		: 3322.67<br />
RAM MB		: 512 MB<br />
RAM MHz		: 266 MHz</p>
<p>SOFTWARE :<br />
Operative System  : 	Gentoo GNU/Linux [kernel v2.6.2]<br />
cfmi.c            : 	my implementation of the classical method</p>
<p>RESULTS  :<br />
N[integer to factorize]: 3369738766071892021 [2^64]<br />
Factor: 204518747<br />
Factor: 16476429743<br />
Factorization has been completed in 1115-1142 seconds.</p>
]]></content:encoded>
			<wfw:commentRss>http://ardoino.com/8-maths-factoring-classical/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
