1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 
11 #include "tomcrypt_private.h"
12 
13 #ifdef LTC_MECC
14 
15 /* http://crypto.stackexchange.com/questions/41468/point-at-infinity-for-jacobian-coordinates
16  * a point at infinity is any point (x,y,0) such that y^2 == x^3, except (0,0,0)
17  */
18 
ltc_ecc_is_point_at_infinity(const ecc_point * P,void * modulus,int * retval)19 int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval)
20 {
21    int err;
22    void  *x3, *y2;
23 
24    /* trivial case */
25    if (!mp_iszero(P->z)) {
26       *retval = 0;
27       return CRYPT_OK;
28    }
29 
30    /* point (0,0,0) is not at infinity */
31    if (mp_iszero(P->x) && mp_iszero(P->y)) {
32       *retval = 0;
33       return CRYPT_OK;
34    }
35 
36    /* initialize */
37    if ((err = mp_init_multi(&x3, &y2, NULL))      != CRYPT_OK)   goto done;
38 
39    /* compute y^2 */
40    if ((err = mp_mulmod(P->y, P->y, modulus, y2)) != CRYPT_OK)   goto cleanup;
41 
42    /* compute x^3 */
43    if ((err = mp_mulmod(P->x, P->x, modulus, x3)) != CRYPT_OK)   goto cleanup;
44    if ((err = mp_mulmod(P->x, x3, modulus, x3))   != CRYPT_OK)   goto cleanup;
45 
46    /* test y^2 == x^3 */
47    err = CRYPT_OK;
48    if ((mp_cmp(x3, y2) == LTC_MP_EQ) && !mp_iszero(y2)) {
49       *retval = 1;
50    } else {
51       *retval = 0;
52    }
53 
54 cleanup:
55    mp_clear_multi(x3, y2, NULL);
56 done:
57    return err;
58 }
59 
60 #endif
61 
62 /* ref:         $Format:%D$ */
63 /* git commit:  $Format:%H$ */
64 /* commit time: $Format:%ai$ */
65