check for either iconv or libiconv.
[gnutls.git] / lib / nettle / ecc_projective_check_point.c
blob86f47487c514336cc2909a880d6032f6dad73879
1 /*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
21 #include "ecc.h"
22 #include <gnutls_errors.h>
24 #ifdef ECC_SECP_CURVES_ONLY
27 @file ecc_projective_check_point.c
31 Checks whether a point lies on the curve y^2 = x^3 - 3x + b
32 @param P The point to check
33 @param modulus The modulus of the field the ECC curve is in
34 @param b The "B" value of the curve
35 @return 0 on success
37 int ecc_projective_check_point (ecc_point * P, mpz_t b, mpz_t modulus)
39 mpz_t t1, t2, t3;
40 int err;
42 if (P == NULL || b == NULL || modulus == NULL)
43 return -1;
45 if (mpz_cmp_ui (P->z, 1) != 0)
47 gnutls_assert ();
48 return -1;
51 if ((err = mp_init_multi (&t1, &t2, &t3, NULL)) != 0)
53 return err;
56 /* t1 = Z * Z */
57 mpz_mul (t1, P->y, P->y);
58 mpz_mod (t1, t1, modulus); /* t1 = y^2 */
60 mpz_mul (t2, P->x, P->x);
61 mpz_mod (t2, t2, modulus);
63 mpz_mul (t2, P->x, t2);
64 mpz_mod (t2, t2, modulus); /* t2 = x^3 */
66 mpz_add (t3, P->x, P->x);
67 if (mpz_cmp (t3, modulus) >= 0)
69 mpz_sub (t3, t3, modulus);
72 mpz_add (t3, t3, P->x); /* t3 = 3x */
73 if (mpz_cmp (t3, modulus) >= 0)
75 mpz_sub (t3, t3, modulus);
78 mpz_sub (t1, t1, t2); /* t1 = y^2 - x^3 */
79 if (mpz_cmp_ui (t1, 0) < 0)
81 mpz_add (t1, t1, modulus);
84 mpz_add (t1, t1, t3); /* t1 = y^2 - x^3 + 3x */
85 if (mpz_cmp (t1, modulus) >= 0)
87 mpz_sub (t1, t1, modulus);
90 mpz_sub (t1, t1, b); /* t1 = y^2 - x^3 + 3x - b */
91 if (mpz_cmp_ui (t1, 0) < 0)
93 mpz_add (t1, t1, modulus);
96 if (mpz_cmp_ui (t1, 0) != 0)
98 err = -1;
100 else
102 err = 0;
105 mp_clear_multi(&t1, &t2, &t3, NULL);
107 return err;
110 #endif