AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

17
AKS AKS Implementation of a Implementation of a Deterministic Primality Deterministic Primality Algorithm Algorithm R. Salembier & P. Southerington R. Salembier & P. Southerington

Transcript of AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Page 1: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

AKSAKS

Implementation of a Deterministic Implementation of a Deterministic Primality AlgorithmPrimality Algorithm

R. Salembier & P. SoutheringtonR. Salembier & P. Southerington

Page 2: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

AKS Primality TestAKS Primality Test

Deterministic primality-proving Deterministic primality-proving algorithmalgorithm Manindra Agrawal, Neeraj Kaval, Nitin Saxena Manindra Agrawal, Neeraj Kaval, Nitin Saxena ““PRIMES is in P,” 6 August 2002PRIMES is in P,” 6 August 2002

Determines whether a number is Determines whether a number is prime or composite.prime or composite. There is no probability associated as with Miller-There is no probability associated as with Miller-

Rabin TestRabin Test

Page 3: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

1.1. If (n = aIf (n = abb for a for a N and b > 1), output N and b > 1), output COMPOSITE.COMPOSITE.

2.2. Find the smallest r such that OFind the smallest r such that Orr(n) > 4 log(n) > 4 log22nn 3.3. If 1 < GCD(a, n) < n for all a ≤ r, output If 1 < GCD(a, n) < n for all a ≤ r, output

COMPOSITE.COMPOSITE. 4.4. If n ≤ r, output PRIME.If n ≤ r, output PRIME. 5.5. For a = 1 toFor a = 1 to if (( x + a)if (( x + a)nn x xnn + a (mod x + a (mod xrr – 1, n)) – 1, n)) output COMPOSITE.output COMPOSITE. 6.6. Output PRIME.Output PRIME.

Original AKS AlgorithmOriginal AKS AlgorithmAugust 6, 2002 version:August 6, 2002 version:

