Itap88 Temat założony przez niniejszego użytkownika |
Dlaczego zmienna typu bool przyjmuje wartości spoza swojego zakresu? » 2021-05-21 20:50:43 Ostatnio postanowiłem napisać własną implementację algorytmu SHA-256. Otrzymany hash jest jednak nie tylko błędny, ale również wykracza poza zakres zmiennej, w której jest zapisany. #include <iostream> #include <cstdlib> #include <ctime> #include <string>
bool K[ 64 ][ 32 ] = { { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1 }, { 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1 }, { 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1 }, { 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1 }, { 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1 }, { 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 }, { 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1 }, { 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 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, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1 }, { 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, } };
void SHR( bool in[ ], int mov, int insize, bool out[ ] ) { for( insize; insize > 0; insize-- ) { if( insize - 1 - mov >= 0 ) { out[ insize - 1 ] = in[ insize - 1 - mov ]; } else { out[ insize - 1 ] = 0; } } return; }
void SHL( bool in[ ], unsigned long long mov, unsigned long long insize, bool out[ ] ) { for( unsigned long long pos = 0; pos < insize; pos++ ) { if( pos + mov < insize ) { out[ pos ] = in[ pos + mov ]; } else { out[ pos ] = 0; } } return; }
void ROTR( bool in[ ], int mov, int insize, bool out[ ] ) { for( int pos = 0; pos < insize; pos++ ) { out[ pos ] = in[ pos ]; } for( int pos = 0; pos < mov; pos++ ) { bool remainder = out[ insize - 1 ]; SHR( out, 1, insize, out ); out[ 0 ] = remainder; } return; }
void XOR( bool in1[ ], bool in2[ ], int insize, bool out[ ] ) { for( int pos = 0; pos < insize; pos++ ) { out[ pos ] = in1[ pos ] ^ in2[ pos ]; } return; }
void ADD( bool in1[ ], bool in2[ ], int insize, bool out[ ] ) { bool remainderA = 0; bool remainderB = 0; for( insize; insize > 0; insize-- ) { remainderA =( in1[ insize - 1 ] & in2[ insize - 1 ] ) |( in1[ insize - 1 ] & remainderB ) |( in2[ insize - 1 ] & remainderB ); out[ insize - 1 ] = in1[ insize - 1 ] ^ in2[ insize - 1 ] ^ remainderB; remainderB = remainderA; } return; }
void sigma0( bool in[ ], int insize, bool out[ ] ) { bool r7[ insize ]; bool r18[ insize ]; bool sh3[ insize ]; ROTR( in, 7, insize, r7 ); ROTR( in, 18, insize, r18 ); SHR( in, 3, insize, sh3 ); XOR( r7, r18, insize, out ); XOR( out, sh3, insize, out ); return; }
void sigma1( bool in[ ], int insize, bool out[ ] ) { bool r17[ insize ]; bool r19[ insize ]; bool sh10[ insize ]; ROTR( in, 17, insize, r17 ); ROTR( in, 19, insize, r19 ); SHR( in, 10, insize, sh10 ); XOR( r17, r19, insize, out ); XOR( out, sh10, insize, out ); return; }
void SIGMA0( bool in[ ], int insize, bool out[ ] ) { bool r2[ insize ]; bool r13[ insize ]; bool r22[ insize ]; ROTR( in, 2, insize, r2 ); ROTR( in, 13, insize, r13 ); ROTR( in, 22, insize, r22 ); XOR( r2, r13, insize, out ); XOR( out, r22, insize, out ); return; }
void SIGMA1( bool in[ ], int insize, bool out[ ] ) { bool r6[ insize ]; bool r11[ insize ]; bool r25[ insize ]; ROTR( in, 6, insize, r6 ); ROTR( in, 11, insize, r11 ); ROTR( in, 25, insize, r25 ); XOR( r6, r11, insize, out ); XOR( out, r25, insize, out ); return; }
void choice( bool inx[ ], bool iny[ ], bool inz[ ], int insize, bool out[ ] ) { for( int pos = 0; pos < insize; pos++ ) { if( inx[ pos ] == 1 ) { out[ pos ] = iny[ pos ]; } else { out[ pos ] = inz[ pos ]; } } return; }
void majority( bool inx[ ], bool iny[ ], bool inz[ ], int insize, bool out[ ] ) { for( int pos = 0; pos < insize; pos++ ) { if(( inx[ pos ] == iny[ pos ] ) ||( inx[ pos ] == inz[ pos ] ) ) { out[ pos ] = inx[ pos ]; } else { out[ pos ] = iny[ pos ]; } } return; }
unsigned long long pow( unsigned long long num, int lvl ) { if( lvl == 0 ) { return num / num; } unsigned long long res = 1; for( int iteration = 1; iteration <= lvl; iteration++ ) { res = res * num; } return res; }
void convUllBool( unsigned long long in, bool out[ ] ) { for( int pos = 0; pos < 64; pos++ ) { out[ 63 - pos ] =( in & pow( 2, pos ) ) >> pos; } return; }
void padding( bool in[ ], unsigned long long insize, bool out[ ] ) { unsigned long long preblocks; if(( insize + 65 > insize ) ) { preblocks =( insize + 65 ) / 512; } else { preblocks = 36028797018963968; } for( unsigned long long pos = 0; pos < 512 * preblocks + 448; pos++ ) { if( pos < insize ) { out[ pos ] = in[ pos ]; } else { out[ pos ] = 0; } } out[ insize ] = 1; bool outsize[ 64 ]; convUllBool( insize, outsize ); for( unsigned long long pos = 0; pos < 64; pos++ ) { out[ pos + 512 * preblocks + 448 ] = outsize[ pos ]; } return; }
void BlockSplit( bool in[ ], int blocks, bool out[ ][ 512 ] ) { for( int block = 0; block < blocks; block++ ) { for( int pos = 0; pos < 512; pos++ ) { out[ block ][ pos ] = in[ pos + 512 * block ]; } } return; }
void MessageSchedule( bool block[ ], bool out[ ][ 32 ] ) { for( int word = 0; word < 16; word++ ) { for( int pos = 0; pos < 32; pos++ ) { out[ word ][ pos ] = block[ pos + 32 * word ]; } } for( int word = 16; word < 64; word++ ) { bool sum[ 2 ][ 32 ]; sigma0( out[ word - 15 ], 32, sum[ 0 ] ); sigma1( out[ word - 2 ], 32, sum[ 1 ] ); ADD( out[ word - 16 ], sum[ 0 ], 32, out[ word ] ); ADD( out[ word - 7 ], sum[ 1 ], 32, sum[ 0 ] ); ADD( out[ word ], sum[ 0 ], 32, out[ word ] ); } return; }
void compression( bool prevH[ ][ 32 ], bool schedule[ ][ 32 ], bool newH[ ][ 32 ] ) { newH = prevH; for( int word = 0; word < 64; word++ ) { bool T[ 2 ][ 32 ]; bool E1[ 32 ]; bool ch[ 32 ]; SIGMA1( newH[ 4 ], 32, E1 ); choice( newH[ 4 ], newH[ 5 ], newH[ 6 ], 32, ch ); ADD( E1, ch, 32, T[ 0 ] ); ADD( newH[ 7 ], T[ 0 ], 32, T[ 0 ] ); ADD( schedule[ word ], T[ 0 ], 32, T[ 0 ] ); ADD( K[ word ], T[ 0 ], 32, T[ 0 ] ); bool Maj[ 32 ]; SIGMA0( newH[ 0 ], 32, T[ 1 ] ); majority( newH[ 0 ], newH[ 1 ], newH[ 2 ], 32, Maj ); ADD( Maj, T[ 1 ], 32, T[ 1 ] ); for( int reg = 7; reg > 0; reg-- ) { for( int pos = 0; pos < 32; pos++ ) { newH[ reg ][ pos ] = newH[ reg - 1 ][ pos ]; } } ADD( T[ 0 ], T[ 1 ], 32, newH[ 0 ] ); ADD( newH[ 4 ], T[ 0 ], 32, newH[ 4 ] ); } for( int reg = 0; reg < 8; reg++ ) { ADD( newH[ reg ], prevH[ reg ], 32, newH[ reg ] ); } }
unsigned long long convStringBool( std::string in, bool out[ ] ) { unsigned long long pos = 0; for( pos; pos < pos + 1 ; pos++ ) { out[ pos ] =( in[ pos / 8 ] >>( pos % 8 ) ) & 1; if(( in[ pos / 8 ] == pos % 8 == 0 ) ) { break; } } SHL( out, 18446744073709551616 - pos, 18446744073709551616, out ); return pos; }
int main() { std::string input; std::cin >> input; bool hashed[ 18446744073709552128 ]; unsigned long long InputLenght = convStringBool( input, hashed ); padding( hashed, InputLenght, hashed ); bool MessageBlocks[(( InputLenght + 65 ) / 512 ) + 1 ][ 512 ]; BlockSplit( hashed,(( InputLenght + 65 ) / 512 ) + 1, MessageBlocks ); bool MessageWords[(( InputLenght + 65 ) / 512 ) + 1 ][ 64 ][ 32 ]; bool currentH[ 8 ][ 32 ]; for( int reg = 0; reg < 8; reg++ ) { for( int pos = 0; pos < 32; pos++ ) { currentH[ reg ][ pos ] = H0[ reg ][ pos ]; } } bool futureH[ 8 ][ 32 ]; for( unsigned long long block = 0; block <(( InputLenght + 65 ) / 512 ) + 1; block++ ) { MessageSchedule( MessageBlocks[ block ], MessageWords[ block ] ); compression( currentH, MessageWords[ block ], futureH ); for( int reg = 0; reg < 8; reg++ ) { for( int pos = 0; pos < 32; pos++ ) { currentH[ reg ][ pos ] = futureH[ reg ][ pos ]; } } } bool result[ 256 ]; for( int reg = 0; reg < 8; reg++ ) { for( int pos = 0; pos < 32; pos++ ) { result[ 32 * reg + pos ] = currentH[ reg ][ pos ]; std::cout << result[ 32 * reg + pos ]; } } return 1; }
Przykładowe wyjście standardowe: cokolwiek 329000000000040165112000001842110000192011203290412800071661120000000001120001122112028000112211208025111280002800022 400020311201522511090321732011193821230131254255255255602511090253120202119000036121202119401651120001120000088900200 251109022400002123013125229001361271120000001091120428019811994408924416109112000112000008166112000001000100028002820 251127329041428019811914240892442481081120001120000012111520811700112000000109112028002814451120 Process returned 1 (0x1) execution time : 4.834 s Press any key to continue.
Czy ktoś byłby mi w stanie wyjaśnić, jak takie działanie programu w ogóle jest możliwe i z czego wynika? |