Corrected bugs in record parsing.
[gnutls.git] / lib / nettle / ecc_projective_isneutral.c
blob742ae5f4224ba29730fa3f0c58b5bddc8ba9ee7c
1 /*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * Author: Ilya Tumaykin
6 * This file is part of GNUTLS.
8 * The GNUTLS library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include "ecc.h"
26 Check if the given point is the neutral point
27 @param P The point to check
28 @param modulus The modulus of the field the ECC curve is in
29 @return 0 if given point is a neutral point
30 @return 1 if given point is not a neutral point
31 @return negative value in case of error
33 int
34 ecc_projective_isneutral (ecc_point * P, mpz_t modulus)
36 mpz_t t1, t2;
37 int err;
39 if (P == NULL || modulus == NULL)
40 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
43 * neutral point is a point with projective
44 * coordinates (x,y,0) such that y^2 == x^3
45 * excluding point (0,0,0)
47 if (mpz_sgn (P->z))
48 /* Z != 0 */
49 return 1;
51 if ((err = mp_init_multi (&t1, &t2, NULL)) != 0)
53 return err;
56 /* t1 == x^3 */
57 mpz_mul (t1, P->x, P->x);
58 mpz_mod (t1, t1, modulus);
59 mpz_mul (t1, t1, P->x);
60 mpz_mod (t1, t1, modulus);
61 /* t2 == y^2 */
62 mpz_mul (t2, P->y, P->y);
63 mpz_mod (t2, t2, modulus);
65 if ((!mpz_cmp (t1, t2)) && (mpz_sgn (t1)))
67 /* Z == 0 and X^3 == Y^2 != 0
68 * it is neutral */
69 err = 0;
70 goto done;
73 /* Z == 0 and X^3 != Y^2 or
74 * Z == X == Y == 0
75 * this should never happen */
76 err = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
77 goto done;
78 done:
79 mp_clear_multi (&t1, &t2, NULL);
80 return err;