在 C++ 中对 GF(2^6) 执行矩阵运算(例如乘积和逆运算)

Performing matrix-operations (e.g. product and inverse) over GF(2^6) in C++

本文关键字:逆运算 运算 执行 GF 中对 C++      更新时间:2023-10-16

我想用C++语言在伽罗瓦场GF(64(上实现一些矩阵运算,例如乘积和逆计算。 我通常使用特征库在双精度类型中执行此类操作。

在 Matlab 中,可以使用函数在 GF 上转换矩阵:A_gf = gf(A, 6(;在 A_gf GF(64(上定义的所有后续操作都在 GF(64( 中自动完成,例如 A: inv(A( 的逆函数。

你知道我是否可以在C++做类似的事情吗?

据我所知,本征不支持有限域算术。本机支持的唯一类型是本机类型。

如注释中所述,您可以添加自己的自定义类型:eigen.tuxfamily.org/dox/TopicCustomizing_CustomScalar.html

您最好使用 FLINT 或 NTL 库。

由于您的有限域具有特征 2(因为阶数是 2 的幂(,因此您可以在 NTL 中使用超过 GF2E 的矩阵优化算术(GF(2^6( 是 GF(2( 的扩展(。 https://libntl.org/doc/GF2E.cpp.html

所以你可能想在弗林特上使用 NTL。 您还希望确保使用 gf2x 作为后端,请查看安装 NTL 的文档。

对于超过GF2E的矩阵,以下是文档 https://libntl.org/doc/mat_GF2E.cpp.html:

#include<NTL/GF2XFactoring.h>
#include<NTL/GF2X.h>
#include<NTL/GF2E.h>
#include<NTL/mat_GF2E.h>
// The two first includes might not be necessary
using namespace NTL;
using namespace std;
int main()
{
// You have to compute first a "good" polynomial as modulus, of degree 64.
/* GF2X P = {(GF2)1, 1, 0, 1, 1, 0, 1}; */
GF2X P;
BuildSparseIrred(P, 6); // generate an irreducible polynomial P
// of degree 6 over GF(17)
GF2E::init(P); // define GF(2^6)
mat_GF2E m;  // declare matrices over GF(2^6)
vec_GF2E x, b;
GF2E d;
random(m, 10, 10);
random(b, 10);
solve(d, x, m, b);
cout << "Matrix : " << m << endl;
cout << "vector : " << b << endl;
cout << "The solution of the system M*x = b is :" << endl;
cout << x << endl;
/* (To be completed) */
return 0;
}

有些情况针对多项式模量进行了优化:

GF2XModulus例程优化了几个重要的特殊情况:

f = X^
  • n + X^k + 1,其中 k <= min((n+1(/2, n-NTL_BITS_PER_LONG(
  • f = X^n + X^{k_3} + X^{k_2} +
  • X^{k_1} + 1, 其中 k_3 <= min((n+1(/2, n-NTL_BITS_PER_LONG(
  • f = X^n + g,其中 deg(g( 很小

因此,理想的做法是找到前两种阶数为 6 的先情况之一的多项式,以加快计算速度。