尝试在反向函数中找到debug

问题描述 投票:0回答:1

代码

<?php

$old_point['x'] = gmp_init('55066263022277343669578718895168534326250603453777594175500187360389116729240');
$old_point['y'] = gmp_init('32670510020758816978083085130507043184471273380659243275938904335757337482424');

function doublePoint($point) {
    $a = gmp_init('0', 10);
    $p = gmp_init('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16);

    // Calculate the slope (m)
    $slope = gmp_mod(
        gmp_mul(
            gmp_invert(
                gmp_mod(gmp_mul(gmp_init(2, 10), $point['y']), $p),
                $p
            ),
            gmp_add(
                gmp_mul(gmp_init(3, 10), gmp_pow($point['x'], 2)),
                $a
            )
        ),
        $p
    );

    // Calculate new point coordinates
    $new_point = [];
    $new_point['x'] = gmp_mod(
        gmp_sub(
            gmp_sub(gmp_pow($slope, 2), $point['x']),
            $point['x']
        ),
        $p
    );

    $new_point['y'] = gmp_mod(
        gmp_sub(
            gmp_mul($slope, gmp_sub($point['x'], $new_point['x'])),
            $point['y']
        ),
        $p
    );

    return $new_point;
}

function reversePoint($new_point) {
    $p = gmp_init('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16);
    
    // The original x-coordinate remains the same
    $old_point['x'] = $new_point['x'];

    // To find the old y-coordinate, we need to derive it
    // We can assume it’s the y-coordinate that would satisfy the elliptic curve equation

    // Given y_new = m * (x_new - x_old) - y_old, rearranging gives us y_old
    // The y-coordinate can be calculated as:
    // y_old = (m * (x_new - x_old) - y_new) mod p
    // where m is the slope we used to calculate the new point
    
    $m = gmp_mod(
        gmp_invert(
            gmp_mod(gmp_mul(gmp_init(2, 10), $new_point['y']), $p),
            $p
        ),
        $p
    );

    // Compute old y-coordinate
    $old_point['y'] = gmp_mod(
        gmp_sub(
            gmp_mul($m, gmp_sub($new_point['x'], $old_point['x'])),
            $new_point['y']
        ),
        $p
    );

    // Adjust y-coordinate for the curve
    $old_point['y'] = gmp_mod(gmp_sub($p, $old_point['y']), $p);

    return $old_point;
}

// Generate new points by doubling the old point
$new_points = doublePoint($old_point);
print_r($new_points);

// Reverse the doubling to get back the old points
$reversed_points = reversePoint($new_points);
print_r($reversed_points);

// Check equality
if (gmp_cmp($old_point['x'], $reversed_points['x']) === 0 && gmp_cmp($old_point['y'], $reversed_points['y']) === 0) {
    echo "The reversed point matches the old point.\n";
} else {
    echo "The reversed point does not match the old point.\n";
}
?>

这是非常重要的椭圆曲线加密倍增函数。代码正确加倍并将 $old_point 转换为新的 $new_points

但我想创建一个反向函数,将 $new_points 转换为 $old_point。我已经创建了一个函数 reversePoint($new_point) 。但它并没有给出旧的观点。你能帮我找到 reversePoint($new_point) 里面的问题吗?

php
1个回答
0
投票

你在reversePoint中计算m也是错误的。 m 的正确值是在 doublePoint 中将其加倍时使用的斜率。您应该将斜率存储在 doublePoint 中并将其传递给reversePoint。你可以做两件事:

  1. 更改 doublePoint,使其与新点一起返回斜率。
  2. 更新reversePoint,使其现在接受斜率作为参数,然后在公式中使用该斜率来计算旧的 y 坐标。

因此,这将为 y_old 进行正确的计算。

© www.soinside.com 2019 - 2024. All rights reserved.