BigInteger Multitasking Agrawal, Kayal, and Saxena (AKS) Primality Test (c) June 19, 2016, by James Pate Williams, Jr. Microsoft TechNet Project Description

This application implements the algorithm described in the paper at the URL http://www.cse.iitk.ac.in/users/manindra/algebra/primality_v6.pdf. I replaced Step 1 of the algorithm with the Miller-Rabin probabilistic primality test. If that test shows that the number is composite, I return the value COMPOSITE. This algorithm is much easier to implement and understand that Wieb Bosmer’s Primality Proving with Cyclotomy also known as the Jacobi sums primality test. As a test we determine that the following Mersenne numbers are prime: M_1279, M_2203, M_2281, M_3217, and M_4253 where M_n = 2 ^ n – 1, and the primes have 386, 664, 687, 969, and 1281 decimal digits, respectively. M_1279 was first proven prime by Raphael M. Robinson on June 25, 1952, using the Lucas-Lehmer test on a SWAC computer. The same author found that M_2203 was prime on October 7, 1952, and M_2281 was prime on October 9, 1952, using the same method and computer. Hans Riesel determined that M_3217 was prime on September 8, 1957, using the Lucas-Lehmer test on a BESK computer. M_4253 was proven prime on November 3, 1961 by Alexander Hurwitz using the Lucas-Lehmer test on an IBM 7090 mainframe computer. See http://www.mersenne.org/primes/ for many more Mersenne primes. All of the computations illustrated below were performed on a late November 2015 Dell XPS 8900 computer with 16 GB RAM Intel(R) Core(TM) i7-6700K CPU @ 4.00 GHz running Windows 10 Pro. The .Net framework is .Net 4.5.2. This is a multitasking version of the original BigInteger variant of the application. Someone with a quad core CPU with 8 virtual processors can try NumberTasks = 8 to see if that speeds up this application more or less. I usually try to limit the number of tasks to actual number of cores.

Preliminary Factorization Results of the Thirteenth Fermat Number (c) February 5, 2024, by James Pate Williams, Jr.

I am working on a factorization of the Thirteenth Fermat number which is 2 ^ 8192 + 1 and is 2,467 decimal digits in length. I am using Pollard’s factoring with cubic integers on the number (2 ^ 2731) ^ 3 + 2. I am also utilizing a homegrown variant of the venerable Pollard and Brent rho method and Arjen K. Lenstra’s Free LIP Elliptic Curve Method. I can factor the seventh Fermat number 2 ^ 128 + 1 in five to thirty minutes using my C# code. The factoring with cubic integers code is in C and uses Free-LIP.

Fermat factoring status (prothsearch.com)

The following is a run of Lenstra’s ECM algorithm:

== Data Menu ==
1 Simple Number
2 Fibonacci Sequence Number
3 Lucas Sequence Number
4 Exit
Enter option (1 – 4): 1
Enter a number to be factored: 2^8192+1
Enter a random number generator seed: 1
== Factoring Menu ==
1 Lenstra’s ECM
2 Lenstra’s Pollard-Rho
3 Pollard’s Factoring with Cubic Integers
Option (1 – 3): 1

2710954639361 p # digits 13
3603109844542291969 p # digits 19
Runtime (s) = 17015.344000

I aborted the previous computation due to the fact I was curious about the number of prime factors that could be found on personal computer. I will try a lot more calculation time in a future run. My homegrown application is able to at least find the first factor of Fermat Number 13.

Factorizations of Some Fibonacci Sequence Numbers, Lucas Sequence Numbers and Some Other Numbers Using Arjen K. Lenstra’s Free Large Integer Package and the Elliptic Curve Method (c) January 28, 2024, by James Pate Williams, Jr.

All of the following computations were performed on a late 2015 Dell XPS 8900 personal computer with a 64-bit Intel Core I7 processor @ 4.0GHz with 16GB of DDR2 RAM.

Factorization of Six Fibonacci Sequence Numbers:

Fibonacci 500
# digits 105
5 ^ 2 p # digits 1
15 c # digits 2
101 p # digits 3
401 p # digits 3
1661 c # digits 4
3001 p # digits 4
10291 c # digits 5
570601 p # digits 6
112128001 p # digits 9
1353439001 p # digits 10
28143378001 p # digits 11
5465167948001 p # digits 13
84817574770589638001 p # digits 20
158414167964045700001 p # digits 21
Runtime (s) = 1.206000

