Overview
We present C++ software for a program which generates a primitive polynomial of degree n modulo p. You can also test a given polynomial for primitivity and find all primitive polynomials.
A sample run from the command line:
$ Primpoly 2 200
Primpoly Version 16.1 - A Program for Computing Primitive Polynomials. Copyright (C) 1999-2020 by Sean Erik O'Connor. All Rights Reserved. Primpoly comes with ABSOLUTELY NO WARRANTY; for details see the GNU General Public License. This is free software, and you are welcome to redistribute it under certain conditions; see the GNU General Public License for details.
Primitive polynomial modulo 2 of degree 200
Self-check passes...
x ^ 200 + x ^ 5 + x ^ 3 + x ^ 2 + 1, 2
Total time: 4.7 seconds on my MacBook Pro.
Now we try a prime modulus p > 2,
Primpoly 337 10
Primitive polynomial modulo 337 of degree 10
x ^ 10 + x + 10, 337
Total time: 20.8 seconds.
Features
- Finds primitive polynomials modulo p of degree n.
- The prime modulus p < 2147483648 for 64-bit machines; p < 32768 for 32-bit machines. In general, p < 2^{m/2 - 1} where m = number of bits in the computer's integer type (defined in Primpoly.h).
- No limit on n, but the program will start to slow for high degree due to the difficulty of factoring large numbers (p^{n}-1)/(p-1) when not using factor tables. Factor tables are available for p = 2, 3, 5, 7, 11, courtesy of Tim's Cunningham Numbers
- Fast, clean implementation of the method of Alanen and Knuth with enhancements due to Sugimoto.
- Please read the user manual which describes all the command line settings, points out some limits in the algorithm and has a detailed example of how to debug the source code.
- Detailed technical memo explaining the theory behind the algorithms.
Download
Primpoly Version 16.1 Source code and executables are distributed under the terms of the GNU General Public License.
Primpoly.cpp | Main program. | View Download |
Primpoly.h | Header file containing parameters and constants. | View Download |
ppArith.cpp | Modulo p integer arithmetic. | View Download |
ppArith.h | Modulo p integer arithmetic class. | View Download |
ppFactor.cpp | Factoring into primes and primality testing. | View Download |
ppFactor.h | Factoring into primes and primality testing class. | View Download |
ppOperationCount.cpp | OperationCount collection. | View Download |
ppOperationCount.h | OperationCount class. | View Download |
ppParser.cpp | Polynomial I/O and factor table I/O. I took the liberty of using my own LALR(1) parser generator for the polynomial input parser and the factor table parser. Here is the polynomial grammar and its LALR(1) parse tables. And here is the factor table grammar and the factor table parse tables The C++ LR parser isn't hard to implement using STL vectors and the automatically generated parse tables above. | View Download |
ppParser.h | Classes for the parser. | View Download |
ppPolynomial.cpp | Polynomial arithmetic. | View Download |
ppPolynomial.h | Polynomial arithmetic class. | View Download |
ppBigInt.cpp | Multiple precision integer arithmetic for non-negative numbers. | View Download |
ppBigInt.h | Multiple precision integer arithmetic class. | View Download |
ppUnitTest.cpp | Unit test for doing a self-check every time we run. Prints to a log file. | View Download |
ppUnitTest.h | Unit test class. | View Download |
makefile | Makefile for Macs, Ubuntu Linux, and Windows/Cygwin. | View Download |
knownGood.txt | Known good test results for automated unit test in makefile. | View Download |
Primpoly.exe.mac.bin | Executable for macOS Catalina 10.15.6 You'll need to download the prime factorization tables below and put them in the same directory as the executable. | Download |
c02minus.txt | Prime factorization tables for 2^{n}-1. | View Download |
c03minus.txt | Prime factorization tables for 3^{n}-1. | View Download |
c05minus.txt | Prime factorization tables for 5^{n}-1. | View Download |
c07minus.txt | Prime factorization tables for 7^{n}-1. | View Download |
c11minus.txt | Prime factorization tables for 11^{n}-1. | View Download |
Download C Version
Primpoly Version 16.1 Source code and executables are distributed under the terms of the GNU General Public License.
This C language version is faster but has limits on the size of p and n since it uses native integer arithmetic. See the user manual for more details. For p=2 we can go as high as n=62. On my MacBook Pro, the time for p=2 and n=62 is 0.38 sec for the C++ version above and 0.002 sec for the C version. For p=2 and n=30, the C++ version takes 0.17 sec but the C version clocks in at 0.02 sec.
Primpoly.c | Main program. | View Download |
Primpoly.h | Header file containing parameters and constants. | View Download |
ppHelperFunc.c | High level helper functions. | View Download |
ppArith.c | Integer arithmetic modulo p. | View Download |
ppFactor.c | Factoring into primes and primality testing. | View Download |
ppIO.c | Polynomial I/O. | View Download |
ppPolyArith.c | Polynomial arithmetic. | View Download |
ppOrder.c | Polynomial order testing. | View Download |
makefile | Makefile for Macs, Ubuntu Linux and Windows/Cygwin. | View Download |
knownGoodC.txt | Known good test results for automated unit test in makefile. | View Download |
PrimpolyC.exe.mac.bin | Executable for macOS Catalina 10.15.6 | Download |
Install and Run
The source builds on Mac, Ubuntu Linux and Windows platforms.
Source Code Setup
First of all, after downloading the source files, strip off the prefix. For example, this download will give you the file Project_SourceCode_PrimpolyC_Primpoly.c. Strip off the prefix and rename the file to Primpoly.c
Next, in order for the makefile to run properly, the directories must be organized in a certain way. For example, let's say the parent directory is /Users/seanoconnor/Desktop/Sean/WebSite/ Mathematics/AbstractAlgebra/PrimitivePolynomials/Project and that we are in it right now.
Let's look at what the subdirectories must be:
You also need to put the hostname of your computer into the makefile so it can build under the correct operating system. For example, on my MacBook Pro running Ubuntu Linux (Bionic Beaver) here is my hostname,
So in the makefile, I need this entry which tells the build is for a Linux machine:
Factor Tables (Primpoly C++ Version Only)
You'll need to have the factor tables in the same directory as the executable or in a subdirectory.
If you run the program and it can't find the factor tables, it will fail the self check and tell you what happened in the unitTest.log file.
Source Level Debugging and Profiling
On Mac OS X, I use the Xcode IDE for debugging and profiling
On Ubuntu Linux, I build with make and compile with clang and use the command line debugger lldb.
On Windows platforms, I use the GNU Cygwin toolset for command line compiling with g++ I use Microsoft Visual Studio. for debugging.
Uses for Primitive Polynomials:
- Generating pseudonoise (PN) sequences for spread spectrum communications and chip fault testing.
- Generating Sobol sequences for high dimensional numerical integration
- Generating CRC and Hamming codes.
- Generating Galois (finite) fields for use in implementing Reed-Solomon and BCH error correcting codes.
Acknowledgements
- Thanks to Martin Becker for pointing out a bug when trying to do the primitive polynomial search on p = 65003, n = 2 where the multiple precision arithmetic base is exceeded and also for pointing out a bug in computing Euler Phi for the number of polynomials when p > 2.
- Thanks to K. Jambunathan of the Indian Statistical Institute, Calcutta, India for first suggesting that I increase the numeric precision of the calculations, allowing higher degree polynomials to be generated.
- Thanks to Ted Ford of Hampshire, Britain for extending the program to print all primitive polynomials, for suggesting I replace the stored table of primitive roots with an algorithmic test, and many interesting observations on maximal length sequences.
- Thanks to Tielin Liang for pointing out a mistake in one of my Lemmas in the theoretical section.
- Thanks to Majid Bakhtiari for pointing out that Mersenne primes are an important special case for which we can quickly find extremely high degree polynomials.
- Thanks to Alan Meghrigian for suggesting valuable corrections to the notes.
- Thanks to John McKay for mentioning Conway polynomials which are a special class of primitive polynomial used to represent finite fields.
- Thanks to Sid Paral for pointing out bugs in array bounds checking in the polynomial parser.
- Thanks to Subrata Nandi for pointing out that I need instructions on how to set up the source code in the proper directory organization to so that the makefile will build properly.
- Thanks to Tony Freitas for pointing out two compile errors and how to fix them in Visual Studio 2017 (Microsoft Windows).
- Thanks to Martin Dowd for pointing out methods for finding all primitive polynomials given a single primitive polynomial.
- Thanks to Max-Julian Jakobitsch for pointing out MathJax rendering was broken and how to fix it.
- And finally, thanks to Eric W. Weisstein's World of Mathematics for referencing this web page under primitive polynomials.