@@ -62,7 +62,27 @@ static void secp256k1_ecmult_const_odd_multiples_table_globalz(secp256k1_ge *pre
6262 unsigned m = 0; \
6363 /* If the top bit of n is 0, we want the negation. */ \
6464 volatile unsigned negative = ((n ) >> (ECMULT_CONST_GROUP_SIZE - 1 )) ^ 1 ; \
65- /* The index is computed by looking at the bottom bits, after making positive. */ \
65+ /* Let n[i] be the i-th bit of n, then the index is
66+ * sum(cnot(n[i]) * 2^i, i=0..l-2)
67+ * where cnot(b) = b if n[l-1] = 1 and 1 - b otherwise.
68+ * For example, if n = 4, in binary 0100, the index is 3, in binary 011.
69+ *
70+ * Proof:
71+ * Let
72+ * x = sum((2*n[i] - 1)*2^i, i=0..l-1)
73+ * = 2*sum(n[i] * 2^i, i=0..l-1) - 2^l + 1
74+ * be the value represented by n.
75+ * The indes is (x - 1)/2 if x > 0 and -(x + 1)/2 otherwise.
76+ * Case x > 0:
77+ * n[l-1] = 1
78+ * index = sum(n[i] * 2^i, i=0..l-1) - 2^(l-1)
79+ * = sum(n[i] * 2^i, i=0..l-2)
80+ * Case x <= 0:
81+ * n[l-1] = 0
82+ * index = -(2*sum(n[i] * 2^i, i=0..l-1) - 2^l + 2)/2
83+ * = 2^(l-1) - 1 - sum(n[i] * 2^i, i=0..l-1)
84+ * = sum((1 - n[i]) * 2^i, i=0..l-2)
85+ */ \
6686 unsigned index = ((unsigned )(- negative ) ^ n ) & ((1U << (ECMULT_CONST_GROUP_SIZE - 1 )) - 1U ); \
6787 secp256k1_fe neg_y ; \
6888 VERIFY_CHECK ((n ) < (1U << ECMULT_CONST_GROUP_SIZE )); \
0 commit comments