aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/bitsliced-aes/bs.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/monniaux/bitsliced-aes/bs.c')
-rw-r--r--test/monniaux/bitsliced-aes/bs.c53
1 files changed, 41 insertions, 12 deletions
diff --git a/test/monniaux/bitsliced-aes/bs.c b/test/monniaux/bitsliced-aes/bs.c
index 4a9df4aa..063f36f5 100644
--- a/test/monniaux/bitsliced-aes/bs.c
+++ b/test/monniaux/bitsliced-aes/bs.c
@@ -2,6 +2,21 @@
#include <string.h>
#include "bs.h"
+
+static inline long compcert_ternary_signedl(long x, long v0, long v1) {
+ return ((-(x==0)) & v0) | ((-(x!=0)) & v1);
+}
+
+static inline word_t compcert_ternary(word_t x, word_t v0, word_t v1) {
+ return compcert_ternary_signedl(x, v0, v1);
+}
+
+#if defined(__K1C__)
+#define TERNARY(x, v0, v1) compcert_ternary((x), (v0), (v1))
+#else
+#define TERNARY(x, v0, v1) ((x) ? (v1) : (v0))
+#endif
+
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) ||\
defined(__amd64__) || defined(__amd32__)|| defined(__amd16__)
#define bs2le(x) (x)
@@ -14,12 +29,6 @@
#error "endianness not supported"
#endif
-#if 1
-#define TERNARY_XY0(t, x) ((-((t) != 0)) & (x))
-#else
-#define TERNARY_XY0(t, x) (((t) != 0) ? (x) : (0))
-#endif
-
void bs_addroundkey(word_t * B, word_t * rk)
{
int i;
@@ -393,15 +402,23 @@ void bs_transpose_dst(word_t * transpose, word_t * blocks)
int offset = i << MUL_SHIFT;
#ifndef UNROLL_TRANSPOSE
- /* DM experiments */
- /* The normal ternary operator costs us a lot!
- from 10145951 to 7995063 */
- int j;
+ int j;
+#ifdef __COMPCERT__
+ word_t *transptr = transpose+offset;
+ word_t bitmask = ONE;
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ word_t old = *transptr;
+ *(transptr++) = TERNARY(w & bitmask, old, old|bitpos);
+ bitmask <<= 1;
+ }
+#else
for(j=0; j < WORD_SIZE; j++)
{
// TODO make const time
- transpose[offset + j] |= TERNARY_XY0(w & (ONE << j), bitpos);
+ transpose[offset + j] |= (w & (ONE << j)) ? bitpos : 0;
}
+#endif
#else
transpose[(offset)+ 0 ] |= (w & (ONE << 0 )) ? (bitpos) : 0;
@@ -494,11 +511,23 @@ void bs_transpose_rev(word_t * blocks)
word_t offset = k / WORD_SIZE;
#ifndef UNROLL_TRANSPOSE
int j;
+#ifdef __COMPCERT__
+ word_t *transptr = transpose + offset;
+ word_t bitmask = ONE;
for(j=0; j < WORD_SIZE; j++)
{
- word_t bit = TERNARY_XY0((w & (ONE << j)), (ONE << (k % WORD_SIZE)));
+ word_t old = *transptr;
+ *transptr = TERNARY(w & bitmask, old, old | bitpos);
+ transptr += WORDS_PER_BLOCK;
+ bitmask <<= 1;
+ }
+#else
+ for(j=0; j < WORD_SIZE; j++)
+ {
+ word_t bit = (w & (ONE << j)) ? (ONE << (k % WORD_SIZE)) : 0;
transpose[j * WORDS_PER_BLOCK + (offset)] |= bit;
}
+#endif
#else
transpose[0 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 0 )) ? bitpos : 0;
transpose[1 * WORDS_PER_BLOCK + (offset )] |= (w & (ONE << 1 )) ? bitpos : 0;