From 2842f81b8017170433b11ff3241fd90407688f4b Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sun, 10 Feb 2019 09:34:09 +0100 Subject: [PATCH 1/3] Fixed an old comment --- src/crypto/variant4_random_math.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/variant4_random_math.h b/src/crypto/variant4_random_math.h index 8724c58c9..f28bdbd62 100644 --- a/src/crypto/variant4_random_math.h +++ b/src/crypto/variant4_random_math.h @@ -24,7 +24,7 @@ enum V4_Settings enum V4_InstructionList { MUL, // a*b - ADD, // a+b + C, -128 <= C <= 127 + ADD, // a+b + C, C is an unsigned 32-bit constant SUB, // a-b ROR, // rotate right "a" by "b & 31" bits ROL, // rotate left "a" by "b & 31" bits From 46f3d3e9758fc552439f1d44fb464e968b096ee8 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sun, 10 Feb 2019 09:43:58 +0100 Subject: [PATCH 2/3] Made inst_data unsigned --- src/crypto/variant4_random_math.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/variant4_random_math.h b/src/crypto/variant4_random_math.h index f28bdbd62..2c69463fe 100644 --- a/src/crypto/variant4_random_math.h +++ b/src/crypto/variant4_random_math.h @@ -215,7 +215,7 @@ static inline int v4_random_math_init(struct V4_Instruction* code, const uint64_ // // Registers R4-R7 are constant and are treated as having the same value because when we do // the same operation twice with two constant source registers, it can be optimized into a single operation - int inst_data[8] = { 0, 1, 2, 3, -1, -1, -1, -1 }; + uint32_t inst_data[8] = { 0, 1, 2, 3, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF }; bool alu_busy[TOTAL_LATENCY + 1][ALU_COUNT]; bool is_rotation[V4_INSTRUCTION_COUNT]; From 33743eb7ac5eee6e4a8a3694198445e473f7b320 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Sun, 10 Feb 2019 10:04:59 +0100 Subject: [PATCH 3/3] Made sure code generator loop always terminates It never did more than 176 iterations for first 10,000,000 heights, so I set fail-safe at 256 iterations. --- src/crypto/variant4_random_math.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/crypto/variant4_random_math.h b/src/crypto/variant4_random_math.h index 2c69463fe..2c190287b 100644 --- a/src/crypto/variant4_random_math.h +++ b/src/crypto/variant4_random_math.h @@ -233,10 +233,17 @@ static inline int v4_random_math_init(struct V4_Instruction* code, const uint64_ int num_retries = 0; code_size = 0; + int total_iterations = 0; + // Generate random code to achieve minimal required latency for our abstract CPU // Try to get this latency for all 4 registers while (((latency[0] < TOTAL_LATENCY) || (latency[1] < TOTAL_LATENCY) || (latency[2] < TOTAL_LATENCY) || (latency[3] < TOTAL_LATENCY)) && (num_retries < 64)) { + // Fail-safe to guarantee loop termination + ++total_iterations; + if (total_iterations > 256) + break; + check_data(&data_index, 1, data, sizeof(data)); const uint8_t c = ((uint8_t*)data)[data_index++];