unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

blake2sp.cpp
Go to the documentation of this file.
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
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
32{
33 void Update();
35 const byte *in;
36 size_t inlen;
37};
38
39
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
55 }
56}
57
58#ifdef RAR_SMP
59THREAD_PROC(Blake2Thread)
60{
62 td->Update();
63}
64#endif
65
66
67void 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 )
78
79 in += fill;
80 inlen -= fill;
81 left = 0;
82 }
83
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 );
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
131void blake2sp_final( blake2sp_state *S, byte *digest )
132{
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}
static void blake2s_init_param(blake2s_state *S, uint32 node_offset, uint32 node_depth)
Definition: blake2s.cpp:58
static void blake2s_final(blake2s_state *S, byte *digest)
Definition: blake2s.cpp:165
static void blake2s_update(blake2s_state *S, const byte *in, size_t inlen)
Definition: blake2s.cpp:123
@ BLAKE2S_OUTBYTES
Definition: blake2s.hpp:11
@ BLAKE2S_BLOCKBYTES
Definition: blake2s.hpp:10
void blake2sp_final(blake2sp_state *S, byte *digest)
Definition: blake2sp.cpp:131
#define PARALLELISM_DEGREE
Definition: blake2sp.cpp:14
void blake2sp_update(blake2sp_state *S, const byte *in, size_t inlen)
Definition: blake2sp.cpp:67
void blake2sp_init(blake2sp_state *S)
Definition: blake2sp.cpp:16
unsigned int uint
Definition: rartypes.hpp:8
static byte S[256]
Definition: rijndael.cpp:10
const byte * in
Definition: blake2sp.cpp:35
blake2s_state * S
Definition: blake2sp.cpp:34
THREAD_PROC(UnpackDecodeThread)
Definition: unpack50mt.cpp:12