nr log)(2

Page 4: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

1.1. If (n = aIf (n = abb for a for a N and b > 1), output N and b > 1), output COMPOSITE.COMPOSITE.

2.2. Find the smallest r such that OFind the smallest r such that Orr(n) > (n) > loglog22nn 3.3. If gcd(a, n) ≠ 1 for all a ≤ r, output If gcd(a, n) ≠ 1 for all a ≤ r, output

COMPOSITE.COMPOSITE. 4.4. For a = 1 to For a = 1 to if (( x + a)if (( x + a)nn ≡≡ x xnn + a (mod x + a (mod xrr-1, n)), output -1, n)), output

PRIMEPRIME..

Lenstra/Pomerance VariantLenstra/Pomerance VariantRevised version we are using:Revised version we are using:

nr log

Page 5: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

ComplexityComplexity

Big-O NotationBig-O Notation Read as ‘order of’Read as ‘order of’ Indicates the dominant termIndicates the dominant term

AKS runs in polynomial timeAKS runs in polynomial time Original: Õ (logOriginal: Õ (log1212 (n)) (n)) Lenstra/Pomerance: Õ (logLenstra/Pomerance: Õ (log7.57.5 (n)) (n))

Page 6: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

LiDIA ImplementationLiDIA Implementation

Perfect Power TestPerfect Power Test used native used native is_power()is_power() function function

GCD TestGCD Test Initially used gcd()Initially used gcd()

Traditional method of GCD calculationTraditional method of GCD calculation Changed to bgcd()Changed to bgcd()

Eliminates divisionEliminates division Uses shifts and addsUses shifts and adds

ExponentiationExponentiation Left-to-Right ExpansionLeft-to-Right Expansion Modular reduction at each stepModular reduction at each step

Page 7: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Division OptimizationDivision Optimization

Split polynomial into three coefficient Split polynomial into three coefficient vectorsvectors Terms with exponents > r-1Terms with exponents > r-1 Term with exponent r-1Term with exponent r-1 Terms with exponents < r-1Terms with exponents < r-1

Subtracted r from each exponent in first Subtracted r from each exponent in first setset

Added first and third vectors togetherAdded first and third vectors together

Page 8: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Division OptimizationDivision Optimization

0

2000

4000

6000

8000

10000

12000

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43

Number of Bits in n

Tim

e (s

eco

nd

s)

Original

Optimized

Page 9: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Squaring “Optimization”Squaring “Optimization”

Constructed squaring routineConstructed squaring routine Dealt with first and last terms Dealt with first and last terms

individuallyindividually Looped through all terms pairing Looped through all terms pairing

togethertogether 91% of test in squaring function91% of test in squaring function Operation times actually increasedOperation times actually increased

Confirmed problem using GMPConfirmed problem using GMP

Page 10: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

NTL ImplementationNTL Implementation

Efficient polynomial multiplicationEfficient polynomial multiplication LiDIA used only classical multiplicationLiDIA used only classical multiplication NTL Automatically chooses from:NTL Automatically chooses from:

Classical algorithmClassical algorithm Karatsuba methodKaratsuba method Two Fast Fourier Transform (FFT) methodsTwo Fast Fourier Transform (FFT) methods

Dramatic performance increaseDramatic performance increase 50% of time spent in FFT50% of time spent in FFT May be able to adjust selection boundariesMay be able to adjust selection boundaries

Page 11: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

LiDIA vs. NTLLiDIA vs. NTL

0

10000

20000

30000

40000

50000

60000

70000

1 5 9 13 17 21 25 29 33 37 41 45 49 53 57

Number of bits of n

Tim

e (s

eco

nd

s)

LiDIA

NTL

Page 12: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

NTL PotentialNTL Potential

Implement native Perfect Power testImplement native Perfect Power test Currently falling back to GMPCurrently falling back to GMP

Possible Alternate ImplementationsPossible Alternate Implementations Manually reduce coefficientsManually reduce coefficients Represent polynomial directly as a ringRepresent polynomial directly as a ring

Let NTL handle polynomial reductionLet NTL handle polynomial reduction

Page 13: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Performance ComparisonPerformance ComparisonCrandall & PapadopoulisCrandall & Papadopoulis

Apple / UMDApple / UMD

0

500

1000

1500

2000

2500

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46

Number of bits of n

Tim

e (s

eco

nd

s)

NTL

Apple

Page 14: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Conjecture ImprovementConjecture Improvement

Improves time to Õ (logImproves time to Õ (log33 (n)) (n)) Conjecture:Conjecture:

If n mod r ≠ 0 mod r andIf n mod r ≠ 0 mod r and If (X – 1)If (X – 1)nn ΞΞ (X (Xnn – 1) mod (X – 1) mod (Xrr -1, n) -1, n) And nAnd n22 mod r ≠ -1 mod r mod r ≠ -1 mod r n is PRIMEn is PRIME

From tests this appears to be trueFrom tests this appears to be true If tests holds for a =1 then it holds for all aIf tests holds for a =1 then it holds for all a

Conjecture is still not provenConjecture is still not proven

Page 15: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Extrapolation with Extrapolation with ConjectureConjecture

0

0.5

1

1.5

2

2.5

3

3.5

1 5 9 13 17 21 25 29 33 37 41 45 49 53 57

Number of bits of n

Tim

e (S

eco

nd

s)

Conjecture

Page 16: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

Future WorkFuture Work

Native GMP ImplementationNative GMP Implementation Allow total control over polynomial operationsAllow total control over polynomial operations Very difficult to do more efficiently than NTLVery difficult to do more efficiently than NTL

Bernstein Exponentiation OptimizationBernstein Exponentiation Optimization Map polynomial ring onto integer ringMap polynomial ring onto integer ring

Qi Cheng MethodQi Cheng Method One round of Elliptic Curve Primality TestOne round of Elliptic Curve Primality Test One iteration of AKSOne iteration of AKS

Page 17: AKS Implementation of a Deterministic Primality Algorithm R. Salembier & P. Southerington.

ConclusionsConclusions

Our implementation comparable to others’Our implementation comparable to others’ NTL better suited to AKS than LiDIANTL better suited to AKS than LiDIA AKS still too slow for practical useAKS still too slow for practical use

ConjectureConjecture Bernstein / Qi ChengBernstein / Qi Cheng

Focus should be on algorithmic Focus should be on algorithmic improvementsimprovements