Reputation: 1236
Code
<?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";
}
?>
This is very important Elliptic curve cryptographic Doubling function. Code is doubling properly and converting $old_point to the new $new_points
But I want to create a reverse function that will convert $new_points to the $old_point. I have created a function already reversePoint($new_point) . But it is not giving the old point. Could you help me finding the problem inside reversePoint($new_point) ?
Upvotes: -1
Views: 88
Reputation: 47
Your calculation of m in reversePoint is also wrong. The proper value of m is the slope that was used when you doubled it in doublePoint. You should store the slope in doublePoint and pass it to reversePoint. You can do 2 things:
So, this will do the right calculation for y_old.
Upvotes: 0