Fibonacci 505
# digits 106
5 p # digits 1
743519377 p # digits 9
44614641121 p # digits 11
770857978613 p # digits 12
960700389041 p # digits 12
12588421794766514566269164716286291055826556238643852856601641 p # digits 62
Runtime (s) = 1.959000

Fibonacci 510
# digits 107
2 ^ 3 p # digits 1
11 p # digits 2
61 p # digits 2
1021 p # digits 4
1597 p # digits 4
3469 p # digits 4
3571 p # digits 4
9521 p # digits 4
53551 p # digits 5
95881 p # digits 5
142445 c # digits 6
1158551 p # digits 7
3415914041 p # digits 10
20778644396941 p # digits 14
20862774425341 p # digits 14
81358225616651 c # digits 14
162716451241291 p # digits 15
Runtime (s) = 2.682000

Fibonacci 515
# digits 108
5 p # digits 1
519121 p # digits 6
5644193 p # digits 7
512119709 p # digits 9
84388938382141 p # digits 14
300367026458796424297447559250634818495937628065437243817852436228914621 p # digits 72
Runtime (s) = 7.861000

Fibonacci 520
# digits 109
131 p # digits 3
451 c # digits 3
521 p # digits 3
2081 p # digits 4
2161 p # digits 4
3121 p # digits 4
24571 p # digits 5
90481 p # digits 5
2519895 c # digits 7
21183761 p # digits 8
57089761 p # digits 8
102193207 p # digits 9
1932300241 p # digits 10
14736206161 p # digits 11
5836312049326721 p # digits 16
42426476041450801 p # digits 17
Runtime (s) = 5.155000

Fibonacci 525
# digits 110
2 p # digits 1
5 p # digits 1
421 p # digits 3
701 p # digits 3
3001 p # digits 4
3965 c # digits 4
4201 p # digits 4
141961 p # digits 6
2553601 p # digits 7
230686501 p # digits 9
8288823481 p # digits 10
82061511001 p # digits 11
19072991752501 c # digits 14
8481116649425701 p # digits 16
17231203730201189308301 p # digits 23
Runtime (s) = 2.026000


Factorization of Six Lucas Sequence Numbers

Lucas 340
113709744839525149336680459091826532688903186653162057995534262332121127
# digits 72
7 p # digits 1
2161 p # digits 4
5441 p # digits 4
897601 p # digits 6
23230657239121 p # digits 14
17276792316211992881 p # digits 20
3834936832404134644974961 p # digits 25
Runtime (s) = 109.103000

Lucas 345
# digits 73
2 ^ 2 p # digits 1
31 p # digits 2
461 p # digits 3
1151 p # digits 4
1529 c # digits 4
324301 p # digits 6
686551 p # digits 6
1485571 p # digits 7
4641631 p # digits 7
19965899801 c # digits 11
117169733521 p # digits 12
3490125311294161 p # digits 16
Runtime (s) = 0.032000


Lucas 350
13985374084677485786380981408251904922622980674054858121032362563653278123
# digits 74
3 p # digits 1
401 p # digits 3
2801 p # digits 4
11521 c # digits 5
28001 p # digits 5
570601 p # digits 6
12317523121 p # digits 11
248773766357061401 p # digits 18
7358192362316341243805801 p # digits 25
Runtime (s) = 21.047000


Lucas 355
69362907070206748494476200566565775354902428015845969798000696945226974645
# digits 74
5 p # digits 1
4261 p # digits 4
6673 p # digits 4
75309701 p # digits 8
309273161 p # digits 9
46165371073 p # digits 11
9207609261398081 p # digits 16
49279722643391864192801 p # digits 23
Runtime (s) = 40.726000


