fix parsing of i64 constants in globals, and add working cube2hash testcase

This commit is contained in:
Alon Zakai 2012-04-01 20:23:17 -07:00
Родитель 4401a0b0e4
Коммит 3c4fc41864
10 изменённых файлов: 310 добавлений и 13 удалений

Просмотреть файл

@ -167,23 +167,27 @@ function JSify(data, functionsOnly, givenFunctions) {
ret[index++] = 0;
}
// Add current value(s)
var currValue = flatten(values[i]);
var currValue = values[i];
if (USE_TYPED_ARRAYS == 2 && typeData.fields[i] == 'i64') {
// 'flatten' out the 64-bit value into two 32-bit halves
ret[index++] = currValue>>>0;
var parts = parseI64Constant(currValue, true);
ret[index++] = parts[0];
ret[index++] = 0;
ret[index++] = 0;
ret[index++] = 0;
ret[index++] = Math.floor(currValue/4294967296);
ret[index++] = parts[1];
ret[index++] = 0;
ret[index++] = 0;
ret[index++] = 0;
} else if (typeof currValue == 'object') {
for (var j = 0; j < currValue.length; j++) {
ret[index++] = currValue[j];
}
} else {
ret[index++] = currValue;
currValue = flatten(currValue);
if (typeof currValue == 'object') {
for (var j = 0; j < currValue.length; j++) {
ret[index++] = currValue[j];
}
} else {
ret[index++] = currValue;
}
}
i += 1;
}

Просмотреть файл

