Skip to content

Commit

Permalink
implement modification in argon2d fill_block routine and in its count…
Browse files Browse the repository at this point in the history
…erpart in mtp

implement modification in argon2d fill_block according MTP paper.
Reference to index and 256 first bits of the blockheader
  • Loading branch information
djm34 committed Feb 19, 2018
1 parent 9257a6b commit 40ea9b8
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
5 changes: 4 additions & 1 deletion argon2ref/argon2.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ int argon2_ctx_from_mtp(argon2_context *context, argon2_instance_t *instance) {
instance->block_header[1] = ((uint32_t*)context->pwd)[1];
instance->block_header[2] = ((uint32_t*)context->pwd)[2];
instance->block_header[3] = ((uint32_t*)context->pwd)[3];

instance->block_header[4] = ((uint32_t*)context->pwd)[4];
instance->block_header[5] = ((uint32_t*)context->pwd)[5];
instance->block_header[6] = ((uint32_t*)context->pwd)[6];
instance->block_header[7] = ((uint32_t*)context->pwd)[7];
// printf("3. Initializatio n: Hashing inputs, allocating memory, filling first blocks\n");
/* 3. Initialization: Hashing inputs, allocating memory, filling first blocks */
result = initialize(instance, context);
Expand Down
2 changes: 1 addition & 1 deletion argon2ref/argon2.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ typedef struct Argon2_instance_t {
argon2_type type;
int print_internals; /* whether to print the memory blocks */
argon2_context *context_ptr; /* points back to original context */
uint32_t block_header[4];
uint32_t block_header[8]; // takes 256 first bits of the blockheader
} argon2_instance_t;


Expand Down
66 changes: 63 additions & 3 deletions argon2ref/ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,60 @@ static void fill_block(const block *prev_block, const block *ref_block,
xor_block(next_block, &blockR);
}

static void fill_block_withIndex(const block *prev_block, const block *ref_block,
block *next_block, int with_xor, uint32_t block_header[8], uint64_t index) {
block blockR, block_tmp;
uint64_t TheIndex[2] = {0,index};
unsigned i;

copy_block(&blockR, ref_block);

xor_block(&blockR, prev_block);

copy_block(&block_tmp, &blockR);
/* Now blockR = ref_block + prev_block and block_tmp = ref_block + prev_block */
if (with_xor) {
/* Saving the next block contents for XOR over: */
xor_block(&block_tmp, next_block);
/* Now blockR = ref_block + prev_block and
block_tmp = ref_block + prev_block + next_block */
}
// blockR.v[16] = ((uint64_t*)block_header)[0];
// blockR.v[17] = ((uint64_t*)block_header)[1];
memcpy(&blockR.v[14], TheIndex, 2 * sizeof(uint64_t)); //index here
memcpy(&blockR.v[16], (uint64_t*)block_header, 2 * sizeof(uint64_t));
memcpy(&blockR.v[18], (uint64_t*)(block_header + 4), 2 * sizeof(uint64_t));
//printf("block header in cpu %llx %llx %llx %llx\n", blockR.v[15], blockR.v[16], blockR.v[17], blockR.v[18]);

/* Apply Blake2 on columns of 64-bit words: (0,1,...,15) , then
(16,17,..31)... finally (112,113,...127) */
for (i = 0; i < 8; ++i) {
BLAKE2_ROUND_NOMSG(
blockR.v[16 * i], blockR.v[16 * i + 1], blockR.v[16 * i + 2],
blockR.v[16 * i + 3], blockR.v[16 * i + 4], blockR.v[16 * i + 5],
blockR.v[16 * i + 6], blockR.v[16 * i + 7], blockR.v[16 * i + 8],
blockR.v[16 * i + 9], blockR.v[16 * i + 10], blockR.v[16 * i + 11],
blockR.v[16 * i + 12], blockR.v[16 * i + 13], blockR.v[16 * i + 14],
blockR.v[16 * i + 15]);
}

/* Apply Blake2 on rows of 64-bit words: (0,1,16,17,...112,113), then
(2,3,18,19,...,114,115).. finally (14,15,30,31,...,126,127) */
for (i = 0; i < 8; i++) {
BLAKE2_ROUND_NOMSG(
blockR.v[2 * i], blockR.v[2 * i + 1], blockR.v[2 * i + 16],
blockR.v[2 * i + 17], blockR.v[2 * i + 32], blockR.v[2 * i + 33],
blockR.v[2 * i + 48], blockR.v[2 * i + 49], blockR.v[2 * i + 64],
blockR.v[2 * i + 65], blockR.v[2 * i + 80], blockR.v[2 * i + 81],
blockR.v[2 * i + 96], blockR.v[2 * i + 97], blockR.v[2 * i + 112],
blockR.v[2 * i + 113]);
}

copy_block(next_block, &block_tmp);
xor_block(next_block, &blockR);
}


static void next_addresses(block *address_block, block *input_block,
const block *zero_block, uint32_t block_header[4]) {
input_block->v[6]++;
Expand Down Expand Up @@ -192,25 +246,31 @@ truc++;
/* 2 Creating a new block */
ref_block =
instance->memory + instance->lane_length * ref_lane + ref_index;

curr_block = instance->memory + curr_offset;
uint64_t TheBlockIndex = instance->lane_length * ref_lane + ref_index;
/*
zRefBlock = &instance->TheRefBlock + curr_offset;
zPrevBlock = &instance->ThePrevBlock + curr_offset;
*/
if (ARGON2_VERSION_10 == instance->version) {
/* version 1.2.1 and earlier: overwrite, not XOR */
fill_block(instance->memory + prev_offset, ref_block, curr_block, 0, instance->block_header);

// fill_block(instance->memory + prev_offset, ref_block, curr_block, 0, instance->block_header);
fill_block_withIndex(instance->memory + prev_offset, ref_block, curr_block, 0, instance->block_header, TheBlockIndex);
} else {
if(0 == position.pass) {
fill_block(instance->memory + prev_offset, ref_block,curr_block, 0, instance->block_header);
// fill_block(instance->memory + prev_offset, ref_block,curr_block, 0, instance->block_header);
fill_block_withIndex(instance->memory + prev_offset, ref_block, curr_block, 0, instance->block_header, TheBlockIndex);
curr_block->ref_block = instance->lane_length * ref_lane + ref_index;
curr_block->prev_block = prev_offset | (instance->lane_length * ref_lane + ref_index) << 32;;
// uint64_t zHistory = (prev_offset) | (instance->lane_length * ref_lane + ref_index) << 32;
// curr_block->BlokHistory = zHistory;
} else {


fill_block(instance->memory + prev_offset, ref_block,curr_block, 1, instance->block_header);
// fill_block(instance->memory + prev_offset, ref_block,curr_block, 1, instance->block_header);
fill_block_withIndex(instance->memory + prev_offset, ref_block, curr_block, 1, instance->block_header, TheBlockIndex);
}
}
}
Expand Down
41 changes: 40 additions & 1 deletion merkletree/mtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,45 @@ void fill_block2(__m128i *state, const block *ref_block, block *next_block, int
}
}

