1 #include "ed25519-donna-portable-identify.h"
2 
3 #define mul32x32_64(a,b) (((uint64_t)(a))*(b))
4 
5 /* platform */
6 #if defined(COMPILER_MSVC)
7 	#include <intrin.h>
8 	#if !defined(_DEBUG)
9 		#undef mul32x32_64
10 		#define mul32x32_64(a,b) __emulu(a,b)
11 	#endif
12 	#undef inline
13 	#define inline __forceinline
14 	#define DONNA_INLINE __forceinline
15 	#define DONNA_NOINLINE __declspec(noinline)
16 	#define ALIGN(x) __declspec(align(x))
17 	#define ROTL32(a,b) _rotl(a,b)
18 	#define ROTR32(a,b) _rotr(a,b)
19 #else
20 	#include <sys/param.h>
21 	#define DONNA_INLINE inline __attribute__((always_inline))
22 	#define DONNA_NOINLINE __attribute__((noinline))
23 	#undef ALIGN
24 	#define ALIGN(x) __attribute__((aligned(x)))
25 	#define ROTL32(a,b) (((a) << (b)) | ((a) >> (32 - b)))
26 	#define ROTR32(a,b) (((a) >> (b)) | ((a) << (32 - b)))
27 #endif
28 
29 /* uint128_t */
30 #if defined(CPU_64BITS) && !defined(ED25519_FORCE_32BIT)
31 	#if defined(COMPILER_CLANG) && (COMPILER_CLANG >= 30100)
32 		#define HAVE_NATIVE_UINT128
33 		typedef unsigned __int128 uint128_t;
34 	#elif defined(COMPILER_MSVC)
35 		#define HAVE_UINT128
36 		typedef struct uint128_t {
37 			uint64_t lo, hi;
38 		} uint128_t;
39 		#define mul64x64_128(out,a,b) out.lo = _umul128(a,b,&out.hi);
40 		#define shr128_pair(out,hi,lo,shift) out = __shiftright128(lo, hi, shift);
41 		#define shl128_pair(out,hi,lo,shift) out = __shiftleft128(lo, hi, shift);
42 		#define shr128(out,in,shift) shr128_pair(out, in.hi, in.lo, shift)
43 		#define shl128(out,in,shift) shl128_pair(out, in.hi, in.lo, shift)
44 		#define add128(a,b) { uint64_t p = a.lo; a.lo += b.lo; a.hi += b.hi + (a.lo < p); }
45 		#define add128_64(a,b) { uint64_t p = a.lo; a.lo += b; a.hi += (a.lo < p); }
46 		#define lo128(a) (a.lo)
47 		#define hi128(a) (a.hi)
48 	#elif defined(COMPILER_GCC) && !defined(HAVE_NATIVE_UINT128)
49 		#if defined(__SIZEOF_INT128__)
50 			#define HAVE_NATIVE_UINT128
51 			typedef unsigned __int128 uint128_t;
52 		#elif (COMPILER_GCC >= 40400)
53 			#define HAVE_NATIVE_UINT128
54 			typedef unsigned uint128_t __attribute__((mode(TI)));
55 		#elif defined(CPU_X86_64)
56 			#define HAVE_UINT128
57 			typedef struct uint128_t {
58 				uint64_t lo, hi;
59 			} uint128_t;
60 			#define mul64x64_128(out,a,b) __asm__ ("mulq %3" : "=a" (out.lo), "=d" (out.hi) : "a" (a), "rm" (b));
61 			#define shr128_pair(out,hi,lo,shift) __asm__ ("shrdq %2,%1,%0" : "+r" (lo) : "r" (hi), "J" (shift)); out = lo;
62 			#define shl128_pair(out,hi,lo,shift) __asm__ ("shldq %2,%1,%0" : "+r" (hi) : "r" (lo), "J" (shift)); out = hi;
63 			#define shr128(out,in,shift) shr128_pair(out,in.hi, in.lo, shift)
64 			#define shl128(out,in,shift) shl128_pair(out,in.hi, in.lo, shift)
65 			#define add128(a,b) __asm__ ("addq %4,%2; adcq %5,%3" : "=r" (a.hi), "=r" (a.lo) : "1" (a.lo), "0" (a.hi), "rm" (b.lo), "rm" (b.hi) : "cc");
66 			#define add128_64(a,b) __asm__ ("addq %4,%2; adcq $0,%3" : "=r" (a.hi), "=r" (a.lo) : "1" (a.lo), "0" (a.hi), "rm" (b) : "cc");
67 			#define lo128(a) (a.lo)
68 			#define hi128(a) (a.hi)
69 		#endif
70 	#endif
71 
72 	#if defined(HAVE_NATIVE_UINT128)
73 		#define HAVE_UINT128
74 		#define mul64x64_128(out,a,b) out = (uint128_t)a * b;
75 		#define shr128_pair(out,hi,lo,shift) out = (uint64_t)((((uint128_t)hi << 64) | lo) >> (shift));
76 		#define shl128_pair(out,hi,lo,shift) out = (uint64_t)(((((uint128_t)hi << 64) | lo) << (shift)) >> 64);
77 		#define shr128(out,in,shift) out = (uint64_t)(in >> (shift));
78 		#define shl128(out,in,shift) out = (uint64_t)((in << shift) >> 64);
79 		#define add128(a,b) a += b;
80 		#define add128_64(a,b) a += (uint64_t)b;
81 		#define lo128(a) ((uint64_t)a)
82 		#define hi128(a) ((uint64_t)(a >> 64))
83 	#endif
84 
85 	#if !defined(HAVE_UINT128)
86 		#error Need a uint128_t implementation!
87 	#endif
88 #endif
89 
90 /* endian */
91 #if !defined(ED25519_OPENSSLRNG)
U32TO8_LE(unsigned char * p,const uint32_t v)92 static inline void U32TO8_LE(unsigned char *p, const uint32_t v) {
93 	p[0] = (unsigned char)(v      );
94 	p[1] = (unsigned char)(v >>  8);
95 	p[2] = (unsigned char)(v >> 16);
96 	p[3] = (unsigned char)(v >> 24);
97 }
98 #endif
99 
100 #if !defined(HAVE_UINT128)
U8TO32_LE(const unsigned char * p)101 static inline uint32_t U8TO32_LE(const unsigned char *p) {
102 	return
103 	(((uint32_t)(p[0])      ) |
104 	 ((uint32_t)(p[1]) <<  8) |
105 	 ((uint32_t)(p[2]) << 16) |
106 	 ((uint32_t)(p[3]) << 24));
107 }
108 #else
U8TO64_LE(const unsigned char * p)109 static inline uint64_t U8TO64_LE(const unsigned char *p) {
110 	return
111 	(((uint64_t)(p[0])      ) |
112 	 ((uint64_t)(p[1]) <<  8) |
113 	 ((uint64_t)(p[2]) << 16) |
114 	 ((uint64_t)(p[3]) << 24) |
115 	 ((uint64_t)(p[4]) << 32) |
116 	 ((uint64_t)(p[5]) << 40) |
117 	 ((uint64_t)(p[6]) << 48) |
118 	 ((uint64_t)(p[7]) << 56));
119 }
120 
U64TO8_LE(unsigned char * p,const uint64_t v)121 static inline void U64TO8_LE(unsigned char *p, const uint64_t v) {
122 	p[0] = (unsigned char)(v      );
123 	p[1] = (unsigned char)(v >>  8);
124 	p[2] = (unsigned char)(v >> 16);
125 	p[3] = (unsigned char)(v >> 24);
126 	p[4] = (unsigned char)(v >> 32);
127 	p[5] = (unsigned char)(v >> 40);
128 	p[6] = (unsigned char)(v >> 48);
129 	p[7] = (unsigned char)(v >> 56);
130 }
131 #endif
132 
133 #include <stdlib.h>
134 #include <string.h>
135 
136 
137