1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*
* Copyright (C) 2011-2012 Free Software Foundation, Inc.
*
* This file is part of GNUTLS.
*
* The GNUTLS library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
#include "ecc.h"
#include <gnutls_errors.h>
#ifdef ECC_SECP_CURVES_ONLY
/*
@file ecc_projective_check_point.c
*/
/*
Checks whether a point lies on the curve y^2 = x^3 - 3x + b
@param P The point to check
@param modulus The modulus of the field the ECC curve is in
@param b The "B" value of the curve
@return 0 on success
*/
int ecc_projective_check_point (ecc_point * P, mpz_t b, mpz_t modulus)
{
mpz_t t1, t2, t3;
int err;
if (P == NULL || b == NULL || modulus == NULL)
return -1;
if (mpz_cmp_ui (P->z, 1) != 0)
{
gnutls_assert ();
return -1;
}
if ((err = mp_init_multi (&t1, &t2, &t3, NULL)) != 0)
{
return err;
}
/* t1 = Z * Z */
mpz_mul (t1, P->y, P->y);
mpz_mod (t1, t1, modulus); /* t1 = y^2 */
mpz_mul (t2, P->x, P->x);
mpz_mod (t2, t2, modulus);
mpz_mul (t2, P->x, t2);
mpz_mod (t2, t2, modulus); /* t2 = x^3 */
mpz_add (t3, P->x, P->x);
if (mpz_cmp (t3, modulus) >= 0)
{
mpz_sub (t3, t3, modulus);
}
mpz_add (t3, t3, P->x); /* t3 = 3x */
if (mpz_cmp (t3, modulus) >= 0)
{
mpz_sub (t3, t3, modulus);
}
mpz_sub (t1, t1, t2); /* t1 = y^2 - x^3 */
if (mpz_cmp_ui (t1, 0) < 0)
{
mpz_add (t1, t1, modulus);
}
mpz_add (t1, t1, t3); /* t1 = y^2 - x^3 + 3x */
if (mpz_cmp (t1, modulus) >= 0)
{
mpz_sub (t1, t1, modulus);
}
mpz_sub (t1, t1, b); /* t1 = y^2 - x^3 + 3x - b */
if (mpz_cmp_ui (t1, 0) < 0)
{
mpz_add (t1, t1, modulus);
}
if (mpz_cmp_ui (t1, 0) != 0)
{
err = -1;
}
else
{
err = 0;
}
mp_clear_multi(&t1, &t2, &t3, NULL);
return err;
}
#endif
|