椭圆曲线乘法

时间:2016-02-08 20:27:22

标签: java multiplication elliptic-curve

在执行椭圆曲线点的乘法时,我无法获得正确的结果。我已经能够得到额外的工作但是当我试图增加一点时,我对于添加到先前值的正确方法感到困惑。我也是在没有Javas EC积分的情况下这样做。

实施例: 预期:12(2,7)=(153,36) 结果:12(2,7)=(55,121)

private static int a = 11;
private static int mod = 167;

public static void main(String[] args) {

    int[] p1 = { 2, 7 };
    int[] p2 = addPoints(new int[] { 1, 4 }, new int[] { 3, 1 });
    System.out.println("ADDING: (" + p2[0] + "," + p2[1] + ")");

    int[] p3 = multiplyPoint(12, p1);

    System.out.println("MULTIPLYING: (" + p3[0] + "," + p3[1] + ")");

}

public static int[] multiplyPoint(int iterations, int[] point) {
    int[] newPoint = new int[2];

    for (int i = 1; i < iterations; i++) {

        System.out.println("(" + newPoint[0] + "," + newPoint[1] + ")");
        newPoint = addPoints(newPoint, point);
    }

    return newPoint;
}

public static int[] addPoints(int[] p1, int[] p2) {
    int[] p3 = { 0, 0 };

    int m;
    if (p1[0] == p2[0] && p1[1] == p2[1])
        m = mod(((3 * p1[0] * p1[0]) + a) / (2 * p1[1]), mod);
    else
        m = mod((int) p2[1] - p1[1], mod) / mod(p2[0] - p1[0], mod);

    p3[0] = mod((int) (Math.pow(m, 2) - p1[0] - p2[0]), mod);
    p3[1] = mod((m * (p1[0] - p3[0]) - p1[1]), mod);

    return p3;
}

/**
 * Used because Java's default modulus operator does not give the correct
 * value for negative numbers
 * 
 * @param value
 *            Number to perform the mod operation on
 * @param divisor
 *            Mod by
 * @return The modulus of number mod divisor... Should now work for any
 *         number
 */
public static int mod(int value, int divisor) {
    return ((value % divisor + divisor) % divisor);
}

由于

编辑: 仍然不成功

public static int[] multiplyPoint(int iterations, int[] point) {
    int[] newPoint = new int[2];
    String binary = Integer.toBinaryString(iterations);
    System.out.println(binary);

    for (int i = 0; i < binary.length(); i++) {

        System.out.println("(" + newPoint[0] + "," + newPoint[1] + ")");

        if (binary.charAt(i) == '1')
            newPoint = addPoints(newPoint, point);
        point = doublePoint(point);

    }

    return newPoint;
}

public static int[] doublePoint(int[] point) {
    int[] newPoint = new int[2];

    int m = mod(((3 * point[0] * point[0]) + a) / (2 * point[1]), mod);
    newPoint[0] = mod((int) (Math.pow(m, 2) - point[0] - point[0]), mod);
    newPoint[1] = mod((m * (point[0] - newPoint[0]) - point[1]), mod);

    return newPoint;
}

public static int[] addPoints(int[] p1, int[] p2) {
    int[] p3 = new int[2];

    int m = mod((int) p2[1] - p1[1], mod) / mod(p2[0] - p1[0], mod);

    p3[0] = mod((int) (Math.pow(m, 2) - p1[0] - p2[0]), mod);
    p3[1] = mod((m * (p1[0] - p3[0]) - p1[1]), mod);

    return p3;
}

0 个答案:

没有答案