Skip to content

Instantly share code, notes, and snippets.

@cchandler
Created July 26, 2011 23:25
Show Gist options
  • Save cchandler/1108349 to your computer and use it in GitHub Desktop.
Save cchandler/1108349 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
unsigned int cpu_luhn_on_packed(unsigned long num){
int len = 15;
unsigned long digit = 0;
int even = 0;
unsigned long sum = 0;
for(len = 15; len >= 0; --len) {
digit = 0;
digit = num & ((unsigned long)15 << (len * 4));
digit = digit >> (len * 4);
if(even) {
digit = digit * 2;
if(digit > 9){
digit = digit - 9;
}
}
sum = sum + digit;
even = !even;
}
return (sum % 10 == 0);
}
unsigned long cpu_bit_pack_CC(unsigned long num){
int i = 0;
int digit = 0;
unsigned long result = 0;
for(i = 15; i >= 0; --i){
digit = num % 10;
num = num / 10;
result = result << 4;
result = result | digit;
}
return result;
}
#define rightrotate(original, bits) ((original >> bits) | (original << (32 -bits)))
#define rightrotateL(original, bits) ((original >> bits) | (original << (64 -bits)))
unsigned int cpu_popNextW(unsigned int *w, int &wIndex)
{
unsigned int nextW=w[wIndex&15];
int thisIndex=wIndex&15;
unsigned int s0 = rightrotate(w[(wIndex+16-15)&15],7) ^ rightrotate(w[(wIndex+16-15)&15],18) ^ (w[(wIndex+16-15)&15] >> 3) ;
unsigned int s1 = rightrotate(w[(wIndex+16-2)&15],17) ^ rightrotate(w[(wIndex+16-2)&15],19) ^ (w[(wIndex+16-2)&15] >> 10);
w[thisIndex] = w[(wIndex+16-16)&15] + s0 + w[(wIndex+16-7)&15] + s1;
++wIndex;
// if (threadIdx.x==0) debugprint("pop %08x\n", nextW);
return nextW;
}
unsigned long long int cpu_popNextW_long(unsigned long long int *w, int &wIndex)
{
unsigned long long int nextW=w[wIndex&15];
int thisIndex=wIndex&15;
unsigned long long int s0 = rightrotateL(w[(wIndex+16-15)&15],1) ^ rightrotateL(w[(wIndex+16-15)&15],8) ^ (w[(wIndex+16-15)&15] >> 7) ;
unsigned long long int s1 = rightrotateL(w[(wIndex+16-2)&15],19) ^ rightrotateL(w[(wIndex+16-2)&15],61) ^ (w[(wIndex+16-2)&15] >> 6);
w[thisIndex] = w[(wIndex+16-16)&15] + s0 + w[(wIndex+16-7)&15] + s1;
++wIndex;
// if (threadIdx.x==0) debugprint("pop %08x\n", nextW);
return nextW;
}
unsigned long long int cpu_popFinalWs_long(unsigned long long int *w, int &wIndex)
{
unsigned long long int nextW=w[wIndex&15];
++wIndex;
return nextW;
}
unsigned int cpu_popFinalWs(unsigned int *w, int &wIndex)
{
unsigned int nextW=w[wIndex&15];
++wIndex;
return nextW;
}
int cpu_sha512(unsigned int num1, unsigned int num2, unsigned long long int *hash){
unsigned long long int d_initVector[8];
d_initVector[0] = 0x6a09e667f3bcc908UL;
d_initVector[1] = 0xbb67ae8584caa73bUL;
d_initVector[2] = 0x3c6ef372fe94f82bUL;
d_initVector[3] = 0xa54ff53a5f1d36f1UL;
d_initVector[4] = 0x510e527fade682d1UL;
d_initVector[5] = 0x9b05688c2b3e6c1fUL;
d_initVector[6] = 0x1f83d9abfb41bd6bUL;
d_initVector[7] = 0x5be0cd19137e2179UL;
unsigned long long int k[80] = {
0x428a2f98d728ae22UL, 0x7137449123ef65cdUL,
0xb5c0fbcfec4d3b2fUL, 0xe9b5dba58189dbbcUL,
0x3956c25bf348b538UL, 0x59f111f1b605d019UL,
0x923f82a4af194f9bUL, 0xab1c5ed5da6d8118UL,
0xd807aa98a3030242UL, 0x12835b0145706fbeUL,
0x243185be4ee4b28cUL, 0x550c7dc3d5ffb4e2UL,
0x72be5d74f27b896fUL, 0x80deb1fe3b1696b1UL,
0x9bdc06a725c71235UL, 0xc19bf174cf692694UL,
0xe49b69c19ef14ad2UL, 0xefbe4786384f25e3UL,
0x0fc19dc68b8cd5b5UL, 0x240ca1cc77ac9c65UL,
0x2de92c6f592b0275UL, 0x4a7484aa6ea6e483UL,
0x5cb0a9dcbd41fbd4UL, 0x76f988da831153b5UL,
0x983e5152ee66dfabUL, 0xa831c66d2db43210UL,
0xb00327c898fb213fUL, 0xbf597fc7beef0ee4UL,
0xc6e00bf33da88fc2UL, 0xd5a79147930aa725UL,
0x06ca6351e003826fUL, 0x142929670a0e6e70UL,
0x27b70a8546d22ffcUL, 0x2e1b21385c26c926UL,
0x4d2c6dfc5ac42aedUL, 0x53380d139d95b3dfUL,
0x650a73548baf63deUL, 0x766a0abb3c77b2a8UL,
0x81c2c92e47edaee6UL, 0x92722c851482353bUL,
0xa2bfe8a14cf10364UL, 0xa81a664bbc423001UL,
0xc24b8b70d0f89791UL, 0xc76c51a30654be30UL,
0xd192e819d6ef5218UL, 0xd69906245565a910UL,
0xf40e35855771202aUL, 0x106aa07032bbd1b8UL,
0x19a4c116b8d2d0c8UL, 0x1e376c085141ab53UL,
0x2748774cdf8eeb99UL, 0x34b0bcb5e19b48a8UL,
0x391c0cb3c5c95a63UL, 0x4ed8aa4ae3418acbUL,
0x5b9cca4f7763e373UL, 0x682e6ff3d6b2b8a3UL,
0x748f82ee5defb2fcUL, 0x78a5636f43172f60UL,
0x84c87814a1f0ab72UL, 0x8cc702081a6439ecUL,
0x90befffa23631e28UL, 0xa4506cebde82bde9UL,
0xbef9a3f7b2c67915UL, 0xc67178f2e372532bUL,
0xca273eceea26619cUL, 0xd186b8c721c0c207UL,
0xeada7dd6cde0eb1eUL, 0xf57d4f7fee6ed178UL,
0x06f067aa72176fbaUL, 0x0a637dc5a2c898a6UL,
0x113f9804bef90daeUL, 0x1b710b35131c471bUL,
0x28db77f523047d84UL, 0x32caab7b40c72493UL,
0x3c9ebe0a15c9bebcUL, 0x431d67c49c100d4cUL,
0x4cc5d4becb3e42b6UL, 0x597f299cfc657e2aUL,
0x5fcb6fab3ad6faecUL, 0x6c44198c4a475817UL,
};
char lookup_table[10] = {48,49,50,51,52,53,54,55,56,57};
int pos = 0;
unsigned int digit = 0;
unsigned int num_1a = 0;
unsigned int num_2a = 0;
unsigned int num_3a = 0;
unsigned int num_4a = 0;
#pragma unroll 999
for(pos = 0; pos <= 3; ++pos) {
digit = 0;
digit = num2 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_1a = num_1a | lookup_table[digit];
if(pos != 3) {num_1a = num_1a << 8;};
}
#pragma unroll 999
for(pos = 4; pos <= 7; ++pos) {
digit = 0;
digit = num2 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_2a = num_2a | lookup_table[digit];
if(pos != 7) {num_2a = num_2a << 8;};
}
#pragma unroll 999
for(pos = 0; pos <= 3; ++pos) {
digit = 0;
digit = num1 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_3a = num_3a | lookup_table[digit];
if(pos != 3) {num_3a = num_3a << 8;};
}
#pragma unroll 999
for(pos = 4; pos <= 7; ++pos) {
digit = 0;
digit = num1 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_4a = num_4a | lookup_table[digit];
if(pos != 7) {num_4a = num_4a << 8;};
}
unsigned long long int w[80] = {'\0'};
for (int i=0; i<80; i++) { w[i] = 0UL; };
// w[0] = 1633837952; // 'abc' + 1 bit for 32 bit
// w[0] = 0x6162638000000000UL; // 'abc' + 1 bit for 64 bit
// w[0] = 0x8000000000000000UL; // '' + 1 bit
w[0] = num_1a;
w[0] = w[0] << 32;
w[0] = w[0] | num_2a;
w[1] = num_3a;
w[1] = w[1] << 32;
w[1] = w[1] | num_4a;
w[2] = (unsigned long long int) 1 << 63;
// printf("Val: %lu",w[2]);
w[15] = 128;
// w[15] = 24;
// w[15] = 0;
int wIndex=0;
unsigned long long int a = d_initVector[0];
unsigned long long int b = d_initVector[1];
unsigned long long int c = d_initVector[2];
unsigned long long int d = d_initVector[3];
unsigned long long int e = d_initVector[4];
unsigned long long int f = d_initVector[5];
unsigned long long int g = d_initVector[6];
unsigned long long int h = d_initVector[7];
// for(int i=16; i < 80; i++){
// unsigned long long int s0 = rightrotateL(w[i-15], 1) ^ rightrotateL(w[i-15], 8) ^ (w[i-15] >> 7);
// unsigned long long int s1 = rightrotateL(w[i-2], 19) ^ rightrotateL(w[i-2], 61) ^ (w[i-2] >> 6);
// w[i] = w[i-16] + s0 + w[i-7] + s1;
// }
for(int i =0; i < 80; i++){
//s0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22)
unsigned long long int s0 = rightrotateL(a,28) ^ rightrotateL(a, 34) ^ rightrotateL(a,39);
//maj := (a and b) xor (a and c) xor (b and c)
unsigned long long int maj = (a & b) ^ (a & c) ^ (b & c);
//t2 := s0 + maj
unsigned long long int t2 = s0 + maj;
//s1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
unsigned long long int s1 = rightrotateL(e, 14) ^ rightrotateL(e, 18) ^ rightrotateL(e,41);
//ch := (e and f) xor ((not e) and g)
unsigned long long int ch = (e & f) ^ ((~e) & g);
//t1 := h + s1 + ch + k[i] + w[i]
unsigned long long int thisW=cpu_popNextW_long(w,wIndex);
unsigned long long int t1 = h + s1 + ch + k[i] + thisW;
// unsigned long long int t1 = h + s1 + ch + k[i] + w[i];
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
hash[0] = a + d_initVector[0];
hash[1] = b + d_initVector[1];
hash[2] = c + d_initVector[2];
hash[3] = d + d_initVector[3];
hash[4] = e + d_initVector[4];
hash[5] = f + d_initVector[5];
hash[6] = g + d_initVector[6];
hash[7] = h + d_initVector[7];
return 0;
}
/*
SHA1 Hash
Modified very of Steve Worley's
*/
int cpu_sha256(unsigned int num1, unsigned int num2, unsigned int *hash){
unsigned int d_initVector[8];
d_initVector[0] = 0x6a09e667;
d_initVector[1] = 0xbb67ae85;
d_initVector[2] = 0x3c6ef372;
d_initVector[3] = 0xa54ff53a;
d_initVector[4] = 0x510e527f;
d_initVector[5] = 0x9b05688c;
d_initVector[6] = 0x1f83d9ab;
d_initVector[7] = 0x5be0cd19;
unsigned int k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
char lookup_table[10] = {48,49,50,51,52,53,54,55,56,57};
int pos = 0;
unsigned int digit = 0;
unsigned int num_1a = 0;
unsigned int num_2a = 0;
unsigned int num_3a = 0;
unsigned int num_4a = 0;
#pragma unroll 999
for(pos = 0; pos <= 3; ++pos) {
digit = 0;
digit = num2 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_1a = num_1a | lookup_table[digit];
if(pos != 3) {num_1a = num_1a << 8;};
}
#pragma unroll 999
for(pos = 4; pos <= 7; ++pos) {
digit = 0;
digit = num2 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_2a = num_2a | lookup_table[digit];
if(pos != 7) {num_2a = num_2a << 8;};
}
#pragma unroll 999
for(pos = 0; pos <= 3; ++pos) {
digit = 0;
digit = num1 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_3a = num_3a | lookup_table[digit];
if(pos != 3) {num_3a = num_3a << 8;};
}
#pragma unroll 999
for(pos = 4; pos <= 7; ++pos) {
digit = 0;
digit = num1 & (0xF << (pos * 4));
digit = digit >> (pos * 4);
num_4a = num_4a | lookup_table[digit];
if(pos != 7) {num_4a = num_4a << 8;};
}
unsigned int w[64] = {'\0'};
for (int i=0; i<64; i++) { w[i] = '\0'; };
// w[0] = 1633837952; // 'abc' + 1 bit
// num_1a = num_1a << 8;
w[0] = num_1a;
w[1] = num_2a;
w[2] = num_3a;
w[3] = num_4a;
w[4] = (unsigned) 8 << 28;
w[15] = 128;
int wIndex=0;
unsigned int a = d_initVector[0];
unsigned int b = d_initVector[1];
unsigned int c = d_initVector[2];
unsigned int d = d_initVector[3];
unsigned int e = d_initVector[4];
unsigned int f = d_initVector[5];
unsigned int g = d_initVector[6];
unsigned int h = d_initVector[7];
/*for(int i=16; i < 64; i++){
unsigned int s0 = rightrotate(w[i-15], 7) ^ rightrotate(w[i-15], 18) ^ (w[i-15] >> 3);
unsigned int s1 = rightrotate(w[i-2], 17) ^ rightrotate(w[i-2], 19) ^ (w[i-2] >> 10);
w[i] = w[i-16] + s0 + w[i-7] + s1;
}*/
for(int i =0; i < 64; i++){
//s0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22)
unsigned int s0 = rightrotate(a,2) ^ rightrotate(a, 13) ^ rightrotate(a,22);
//maj := (a and b) xor (a and c) xor (b and c)
unsigned int maj = (a & b) ^ (a & c) ^ (b & c);
//t2 := s0 + maj
unsigned int t2 = s0 + maj;
//s1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25)
unsigned int s1 = rightrotate(e, 6) ^ rightrotate(e, 11) ^ rightrotate(e,25);
//ch := (e and f) xor ((not e) and g)
unsigned int ch = (e & f) ^ ((~e) & g);
//t1 := h + s1 + ch + k[i] + w[i]
unsigned int thisW=cpu_popNextW(w,wIndex);
//unsigned int t1 = h + s1 + ch + k[i] + w[i];
unsigned int t1 = h + s1 + ch + k[i] + thisW;
h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
hash[0] = a + d_initVector[0];
hash[1] = b + d_initVector[1];
hash[2] = c + d_initVector[2];
hash[3] = d + d_initVector[3];
hash[4] = e + d_initVector[4];
hash[5] = f + d_initVector[5];
hash[6] = g + d_initVector[6];
hash[7] = h + d_initVector[7];
return 0;
}
int main(){
printf("Test hash\n");
long long cc = 4111111111111111;
unsigned long packed = 0;
unsigned long long int* hash;
hash = (unsigned long long int*) malloc(sizeof(unsigned long long int) * 8);
packed = cpu_bit_pack_CC(cc);
printf("Valid? %d\n", cpu_luhn_on_packed(packed));
unsigned int part1 = 0xFFFFFFFF & packed;
packed = packed >> 32;
unsigned int part2 = 0xFFFFFFFF & packed;
cpu_sha512(part2,part1,hash);
char buffer[512];
sprintf(buffer,"%016llx%016llx%016llx%016llx%016llx%016llx%016llx%016llx", hash[0],hash[1],hash[2],hash[3],hash[4],hash[5],hash[6],hash[7]);
printf("Buffer %s\n", buffer);
// unsigned long long int test = 0x8000000000000000UL;
// printf("Start: %llx\n", test);
// printf("Round 1: %llx\n", rightrotateL(test,1));
// printf("Round 2: %llx\n", rightrotateL(test,2));
// printf("Round 3: %llx\n", rightrotateL(test,3));
//free(hash);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment