1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 #ifdef LTC_MECC
7 
8 /* http://crypto.stackexchange.com/questions/41468/point-at-infinity-for-jacobian-coordinates
9  * a point at infinity is any point (x,y,0) such that y^2 == x^3, except (0,0,0)
10  */
11 
ltc_ecc_is_point_at_infinity(const ecc_point * P,void * modulus,int * retval)12 int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval)
13 {
14    int err;
15    void  *x3, *y2;
16 
17    /* trivial case */
18    if (!mp_iszero(P->z)) {
19       *retval = 0;
20       return CRYPT_OK;
21    }
22 
23    /* point (0,0,0) is not at infinity */
24    if (mp_iszero(P->x) && mp_iszero(P->y)) {
25       *retval = 0;
26       return CRYPT_OK;
27    }
28 
29    /* initialize */
30    if ((err = mp_init_multi(&x3, &y2, LTC_NULL))  != CRYPT_OK)   goto done;
31 
32    /* compute y^2 */
33    if ((err = mp_mulmod(P->y, P->y, modulus, y2)) != CRYPT_OK)   goto cleanup;
34 
35    /* compute x^3 */
36    if ((err = mp_mulmod(P->x, P->x, modulus, x3)) != CRYPT_OK)   goto cleanup;
37    if ((err = mp_mulmod(P->x, x3, modulus, x3))   != CRYPT_OK)   goto cleanup;
38 
39    /* test y^2 == x^3 */
40    err = CRYPT_OK;
41    if ((mp_cmp(x3, y2) == LTC_MP_EQ) && !mp_iszero(y2)) {
42       *retval = 1;
43    } else {
44       *retval = 0;
45    }
46 
47 cleanup:
48    mp_clear_multi(x3, y2, LTC_NULL);
49 done:
50    return err;
51 }
52 
53 #endif
54