void fill_block2_withIndex(__m128i *state, const block *ref_block, block *next_block, int with_xor, uint32_t block_header[8], uint64_t blockIndex) {
__m128i block_XY[ARGON2_OWORDS_IN_BLOCK];
unsigned int i;
uint64_t TheIndex[2]={0,blockIndex};
if (with_xor) {
for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) {
state[i] = _mm_xor_si128(
state[i], _mm_loadu_si128((const __m128i *)ref_block->v + i));
block_XY[i] = _mm_xor_si128(
state[i], _mm_loadu_si128((const __m128i *)next_block->v + i));
}
}
else {
for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) {
block_XY[i] = state[i] = _mm_xor_si128(
state[i], _mm_loadu_si128((const __m128i *)ref_block->v + i));
}
}
memcpy(&state[7], TheIndex, sizeof(__m128i));
memcpy(&state[8], block_header, sizeof(__m128i));
memcpy(&state[9], block_header + 4, sizeof(__m128i));
for (i = 0; i < 8; ++i) {
BLAKE2_ROUND(state[8 * i + 0], state[8 * i + 1], state[8 * i + 2],
state[8 * i + 3], state[8 * i + 4], state[8 * i + 5],
state[8 * i + 6], state[8 * i + 7]);
}

for (i = 0; i < 8; ++i) {
BLAKE2_ROUND(state[8 * 0 + i], state[8 * 1 + i], state[8 * 2 + i],
state[8 * 3 + i], state[8 * 4 + i], state[8 * 5 + i],
state[8 * 6 + i], state[8 * 7 + i]);
}

for (i = 0; i < ARGON2_OWORDS_IN_BLOCK; i++) {
state[i] = _mm_xor_si128(state[i], block_XY[i]);
_mm_storeu_si128((__m128i *)next_block->v + i, state[i]);
}
}



void copy_block(block *dst, const block *src) {
Expand Down Expand Up @@ -294,7 +333,7 @@ int mtp_solver_withblock(uint32_t TheNonce, argon2_instance_t *instance, unsigne
__m128i state_test[64];
memset(state_test, 0, sizeof(state_test));
memcpy(state_test, &instance->memory[instance->memory[ij].prev_block & 0xffffffff].v, ARGON2_BLOCK_SIZE);
fill_block2(state_test, &instance->memory[instance->memory[ij].ref_block], &X_IJ, 0,instance->block_header);
fill_block2_withIndex(state_test, &instance->memory[instance->memory[ij].ref_block], &X_IJ, 0,instance->block_header, instance->memory[ij].ref_block);
X_IJ.prev_block = instance->memory[ij].prev_block;
X_IJ.ref_block = instance->memory[ij].ref_block;

Expand Down

0 comments on commit 40ea9b8

Please sign in to comment.