3 /* nettle, low-level cryptographics library
5 * Copyright (C) 2013 Niels Möller
7 * The nettle library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at your
10 * option) any later version.
12 * The nettle library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the nettle library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
30 #include "ecc-internal.h"
33 ecc_point_init (struct ecc_point
*p
, const struct ecc_curve
*ecc
)
36 p
->p
= gmp_alloc_limbs (2*ecc
->size
);
40 ecc_point_clear (struct ecc_point
*p
)
42 gmp_free_limbs (p
->p
, 2*p
->ecc
->size
);
46 ecc_point_set (struct ecc_point
*p
, const mpz_t x
, const mpz_t y
)
55 if (mpz_sgn (x
) < 0 || mpz_limbs_cmp (x
, p
->ecc
->p
, size
) >= 0
56 || mpz_sgn (y
) < 0 || mpz_limbs_cmp (y
, p
->ecc
->p
, size
) >= 0)
62 /* Check that y^2 = x^3 - 3*x + b (mod p) */
65 mpz_sub_ui (rhs
, rhs
, 3);
66 mpz_mul (rhs
, rhs
, x
);
67 mpz_add (rhs
, rhs
, mpz_roinit_n (t
, p
->ecc
->b
, size
));
69 res
= mpz_congruent_p (lhs
, rhs
, mpz_roinit_n (t
, p
->ecc
->p
, size
));
77 mpz_limbs_copy (p
->p
, x
, size
);
78 mpz_limbs_copy (p
->p
+ size
, y
, size
);
84 ecc_point_get (const struct ecc_point
*p
, mpz_t x
, mpz_t y
)
86 mp_size_t size
= p
->ecc
->size
;
88 mpz_set_n (x
, p
->p
, size
);
90 mpz_set_n (y
, p
->p
+ size
, size
);