Path: blob/linux/scryptjane/scrypt-jane-romix-template.h
1201 views
#if !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_HAVE_ROMIX)12#if defined(SCRYPT_CHOOSE_COMPILETIME)3#undef SCRYPT_ROMIX_FN4#define SCRYPT_ROMIX_FN scrypt_ROMix5#endif67#undef SCRYPT_HAVE_ROMIX8#define SCRYPT_HAVE_ROMIX910#if !defined(SCRYPT_CHUNKMIX_FN)1112#define SCRYPT_CHUNKMIX_FN scrypt_ChunkMix_basic1314/*15Bout = ChunkMix(Bin)16172*r: number of blocks in the chunk18*/19static void /* asm_calling_convention */20SCRYPT_CHUNKMIX_FN(scrypt_mix_word_t *Bout/*[chunkWords]*/, scrypt_mix_word_t *Bin/*[chunkWords]*/, scrypt_mix_word_t *Bxor/*[chunkWords]*/, uint32_t r) {21scrypt_mix_word_t MM16 X[SCRYPT_BLOCK_WORDS], *block;22uint32_t i, j, blocksPerChunk = r * 2, half = 0;2324/* 1: X = B_{2r - 1} */25block = scrypt_block(Bin, blocksPerChunk - 1);26for (i = 0; i < SCRYPT_BLOCK_WORDS; i++)27X[i] = block[i];2829if (Bxor) {30block = scrypt_block(Bxor, blocksPerChunk - 1);31for (i = 0; i < SCRYPT_BLOCK_WORDS; i++)32X[i] ^= block[i];33}3435/* 2: for i = 0 to 2r - 1 do */36for (i = 0; i < blocksPerChunk; i++, half ^= r) {37/* 3: X = H(X ^ B_i) */38block = scrypt_block(Bin, i);39for (j = 0; j < SCRYPT_BLOCK_WORDS; j++)40X[j] ^= block[j];4142if (Bxor) {43block = scrypt_block(Bxor, i);44for (j = 0; j < SCRYPT_BLOCK_WORDS; j++)45X[j] ^= block[j];46}47SCRYPT_MIX_FN(X);4849/* 4: Y_i = X */50/* 6: B'[0..r-1] = Y_even */51/* 6: B'[r..2r-1] = Y_odd */52block = scrypt_block(Bout, (i / 2) + half);53for (j = 0; j < SCRYPT_BLOCK_WORDS; j++)54block[j] = X[j];55}56}57#endif5859/*60X = ROMix(X)6162X: chunk to mix63Y: scratch chunk64N: number of rounds65V[N]: array of chunks to randomly index in to662*r: number of blocks in a chunk67*/6869static void NOINLINE FASTCALL70SCRYPT_ROMIX_FN(scrypt_mix_word_t *X/*[chunkWords]*/, scrypt_mix_word_t *Y/*[chunkWords]*/, scrypt_mix_word_t *V/*[N * chunkWords]*/, uint32_t N, uint32_t r) {71uint32_t i, j, chunkWords = SCRYPT_BLOCK_WORDS * r * 2;72scrypt_mix_word_t *block = V;7374SCRYPT_ROMIX_TANGLE_FN(X, r * 2);7576/* 1: X = B */77/* implicit */7879/* 2: for i = 0 to N - 1 do */80memcpy(block, X, chunkWords * sizeof(scrypt_mix_word_t));81for (i = 0; i < N - 1; i++, block += chunkWords) {82/* 3: V_i = X */83/* 4: X = H(X) */84SCRYPT_CHUNKMIX_FN(block + chunkWords, block, NULL, r);85}86SCRYPT_CHUNKMIX_FN(X, block, NULL, r);8788/* 6: for i = 0 to N - 1 do */89for (i = 0; i < N; i += 2) {90/* 7: j = Integerify(X) % N */91j = X[chunkWords - SCRYPT_BLOCK_WORDS] & (N - 1);9293/* 8: X = H(Y ^ V_j) */94SCRYPT_CHUNKMIX_FN(Y, X, scrypt_item(V, j, chunkWords), r);9596/* 7: j = Integerify(Y) % N */97j = Y[chunkWords - SCRYPT_BLOCK_WORDS] & (N - 1);9899/* 8: X = H(Y ^ V_j) */100SCRYPT_CHUNKMIX_FN(X, Y, scrypt_item(V, j, chunkWords), r);101}102103/* 10: B' = X */104/* implicit */105106SCRYPT_ROMIX_UNTANGLE_FN(X, r * 2);107}108109/*110* Special version with hard-coded r = 1111* - mikaelh112*/113static void NOINLINE FASTCALL114scrypt_ROMix_1(scrypt_mix_word_t *X/*[chunkWords]*/, scrypt_mix_word_t *Y/*[chunkWords]*/, scrypt_mix_word_t *V/*[N * chunkWords]*/, uint32_t N) {115const uint32_t r = 1;116uint32_t i, j, chunkWords = SCRYPT_BLOCK_WORDS * r * 2;117scrypt_mix_word_t *block = V;118119SCRYPT_ROMIX_TANGLE_FN(X, r * 2);120121/* 1: X = B */122/* implicit */123124/* 2: for i = 0 to N - 1 do */125memcpy(block, X, chunkWords * sizeof(scrypt_mix_word_t));126for (i = 0; i < N - 1; i++, block += chunkWords) {127/* 3: V_i = X */128/* 4: X = H(X) */129#ifdef SCRYPT_CHUNKMIX_1_FN130SCRYPT_CHUNKMIX_1_FN(block + chunkWords, block);131#else132SCRYPT_CHUNKMIX_FN(block + chunkWords, block, NULL, r);133#endif134}135#ifdef SCRYPT_CHUNKMIX_1_FN136SCRYPT_CHUNKMIX_1_FN(X, block);137#else138SCRYPT_CHUNKMIX_FN(X, block, NULL, r);139#endif140141/* 6: for i = 0 to N - 1 do */142for (i = 0; i < N; i += 2) {143/* 7: j = Integerify(X) % N */144j = X[chunkWords - SCRYPT_BLOCK_WORDS] & (N - 1);145146/* 8: X = H(Y ^ V_j) */147#ifdef SCRYPT_CHUNKMIX_1_XOR_FN148SCRYPT_CHUNKMIX_1_XOR_FN(Y, X, scrypt_item(V, j, chunkWords));149#else150SCRYPT_CHUNKMIX_FN(Y, X, scrypt_item(V, j, chunkWords), r);151#endif152153/* 7: j = Integerify(Y) % N */154j = Y[chunkWords - SCRYPT_BLOCK_WORDS] & (N - 1);155156/* 8: X = H(Y ^ V_j) */157#ifdef SCRYPT_CHUNKMIX_1_XOR_FN158SCRYPT_CHUNKMIX_1_XOR_FN(X, Y, scrypt_item(V, j, chunkWords));159#else160SCRYPT_CHUNKMIX_FN(X, Y, scrypt_item(V, j, chunkWords), r);161#endif162}163164/* 10: B' = X */165/* implicit */166167SCRYPT_ROMIX_UNTANGLE_FN(X, r * 2);168}169170#endif /* !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_HAVE_ROMIX) */171172173#undef SCRYPT_CHUNKMIX_FN174#undef SCRYPT_ROMIX_FN175#undef SCRYPT_MIX_FN176#undef SCRYPT_ROMIX_TANGLE_FN177#undef SCRYPT_ROMIX_UNTANGLE_FN178179180181