#include <stdint.h>
#include <string.h>
+#include <avr/pgmspace.h>
#include "bmw_small.h"
+
#define SHL32(a,n) ((a)<<(n))
#define SHR32(a,n) ((a)>>(n))
#define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
#define ROTR32(a,n) (((a)>>(n))|((a)<<(32-(n))))
-#define BUG24 1
+#define BUG24 0
+#define F0_HACK 1
#define DEBUG 0
#if DEBUG
r = ROTR32(x, 5);
return r;
}
+/*
+#define K 0x05555555L
+static
+uint32_t k_lut[] PROGMEM = {
+ 16L*K, 17L*K, 18L*K, 19L*K, 20L*K, 21L*K, 22L*K, 23L*K,
+ 24L*K, 25L*K, 26L*K, 27L*K, 28L*K, 29L*K, 30L*K, 31L*K
+};
+*/
+/* same as above but precomputed to avoid compiler warnings */
+
+static
+uint32_t k_lut[] PROGMEM = {
+ 0x55555550L, 0x5aaaaaa5L, 0x5ffffffaL,
+ 0x6555554fL, 0x6aaaaaa4L, 0x6ffffff9L,
+ 0x7555554eL, 0x7aaaaaa3L, 0x7ffffff8L,
+ 0x8555554dL, 0x8aaaaaa2L, 0x8ffffff7L,
+ 0x9555554cL, 0x9aaaaaa1L, 0x9ffffff6L,
+ 0xa555554bL };
+
uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m){
uint32_t(*s[])(uint32_t) = {bmw_small_s1, bmw_small_s2, bmw_small_s3, bmw_small_s0};
uint32_t r;
uint8_t i;
- r = 0x05555555*(j+16);
+ /* r = 0x05555555*(j+16); */
+ r = pgm_read_dword(k_lut+j);
for(i=0; i<16; ++i){
r += s[i%4](q[j+i]);
}
bmw_small_r7};
uint32_t r;
uint8_t i;
- r = 0x05555555*(j+16);
+ /* r = 0x05555555*(j+16); */
+ r = pgm_read_dword(k_lut+j);
for(i=0; i<14; i+=2){
r += q[j+i];
}
r -= ((uint32_t*)m)[(j+10)%16];
return r;
}
+#if F0_HACK
+static
+uint8_t f0_lut[] PROGMEM = {
+ 5<<1, ( 7<<1)+1, (10<<1)+0, (13<<1)+0, (14<<1)+0,
+ 6<<1, ( 8<<1)+1, (11<<1)+0, (14<<1)+0, (15<<1)+1,
+ 0<<1, ( 7<<1)+0, ( 9<<1)+0, (12<<1)+1, (15<<1)+0,
+ 0<<1, ( 1<<1)+1, ( 8<<1)+0, (10<<1)+1, (13<<1)+0,
+ 1<<1, ( 2<<1)+0, ( 9<<1)+0, (11<<1)+1, (14<<1)+1,
+ 3<<1, ( 2<<1)+1, (10<<1)+0, (12<<1)+1, (15<<1)+0,
+ 4<<1, ( 0<<1)+1, ( 3<<1)+1, (11<<1)+1, (13<<1)+0,
+ 1<<1, ( 4<<1)+1, ( 5<<1)+1, (12<<1)+1, (14<<1)+1,
+ 2<<1, ( 5<<1)+1, ( 6<<1)+1, (13<<1)+0, (15<<1)+1,
+ 0<<1, ( 3<<1)+1, ( 6<<1)+0, ( 7<<1)+1, (14<<1)+0,
+ 8<<1, ( 1<<1)+1, ( 4<<1)+1, ( 7<<1)+1, (15<<1)+0,
+ 8<<1, ( 0<<1)+1, ( 2<<1)+1, ( 5<<1)+1, ( 9<<1)+0,
+ 1<<1, ( 3<<1)+0, ( 6<<1)+1, ( 9<<1)+1, (10<<1)+0,
+ 2<<1, ( 4<<1)+0, ( 7<<1)+0, (10<<1)+0, (11<<1)+0,
+ 3<<1, ( 5<<1)+1, ( 8<<1)+0, (11<<1)+1, (12<<1)+1,
+ 12<<1, ( 4<<1)+1, ( 6<<1)+1, ( 9<<1)+1, (13<<1)+0
+};
+
+void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
+ uint8_t i,j=-1,v,sign,l=0;
+ uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
+ bmw_small_s3, bmw_small_s4 };
+ for(i=0; i<16; ++i){
+ h[i] ^= ((uint32_t*)m)[i];
+ }
+ dump_x(h, 16, 'T');
+ // memset(q, 0, 4*16);
+ for(i=0; i<5*16; ++i){
+ v = pgm_read_byte(f0_lut+i);
+ sign = v&1;
+ v >>=1;
+ if(i==l){
+ j++;
+ l+=5;
+ q[j] = h[v];
+ continue;
+ }
+ if(sign){
+ q[j] -= h[v];
+ }else{
+ q[j] += h[v];
+ }
+ }
+ dump_x(q, 16, 'W');
+ for(i=0; i<16; ++i){
+ q[i] = s[i%5](q[i]);
+ }
+}
-void bmw_small_f0(uint32_t* q, const uint32_t* h, const void* m){
- uint32_t t[16];
+#else
+void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
uint8_t i;
uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
bmw_small_s3, bmw_small_s4 };
for(i=0; i<16; ++i){
- t[i] = h[i] ^ ((uint32_t*)m)[i];
+ h[i] ^= ((uint32_t*)m)[i];
}
- dump_x(t, 16, 'T');
- q[ 0] = (t[ 5] - t[ 7] + t[10] + t[13] + t[14]);
- q[ 1] = (t[ 6] - t[ 8] + t[11] + t[14] - t[15]);
- q[ 2] = (t[ 0] + t[ 7] + t[ 9] - t[12] + t[15]);
- q[ 3] = (t[ 0] - t[ 1] + t[ 8] - t[10] + t[13]);
- q[ 4] = (t[ 1] + t[ 2] + t[ 9] - t[11] - t[14]);
- q[ 5] = (t[ 3] - t[ 2] + t[10] - t[12] + t[15]);
- q[ 6] = (t[ 4] - t[ 0] - t[ 3] - t[11] + t[13]);
- q[ 7] = (t[ 1] - t[ 4] - t[ 5] - t[12] - t[14]);
- q[ 8] = (t[ 2] - t[ 5] - t[ 6] + t[13] - t[15]);
- q[ 9] = (t[ 0] - t[ 3] + t[ 6] - t[ 7] + t[14]);
- q[10] = (t[ 8] - t[ 1] - t[ 4] - t[ 7] + t[15]);
- q[11] = (t[ 8] - t[ 0] - t[ 2] - t[ 5] + t[ 9]);
- q[12] = (t[ 1] + t[ 3] - t[ 6] - t[ 9] + t[10]);
- q[13] = (t[ 2] + t[ 4] + t[ 7] + t[10] + t[11]);
- q[14] = (t[ 3] - t[ 5] + t[ 8] - t[11] - t[12]);
- q[15] = (t[12] - t[ 4] - t[ 6] - t[ 9] + t[13]);
+ dump_x(h, 16, 'T');
+ q[ 0] = (h[ 5] - h[ 7] + h[10] + h[13] + h[14]);
+ q[ 1] = (h[ 6] - h[ 8] + h[11] + h[14] - h[15]);
+ q[ 2] = (h[ 0] + h[ 7] + h[ 9] - h[12] + h[15]);
+ q[ 3] = (h[ 0] - h[ 1] + h[ 8] - h[10] + h[13]);
+ q[ 4] = (h[ 1] + h[ 2] + h[ 9] - h[11] - h[14]);
+ q[ 5] = (h[ 3] - h[ 2] + h[10] - h[12] + h[15]);
+ q[ 6] = (h[ 4] - h[ 0] - h[ 3] - h[11] + h[13]);
+ q[ 7] = (h[ 1] - h[ 4] - h[ 5] - h[12] - h[14]);
+ q[ 8] = (h[ 2] - h[ 5] - h[ 6] + h[13] - h[15]);
+ q[ 9] = (h[ 0] - h[ 3] + h[ 6] - h[ 7] + h[14]);
+ q[10] = (h[ 8] - h[ 1] - h[ 4] - h[ 7] + h[15]);
+ q[11] = (h[ 8] - h[ 0] - h[ 2] - h[ 5] + h[ 9]);
+ q[12] = (h[ 1] + h[ 3] - h[ 6] - h[ 9] + h[10]);
+ q[13] = (h[ 2] + h[ 4] + h[ 7] + h[10] + h[11]);
+ q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
+ q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
dump_x(q, 16, 'W');
for(i=0; i<16; ++i){
q[i] = s[i%5](q[i]);
}
}
+#endif
void bmw_small_f1(uint32_t* q, const void* m){
uint8_t i;