Lucas 360
769246427201094785080787978422393713094534885688979999504447628313150135520
# digits 75
2 ^ 5 p # digits 1
3 ^ 2 p # digits 1
23 p # digits 2
41 p # digits 2
105 c # digits 3
107 p # digits 3
241 p # digits 3
2161 p # digits 4
2521 p # digits 4
3439 c # digits 4
8641 p # digits 4
20641 p # digits 5
103681 p # digits 6
109441 p # digits 6
191306797 c # digits 9
10783342081 p # digits 11
13373763765986881 p # digits 17
Runtime (s) = 0.032000


Lucas 365
19076060504701386559675231910437330047906343529583769121365013189782992678011
# digits 77
11 p # digits 2
151549 p # digits 6
514651 p # digits 6
7015301 p # digits 7
8942501 p # digits 7
9157663121 p # digits 10
11899937029 p # digits 11
3252336525249736694804553589211 p # digits 31


The following two numbers were first factorized by J. M. Pollard on an 8-bit Phillips P2012 personal computer with 64 KB RAM and two 640 KB disc drives. The times required by Pollard were 41 and 47 hours.

2^144-3
22300745198530623141535718272648361505980413
# digits 44
492729991333 p # digits 12
45259565260477899162010980272761 p # digits 32
Runtime (s) = 0.086000


2^153+3
11417981541647679048466287755595961091061972995
# digits 47
5 p # digits 1
11 p # digits 2
600696432006490087537 p # digits 21
345598297796034189382757 p # digits 24
Runtime (s) = 0.676000


Partial factorization of the Twelfth Fermat Number 2^4096+1
# digits 1234
114689 p # digits 6
26017793 p # digits 8
63766529 p # digits 8
190274191361 p # digits 12
Runtime (s) = 1532.878000

Brute Force Elliptic Curve Point Counting Algorithm Implementation by James Pate Williams, Jr.

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <chrono>
#include <vector>
using namespace std;

typedef long long ll;

typedef struct ecPoint
{
	ll x, y;
} ECPOINT, *PECPOINT;

typedef struct ecPointOrder
{
	ll order;
	ECPOINT pt;
} ECPOINTORDER, * PECPOINTORDER;

int JACOBI(ll a, ll n)
{
	int e = 0, s;
	ll a1, b = a, m, n1;

	if (a == 0) return 0;
	if (a == 1) return 1;
	while ((b & 1) == 0)
	{
		b >>= 1;
		e++;
	}
	a1 = b;
	m = n % 8;
	if (!(e & 1)) s = 1;
	else if (m == 1 || m == 7) s = +1;
	else if (m == 3 || m == 5) s = -1;
	if (n % 4 == 3 && a1 % 4 == 3) s = -s;
	if (a1 != 1) n1 = n % a1; else n1 = 1;
	return s * JACOBI(n1, a1);
}

ll ExpMod(ll x, ll b, ll n)
/* returns x ^ b mod n */
{
	ll a = 1LL, s = x;

	if (b == 0)
		return 1LL;
	while (b != 0) {
		if (b & 1l) a = (a * s) % n;
		b >>= 1;
		if (b != 0) s = (s * s) % n;
	}
	if (a < 0)
		a += n;
	return a;
}

ll ExtendedEuclidean(ll b, ll n)
{
	ll b0 = b, n0 = n, t = 1, t0 = 0, temp, q, r;

	q = n0 / b0;
	r = n0 - q * b0;

	while (r > 0) {
		temp = t0 - q * t;
		if (temp >= 0) temp = temp % n;
		else temp = n - (-temp % n);
		t0 = t;
		t = temp;
		n0 = b0;
		b0 = r;
		q = n0 / b0;
		r = n0 - q * b0;
	}
	if (b0 != 1) return 0;
	else return t % n;
}

ll Weierstrass(ll a, ll b, ll x, ll p)
{
	return ((((x * x) % p) * x) % p + (a * x) % p + b) % p;
}

