From 6247f485b6be461d5036608b9e8cd2f5adc912f1 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 26 Dec 2021 22:50:09 -0500 Subject: [PATCH] Optimization: avoid unnecessary doublings in precomputation --- src/ecmult_gen_compute_table_impl.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ecmult_gen_compute_table_impl.h b/src/ecmult_gen_compute_table_impl.h index 5ad0b0b3..80c6af64 100644 --- a/src/ecmult_gen_compute_table_impl.h +++ b/src/ecmult_gen_compute_table_impl.h @@ -54,16 +54,18 @@ static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage* table, cons secp256k1_gej_set_infinity(&sum); for (tooth = 0; tooth < teeth; ++tooth) { /* Here u = 2^((block*teeth + tooth)*spacing) * gen/2. */ - int bit_off; /* Make sum = sum(2^((block*teeth + t)*spacing), t=0..tooth) * gen/2. */ secp256k1_gej_add_var(&sum, &sum, &u, NULL); /* Make u = 2^((block*teeth + tooth)*spacing + 1) * gen/2. */ secp256k1_gej_double_var(&u, &u, NULL); /* Make ds[tooth] = u = 2^((block*teeth + tooth)*spacing + 1) * gen/2. */ ds[tooth] = u; - /* Make u = 2^((block*teeth + tooth + 1)*spacing) * gen/2. */ - for (bit_off = 1; bit_off < spacing; ++bit_off) { - secp256k1_gej_double_var(&u, &u, NULL); + /* Make u = 2^((block*teeth + tooth + 1)*spacing) * gen/2, unless at the end. */ + if (block + tooth != blocks + teeth - 2) { + int bit_off; + for (bit_off = 1; bit_off < spacing; ++bit_off) { + secp256k1_gej_double_var(&u, &u, NULL); + } } } /* Now u = 2^((block*teeth + teeth)*spacing) * gen/2