1 文本格式
using System;
namespace Legalsoft.Truffer
{
public class Primpolytest
{
private int N { get; set; } = 32;
private int nfactors { get; set; } = 5;
private ulong[] factors { get; set; }
private int[] t { get; set; }
private int[] a;
private int[] p;
public Primpolytest()
{
this.N = 32;
this.nfactors = 5;
this.factors = new ulong[nfactors];
this.t = new int[N * N];
this.a = new int[N * N];
this.p = new int[N * N];
ulong[] factordata = { 3, 5, 17, 257, 65537 };
for (int i = 0; i < nfactors; i++)
{
factors[i] = factordata[i];
}
}
public int ispident()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (i == j)
{
if (p[i * N + j] != 1)
{
return 0;
}
}
else
{
if (p[i * N + j] != 0)
{
return 0;
}
}
}
}
return 1;
}
public void mattimeseq(int[] a, int[] b)
{
int[] tmp = new int[N * N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int sum = 0;
for (int k = 0; k < N; k++)
{
sum += a[i * N + k] * b[k * N + j];
}
tmp[i * N + j] = sum & 1;
}
}
for (int k = 0; k < N * N; k++)
{
a[k] = tmp[k];
}
}
public void matpow(ulong n)
{
for (int k = 0; k < N * N; k++)
{
p[k] = 0;
}
for (int k = 0; k < N; k++)
{
p[k * N + k] = 1;
}
while (true)
{
if ((n & 1) != 0)
{
mattimeseq(p, a);
}
n >>= 1;
if (n == 0)
{
break;
}
mattimeseq(a, a);
}
}
public int test(ulong n)
{
ulong nn = n;
ulong tnm1 = (ulong)(((ulong)1 << N) - 1);
if (n > (ulong)(tnm1 >> 1))
{
throw new Exception("not a polynomial of degree N");
}
for (int k = 0; k < N * N; k++)
{
t[k] = 0;
}
for (int i = 1; i < N; i++)
{
t[i * N + (i - 1)] = 1;
}
int j = 0;
while (nn != 0)
{
if ((nn & 1) != 0)
{
t[j] = 1;
}
nn >>= 1;
j++;
}
t[N - 1] = 1;
for (int k = 0; k < N * N; k++)
{
a[k] = t[k];
}
matpow(tnm1);
if (ispident() != 1)
{
return 0;
}
for (int i = 0; i < nfactors; i++)
{
ulong pow = tnm1 / factors[i];
for (int k = 0; k < N * N; k++)
{
a[k] = t[k];
}
matpow(pow);
if (ispident() == 1)
{
return 0;
}
}
return 1;
}
}
}
2 代码格式
using System;
namespace Legalsoft.Truffer
{
public class Primpolytest
{
private int N { get; set; } = 32;
private int nfactors { get; set; } = 5;
private ulong[] factors { get; set; }
private int[] t { get; set; }
private int[] a;
private int[] p;
public Primpolytest()
{
this.N = 32;
this.nfactors = 5;
this.factors = new ulong[nfactors];
this.t = new int[N * N];
this.a = new int[N * N];
this.p = new int[N * N];
ulong[] factordata = { 3, 5, 17, 257, 65537 };
for (int i = 0; i < nfactors; i++)
{
factors[i] = factordata[i];
}
}
public int ispident()
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (i == j)
{
if (p[i * N + j] != 1)
{
return 0;
}
}
else
{
if (p[i * N + j] != 0)
{
return 0;
}
}
}
}
return 1;
}
public void mattimeseq(int[] a, int[] b)
{
int[] tmp = new int[N * N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
int sum = 0;
for (int k = 0; k < N; k++)
{
sum += a[i * N + k] * b[k * N + j];
}
tmp[i * N + j] = sum & 1;
}
}
for (int k = 0; k < N * N; k++)
{
a[k] = tmp[k];
}
}
public void matpow(ulong n)
{
for (int k = 0; k < N * N; k++)
{
p[k] = 0;
}
for (int k = 0; k < N; k++)
{
p[k * N + k] = 1;
}
while (true)
{
if ((n & 1) != 0)
{
mattimeseq(p, a);
}
n >>= 1;
if (n == 0)
{
break;
}
mattimeseq(a, a);
}
}
public int test(ulong n)
{
ulong nn = n;
ulong tnm1 = (ulong)(((ulong)1 << N) - 1);
if (n > (ulong)(tnm1 >> 1))
{
throw new Exception("not a polynomial of degree N");
}
for (int k = 0; k < N * N; k++)
{
t[k] = 0;
}
for (int i = 1; i < N; i++)
{
t[i * N + (i - 1)] = 1;
}
int j = 0;
while (nn != 0)
{
if ((nn & 1) != 0)
{
t[j] = 1;
}
nn >>= 1;
j++;
}
t[N - 1] = 1;
for (int k = 0; k < N * N; k++)
{
a[k] = t[k];
}
matpow(tnm1);
if (ispident() != 1)
{
return 0;
}
for (int i = 0; i < nfactors; i++)
{
ulong pow = tnm1 / factors[i];
for (int k = 0; k < N * N; k++)
{
a[k] = t[k];
}
matpow(pow);
if (ispident() == 1)
{
return 0;
}
}
return 1;
}
}
}