ll EPoints(
	bool print,
	ll a, ll b, ll p,
	vector<ECPOINT>& e)
	/* returns the number of points on the elliptic
	curve y ^ 2 = x ^ 3 + ax + b mod p */
{
	ll count = 0, m = (p + 1) / 4, x, y;
	ECPOINT pt{};

	if (p % 4 == 3) {
		for (x = 0; x < p; x++) {
			y = Weierstrass(a, b, x, p);
			if (JACOBI(y, p) != -1) {
				y = ExpMod(y, m, p);
				if (print)
				{
					cout << "(" << setw(2) << x << ", ";
					cout << y << ") " << endl;
				}
				pt.x = x;
				pt.y = y;
				e.push_back(pt);
				count++;
				y = -y % p;
				if (y < 0) y += p;
				if (y != 0)
				{
					if (print)
					{
						cout << "(" << setw(2) << x << ", ";
						cout << y << ") " << endl;
					}
					pt.x = x;
					pt.y = y;
					e.push_back(pt);
					count++;
				}
				if (print && count % 5 == 0)
					cout << endl;
			}
		}
		if (print && count % 5 != 0)
			cout << endl;
	}
	return count;
}

void Add(ll a, ll p, ECPOINT P,
	ECPOINT Q, ECPOINT& R)
	/* elliptic curve point partial addition */
{
	ll i, lambda;

	if (P.x == Q.x && P.y == 0 && Q.y == 0) {
		R.x = 0;
		R.y = 1;
		return;
	}
	if (P.x == Q.x && P.y == p - Q.y) {
		R.x = 0;
		R.y = 1;
		return;
	}
	if (P.x == 0 && P.y == 1) {
		R = Q;
		return;
	}
	if (Q.x == 0 && Q.y == 1) {
		R = P;
		return;
	}
	if (P.x != Q.x) {
		i = Q.x - P.x;
		if (i < 0) i += p;
		i = ExtendedEuclidean(i, p);
		lambda = ((Q.y - P.y) * i) % p;
	}
	else {
		i = ExtendedEuclidean((2 * P.y) % p, p);
		lambda = ((3 * P.x * P.x + a) * i) % p;
	}
	if (lambda < 0) lambda += p;
	R.x = (lambda * lambda - P.x - Q.x) % p;
	R.y = (lambda * (P.x - R.x) - P.y) % p;
	if (R.x < 0) R.x += p;
	if (R.y < 0) R.y += p;
}

void Multiply(
	ll a, ll k, ll p,
	ECPOINT P,
	ECPOINT& R)
{
	ECPOINT S;

	R.x = 0;
	R.y = 1;
	S = P;
	while (k != 0) {
		if (k & 1) Add(a, p, R, S, R);
		k >>= 1;
		if (k != 0) Add(a, p, S, S, S);
	}
}

ll Order(ll a, ll p, ECPOINT P)
{
	ll order = 1;
	ECPOINT Q = P, R{};

	do {
		order++;
		Add(a, p, P, Q, R);
		Q = R;
	} while (R.x != 0 && R.y != 1);
	return order;
}

const int PrimeSize = 10000000;
bool sieve[PrimeSize];

void PopulateSieve() {

	// sieve of Eratosthenes

	int c, inc, i, n = PrimeSize - 1;

	for (i = 0; i < n; i++)
		sieve[i] = false;

	sieve[1] = false;
	sieve[2] = true;

	for (i = 3; i <= n; i++)
		sieve[i] = (i & 1) == 1 ? true : false;

	c = 3;

	do {
		i = c * c;
		inc = c + c;

		while (i <= n) {
			sieve[i] = false;
			i += inc;
		}

		c += 2;
		while (!sieve[c])
			c++;
	} while (c * c <= n);
}

ll Partition(
	vector<ECPOINTORDER>& a, ll n, ll lo, ll hi)
{
	ll pivotIndex = lo + (hi - lo) / 2;
	ECPOINTORDER po = a[(unsigned int)pivotIndex];
	ECPOINTORDER x = po;
	ECPOINTORDER t = x;

	a[(unsigned int)pivotIndex] = a[(unsigned int)hi];
	a[(unsigned int)hi] = t;

	ll storeIndex = lo;

	for (unsigned int i = (unsigned int)lo; i < (unsigned int)hi; i++)
	{
		if (a[i].order < x.order)
		{
			t = a[i];
			a[i] = a[(unsigned int)storeIndex];
			a[(unsigned int)(storeIndex++)] = t;
		}
	}

	t = a[(unsigned int)storeIndex];
	a[(unsigned int)storeIndex] = a[(unsigned int)hi];
	a[(unsigned int)hi] = t;

	return storeIndex;
}

