"Fossies" - the Fresh Open Source Software Archive

Member "unrar/blake2sp.cpp" (4 May 2022, 3814 Bytes) of package /linux/misc/unrarsrc-6.1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "blake2sp.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2    BLAKE2 reference source code package - reference C implementations
    3 
    4    Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
    5 
    6    To the extent possible under law, the author(s) have dedicated all copyright
    7    and related and neighboring rights to this software to the public domain
    8    worldwide. This software is distributed without any warranty.
    9 
   10    You should have received a copy of the CC0 Public Domain Dedication along with
   11    this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
   12 */
   13 
   14 #define PARALLELISM_DEGREE 8
   15 
   16 void blake2sp_init( blake2sp_state *S )
   17 {
   18   memset( S->buf, 0, sizeof( S->buf ) );
   19   S->buflen = 0;
   20 
   21   blake2s_init_param( &S->R, 0, 1 ); // Init root.
   22 
   23   for( uint i = 0; i < PARALLELISM_DEGREE; ++i )
   24     blake2s_init_param( &S->S[i], i, 0 ); // Init leaf.
   25 
   26   S->R.last_node = 1;
   27   S->S[PARALLELISM_DEGREE - 1].last_node = 1;
   28 }
   29 
   30 
   31 struct Blake2ThreadData
   32 {
   33   void Update();
   34   blake2s_state *S;
   35   const byte *in;
   36   size_t inlen;
   37 };
   38 
   39 
   40 void Blake2ThreadData::Update()
   41 {
   42   size_t inlen__ = inlen;
   43   const byte *in__ = ( const byte * )in;
   44 
   45   while( inlen__ >= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES )
   46   {
   47 #ifdef USE_SSE
   48     // We gain 5% in i7 SSE mode by prefetching next data block.
   49     if (_SSE_Version>=SSE_SSE && inlen__ >= 2 * PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES)
   50       _mm_prefetch((char*)(in__ +  PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES), _MM_HINT_T0);
   51 #endif
   52     blake2s_update( S, in__, BLAKE2S_BLOCKBYTES );
   53     in__ += PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
   54     inlen__ -= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
   55   }
   56 }
   57 
   58 #ifdef RAR_SMP
   59 THREAD_PROC(Blake2Thread)
   60 {
   61   Blake2ThreadData *td=(Blake2ThreadData *)Data;
   62   td->Update();
   63 }
   64 #endif
   65 
   66 
   67 void blake2sp_update( blake2sp_state *S, const byte *in, size_t inlen )
   68 {
   69   size_t left = S->buflen;
   70   size_t fill = sizeof( S->buf ) - left;
   71 
   72   if( left && inlen >= fill )
   73   {
   74     memcpy( S->buf + left, in, fill );
   75 
   76     for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
   77       blake2s_update( &S->S[i], S->buf + i * BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
   78 
   79     in += fill;
   80     inlen -= fill;
   81     left = 0;
   82   }
   83 
   84   Blake2ThreadData btd_array[PARALLELISM_DEGREE];
   85 
   86 #ifdef RAR_SMP
   87   uint ThreadNumber = inlen < 0x1000 ? 1 : S->MaxThreads;
   88   
   89   if (ThreadNumber==6 || ThreadNumber==7) // 6 and 7 threads work slower than 4 here.
   90     ThreadNumber=4;
   91 #else
   92   uint ThreadNumber=1;
   93 #endif
   94 
   95   for (size_t id__=0;id__<PARALLELISM_DEGREE;)
   96   {
   97     for (uint Thread=0;Thread<ThreadNumber && id__<PARALLELISM_DEGREE;Thread++)
   98     {
   99       Blake2ThreadData *btd=btd_array+Thread;
  100 
  101       btd->inlen = inlen;
  102       btd->in = in + id__ * BLAKE2S_BLOCKBYTES;
  103       btd->S = &S->S[id__];
  104   
  105 #ifdef RAR_SMP
  106       if (ThreadNumber>1)
  107         S->ThPool->AddTask(Blake2Thread,(void*)btd);
  108       else
  109         btd->Update();
  110 #else
  111       btd->Update();
  112 #endif
  113       id__++;
  114     }
  115 #ifdef RAR_SMP
  116     if (S->ThPool!=NULL) // Can be NULL in -mt1 mode.
  117       S->ThPool->WaitDone();
  118 #endif // RAR_SMP
  119   }
  120 
  121   in += inlen - inlen % ( PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES );
  122   inlen %= PARALLELISM_DEGREE * BLAKE2S_BLOCKBYTES;
  123 
  124   if( inlen > 0 )
  125     memcpy( S->buf + left, in, (size_t)inlen );
  126 
  127   S->buflen = left + (size_t)inlen;
  128 }
  129 
  130 
  131 void blake2sp_final( blake2sp_state *S, byte *digest )
  132 {
  133   byte hash[PARALLELISM_DEGREE][BLAKE2S_OUTBYTES];
  134 
  135   for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
  136   {
  137     if( S->buflen > i * BLAKE2S_BLOCKBYTES )
  138     {
  139       size_t left = S->buflen - i * BLAKE2S_BLOCKBYTES;
  140 
  141       if( left > BLAKE2S_BLOCKBYTES ) left = BLAKE2S_BLOCKBYTES;
  142 
  143       blake2s_update( &S->S[i], S->buf + i * BLAKE2S_BLOCKBYTES, left );
  144     }
  145 
  146     blake2s_final( &S->S[i], hash[i] );
  147   }
  148 
  149   for( size_t i = 0; i < PARALLELISM_DEGREE; ++i )
  150     blake2s_update( &S->R, hash[i], BLAKE2S_OUTBYTES );
  151 
  152   blake2s_final( &S->R, digest );
  153 }