@ -683,16 +683,14 @@ function parseArbitraryInt(str, bits) {
return ret;
}
function parseI64Constant(str) {
assert(USE_TYPED_ARRAYS == 2);
function parseI64Constant(str, legalized) {
if (!isNumber(str)) {
// This is a variable. Copy it, so we do not modify the original
return legalizedI64s ? str : makeCopyI64(str);
}
var parsed = parseArbitraryInt(str, 64);
if (legalizedI64s) return parsed;
if (legalizedI64s || legalized) return parsed;
return '[' + parsed[0] + ',' + parsed[1] + ']';
}

14
tests/cube2hash/Makefile Normal file
Просмотреть файл

@ -0,0 +1,14 @@
all: cube2hash.bc
cube2hash.bc: cube2crypto.o tiger.o hashstring.o
$(CXX) $^ -o $@
hashstring.o: hashstring.cpp
$(CXX) -c $^ -o $@
cube2crypto.o: cube2crypto.c cube2crypto.h
$(CC) -c $< -o $@
tiger.o: tiger.c tiger.h
$(CC) -c $< -o $@

Просмотреть файл

@ -0,0 +1,23 @@
#include <stdlib.h>
#include "util.h"
#include "tiger.h"
#include "cube2crypto.h"
char *cube2crypto_hashstring(char *string)
{
char *result = (char *)malloc(49);
union hashval hv;
tiger_hash((uchar *)string, strlen(string), &hv);
int i;
for(i = 0; i < sizeof(hv.bytes); i++)
{
uchar c = hv.bytes[i];
*(result+(i*2)) = "0123456789ABCDEF"[c&0xF];
*(result+(i*2)+1) = "0123456789ABCDEF"[c>>4];
}
*(result+(i*2)+2) = '\0';
return result;
}

Просмотреть файл

@ -0,0 +1,9 @@
#ifdef __cplusplus
extern "C" {
#endif
char *cube2crypto_hashstring(char *string);
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif

Просмотреть файл

@ -0,0 +1,28 @@
#include "cube2crypto.h"
#include <stdlib.h>
#include <stdio.h>
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
void help()
{
printf("Usage: hashstring <seed>\n");
}
int main(int argc, char **argv)
{
if(argc != 2 || !argv[1])
{
help();
return EXIT_FAILURE;
}
char *answer = cube2crypto_hashstring(argv[1]);
printf("hash value: %s\n", answer);
free(answer);
return EXIT_SUCCESS;
}

175
tests/cube2hash/tiger.c Normal file
Просмотреть файл

@ -0,0 +1,175 @@
///////////////////////// cryptography /////////////////////////////////
/* Based off the reference implementation of Tiger, a cryptographically
* secure 192 bit hash function by Ross Anderson and Eli Biham. More info at:
* http://www.cs.technion.ac.il/~biham/Reports/Tiger/
*/
#define TIGER_PASSES 3
#include "tiger.h"
#include "util.h"
chunk sboxes[4*256];
#define sb1 (sboxes)
#define sb2 (sboxes+256)
#define sb3 (sboxes+256*2)
#define sb4 (sboxes+256*3)
#define round(a, b, c, x) \
c ^= x; \
a -= sb1[((c)>>(0*8))&0xFF] ^ sb2[((c)>>(2*8))&0xFF] ^ \
sb3[((c)>>(4*8))&0xFF] ^ sb4[((c)>>(6*8))&0xFF] ; \
b += sb4[((c)>>(1*8))&0xFF] ^ sb3[((c)>>(3*8))&0xFF] ^ \
sb2[((c)>>(5*8))&0xFF] ^ sb1[((c)>>(7*8))&0xFF] ; \
b *= mul;
void tiger_compress(const chunk *str, chunk state[3])
{
chunk a, b, c;
chunk aa, bb, cc;
chunk x0, x1, x2, x3, x4, x5, x6, x7;
a = state[0];
b = state[1];
c = state[2];
x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3];
x4=str[4]; x5=str[5]; x6=str[6]; x7=str[7];
aa = a;
bb = b;
cc = c;
int pass;
for(pass = 0; pass < TIGER_PASSES; pass++)
{
if(pass)
{
x0 -= x7 ^ 0xA5A5A5A5A5A5A5A5ULL; x1 ^= x0; x2 += x1; x3 -= x2 ^ ((~x1)<<19);
x4 ^= x3; x5 += x4; x6 -= x5 ^ ((~x4)>>23); x7 ^= x6;
x0 += x7; x1 -= x0 ^ ((~x7)<<19); x2 ^= x1; x3 += x2;
x4 -= x3 ^ ((~x2)>>23); x5 ^= x4; x6 += x5; x7 -= x6 ^ 0x0123456789ABCDEFULL;
}
uint mul = !pass ? 5 : (pass==1 ? 7 : 9);
round(a, b, c, x0) round(b, c, a, x1) round(c, a, b, x2) round(a, b, c, x3)
round(b, c, a, x4) round(c, a, b, x5) round(a, b, c, x6) round(b, c, a, x7)
chunk tmp = a; a = c; c = b; b = tmp;
}
a ^= aa;
b -= bb;
c += cc;
state[0] = a;
state[1] = b;
state[2] = c;
}
void tiger_gensboxes()
{
const char *str = "Tiger - A Fast New Hash Function, by Ross Anderson and Eli Biham";
chunk state[3] = { 0x0123456789ABCDEFULL, 0xFEDCBA9876543210ULL, 0xF096A5B4C3B2E187ULL };
uchar temp[64];
int i, j, col, sb, pass;
if(BIGENDIAN)
{
for(j = 0; j < 64; j++)
{
temp[j^7] = str[j];
}
}
else
{
for(j = 0; j < 64; j++)
{
temp[j] = str[j];
}
}
for(i = 0; i < 1024; i++)
{
for(col = 0; col < 8; col++)
{
((uchar *)&sboxes[i])[col] = i&0xFF;
}
}
int abc = 2;
for(pass = 0; pass < 5; pass++)
{
for(i = 0; i < 256; i++)
{
for(sb = 0; sb < 1024; sb += 256)
{
abc++;
if(abc >= 3) { abc = 0; tiger_compress((chunk *)temp, state); }
for(col = 0; col < 8; col++)
{
uchar val = ((uchar *)&sboxes[sb+i])[col];
((uchar *)&sboxes[sb+i])[col] = ((uchar *)&sboxes[sb + ((uchar *)&state[abc])[col]])[col];
((uchar *)&sboxes[sb + ((uchar *)&state[abc])[col]])[col] = val;
}
}
}
}
}
void tiger_hash(const uchar *str, int length, union hashval *val)
{
static int init = false;
if(!init) { tiger_gensboxes(); init = true; }
uchar temp[64];
val->chunks[0] = 0x0123456789ABCDEFULL;
val->chunks[1] = 0xFEDCBA9876543210ULL;
val->chunks[2] = 0xF096A5B4C3B2E187ULL;
int i, j;
for(i = length; i >= 64; i -= 64, str += 64)
{
if(BIGENDIAN)
{
for(j = 0; j < 64; j++)
{
temp[j^7] = str[j];
}
tiger_compress((chunk *)temp, val->chunks);
}
else
{
tiger_compress((chunk *)str, val->chunks);
}
}
if(BIGENDIAN)
{
for(j = 0; j < i; j++) temp[j^7] = str[j];
temp[j^7] = 0x01;
while(++j&7) temp[j^7] = 0;
}
else
{
for(j = 0; j < i; j++) temp[j] = str[j];
temp[j] = 0x01;
while(++j&7) temp[j] = 0;
}
if(j > 56)
{
while(j < 64) temp[j++] = 0;
tiger_compress((chunk *)temp, val->chunks);
j = 0;
}
while(j < 56) temp[j++] = 0;
*(chunk *)(temp+56) = (chunk)length<<3;
tiger_compress((chunk *)temp, val->chunks);
}

12
tests/cube2hash/tiger.h Normal file
Просмотреть файл

@ -0,0 +1,12 @@
#ifndef _TIGER_H
#define _TIGER_H
union hashval
{
unsigned char bytes[3*8];
unsigned long long int chunks[3];
};
void tiger_hash(const unsigned char *str, int length, union hashval *val);
#endif

22
tests/cube2hash/util.h Normal file
Просмотреть файл

@ -0,0 +1,22 @@
#ifndef _UTIL_H
#define _UTIL_H
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#define BIGENDIAN 0
#ifndef bool
#define bool unsigned short int
#define true 1
#define false 0
#endif
typedef unsigned long long int chunk;
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
#endif

Просмотреть файл

@ -860,10 +860,22 @@ m_divisor is 1091269979
'''
self.do_run(src, open(path_from_root('tests', 'i64_precise.txt')).read())
print 'TODO: make precise the default, and imprecise in -O3'
print 'TODO: make precise the default, and imprecise in -O3. Remove precise setting in this test and cube2hash'
print 'TODO: only include this code when needed'
1/0.
def test_cube2hash(self):
# A good test of i64 math
Settings.PRECISE_I64_MATH = 1
self.do_run('', 'Usage: hashstring <seed>',
libraries=self.get_library('cube2hash', ['cube2hash.bc'], configure=None),
includes=[path_from_root('tests', 'cube2hash')])
for text, output in [('fleefl', '892BDB6FD3F62E863D63DA55851700FDE3ACF30204798CE9'),
('fleefl2', 'AA2CC5F96FC9D540CA24FDAF1F71E2942753DB83E8A81B61'),
('64bitisslow', '64D8470573635EC354FEE7B7F87C566FCAF1EFB491041670')]:
self.do_run('', 'hash value: ' + output, [text], no_build=True)
def test_unaligned(self):
if Settings.QUANTUM_SIZE == 1: return self.skip('No meaning to unaligned addresses in q1')