static void DoQuickSort(
	vector<ECPOINTORDER>& a, ll n, ll p, ll r)
{
	if (p < r)
	{
		ll q = Partition(a, n, p, r);

		DoQuickSort(a, n, p, q - 1);
		DoQuickSort(a, n, q + 1, r);
	}
}

void QuickSort(vector<ECPOINTORDER>& a, ll n)
{
	DoQuickSort(a, n, 0, n - 1);
}

int main()
{
	PopulateSieve();

	while (true)
	{
		bool noncyclic = false;
		int option;
		ll a, b, p;

		cout << "Enter 1 to continue or 0 to quit = ";
		cin >> option;

		if (option == 0)
			break;

		cout << "Weierstrass parameter a = ";
		cin >> a;
		cout << "Weierstrass parameter b = ";
		cin >> b;
		cout << "Enter the prime parameter p = ";
		cin >> p;

		if (!sieve[p])
		{
			cout << "p is not a prime number. " << endl;
			continue;
		}

		if (p % 4 != 3)
		{
			cout << "p mod 4 must be 3 for this algorithm." << endl;
			continue;
		}

		bool print = false, enumerate = false;
		vector<ECPOINT> e;
		vector<ECPOINTORDER> eOrder;

		cout << "Enumerate points (0 for no or 1 for yes)? ";
		cin >> option;

		enumerate = option == 1;
		auto time0 = chrono::high_resolution_clock::now();
		ll count = EPoints(print, a, b, p, e);
		auto time1 = chrono::high_resolution_clock::now();
		auto elapsed = time1 - time0;
		ll h = count + 1;
		ll maxOrder = 0;
		ll minOrder = h;
		ll order = -1;
		ECPOINT P{}, Q{};

		if (enumerate)
			cout << "The points (x, y) on E and their orders are:" << endl;

		for (unsigned int i = 0; i < count; i++)
		{
			ECPOINTORDER ptOrder{};

			order = Order(a, p, e[i]);
			ptOrder.order = order;
			ptOrder.pt = e[i];
			eOrder.push_back(ptOrder);

			if (order < h)
				noncyclic = true;
			if (order < minOrder) {
				P = e[i];
				minOrder = order;
			}
			if (order > maxOrder) {
				Q = e[i];
				maxOrder = order;
			}
		}
		if (enumerate)
		{
			QuickSort(eOrder, count);

			for (unsigned int i = 0; i < eOrder.size(); i++)
			{
				cout << "(" << eOrder[i].pt.x << ", ";
				cout << eOrder[i].pt.y << ") ";
				cout << eOrder[i].order << "\t";
				if ((i + 1) % 5 == 0)
					cout << endl;
			}
			if (count % 5 != 0)
				cout << endl;
		}
		cout << "#E = " << h << endl;
		cout << "Noncyclic = " << noncyclic << endl;
		cout << "Minimum order = " << minOrder << endl;
		cout << "Maximum order = " << maxOrder << endl;
		cout << "(" << P.x << ", " << P.y << ")" << endl;
		cout << "(" << Q.x << ", " << Q.y << ")" << endl;

		long long runtime = chrono::duration_cast<chrono::milliseconds>(elapsed).count();
		if (runtime != 0)
			cout << "Point counting runtime in milliseconds = " << runtime << endl;
		else
		{
			runtime = chrono::duration_cast<chrono::microseconds>(elapsed).count();
			cout << "Point counting runtime in microseconds = " << runtime << endl;
		}
	}
	return 0;
}

Stinson Exercise 5.6 Galois Field

/*
Author:  Pate Williams (c) 1997

Exercise
"5.6 The field GF(2 ^ 5) can be constructed as
Z_2[x]/(x ^ 5 + x ^ 2 + 1). Perform the following
computations in this field.
(a) Compute (x ^ 4 + x ^ 2) * (x ^ 3 + x + 1).
(b) Using the Extended Euclidean algorithm
compute (x ^ 3 + x ^ 2) ^ - 1.
(c) Using the square and multiply algorithm,
compute x ^ 25." -Douglas R. Stinson-
See "Cryptography: Theory and Practice" by
Douglas R. Stinson page 201.
*/

#include <math.h>
#include <stdio.h>

