Commit bbc53b75 authored by Davide Galassi's avatar Davide Galassi

ECP add handling of sum by inverse

parent b8fdb4f6
Pipeline #58825850 passed with stages
in 1 minute and 26 seconds
......@@ -9,11 +9,6 @@ int cry_ecp_add(cry_ecp *pr, const cry_ecp *p1, const cry_ecp *p2,
cry_mpi num, den, lam;
cry_ecp r;
if (cry_mpi_cmp(&p1->x, &p2->x) == 0 &&
cry_mpi_cmp(&p1->y, &p2->y) == 0) {
return cry_ecp_dbl(pr, p1, grp);
}
/* Check if one of the two points is the infinity point */
if (cry_ecp_is_zero(p1))
return (pr != p2) ? cry_ecp_copy(pr, p2) : 0;
......@@ -26,7 +21,21 @@ int cry_ecp_add(cry_ecp *pr, const cry_ecp *p1, const cry_ecp *p2,
}
CHK(cry_mpi_sub(&num, &p2->y, &p1->y)); /* num = y2 - y1 */
CHK(cry_mpi_mod(&num, &num, &grp->p));
CHK(cry_mpi_sub(&den, &p2->x, &p1->x)); /* den = x2 - x1 */
CHK(cry_mpi_mod(&den, &den, &grp->p));
if (cry_mpi_is_zero(&den)) {
if (cry_mpi_is_zero(&num))
res = cry_ecp_dbl(pr, p1, grp);
else
cry_ecp_set_zero(pr);
/*
* Shall we check that p1.y+p2.y==0 (mod p) ?
* Is guaranteed that p1.x==p2.x is sufficient condition to say that
* p2 is the inverse of p1???
*/
goto e;
}
CHK(cry_mpi_inv(&den, &den, &grp->p)); /* den^(-1) (mod p) */
CHK(cry_mpi_mul(&lam, &num, &den)); /* lam = num / den */
......
......@@ -18,24 +18,21 @@ static void load_curve(cry_ecp_grp *ec)
void ecp_test(void)
{
#if 0
cry_ecp_grp ec;
cry_ecp p;
int i = 1;
load_curve(&ec);
cry_ecp_init(&p);
cry_ecp_dbl(&p, &ec.g, &ec);
cry_mpi_print(&ec.g.x, 10);
cry_mpi_print(&ec.g.y, 10);
int i = 0;
while (cry_mpi_cmp(&p.x, &ec.g.x) != 0 ||
cry_mpi_cmp(&p.y, &ec.g.y) != 0) {
printf("-----\n");
cry_ecp_copy(&p, &ec.g);
do {
printf("n = %d\n", i);
cry_mpi_print(&p.x, 10);
cry_mpi_print(&p.y, 10);
if (++i == 19)
printf("*\n");
cry_mpi_print(&p.z, 10);
printf("--------------------\n");
cry_ecp_add(&p, &p, &ec.g, &ec);
}
#endif
i++;
} while (cry_mpi_cmp(&p.x, &ec.g.x) != 0 ||
cry_mpi_cmp(&p.y, &ec.g.y) != 0);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment