ge_operations.h (3660B)
1 /* ge_operations.h 2 * 3 * Copyright (C) 2006-2025 wolfSSL Inc. 4 * 5 * This file is part of wolfSSL. 6 * 7 * wolfSSL is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * wolfSSL is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 */ 21 22 23 /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */ 24 25 #ifndef WOLF_CRYPT_GE_OPERATIONS_H 26 #define WOLF_CRYPT_GE_OPERATIONS_H 27 28 #include <wolfssl/wolfcrypt/settings.h> 29 30 #ifdef HAVE_ED25519 31 32 #include <wolfssl/wolfcrypt/fe_operations.h> 33 34 /* 35 ge means group element. 36 37 Here the group is the set of pairs (x,y) of field elements (see fe.h) 38 satisfying -x^2 + y^2 = 1 + d x^2y^2 39 where d = -121665/121666. 40 41 Representations: 42 ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z 43 ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT 44 ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T 45 ge_precomp (Duif): (y+x,y-x,2dxy) 46 */ 47 48 #ifdef ED25519_SMALL 49 ALIGN16 typedef byte ge[F25519_SIZE]; 50 #elif defined(CURVED25519_ASM_64BIT) 51 ALIGN16 typedef sword64 ge[4]; 52 #elif defined(CURVED25519_ASM_32BIT) 53 ALIGN16 typedef sword32 ge[8]; 54 #elif defined(CURVED25519_128BIT) 55 ALIGN16 typedef sword64 ge[5]; 56 #else 57 ALIGN16 typedef sword32 ge[10]; 58 #endif 59 60 typedef struct { 61 ge X; 62 ge Y; 63 ge Z; 64 } ge_p2; 65 66 typedef struct { 67 ge X; 68 ge Y; 69 ge Z; 70 ge T; 71 } ge_p3; 72 73 #ifdef __cplusplus 74 extern "C" { 75 #endif 76 77 WOLFSSL_LOCAL int ge_compress_key(byte* out, const byte* xIn, const byte* yIn, 78 word32 keySz); 79 WOLFSSL_LOCAL int ge_frombytes_negate_vartime(ge_p3 *h,const unsigned char *s); 80 81 WOLFSSL_LOCAL int ge_double_scalarmult_vartime(ge_p2 *r, const unsigned char *a, 82 const ge_p3 *A, const unsigned char *b); 83 WOLFSSL_LOCAL void ge_scalarmult_base(ge_p3 *h,const unsigned char *a); 84 WOLFSSL_LOCAL void sc_reduce(byte* s); 85 WOLFSSL_LOCAL void sc_muladd(byte* s, const byte* a, const byte* b, 86 const byte* c); 87 WOLFSSL_LOCAL void ge_tobytes(unsigned char *s,const ge_p2 *h); 88 #ifndef GE_P3_TOBYTES_IMPL 89 #define ge_p3_tobytes(s, h) ge_tobytes((s), (const ge_p2 *)(h)) 90 #else 91 WOLFSSL_LOCAL void ge_p3_tobytes(unsigned char *s,const ge_p3 *h); 92 #endif 93 94 95 #ifndef ED25519_SMALL 96 typedef struct { 97 ge X; 98 ge Y; 99 ge Z; 100 ge T; 101 } ge_p1p1; 102 103 typedef struct { 104 ge yplusx; 105 ge yminusx; 106 ge xy2d; 107 } ge_precomp; 108 109 typedef struct { 110 ge YplusX; 111 ge YminusX; 112 ge Z; 113 ge T2d; 114 } ge_cached; 115 116 #ifdef CURVED25519_ASM 117 void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p); 118 void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p); 119 void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p); 120 #define ge_p3_dbl(r, p) ge_p2_dbl((ge_p1p1 *)(r), (ge_p2 *)(p)) 121 void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q); 122 void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q); 123 void ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); 124 void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); 125 #endif 126 #endif /* !ED25519_SMALL */ 127 128 #ifdef __cplusplus 129 } /* extern "C" */ 130 #endif 131 132 #endif /* HAVE_ED25519 */ 133 134 #endif /* WOLF_CRYPT_GE_OPERATIONS_H */