1 /* Copyright (c) 2020, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef OPENSSL_HEADER_CURVE25519_INTERNAL_H
16 #define OPENSSL_HEADER_CURVE25519_INTERNAL_H
17 
18 #include <GFp/base.h>
19 
20 #include "../internal.h"
21 
22 
23 #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_APPLE)
24 #define BORINGSSL_X25519_NEON
25 
26 // x25519_NEON is defined in asm/x25519-arm.S.
27 void GFp_x25519_NEON(uint8_t out[32], const uint8_t scalar[32],
28                      const uint8_t point[32]);
29 #endif
30 
31 #if defined(BORINGSSL_HAS_UINT128)
32 #define BORINGSSL_CURVE25519_64BIT
33 #endif
34 
35 #if defined(BORINGSSL_CURVE25519_64BIT)
36 // An element t,
37 // entries t[0]...t[4], represents the integer t[0]+2^51 t[1]+2^102 t[2]+2^153
38 // t[3]+2^204 t[4].
39 // fe limbs are bounded by 1.125*2^51.
40 // fe_loose limbs are bounded by 3.375*2^51.
41 typedef uint64_t fe_limb_t;
42 #define FE_NUM_LIMBS 5
43 #else
44 // An element t,
45 // entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
46 // t[3]+2^102 t[4]+...+2^230 t[9].
47 // fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
48 // fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc.
49 typedef uint32_t fe_limb_t;
50 #define FE_NUM_LIMBS 10
51 #endif
52 
53 // fe means field element. Here the field is \Z/(2^255-19).
54 // Multiplication and carrying produce fe from fe_loose.
55 // Keep in sync with `Elem` and `ELEM_LIMBS` in curve25519/ops.rs.
56 typedef struct fe { fe_limb_t v[FE_NUM_LIMBS]; } fe;
57 
58 // Addition and subtraction produce fe_loose from (fe, fe).
59 // Keep in sync with `Elem` and `ELEM_LIMBS` in curve25519/ops.rs.
60 typedef struct fe_loose { fe_limb_t v[FE_NUM_LIMBS]; } fe_loose;
61 
fe_limbs_copy(fe_limb_t r[],const fe_limb_t a[])62 static inline void fe_limbs_copy(fe_limb_t r[], const fe_limb_t a[]) {
63   for (size_t i = 0; i < FE_NUM_LIMBS; ++i) {
64     r[i] = a[i];
65   }
66 }
67 
68 // ge means group element.
69 //
70 // Here the group is the set of pairs (x,y) of field elements (see fe.h)
71 // satisfying -x^2 + y^2 = 1 + d x^2y^2
72 // where d = -121665/121666.
73 //
74 // Representations:
75 //   ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
76 //   ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
77 //   ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
78 //   ge_precomp (Duif): (y+x,y-x,2dxy)
79 
80 // Keep in sync with `Point` in curve25519/ops.rs.
81 typedef struct {
82   fe X;
83   fe Y;
84   fe Z;
85 } ge_p2;
86 
87 
88 // Keep in sync with `ExtPoint` in curve25519/ops.rs.
89 typedef struct {
90   fe X;
91   fe Y;
92   fe Z;
93   fe T;
94 } ge_p3;
95 
96 typedef struct {
97   fe_loose X;
98   fe_loose Y;
99   fe_loose Z;
100   fe_loose T;
101 } ge_p1p1;
102 
103 typedef struct {
104   fe_loose yplusx;
105   fe_loose yminusx;
106   fe_loose xy2d;
107 } ge_precomp;
108 
109 typedef struct {
110   fe_loose YplusX;
111   fe_loose YminusX;
112   fe_loose Z;
113   fe_loose T2d;
114 } ge_cached;
115 
116 #endif  // OPENSSL_HEADER_CURVE25519_INTERNAL_H
117