首页 > 解决方案 > 使用 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。谁能告诉我跟踪的实现有什么问题?

标签: javacryptographybouncycastle

解决方案


看起来您在 GF(2 m ) 中没有正确地进行场算术。支持正确字段算术的类在包中org.bouncycastle.math.ec。看看ECFieldElement.F2mECCurve.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();

推荐阅读