1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

Account for case when point.y == 0 when doubling.

This commit is contained in:
Ondrej Mikle 2014-07-04 17:40:07 +02:00
parent 6d61cefdb3
commit d827b2c862
2 changed files with 16 additions and 6 deletions

21
ecdsa.c
View File

@ -54,14 +54,12 @@ void point_add(const curve_point *cp1, curve_point *cp2)
point_copy(cp1, cp2); point_copy(cp1, cp2);
return; return;
} }
if (point_is_equal(cp1, cp2)) { if (point_is_negative_of(cp1, cp2)) {
point_double(cp2); point_set_infinity(cp2);
return; return;
} }
if (point_is_negative_of(cp1, cp2)) { if (point_is_equal(cp1, cp2)) {
// set to point at infinity point_double(cp2);
bn_zero(&(cp2->x));
bn_zero(&(cp2->y));
return; return;
} }
@ -98,6 +96,10 @@ void point_double(curve_point *cp)
if (point_is_infinity(cp)) { if (point_is_infinity(cp)) {
return; return;
} }
if (bn_is_zero(&(cp->y))) {
point_set_infinity(cp);
return;
}
memcpy(&inverse_y, &(cp->y), sizeof(bignum256)); memcpy(&inverse_y, &(cp->y), sizeof(bignum256));
bn_inverse(&inverse_y, &prime256k1); bn_inverse(&inverse_y, &prime256k1);
@ -151,6 +153,13 @@ void point_multiply(const bignum256 *k, const curve_point *p, curve_point *res)
bn_mod(&(res->y), &prime256k1); bn_mod(&(res->y), &prime256k1);
} }
// set point to internal representation of point at infinity
void point_set_infinity(curve_point *p)
{
bn_zero(&(p->x));
bn_zero(&(p->y));
}
// return true iff p represent point at infinity // return true iff p represent point at infinity
// both coords are zero in internal representation // both coords are zero in internal representation
int point_is_infinity(const curve_point *p) int point_is_infinity(const curve_point *p)

View File

@ -36,6 +36,7 @@ void point_copy(const curve_point *cp1, curve_point *cp2);
void point_add(const curve_point *cp1, curve_point *cp2); void point_add(const curve_point *cp1, curve_point *cp2);
void point_double(curve_point *cp); void point_double(curve_point *cp);
void point_multiply(const bignum256 *k, const curve_point *p, curve_point *res); void point_multiply(const bignum256 *k, const curve_point *p, curve_point *res);
void point_set_infinity(curve_point *p);
int point_is_infinity(const curve_point *p); int point_is_infinity(const curve_point *p);
int point_is_equal(const curve_point *p, const curve_point *q); int point_is_equal(const curve_point *p, const curve_point *q);
int point_is_negative_of(const curve_point *p, const curve_point *q); int point_is_negative_of(const curve_point *p, const curve_point *q);