tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 1 | #include "Bitvec.h"
|
| 2 |
|
| 3 | #include "Random.h"
|
| 4 |
|
| 5 | #include <assert.h>
|
| 6 | #include <stdio.h>
|
| 7 |
|
| 8 | #ifndef DEBUG
|
| 9 | #undef assert
|
| 10 | void assert ( bool )
|
| 11 | {
|
| 12 | }
|
| 13 | #endif
|
| 14 |
|
| 15 | //----------------------------------------------------------------------------
|
| 16 |
|
| 17 | void printbits ( void * blob, int len )
|
| 18 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 19 | uint8_t * data = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 20 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 21 | printf("[");
|
| 22 | for(int i = 0; i < len; i++)
|
| 23 | {
|
| 24 | unsigned char byte = data[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 25 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 26 | int hi = (byte >> 4);
|
| 27 | int lo = (byte & 0xF);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 28 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 29 | if(hi) printf("%01x",hi);
|
| 30 | else printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 31 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 32 | if(lo) printf("%01x",lo);
|
| 33 | else printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 34 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 35 | if(i != len-1) printf(" ");
|
| 36 | }
|
| 37 | printf("]");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 38 | }
|
| 39 |
|
| 40 | void printbits2 ( uint8_t * k, int nbytes )
|
| 41 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 42 | printf("[");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 43 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 44 | for(int i = nbytes-1; i >= 0; i--)
|
| 45 | {
|
| 46 | uint8_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 47 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 48 | for(int j = 7; j >= 0; j--)
|
| 49 | {
|
| 50 | uint8_t c = (b & (1 << j)) ? '#' : ' ';
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 51 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 52 | putc(c,stdout);
|
| 53 | }
|
| 54 | }
|
| 55 | printf("]");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 56 | }
|
| 57 |
|
| 58 | void printhex32 ( void * blob, int len )
|
| 59 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 60 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 61 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 62 | uint32_t * d = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 63 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 64 | printf("{ ");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 65 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 66 | for(int i = 0; i < len/4; i++)
|
| 67 | {
|
| 68 | printf("0x%08x, ",d[i]);
|
| 69 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 70 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 71 | printf("}");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 72 | }
|
| 73 |
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 74 | void printbytes ( void * blob, int len )
|
| 75 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 76 | uint8_t * d = (uint8_t*)blob;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 77 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 78 | printf("{ ");
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 79 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 80 | for(int i = 0; i < len; i++)
|
| 81 | {
|
| 82 | printf("0x%02x, ",d[i]);
|
| 83 | }
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 84 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 85 | printf(" };");
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 86 | }
|
| 87 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 88 | //-----------------------------------------------------------------------------
|
| 89 |
|
| 90 | uint32_t getbit ( void * block, int len, uint32_t bit )
|
| 91 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 92 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 93 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 94 | int byte = bit >> 3;
|
| 95 | bit = bit & 0x7;
|
| 96 |
|
| 97 | if(byte < len) return (b[byte] >> bit) & 1;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 98 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 99 | return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 100 | }
|
| 101 |
|
| 102 | uint32_t getbit_wrap ( void * block, int len, uint32_t bit )
|
| 103 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 104 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 105 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 106 | int byte = bit >> 3;
|
| 107 | bit = bit & 0x7;
|
| 108 |
|
| 109 | byte %= len;
|
| 110 |
|
| 111 | return (b[byte] >> bit) & 1;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 112 | }
|
| 113 |
|
| 114 | void setbit ( void * block, int len, uint32_t bit )
|
| 115 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 116 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 117 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 118 | int byte = bit >> 3;
|
| 119 | bit = bit & 0x7;
|
| 120 |
|
| 121 | if(byte < len) b[byte] |= (1 << bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 122 | }
|
| 123 |
|
| 124 | void setbit ( void * block, int len, uint32_t bit, uint32_t val )
|
| 125 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 126 | val ? setbit(block,len,bit) : clearbit(block,len,bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 127 | }
|
| 128 |
|
| 129 | void clearbit ( void * block, int len, uint32_t bit )
|
| 130 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 131 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 132 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 133 | int byte = bit >> 3;
|
| 134 | bit = bit & 0x7;
|
| 135 |
|
| 136 | if(byte < len) b[byte] &= ~(1 << bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 137 | }
|
| 138 |
|
| 139 | void flipbit ( void * block, int len, uint32_t bit )
|
| 140 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 141 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 142 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 143 | int byte = bit >> 3;
|
| 144 | bit = bit & 0x7;
|
| 145 |
|
| 146 | if(byte < len) b[byte] ^= (1 << bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 147 | }
|
| 148 |
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 149 | // from the "Bit Twiddling Hacks" webpage
|
| 150 |
|
| 151 | int countbits ( uint32_t v )
|
| 152 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 153 | v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
|
| 154 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
|
| 155 | int c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 156 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 157 | return c;
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 158 | }
|
| 159 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 160 | //-----------------------------------------------------------------------------
|
| 161 |
|
| 162 | void lshift1 ( void * blob, int len, int c )
|
| 163 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 164 | int nbits = len*8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 165 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 166 | for(int i = nbits-1; i >= 0; i--)
|
| 167 | {
|
| 168 | setbit(blob,len,i,getbit(blob,len,i-c));
|
| 169 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 170 | }
|
| 171 |
|
| 172 |
|
| 173 | void lshift8 ( void * blob, int nbytes, int c )
|
| 174 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 175 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 176 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 177 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 178 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 179 | int b = c >> 3;
|
| 180 | c &= 7;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 181 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 182 | for(int i = nbytes-1; i >= b; i--)
|
| 183 | {
|
| 184 | k[i] = k[i-b];
|
| 185 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 186 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 187 | for(int i = b-1; i >= 0; i--)
|
| 188 | {
|
| 189 | k[i] = 0;
|
| 190 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 191 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 192 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 193 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 194 | for(int i = nbytes-1; i >= 0; i--)
|
| 195 | {
|
| 196 | uint8_t a = k[i];
|
| 197 | uint8_t b = (i == 0) ? 0 : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 198 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 199 | k[i] = (a << c) | (b >> (8-c));
|
| 200 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 201 | }
|
| 202 |
|
| 203 | void lshift32 ( void * blob, int len, int c )
|
| 204 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 205 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 206 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 207 | int nbytes = len;
|
| 208 | int ndwords = nbytes / 4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 209 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 210 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 211 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 212 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 213 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 214 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 215 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 216 | int b = c / 32;
|
| 217 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 218 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 219 | for(int i = ndwords-1; i >= b; i--)
|
| 220 | {
|
| 221 | k[i] = k[i-b];
|
| 222 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 223 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 224 | for(int i = b-1; i >= 0; i--)
|
| 225 | {
|
| 226 | k[i] = 0;
|
| 227 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 228 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 229 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 230 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 231 | for(int i = ndwords-1; i >= 0; i--)
|
| 232 | {
|
| 233 | uint32_t a = k[i];
|
| 234 | uint32_t b = (i == 0) ? 0 : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 235 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 236 | k[i] = (a << c) | (b >> (32-c));
|
| 237 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 238 | }
|
| 239 |
|
| 240 | //-----------------------------------------------------------------------------
|
| 241 |
|
| 242 | void rshift1 ( void * blob, int len, int c )
|
| 243 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 244 | int nbits = len*8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 245 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 246 | for(int i = 0; i < nbits; i++)
|
| 247 | {
|
| 248 | setbit(blob,len,i,getbit(blob,len,i+c));
|
| 249 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 250 | }
|
| 251 |
|
| 252 | void rshift8 ( void * blob, int nbytes, int c )
|
| 253 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 254 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 255 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 256 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 257 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 258 | int b = c >> 3;
|
| 259 | c &= 7;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 260 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 261 | for(int i = 0; i < nbytes-b; i++)
|
| 262 | {
|
| 263 | k[i] = k[i+b];
|
| 264 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 265 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 266 | for(int i = nbytes-b; i < nbytes; i++)
|
| 267 | {
|
| 268 | k[i] = 0;
|
| 269 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 270 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 271 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 272 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 273 | for(int i = 0; i < nbytes; i++)
|
| 274 | {
|
| 275 | uint8_t a = (i == nbytes-1) ? 0 : k[i+1];
|
| 276 | uint8_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 277 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 278 | k[i] = (a << (8-c) ) | (b >> c);
|
| 279 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 280 | }
|
| 281 |
|
| 282 | void rshift32 ( void * blob, int len, int c )
|
| 283 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 284 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 285 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 286 | int nbytes = len;
|
| 287 | int ndwords = nbytes / 4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 288 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 289 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 290 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 291 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 292 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 293 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 294 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 295 | int b = c / 32;
|
| 296 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 297 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 298 | for(int i = 0; i < ndwords-b; i++)
|
| 299 | {
|
| 300 | k[i] = k[i+b];
|
| 301 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 302 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 303 | for(int i = ndwords-b; i < ndwords; i++)
|
| 304 | {
|
| 305 | k[i] = 0;
|
| 306 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 307 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 308 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 309 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 310 | for(int i = 0; i < ndwords; i++)
|
| 311 | {
|
| 312 | uint32_t a = (i == ndwords-1) ? 0 : k[i+1];
|
| 313 | uint32_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 314 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 315 | k[i] = (a << (32-c) ) | (b >> c);
|
| 316 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 317 | }
|
| 318 |
|
| 319 | //-----------------------------------------------------------------------------
|
| 320 |
|
| 321 | void lrot1 ( void * blob, int len, int c )
|
| 322 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 323 | int nbits = len * 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 324 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 325 | for(int i = 0; i < c; i++)
|
| 326 | {
|
| 327 | uint32_t bit = getbit(blob,len,nbits-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 328 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 329 | lshift1(blob,len,1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 330 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 331 | setbit(blob,len,0,bit);
|
| 332 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 333 | }
|
| 334 |
|
| 335 | void lrot8 ( void * blob, int len, int c )
|
| 336 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 337 | int nbytes = len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 338 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 339 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 340 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 341 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 342 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 343 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 344 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 345 | int b = c / 8;
|
| 346 | c &= (8-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 347 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 348 | for(int j = 0; j < b; j++)
|
| 349 | {
|
| 350 | uint8_t t = k[nbytes-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 351 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 352 | for(int i = nbytes-1; i > 0; i--)
|
| 353 | {
|
| 354 | k[i] = k[i-1];
|
| 355 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 356 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 357 | k[0] = t;
|
| 358 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 359 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 360 | uint8_t t = k[nbytes-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 361 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 362 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 363 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 364 | for(int i = nbytes-1; i >= 0; i--)
|
| 365 | {
|
| 366 | uint8_t a = k[i];
|
| 367 | uint8_t b = (i == 0) ? t : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 368 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 369 | k[i] = (a << c) | (b >> (8-c));
|
| 370 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 371 | }
|
| 372 |
|
| 373 | void lrot32 ( void * blob, int len, int c )
|
| 374 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 375 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 376 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 377 | int nbytes = len;
|
| 378 | int ndwords = nbytes/4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 379 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 380 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 381 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 382 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 383 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 384 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 385 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 386 | int b = c / 32;
|
| 387 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 388 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 389 | for(int j = 0; j < b; j++)
|
| 390 | {
|
| 391 | uint32_t t = k[ndwords-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 392 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 393 | for(int i = ndwords-1; i > 0; i--)
|
| 394 | {
|
| 395 | k[i] = k[i-1];
|
| 396 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 397 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 398 | k[0] = t;
|
| 399 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 400 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 401 | uint32_t t = k[ndwords-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 402 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 403 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 404 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 405 | for(int i = ndwords-1; i >= 0; i--)
|
| 406 | {
|
| 407 | uint32_t a = k[i];
|
| 408 | uint32_t b = (i == 0) ? t : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 409 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 410 | k[i] = (a << c) | (b >> (32-c));
|
| 411 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 412 | }
|
| 413 |
|
| 414 | //-----------------------------------------------------------------------------
|
| 415 |
|
| 416 | void rrot1 ( void * blob, int len, int c )
|
| 417 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 418 | int nbits = len * 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 419 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 420 | for(int i = 0; i < c; i++)
|
| 421 | {
|
| 422 | uint32_t bit = getbit(blob,len,0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 423 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 424 | rshift1(blob,len,1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 425 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 426 | setbit(blob,len,nbits-1,bit);
|
| 427 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 428 | }
|
| 429 |
|
| 430 | void rrot8 ( void * blob, int len, int c )
|
| 431 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 432 | int nbytes = len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 433 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 434 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 435 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 436 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 437 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 438 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 439 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 440 | int b = c / 8;
|
| 441 | c &= (8-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 442 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 443 | for(int j = 0; j < b; j++)
|
| 444 | {
|
| 445 | uint8_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 446 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 447 | for(int i = 0; i < nbytes-1; i++)
|
| 448 | {
|
| 449 | k[i] = k[i+1];
|
| 450 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 451 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 452 | k[nbytes-1] = t;
|
| 453 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 454 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 455 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 456 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 457 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 458 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 459 | uint8_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 460 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 461 | for(int i = 0; i < nbytes; i++)
|
| 462 | {
|
| 463 | uint8_t a = (i == nbytes-1) ? t : k[i+1];
|
| 464 | uint8_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 465 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 466 | k[i] = (a << (8-c)) | (b >> c);
|
| 467 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 468 | }
|
| 469 |
|
| 470 | void rrot32 ( void * blob, int len, int c )
|
| 471 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 472 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 473 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 474 | int nbytes = len;
|
| 475 | int ndwords = nbytes/4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 476 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 477 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 478 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 479 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 480 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 481 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 482 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 483 | int b = c / 32;
|
| 484 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 485 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 486 | for(int j = 0; j < b; j++)
|
| 487 | {
|
| 488 | uint32_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 489 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 490 | for(int i = 0; i < ndwords-1; i++)
|
| 491 | {
|
| 492 | k[i] = k[i+1];
|
| 493 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 494 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 495 | k[ndwords-1] = t;
|
| 496 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 497 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 498 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 499 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 500 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 501 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 502 | uint32_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 503 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 504 | for(int i = 0; i < ndwords; i++)
|
| 505 | {
|
| 506 | uint32_t a = (i == ndwords-1) ? t : k[i+1];
|
| 507 | uint32_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 508 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 509 | k[i] = (a << (32-c)) | (b >> c);
|
| 510 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 511 | }
|
| 512 |
|
| 513 | //-----------------------------------------------------------------------------
|
| 514 |
|
| 515 | uint32_t window1 ( void * blob, int len, int start, int count )
|
| 516 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 517 | int nbits = len*8;
|
| 518 | start %= nbits;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 519 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 520 | uint32_t t = 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 521 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 522 | for(int i = 0; i < count; i++)
|
| 523 | {
|
| 524 | setbit(&t,sizeof(t),i, getbit_wrap(blob,len,start+i));
|
| 525 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 526 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 527 | return t;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 528 | }
|
| 529 |
|
| 530 | uint32_t window8 ( void * blob, int len, int start, int count )
|
| 531 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 532 | int nbits = len*8;
|
| 533 | start %= nbits;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 534 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 535 | uint32_t t = 0;
|
| 536 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 537 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 538 | if(count == 0) return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 539 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 540 | int c = start & (8-1);
|
| 541 | int d = start / 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 542 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 543 | for(int i = 0; i < 4; i++)
|
| 544 | {
|
| 545 | int ia = (i + d + 1) % len;
|
| 546 | int ib = (i + d + 0) % len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 547 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 548 | uint32_t a = k[ia];
|
| 549 | uint32_t b = k[ib];
|
| 550 |
|
| 551 | uint32_t m = (a << (8-c)) | (b >> c);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 552 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 553 | t |= (m << (8*i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 554 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 555 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 556 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 557 | t &= ((1 << count)-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 558 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 559 | return t;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 560 | }
|
| 561 |
|
| 562 | uint32_t window32 ( void * blob, int len, int start, int count )
|
| 563 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 564 | int nbits = len*8;
|
| 565 | start %= nbits;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 566 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 567 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 568 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 569 | int ndwords = len / 4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 570 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 571 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 572 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 573 | if(count == 0) return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 574 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 575 | int c = start & (32-1);
|
| 576 | int d = start / 32;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 577 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 578 | if(c == 0) return (k[d] & ((1 << count) - 1));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 579 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 580 | int ia = (d + 1) % ndwords;
|
| 581 | int ib = (d + 0) % ndwords;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 582 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 583 | uint32_t a = k[ia];
|
| 584 | uint32_t b = k[ib];
|
| 585 |
|
| 586 | uint32_t t = (a << (32-c)) | (b >> c);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 587 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 588 | t &= ((1 << count)-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 589 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 590 | return t;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 591 | }
|
| 592 |
|
| 593 | //-----------------------------------------------------------------------------
|
| 594 |
|
| 595 | bool test_shift ( void )
|
| 596 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 597 | int nbits = 64;
|
| 598 | int nbytes = nbits / 8;
|
| 599 | int reps = 10000;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 600 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 601 | for(int j = 0; j < reps; j++)
|
| 602 | {
|
| 603 | if(j % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 604 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 605 | uint64_t a = rand_u64();
|
| 606 | uint64_t b;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 607 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 608 | for(int i = 0; i < nbits; i++)
|
| 609 | {
|
| 610 | b = a; lshift1 (&b,nbytes,i); assert(b == (a << i));
|
| 611 | b = a; lshift8 (&b,nbytes,i); assert(b == (a << i));
|
| 612 | b = a; lshift32 (&b,nbytes,i); assert(b == (a << i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 613 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 614 | b = a; rshift1 (&b,nbytes,i); assert(b == (a >> i));
|
| 615 | b = a; rshift8 (&b,nbytes,i); assert(b == (a >> i));
|
| 616 | b = a; rshift32 (&b,nbytes,i); assert(b == (a >> i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 617 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 618 | b = a; lrot1 (&b,nbytes,i); assert(b == ROTL64(a,i));
|
| 619 | b = a; lrot8 (&b,nbytes,i); assert(b == ROTL64(a,i));
|
| 620 | b = a; lrot32 (&b,nbytes,i); assert(b == ROTL64(a,i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 621 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 622 | b = a; rrot1 (&b,nbytes,i); assert(b == ROTR64(a,i));
|
| 623 | b = a; rrot8 (&b,nbytes,i); assert(b == ROTR64(a,i));
|
| 624 | b = a; rrot32 (&b,nbytes,i); assert(b == ROTR64(a,i));
|
| 625 | }
|
| 626 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 627 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 628 | printf("PASS\n");
|
| 629 | return true;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 630 | }
|
| 631 |
|
| 632 | //-----------------------------------------------------------------------------
|
| 633 |
|
| 634 | template < int nbits >
|
| 635 | bool test_window2 ( void )
|
| 636 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 637 | struct keytype
|
| 638 | {
|
| 639 | uint8_t bytes[nbits/8];
|
| 640 | };
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 641 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 642 | int nbytes = nbits / 8;
|
| 643 | int reps = 10000;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 644 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 645 | for(int j = 0; j < reps; j++)
|
| 646 | {
|
| 647 | if(j % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 648 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 649 | keytype k;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 650 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 651 | rand_p(&k,nbytes);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 652 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 653 | for(int start = 0; start < nbits; start++)
|
| 654 | {
|
| 655 | for(int count = 0; count < 32; count++)
|
| 656 | {
|
| 657 | uint32_t a = window1(&k,nbytes,start,count);
|
| 658 | uint32_t b = window8(&k,nbytes,start,count);
|
| 659 | uint32_t c = window(&k,nbytes,start,count);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 660 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 661 | assert(a == b);
|
| 662 | assert(a == c);
|
| 663 | }
|
| 664 | }
|
| 665 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 666 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 667 | printf("PASS %d\n",nbits);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 668 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 669 | return true;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 670 | }
|
| 671 |
|
| 672 | bool test_window ( void )
|
| 673 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 674 | int reps = 10000;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 675 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 676 | for(int j = 0; j < reps; j++)
|
| 677 | {
|
| 678 | if(j % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 679 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 680 | int nbits = 64;
|
| 681 | int nbytes = nbits / 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 682 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 683 | uint64_t x = rand_u64();
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 684 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 685 | for(int start = 0; start < nbits; start++)
|
| 686 | {
|
| 687 | for(int count = 0; count < 32; count++)
|
| 688 | {
|
| 689 | uint32_t a = (uint32_t)ROTR64(x,start);
|
| 690 | a &= ((1 << count)-1);
|
| 691 |
|
| 692 | uint32_t b = window1 (&x,nbytes,start,count);
|
| 693 | uint32_t c = window8 (&x,nbytes,start,count);
|
| 694 | uint32_t d = window32(&x,nbytes,start,count);
|
| 695 | uint32_t e = window (x,start,count);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 696 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 697 | assert(a == b);
|
| 698 | assert(a == c);
|
| 699 | assert(a == d);
|
| 700 | assert(a == e);
|
| 701 | }
|
| 702 | }
|
| 703 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 704 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 705 | printf("PASS 64\n");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 706 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 707 | test_window2<8>();
|
| 708 | test_window2<16>();
|
| 709 | test_window2<24>();
|
| 710 | test_window2<32>();
|
| 711 | test_window2<40>();
|
| 712 | test_window2<48>();
|
| 713 | test_window2<56>();
|
| 714 | test_window2<64>();
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 715 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame^] | 716 | return true;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 717 | }
|
| 718 |
|
| 719 | //-----------------------------------------------------------------------------
|