#define SIZE 32l

void poly_mul(long m, long n, long *a, long *b, long *c, long *p)
{
	long ai, bj, i, j, k, sum;

	*p = m + n;
	for (k = 0; k <= *p; k++) {
		sum = 0;
		for (i = 0; i <= k; i++) {
			j = k - i;
			if (i > m) ai = 0; else ai = a[i];
			if (j > n) bj = 0; else bj = b[j];
			sum += ai * bj;
		}
		c[k] = sum;
	}
}

void poly_div(long m, long n, long *u, long *v,
	long *q, long *r, long *p, long *s)
{
	long j, jk, k, nk, vn = v[n];

	for (j = 0; j <= m; j++) r[j] = u[j];
	if (m < n) {
		*p = 0, *s = m;
		q[0] = 0;
	}
	else {
		*p = m - n, *s = n - 1;
		for (k = *p; k >= 0; k--) {
			nk = n + k;
			q[k] = r[nk] * pow(vn, k);
			for (j = nk - 1; j >= 0; j--) {
				jk = j - k;
				if (jk >= 0)
					r[j] = vn * r[j] - r[nk] * v[j - k];
				else
					r[j] = vn * r[j];
			}
		}
		while (*p > 0 && q[*p] == 0) *p = *p - 1;
		while (*s > 0 && r[*s] == 0) *s = *s - 1;
	}
}

void poly_exp_mod(long degreeA, long degreem, long n,
	long modulus, long *A, long *m, long *s,
	long *ds)
{
	int zero;
	long dp, dq, dx = degreeA, i;
	long p[SIZE], q[SIZE], x[SIZE];

	*ds = 0, s[0] = 1;
	for (i = 0; i <= dx; i++) x[i] = A[i];
	while (n > 0) {
		if ((n & 1) == 1) {
			/* s = (s * x) % m; */
			poly_mul(*ds, dx, s, x, p, &dp);
			poly_div(dp, degreem, p, m, q, s, &dq, ds);
			for (i = 0; i <= *ds; i++) s[i] %= modulus;
			zero = s[*ds] == 0, i = *ds;
			while (i > 0 && zero) {
				if (zero) *ds = *ds - 1;
				zero = s[--i] == 0;
			}
		}
		n >>= 1;
		/* x = (x * x) % m; */
		poly_mul(dx, dx, x, x, p, &dp);
		poly_div(dp, degreem, p, m, q, x, &dq, &dx);
		for (i = 0; i <= dx; i++) x[i] %= modulus;
		zero = x[dx] == 0, i = dx;
		while (i > 0 && zero) {
			if (zero) dx--;
			zero = x[--i] == 0;
		}
	}
}

void poly_copy(long db, long *a, long *b, long *da)
/* a = b */
{
	long i;

	*da = db;
	for (i = 0; i <= db; i++) a[i] = b[i];
}

int poly_Extended_Euclidean(long db, long dn,
	long *b, long *n,
	long *t, long *dt)
{
	int nonzero;
	long db0, dn0, dq, dr, dt0 = 0, dtemp, du, i;
	long b0[SIZE], n0[SIZE], q[SIZE];
	long r[SIZE], t0[SIZE], temp[SIZE], u[SIZE];

	*dt = 0;
	poly_copy(dn, n0, n, &dn0);
	poly_copy(db, b0, b, &db0);
	t0[0] = 0;
	t[0] = 1;
	poly_div(dn0, db0, n0, b0, q, r, &dq, &dr);
	nonzero = r[0] != 0;
	for (i = 1; !nonzero && i <= dr; i++)
		nonzero = r[i] != 0;
	while (nonzero) {
		poly_mul(dq, *dt, q, t, u, &du);
		if (dt0 < du)
			for (i = dt0 + 1; i <= du; i++) t0[i] = 0;
		for (i = 0; i <= du; i++)
			temp[i] = t0[i] - u[i];
		dtemp = du;
		poly_copy(*dt, t0, t, &dt0);
		poly_copy(dtemp, t, temp, dt);
		poly_copy(db0, n0, b0, &dn0);
		poly_copy(dr, b0, r, &db0);
		poly_div(dn0, db0, n0, b0, q, r, &dq, &dr);
		nonzero = r[0] != 0;
		for (i = 1; !nonzero && i <= dr; i++)
			nonzero = r[i] != 0;
	}
	if (db0 != 0 && b0[0] != 1) return 0;
	return 1;
}

