]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Detect correct polyval implementation (mostly)
authorNick Mathewson <nickm@torproject.org>
Sat, 19 Apr 2025 13:10:58 +0000 (09:10 -0400)
committerNick Mathewson <nickm@torproject.org>
Wed, 21 May 2025 13:43:51 +0000 (09:43 -0400)
I'm saying "mostly" because this will be wrong on really old intel;
we'll need a cpuid workaround if we need to support those.

src/ext/polyval/polyval.h

index 8ad48d877068a51aa73bf42ac102ae1fbfeb711f..5072f05619507384d130fc294147161203f67c43 100644 (file)
 #include "orconfig.h"
 #include "lib/cc/torint.h"
 
+/* Decide which implementation to use. */
+#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) \
+  || defined(_M_X64) || defined(_M_IX86) || defined(__i486)       \
+  || defined(__i386__)
+/* Use intel intrinsics for carryless multiply.
+ *
+ * TODO: In theory we should detect whether we have the relevant instructions,
+ * but they are all at least 15 years old.
+ */
 #define PV_USE_PCLMUL
+#elif SIZEOF_VOID_P >= 8
+/* It's a 64-bit architecture; use the generic 64-bit constant-time
+ * implementation.
+ */
+#define PV_USE_CTMUL64
+#elif SIZEOF_VOID_P == 4
+/* It's a 64-bit architecture; use the generic 32-bit constant-time
+ * implementation.
+ */
+#define PV_USE_CTMUL
+#else
+#error "sizeof(void*) is implausibly weird."
+#endif
 
+/**
+ * Declare a 128 bit integer type.
+ # The exact representation will depend on which implementation we've chosen.
+ */
 #ifdef PV_USE_PCLMUL
 #include <emmintrin.h>
 typedef __m128i pv_u128_;
 #elif defined(PV_USE_CTMUL64)
-/** A 128-bit integer represented as its low and high portion. */
 typedef struct pv_u128_ {
   uint64_t lo;
   uint64_t hi;