Branch data Line data Source code
1 : : /* Edwards curve operations
2 : : * Daniel Beer <dlbeer@gmail.com>, 9 Jan 2014
3 : : *
4 : : * This file is in the public domain.
5 : : */
6 : :
7 : : #ifndef ED25519_H_
8 : : #define ED25519_H_
9 : :
10 : : #ifndef COMPACT_DISABLE_ED25519
11 : : #include "f25519.h"
12 : :
13 : : /* This is not the Ed25519 signature system. Rather, we're implementing
14 : : * basic operations on the twisted Edwards curve over (Z mod 2^255-19):
15 : : *
16 : : * -x^2 + y^2 = 1 - (121665/121666)x^2y^2
17 : : *
18 : : * With the positive-x base point y = 4/5.
19 : : *
20 : : * These functions will not leak secret data through timing.
21 : : *
22 : : * For more information, see:
23 : : *
24 : : * Bernstein, D.J. & Lange, T. (2007) "Faster addition and doubling on
25 : : * elliptic curves". Document ID: 95616567a6ba20f575c5f25e7cebaf83.
26 : : *
27 : : * Hisil, H. & Wong, K K. & Carter, G. & Dawson, E. (2008) "Twisted
28 : : * Edwards curves revisited". Advances in Cryptology, ASIACRYPT 2008,
29 : : * Vol. 5350, pp. 326-343.
30 : : */
31 : :
32 : : /* Projective coordinates */
33 : : struct ed25519_pt {
34 : : uint8_t x[F25519_SIZE];
35 : : uint8_t y[F25519_SIZE];
36 : : uint8_t t[F25519_SIZE];
37 : : uint8_t z[F25519_SIZE];
38 : : };
39 : :
40 : : extern const struct ed25519_pt ed25519_base;
41 : : extern const struct ed25519_pt ed25519_neutral;
42 : :
43 : : /* Convert between projective and affine coordinates (x/y in F25519) */
44 : : void ed25519_project(struct ed25519_pt *p,
45 : : const uint8_t *x, const uint8_t *y);
46 : :
47 : : void ed25519_unproject(uint8_t *x, uint8_t *y,
48 : : const struct ed25519_pt *p);
49 : :
50 : : /* Compress/uncompress points. try_unpack() will check that the
51 : : * compressed point is on the curve, returning 1 if the unpacked point
52 : : * is valid, and 0 otherwise.
53 : : */
54 : : #define ED25519_PACK_SIZE F25519_SIZE
55 : :
56 : : void ed25519_pack(uint8_t *c, const uint8_t *x, const uint8_t *y);
57 : : uint8_t ed25519_try_unpack(uint8_t *x, uint8_t *y, const uint8_t *c);
58 : :
59 : : /* Add, double and scalar multiply */
60 : : #define ED25519_EXPONENT_SIZE 32
61 : :
62 : : /* Prepare an exponent by clamping appropriate bits */
63 : 2 : static inline void ed25519_prepare(uint8_t *e)
64 : : {
65 : 2 : e[0] &= 0xf8;
66 : 2 : e[31] &= 0x7f;
67 : 2 : e[31] |= 0x40;
68 : 2 : }
69 : :
70 : : /* Order of the group generated by the base point */
71 : 12 : static inline void ed25519_copy(struct ed25519_pt *dst,
72 : : const struct ed25519_pt *src)
73 : : {
74 : 12 : memcpy(dst, src, sizeof(*dst));
75 : 12 : }
76 : :
77 : : void ed25519_add(struct ed25519_pt *r,
78 : : const struct ed25519_pt *a, const struct ed25519_pt *b);
79 : : void ed25519_double(struct ed25519_pt *r, const struct ed25519_pt *a);
80 : : void ed25519_smult(struct ed25519_pt *r, const struct ed25519_pt *a,
81 : : const uint8_t *e);
82 : :
83 : : #endif
84 : : #endif
|