void poly_mod(long da, long p, long *a, long *new_da)
{
	int zero;
	long i;

	for (i = 0; i <= da; i++) {
		a[i] %= p;
		if (a[i] < 0) a[i] += p;
	}
	zero = a[da] == 0;
	for (i = da - 1; zero && i >= 0; i--) {
		da--;
		zero = a[i] == 0;
	}
	*new_da = da;
}

void poly_write(char *label, long da, long *a)
{
	long i;

	printf("%s", label);
	for (i = da; i >= 0; i--)
		printf("%ld ", a[i]);
	printf("\n");
}

int main(void)
{
	long da = 4, db = 3, dc = 3, dd = 5;
	long de, dq, dr, ds, p = 2;
	long a[5] = { 0, 0, 1, 0, 1 };
	long b[4] = { 1, 1, 0, 1 };
	long c[4] = { 0, 0, 1, 1 };
	long d[6] = { 1, 0, 1, 0, 0, 1 };
	long e[SIZE], q[SIZE], r[SIZE], s[SIZE];

	poly_write("A = ", da, a);
	poly_write("B = ", db, b);
	poly_write("C = ", dc, c);
	poly_write("D = ", dd, d);
	poly_mul(da, db, a, b, e, &de);
	poly_mod(de, p, e, &de);
	poly_div(de, dd, e, d, q, r, &dq, &dr);
	poly_mod(dq, p, q, &dq);
	poly_mod(dr, p, r, &dr);
	poly_write("A * B mod 2 = ", de, e);
	poly_write("A * B / D mod 2 = ", dq, q);
	poly_write("A * B mod D, 2  = ", dr, r);
	if (!poly_Extended_Euclidean(dc, dd, c, d, e, &de))
		printf("*error*\in poly_Extended_Euclidean\n");
	poly_mod(de, p, e, &de);
	poly_write("C ^ - 1 mod D = ", de, e);
	poly_mul(dc, de, c, e, s, &ds);
	poly_mod(ds, p, s, &ds);
	poly_div(ds, dd, s, d, q, r, &dq, &dr);
	poly_mod(dr, p, r, &dr);
	poly_write("C * C ^ - 1 mod D, 2 = ", dr, r);
	dq = 1;
	q[0] = 0;
	q[1] = 1;
	poly_exp_mod(dq, dd, 7, p, q, d, s, &ds);
	poly_mod(ds, p, s, &ds);
	poly_write("x ^  7 mod D, 2 = ", ds, s);
	poly_exp_mod(dq, dd, 9, p, q, d, s, &ds);
	poly_mod(ds, p, s, &ds);
	poly_write("x ^  9 mod D, 2 = ", ds, s);
	poly_exp_mod(dq, dd, 10, p, q, d, s, &ds);
	poly_mod(ds, p, s, &ds);
	poly_write("x ^ 10 mod D, 2 = ", ds, s);
	poly_exp_mod(dq, dd, 25, p, q, d, s, &ds);
	poly_mod(ds, p, s, &ds);
	poly_write("x ^ 25 mod D, 2 = ", ds, s);
	return 0;
}

More Elliptic Curve Point Counting Results Using the Shanks-Mestre Algorithm by James Pate Williams, Jr.

As can be seen the C++ application appears to be much faster than the C# application. Also, in my humble opinion, C++ with header files and C++ source code files is much more elegant than C# with only class source code. I leave it to someone else to add C# interfaces to my class source code files.

Elliptic Curve Point Counting Using the Shanks-Mestre Algorithm in C# and C++ by James Pate Williams, Jr.

A few years ago, I implemented the Shanks-Mestre elliptic curve point counting algorithm in C# using the BigInteger data structure and the algorithm found in Henri Cohen’s textbook A Course in Computational Algebraic Number Theory. I translated the C# application to C++ in August 21-22, 2023. The C++ code uses the long long 64-bit signed integer data type. Below are some results. I also implemented Schoof’s point counting algorithm in C#.