java - 使用 Bouncy Castle 获取椭圆曲线的 X 坐标
问题描述
我试图计算椭圆曲线 F2m (m = 163) 的 x 坐标的 Tr(x) 运算。为此,我使用了具有相应类型的“Bouncy Castle”。我的椭圆曲线的迹线等于 0 或 1,我的代码如下:
public int CalculateTrace_Test(byte[] array)
{
int m = 163;
BigInteger two = new BigInteger("2", 10);
BigInteger x = new BigInteger(array);
BigInteger xi = x;
BigInteger temp = x;
for (int i = 1; i < m; i++)
{
var next = xi.ModPow(two.Pow(i), fx);
temp = temp.Xor(next);
}
return temp.IntValue;
}
这里 fx 是由不可约多项式 形成的整数f(x) = x^163+x^7+x^6+x^3 + 1
。
所以我的问题是它不起作用,结果,我什么都有,但不是 1 或 0。谁能告诉我跟踪的实现有什么问题?
解决方案
看起来您在 GF(2 m ) 中没有正确地进行场算术。支持正确字段算术的类在包中org.bouncycastle.math.ec
。看看ECFieldElement.F2m
和ECCurve.F2m
。此外,对于您对应于 SECT163 归约多项式的特定情况,该类SecT163FieldElement
可能特别有用。
这里有一些直接从类中复制的代码org.bouncycastle.math.ec.tools.TraceOptimizer
。代码假设有限域具有特征 2。
private static int calculateTrace(ECFieldElement fe) {
int m = fe.getFieldSize();
ECFieldElement tr = fe;
for (int i = 1; i < m; ++i) {
fe = fe.square();
tr = tr.add(fe);
}
BigInteger b = tr.toBigInteger();
if (b.bitLength() > 1) {
throw new IllegalStateException();
}
return b.intValue();
推荐阅读
- sql-server - 使用单个连接更新 SQL Server 数据库中动态变化的记录数
- php - 错误:PHP 中的数组到字符串转换
- python - pandas df中多列的唯一记录计数
- php - 有什么方法可以避免 html 代码重复?
- python - python Fabric如何将\输入到代码分支中
- google-static-maps - 在谷歌静态地图中使用自定义图标
- javascript - 在javascript ui datepicker中使用另一个函数的变量
- php - 没有索引方法的 URL
- html - Div 没有占据其父级的全部高度,使用“vh”联合定位?
- r - eval 中的错误(predvars、data、env):找不到对象“Rm”