3 Copyright (C) 2013 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free
17 Software Foundation; either version 2 of the License, or (at your
18 option) any later version.
20 or both in parallel, as here.
22 GNU Nettle is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received copies of the GNU General Public License and
28 the GNU Lesser General Public License along with this program. If
29 not, see http://www.gnu.org/licenses/.
32 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
34 #ifndef NETTLE_ECC_H_INCLUDED
35 #define NETTLE_ECC_H_INCLUDED
37 #include "nettle-types.h"
45 #define ecc_point_init nettle_ecc_point_init
46 #define ecc_point_clear nettle_ecc_point_clear
47 #define ecc_point_set nettle_ecc_point_set
48 #define ecc_point_get nettle_ecc_point_get
49 #define ecc_point_mul nettle_ecc_point_mul
50 #define ecc_point_mul_g nettle_ecc_point_mul_g
51 #define ecc_scalar_init nettle_ecc_scalar_init
52 #define ecc_scalar_clear nettle_ecc_scalar_clear
53 #define ecc_scalar_set nettle_ecc_scalar_set
54 #define ecc_scalar_get nettle_ecc_scalar_get
55 #define ecc_scalar_random nettle_ecc_scalar_random
56 #define ecc_point_mul nettle_ecc_point_mul
57 #define ecc_bit_size nettle_ecc_bit_size
58 #define ecc_size nettle_ecc_size
59 #define ecc_size_a nettle_ecc_size_a
60 #define ecc_size_j nettle_ecc_size_j
64 /* High level interface, for ECDSA, DH, etc */
66 /* Represents a point on the ECC curve */
69 const struct ecc_curve *ecc;
70 /* Allocated using the same allocation function as GMP. */
74 /* Represents a non-zero scalar, an element of Z_q^*, where q is the
75 group order of the curve. */
78 const struct ecc_curve *ecc;
79 /* Allocated using the same allocation function as GMP. */
84 ecc_point_init (struct ecc_point *p, const struct ecc_curve *ecc);
86 ecc_point_clear (struct ecc_point *p);
88 /* Fails and returns zero if the point is not on the curve. */
90 ecc_point_set (struct ecc_point *p, const mpz_t x, const mpz_t y);
92 ecc_point_get (const struct ecc_point *p, mpz_t x, mpz_t y);
95 ecc_scalar_init (struct ecc_scalar *s, const struct ecc_curve *ecc);
97 ecc_scalar_clear (struct ecc_scalar *s);
99 /* Fails and returns zero if the scalar is not in the proper range. */
101 ecc_scalar_set (struct ecc_scalar *s, const mpz_t z);
103 ecc_scalar_get (const struct ecc_scalar *s, mpz_t z);
104 /* Generates a random scalar, suitable as an ECDSA private key or a
107 ecc_scalar_random (struct ecc_scalar *s,
108 void *random_ctx, nettle_random_func *random);
110 /* Computes r = n p */
112 ecc_point_mul (struct ecc_point *r, const struct ecc_scalar *n,
113 const struct ecc_point *p);
115 /* Computes r = n g */
117 ecc_point_mul_g (struct ecc_point *r, const struct ecc_scalar *n);
120 /* Low-level interface */
122 /* Points on a curve are represented as arrays of mp_limb_t, with
123 curve-specific representation. For the secp curves, we use Jacobian
124 coordinates (possibly in Montgomery form for mod multiplication).
125 For curve25519 we use homogeneous coordinates on an equivalent
126 Edwards curve. The suffix "_h" denotes this internal
129 Since we use additive notation for the groups, the infinity point
130 on the curve is denoted 0. The infinity point can be represented
131 with x = y = 0 in affine coordinates, and Z = 0 in Jacobian
132 coordinates. However, note that most of the ECC functions do *not*
133 support infinity as an input or output.
136 /* Returns the bit size of a single coordinate (and of the prime p). */
138 ecc_bit_size (const struct ecc_curve *ecc);
140 /* Returns the size of a single coordinate. */
142 ecc_size (const struct ecc_curve *ecc);
144 /* Size of a point, using affine coordinates x, y. */
146 ecc_size_a (const struct ecc_curve *ecc);
148 /* Size of a point, using jacobian coordinates X, Y and Z. */
150 ecc_size_j (const struct ecc_curve *ecc);
152 /* FIXME: Define a generic ecc_dup, ecc_add, for any type of curve. Do
153 they need to handle infinity points? */
159 #endif /* NETTLE_ECC_H_INCLUDED */