19extern const char LSH512_AVX_FNAME[] = __FILE__;
21#if defined(CRYPTOPP_AVX2_AVAILABLE) && defined(CRYPTOPP_ENABLE_64BIT_SSE)
23#if defined(CRYPTOPP_AVX2_AVAILABLE)
24# include <emmintrin.h>
25# include <immintrin.h>
29#if (CRYPTOPP_GCC_VERSION >= 40500)
30# include <x86intrin.h>
33ANONYMOUS_NAMESPACE_BEGIN
37const unsigned int LSH512_MSG_BLK_BYTE_LEN = 256;
40const unsigned int LSH512_HASH_VAL_MAX_BYTE_LEN = 64;
43const unsigned int CV_WORD_LEN = 16;
44const unsigned int CONST_WORD_LEN = 8;
46const unsigned int NUM_STEPS = 28;
48const unsigned int ROT_EVEN_ALPHA = 23;
49const unsigned int ROT_EVEN_BETA = 59;
50const unsigned int ROT_ODD_ALPHA = 7;
51const unsigned int ROT_ODD_BETA = 3;
53const unsigned int LSH_TYPE_512_512 = 0x0010040;
54const unsigned int LSH_TYPE_512_384 = 0x0010030;
55const unsigned int LSH_TYPE_512_256 = 0x0010020;
56const unsigned int LSH_TYPE_512_224 = 0x001001C;
63const unsigned int LSH_SUCCESS = 0x0;
66const unsigned int LSH_ERR_INVALID_DATABITLEN = 0x2403;
67const unsigned int LSH_ERR_INVALID_STATE = 0x2404;
71const unsigned int AlgorithmType = 80;
72const unsigned int RemainingBits = 81;
76NAMESPACE_BEGIN(CryptoPP)
80extern const word64 LSH512_IV224[CV_WORD_LEN];
81extern const word64 LSH512_IV256[CV_WORD_LEN];
82extern const word64 LSH512_IV384[CV_WORD_LEN];
83extern const word64 LSH512_IV512[CV_WORD_LEN];
84extern const word64 LSH512_StepConstants[CONST_WORD_LEN * NUM_STEPS];
89ANONYMOUS_NAMESPACE_BEGIN
92using CryptoPP::word32;
93using CryptoPP::word64;
94using CryptoPP::rotlFixed;
95using CryptoPP::rotlConstant;
97using CryptoPP::GetBlock;
98using CryptoPP::LittleEndian;
99using CryptoPP::ConditionalByteReverse;
100using CryptoPP::LITTLE_ENDIAN_ORDER;
102using CryptoPP::LSH::LSH512_IV224;
103using CryptoPP::LSH::LSH512_IV256;
104using CryptoPP::LSH::LSH512_IV384;
105using CryptoPP::LSH::LSH512_IV512;
106using CryptoPP::LSH::LSH512_StepConstants;
110typedef word64 lsh_u64;
115struct LSH512_AVX2_Context
117 LSH512_AVX2_Context(word64* state, word64 algType, word64& remainingBitLength) :
118 cv_l(state+0), cv_r(state+8), sub_msgs(state+16),
119 last_block(reinterpret_cast<
byte*>(state+48)),
120 remain_databitlen(remainingBitLength),
121 alg_type(static_cast<lsh_type>(algType)) {}
127 lsh_u64& remain_databitlen;
131struct LSH512_AVX2_Internal
133 LSH512_AVX2_Internal(word64* state) :
134 submsg_e_l(state+16), submsg_e_r(state+24),
135 submsg_o_l(state+32), submsg_o_r(state+40) { }
159inline bool LSH_IS_LSH512(lsh_uint val) {
160 return (val & 0xf0000) == 0x10000;
163inline lsh_uint LSH_GET_SMALL_HASHBIT(lsh_uint val) {
167inline lsh_uint LSH_GET_HASHBYTE(lsh_uint val) {
171inline lsh_uint LSH_GET_HASHBIT(lsh_uint val) {
172 return (LSH_GET_HASHBYTE(val) << 3) - LSH_GET_SMALL_HASHBIT(val);
175inline lsh_u64 loadLE64(lsh_u64 v) {
179lsh_u64 ROTL64(lsh_u64 x, lsh_u32 r) {
184inline void load_msg_blk(LSH512_AVX2_Internal* i_state,
const lsh_u8 msgblk[LSH512_MSG_BLK_BYTE_LEN])
186 lsh_u64* submsg_e_l = i_state->submsg_e_l;
187 lsh_u64* submsg_e_r = i_state->submsg_e_r;
188 lsh_u64* submsg_o_l = i_state->submsg_o_l;
189 lsh_u64* submsg_o_r = i_state->submsg_o_r;
191 _mm256_storeu_si256(M256_CAST(submsg_e_l+0),
192 _mm256_loadu_si256(CONST_M256_CAST(msgblk+0)));
193 _mm256_storeu_si256(M256_CAST(submsg_e_l+4),
194 _mm256_loadu_si256(CONST_M256_CAST(msgblk+32)));
196 _mm256_storeu_si256(M256_CAST(submsg_e_r+0),
197 _mm256_loadu_si256(CONST_M256_CAST(msgblk+64)));
198 _mm256_storeu_si256(M256_CAST(submsg_e_r+4),
199 _mm256_loadu_si256(CONST_M256_CAST(msgblk+96)));
201 _mm256_storeu_si256(M256_CAST(submsg_o_l+0),
202 _mm256_loadu_si256(CONST_M256_CAST(msgblk+128)));
203 _mm256_storeu_si256(M256_CAST(submsg_o_l+4),
204 _mm256_loadu_si256(CONST_M256_CAST(msgblk+160)));
206 _mm256_storeu_si256(M256_CAST(submsg_o_r+0),
207 _mm256_loadu_si256(CONST_M256_CAST(msgblk+192)));
208 _mm256_storeu_si256(M256_CAST(submsg_o_r+4),
209 _mm256_loadu_si256(CONST_M256_CAST(msgblk+224)));
212inline void msg_exp_even(LSH512_AVX2_Internal* i_state)
214 CRYPTOPP_ASSERT(i_state != NULLPTR);
216 lsh_u64* submsg_e_l = i_state->submsg_e_l;
217 lsh_u64* submsg_e_r = i_state->submsg_e_r;
218 lsh_u64* submsg_o_l = i_state->submsg_o_l;
219 lsh_u64* submsg_o_r = i_state->submsg_o_r;
221 _mm256_storeu_si256(M256_CAST(submsg_e_l+0), _mm256_add_epi64(
222 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_l+0)),
223 _mm256_permute4x64_epi64(
224 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_l+0)),
225 _MM_SHUFFLE(1,0,2,3))));
226 _mm256_storeu_si256(M256_CAST(submsg_e_l+4), _mm256_add_epi64(
227 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_l+4)),
228 _mm256_permute4x64_epi64(
229 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_l+4)),
230 _MM_SHUFFLE(2,1,0,3))));
232 _mm256_storeu_si256(M256_CAST(submsg_e_r+0), _mm256_add_epi64(
233 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_r+0)),
234 _mm256_permute4x64_epi64(
235 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_r+0)),
236 _MM_SHUFFLE(1,0,2,3))));
237 _mm256_storeu_si256(M256_CAST(submsg_e_r+4), _mm256_add_epi64(
238 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_r+4)),
239 _mm256_permute4x64_epi64(
240 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_r+4)),
241 _MM_SHUFFLE(2,1,0,3))));
244inline void msg_exp_odd(LSH512_AVX2_Internal* i_state)
246 CRYPTOPP_ASSERT(i_state != NULLPTR);
248 lsh_u64* submsg_e_l = i_state->submsg_e_l;
249 lsh_u64* submsg_e_r = i_state->submsg_e_r;
250 lsh_u64* submsg_o_l = i_state->submsg_o_l;
251 lsh_u64* submsg_o_r = i_state->submsg_o_r;
253 _mm256_storeu_si256(M256_CAST(submsg_o_l+0),
255 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_l+0)),
256 _mm256_permute4x64_epi64(
257 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_l+0)),
258 _MM_SHUFFLE(1,0,2,3))));
259 _mm256_storeu_si256(M256_CAST(submsg_o_l+4),
261 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_l+4)),
262 _mm256_permute4x64_epi64(
263 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_l+4)),
264 _MM_SHUFFLE(2,1,0,3))));
266 _mm256_storeu_si256(M256_CAST(submsg_o_r+0),
268 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_r+0)),
269 _mm256_permute4x64_epi64(
270 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_r+0)),
271 _MM_SHUFFLE(1,0,2,3))));
272 _mm256_storeu_si256(M256_CAST(submsg_o_r+4),
274 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_r+4)),
275 _mm256_permute4x64_epi64(
276 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_r+4)),
277 _MM_SHUFFLE(2,1,0,3))));
280inline void load_sc(
const lsh_u64** p_const_v,
size_t i)
282 *p_const_v = &LSH512_StepConstants[i];
285inline void msg_add_even(lsh_u64 cv_l[8], lsh_u64 cv_r[8], LSH512_AVX2_Internal* i_state)
287 CRYPTOPP_ASSERT(i_state != NULLPTR);
289 lsh_u64* submsg_e_l = i_state->submsg_e_l;
290 lsh_u64* submsg_e_r = i_state->submsg_e_r;
292 _mm256_storeu_si256(M256_CAST(cv_l), _mm256_xor_si256(
293 _mm256_loadu_si256(CONST_M256_CAST(cv_l)),
294 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_l))));
295 _mm256_storeu_si256(M256_CAST(cv_r), _mm256_xor_si256(
296 _mm256_loadu_si256(CONST_M256_CAST(cv_r)),
297 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_r))));
299 _mm256_storeu_si256(M256_CAST(cv_l+4), _mm256_xor_si256(
300 _mm256_loadu_si256(CONST_M256_CAST(cv_l+4)),
301 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_l+4))));
302 _mm256_storeu_si256(M256_CAST(cv_r+4), _mm256_xor_si256(
303 _mm256_loadu_si256(CONST_M256_CAST(cv_r+4)),
304 _mm256_loadu_si256(CONST_M256_CAST(submsg_e_r+4))));
307inline void msg_add_odd(lsh_u64 cv_l[8], lsh_u64 cv_r[8], LSH512_AVX2_Internal* i_state)
309 CRYPTOPP_ASSERT(i_state != NULLPTR);
311 lsh_u64* submsg_o_l = i_state->submsg_o_l;
312 lsh_u64* submsg_o_r = i_state->submsg_o_r;
314 _mm256_storeu_si256(M256_CAST(cv_l), _mm256_xor_si256(
315 _mm256_loadu_si256(CONST_M256_CAST(cv_l)),
316 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_l))));
317 _mm256_storeu_si256(M256_CAST(cv_r), _mm256_xor_si256(
318 _mm256_loadu_si256(CONST_M256_CAST(cv_r)),
319 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_r))));
321 _mm256_storeu_si256(M256_CAST(cv_l+4), _mm256_xor_si256(
322 _mm256_loadu_si256(CONST_M256_CAST(cv_l+4)),
323 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_l+4))));
324 _mm256_storeu_si256(M256_CAST(cv_r+4), _mm256_xor_si256(
325 _mm256_loadu_si256(CONST_M256_CAST(cv_r+4)),
326 _mm256_loadu_si256(CONST_M256_CAST(submsg_o_r+4))));
329inline void add_blk(lsh_u64 cv_l[8], lsh_u64 cv_r[8])
331 _mm256_storeu_si256(M256_CAST(cv_l), _mm256_add_epi64(
332 _mm256_loadu_si256(CONST_M256_CAST(cv_l)),
333 _mm256_loadu_si256(CONST_M256_CAST(cv_r))));
334 _mm256_storeu_si256(M256_CAST(cv_l+4), _mm256_add_epi64(
335 _mm256_loadu_si256(CONST_M256_CAST(cv_l+4)),
336 _mm256_loadu_si256(CONST_M256_CAST(cv_r+4))));
339template <
unsigned int R>
340inline void rotate_blk(lsh_u64 cv[8])
342 _mm256_storeu_si256(M256_CAST(cv), _mm256_or_si256(
343 _mm256_slli_epi64(_mm256_loadu_si256(CONST_M256_CAST(cv)), R),
344 _mm256_srli_epi64(_mm256_loadu_si256(CONST_M256_CAST(cv)), 64-R)));
345 _mm256_storeu_si256(M256_CAST(cv+4), _mm256_or_si256(
346 _mm256_slli_epi64(_mm256_loadu_si256(CONST_M256_CAST(cv+4)), R),
347 _mm256_srli_epi64(_mm256_loadu_si256(CONST_M256_CAST(cv+4)), 64-R)));
350inline void xor_with_const(lsh_u64 cv_l[8],
const lsh_u64 const_v[8])
352 _mm256_storeu_si256(M256_CAST(cv_l), _mm256_xor_si256(
353 _mm256_loadu_si256(CONST_M256_CAST(cv_l)),
354 _mm256_loadu_si256(CONST_M256_CAST(const_v))));
355 _mm256_storeu_si256(M256_CAST(cv_l+4), _mm256_xor_si256(
356 _mm256_loadu_si256(CONST_M256_CAST(cv_l+4)),
357 _mm256_loadu_si256(CONST_M256_CAST(const_v+4))));
360inline void rotate_msg_gamma(lsh_u64 cv_r[8])
363 _mm256_storeu_si256(M256_CAST(cv_r+0),
365 _mm256_loadu_si256(CONST_M256_CAST(cv_r+0)),
367 9,8,15,14, 13,12,11,10, 3,2,1,0, 7,6,5,4,
368 13,12,11,10, 9,8,15,14, 7,6,5,4, 3,2,1,0)));
369 _mm256_storeu_si256(M256_CAST(cv_r+4),
371 _mm256_loadu_si256(CONST_M256_CAST(cv_r+4)),
373 8,15,14,13, 12,11,10,9, 2,1,0,7, 6,5,4,3,
374 12,11,10,9, 8,15,14,13, 6,5,4,3, 2,1,0,7)));
377inline void word_perm(lsh_u64 cv_l[8], lsh_u64 cv_r[8])
380 _mm256_storeu_si256(M256_CAST(cv_l+0), _mm256_permute4x64_epi64(
381 _mm256_loadu_si256(CONST_M256_CAST(cv_l+0)), _MM_SHUFFLE(3,1,0,2)));
382 _mm256_storeu_si256(M256_CAST(cv_l+4), _mm256_permute4x64_epi64(
383 _mm256_loadu_si256(CONST_M256_CAST(cv_l+4)), _MM_SHUFFLE(3,1,0,2)));
384 _mm256_storeu_si256(M256_CAST(cv_r+0), _mm256_permute4x64_epi64(
385 _mm256_loadu_si256(CONST_M256_CAST(cv_r+0)), _MM_SHUFFLE(1,2,3,0)));
386 _mm256_storeu_si256(M256_CAST(cv_r+4), _mm256_permute4x64_epi64(
387 _mm256_loadu_si256(CONST_M256_CAST(cv_r+4)), _MM_SHUFFLE(1,2,3,0)));
389 temp[0] = _mm256_loadu_si256(CONST_M256_CAST(cv_l+0));
390 temp[1] = _mm256_loadu_si256(CONST_M256_CAST(cv_r+0));
392 _mm256_storeu_si256(M256_CAST(cv_l+0),
393 _mm256_loadu_si256(CONST_M256_CAST(cv_l+4)));
394 _mm256_storeu_si256(M256_CAST(cv_l+4),
395 _mm256_loadu_si256(CONST_M256_CAST(cv_r+4)));
397 _mm256_storeu_si256(M256_CAST(cv_r+0), temp[0]);
398 _mm256_storeu_si256(M256_CAST(cv_r+4), temp[1]);
405template <
unsigned int Alpha,
unsigned int Beta>
406inline void mix(lsh_u64 cv_l[8], lsh_u64 cv_r[8],
const lsh_u64 const_v[8])
409 rotate_blk<Alpha>(cv_l);
410 xor_with_const(cv_l, const_v);
412 rotate_blk<Beta>(cv_r);
414 rotate_msg_gamma(cv_r);
421inline void compress(LSH512_AVX2_Context* ctx,
const lsh_u8 pdMsgBlk[LSH512_MSG_BLK_BYTE_LEN])
423 CRYPTOPP_ASSERT(ctx != NULLPTR);
425 LSH512_AVX2_Internal s_state(ctx->cv_l);
426 LSH512_AVX2_Internal* i_state = &s_state;
428 const lsh_u64* const_v = NULL;
429 lsh_u64 *cv_l = ctx->cv_l;
430 lsh_u64 *cv_r = ctx->cv_r;
432 load_msg_blk(i_state, pdMsgBlk);
434 msg_add_even(cv_l, cv_r, i_state);
435 load_sc(&const_v, 0);
436 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
437 word_perm(cv_l, cv_r);
439 msg_add_odd(cv_l, cv_r, i_state);
440 load_sc(&const_v, 8);
441 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
442 word_perm(cv_l, cv_r);
444 for (
size_t i = 1; i < NUM_STEPS / 2; i++)
446 msg_exp_even(i_state);
447 msg_add_even(cv_l, cv_r, i_state);
448 load_sc(&const_v, 16 * i);
449 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
450 word_perm(cv_l, cv_r);
452 msg_exp_odd(i_state);
453 msg_add_odd(cv_l, cv_r, i_state);
454 load_sc(&const_v, 16 * i + 8);
455 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
456 word_perm(cv_l, cv_r);
459 msg_exp_even(i_state);
460 msg_add_even(cv_l, cv_r, i_state);
465inline void load_iv(word64 cv_l[8], word64 cv_r[8],
const word64 iv[16])
468 _mm256_storeu_si256(M256_CAST(cv_l+0),
469 _mm256_load_si256(CONST_M256_CAST(iv+0)));
470 _mm256_storeu_si256(M256_CAST(cv_l+4),
471 _mm256_load_si256(CONST_M256_CAST(iv+4)));
473 _mm256_storeu_si256(M256_CAST(cv_r+0),
474 _mm256_load_si256(CONST_M256_CAST(iv+8)));
475 _mm256_storeu_si256(M256_CAST(cv_r+4),
476 _mm256_load_si256(CONST_M256_CAST(iv+12)));
479inline void zero_iv(lsh_u64 cv_l[8], lsh_u64 cv_r[8])
481 _mm256_storeu_si256(M256_CAST(cv_l+0), _mm256_setzero_si256());
482 _mm256_storeu_si256(M256_CAST(cv_l+4), _mm256_setzero_si256());
483 _mm256_storeu_si256(M256_CAST(cv_r+0), _mm256_setzero_si256());
484 _mm256_storeu_si256(M256_CAST(cv_r+4), _mm256_setzero_si256());
487inline void zero_submsgs(LSH512_AVX2_Context* ctx)
489 lsh_u64* sub_msgs = ctx->sub_msgs;
491 _mm256_storeu_si256(M256_CAST(sub_msgs+ 0),
492 _mm256_setzero_si256());
493 _mm256_storeu_si256(M256_CAST(sub_msgs+ 4),
494 _mm256_setzero_si256());
496 _mm256_storeu_si256(M256_CAST(sub_msgs+ 8),
497 _mm256_setzero_si256());
498 _mm256_storeu_si256(M256_CAST(sub_msgs+12),
499 _mm256_setzero_si256());
502inline void init224(LSH512_AVX2_Context* ctx)
504 CRYPTOPP_ASSERT(ctx != NULLPTR);
507 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV224);
510inline void init256(LSH512_AVX2_Context* ctx)
512 CRYPTOPP_ASSERT(ctx != NULLPTR);
515 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV256);
518inline void init384(LSH512_AVX2_Context* ctx)
520 CRYPTOPP_ASSERT(ctx != NULLPTR);
523 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV384);
526inline void init512(LSH512_AVX2_Context* ctx)
528 CRYPTOPP_ASSERT(ctx != NULLPTR);
531 load_iv(ctx->cv_l, ctx->cv_r, LSH512_IV512);
536inline void fin(LSH512_AVX2_Context* ctx)
538 CRYPTOPP_ASSERT(ctx != NULLPTR);
540 _mm256_storeu_si256(M256_CAST(ctx->cv_l+0), _mm256_xor_si256(
541 _mm256_loadu_si256(CONST_M256_CAST(ctx->cv_l+0)),
542 _mm256_loadu_si256(CONST_M256_CAST(ctx->cv_r+0))));
544 _mm256_storeu_si256(M256_CAST(ctx->cv_l+4), _mm256_xor_si256(
545 _mm256_loadu_si256(CONST_M256_CAST(ctx->cv_l+4)),
546 _mm256_loadu_si256(CONST_M256_CAST(ctx->cv_r+4))));
551inline void get_hash(LSH512_AVX2_Context* ctx, lsh_u8* pbHashVal)
553 CRYPTOPP_ASSERT(ctx != NULLPTR);
554 CRYPTOPP_ASSERT(ctx->alg_type != 0);
555 CRYPTOPP_ASSERT(pbHashVal != NULLPTR);
557 lsh_uint alg_type = ctx->alg_type;
558 lsh_uint hash_val_byte_len = LSH_GET_HASHBYTE(alg_type);
559 lsh_uint hash_val_bit_len = LSH_GET_SMALL_HASHBIT(alg_type);
562 memcpy(pbHashVal, ctx->cv_l, hash_val_byte_len);
563 if (hash_val_bit_len){
564 pbHashVal[hash_val_byte_len-1] &= (((lsh_u8)0xff) << hash_val_bit_len);
570lsh_err lsh512_init_avx2(LSH512_AVX2_Context* ctx)
572 CRYPTOPP_ASSERT(ctx != NULLPTR);
573 CRYPTOPP_ASSERT(ctx->alg_type != 0);
575 lsh_u32 alg_type = ctx->alg_type;
576 const lsh_u64* const_v = NULL;
577 ctx->remain_databitlen = 0;
583 case LSH_TYPE_512_512:
586 case LSH_TYPE_512_384:
589 case LSH_TYPE_512_256:
592 case LSH_TYPE_512_224:
599 lsh_u64* cv_l = ctx->cv_l;
600 lsh_u64* cv_r = ctx->cv_r;
603 cv_l[0] = LSH512_HASH_VAL_MAX_BYTE_LEN;
604 cv_l[1] = LSH_GET_HASHBIT(alg_type);
606 for (
size_t i = 0; i < NUM_STEPS / 2; i++)
609 load_sc(&const_v, i * 16);
610 mix<ROT_EVEN_ALPHA, ROT_EVEN_BETA>(cv_l, cv_r, const_v);
611 word_perm(cv_l, cv_r);
613 load_sc(&const_v, i * 16 + 8);
614 mix<ROT_ODD_ALPHA, ROT_ODD_BETA>(cv_l, cv_r, const_v);
615 word_perm(cv_l, cv_r);
621lsh_err lsh512_update_avx2(LSH512_AVX2_Context* ctx,
const lsh_u8* data,
size_t databitlen)
623 CRYPTOPP_ASSERT(ctx != NULLPTR);
624 CRYPTOPP_ASSERT(data != NULLPTR);
625 CRYPTOPP_ASSERT(databitlen % 8 == 0);
626 CRYPTOPP_ASSERT(ctx->alg_type != 0);
631 if (databitlen == 0){
636 size_t databytelen = databitlen >> 3;
638 const size_t pos2 = 0;
640 size_t remain_msg_byte =
static_cast<size_t>(ctx->remain_databitlen >> 3);
642 const size_t remain_msg_bit = 0;
644 if (remain_msg_byte >= LSH512_MSG_BLK_BYTE_LEN){
645 return LSH_ERR_INVALID_STATE;
647 if (remain_msg_bit > 0){
648 return LSH_ERR_INVALID_DATABITLEN;
651 if (databytelen + remain_msg_byte < LSH512_MSG_BLK_BYTE_LEN){
652 memcpy(ctx->last_block + remain_msg_byte, data, databytelen);
653 ctx->remain_databitlen += (lsh_uint)databitlen;
654 remain_msg_byte += (lsh_uint)databytelen;
656 ctx->last_block[remain_msg_byte] = data[databytelen] & ((0xff >> pos2) ^ 0xff);
661 if (remain_msg_byte > 0){
662 size_t more_byte = LSH512_MSG_BLK_BYTE_LEN - remain_msg_byte;
663 memcpy(ctx->last_block + remain_msg_byte, data, more_byte);
664 compress(ctx, ctx->last_block);
666 databytelen -= more_byte;
668 ctx->remain_databitlen = 0;
671 while (databytelen >= LSH512_MSG_BLK_BYTE_LEN)
677 data += LSH512_MSG_BLK_BYTE_LEN;
678 databytelen -= LSH512_MSG_BLK_BYTE_LEN;
681 if (databytelen > 0){
682 memcpy(ctx->last_block, data, databytelen);
683 ctx->remain_databitlen = (lsh_uint)(databytelen << 3);
687 ctx->last_block[databytelen] = data[databytelen] & ((0xff >> pos2) ^ 0xff);
688 ctx->remain_databitlen += pos2;
693lsh_err lsh512_final_avx2(LSH512_AVX2_Context* ctx, lsh_u8* hashval)
695 CRYPTOPP_ASSERT(ctx != NULLPTR);
696 CRYPTOPP_ASSERT(hashval != NULLPTR);
702 size_t remain_msg_byte =
static_cast<size_t>(ctx->remain_databitlen >> 3);
704 const size_t remain_msg_bit = 0;
706 if (remain_msg_byte >= LSH512_MSG_BLK_BYTE_LEN){
707 return LSH_ERR_INVALID_STATE;
711 ctx->last_block[remain_msg_byte] |= (0x1 << (7 - remain_msg_bit));
714 ctx->last_block[remain_msg_byte] = 0x80;
716 memset(ctx->last_block + remain_msg_byte + 1, 0, LSH512_MSG_BLK_BYTE_LEN - remain_msg_byte - 1);
718 compress(ctx, ctx->last_block);
721 get_hash(ctx, hashval);
726ANONYMOUS_NAMESPACE_END
728NAMESPACE_BEGIN(CryptoPP)
731void LSH512_Base_Restart_AVX2(word64* state)
733 state[RemainingBits] = 0;
734 LSH512_AVX2_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
735 lsh_err err = lsh512_init_avx2(&ctx);
737 if (err != LSH_SUCCESS)
742void LSH512_Base_Update_AVX2(word64* state,
const byte *input,
size_t size)
744 LSH512_AVX2_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
745 lsh_err err = lsh512_update_avx2(&ctx, input, 8*size);
747 if (err != LSH_SUCCESS)
752void LSH512_Base_TruncatedFinal_AVX2(word64* state,
byte *hash,
size_t)
754 LSH512_AVX2_Context ctx(state, state[AlgorithmType], state[RemainingBits]);
755 lsh_err err = lsh512_final_avx2(&ctx, hash);
757 if (err != LSH_SUCCESS)
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Library configuration file.
unsigned char byte
8-bit unsigned datatype
unsigned int word32
32-bit unsigned datatype
Classes for the LSH hash functions.
Utility functions for the Crypto++ library.
T ConditionalByteReverse(ByteOrder order, T value)
Reverses bytes in a value depending upon endianness.
T rotlFixed(T x, unsigned int y)
Performs a left rotate.