@@ -54,6 +54,9 @@ static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned c
5454 * (arbitrarily) set r2 = k + 5 (mod n) and r1 = k - r2 * lambda (mod n).
5555 */
5656static void secp256k1_scalar_split_lambda (secp256k1_scalar * r1 , secp256k1_scalar * r2 , const secp256k1_scalar * k ) {
57+ VERIFY_CHECK (r1 != k );
58+ VERIFY_CHECK (r2 != k );
59+ VERIFY_CHECK (r1 != r2 );
5760 * r2 = (* k + 5 ) % EXHAUSTIVE_TEST_ORDER ;
5861 * r1 = (* k + (EXHAUSTIVE_TEST_ORDER - * r2 ) * EXHAUSTIVE_TEST_LAMBDA ) % EXHAUSTIVE_TEST_ORDER ;
5962}
@@ -71,6 +74,14 @@ static void secp256k1_scalar_split_lambda_verify(const secp256k1_scalar *r1, con
7174#endif
7275
7376/*
77+ * The function below splits k into r1 and r2, such that
78+ * - r1 + lambda * r2 == k (mod n)
79+ * - either r1 < 2^128 or -r1 mod n < 2^128
80+ * - either r2 < 2^128 or -r2 mod n < 2^128
81+ *
82+ * It is required that `r1`, `r2`, and `k` all point to different objects. An
83+ * explanation for this function and a proof of the bounds follows below.
84+ *
7485 * Both lambda and beta are primitive cube roots of unity. That is lamba^3 == 1 mod n and
7586 * beta^3 == 1 mod p, where n is the curve order and p is the field order.
7687 *
@@ -113,12 +124,6 @@ static void secp256k1_scalar_split_lambda_verify(const secp256k1_scalar *r1, con
113124 * (Note that d is also equal to the curve order, n, here because [a1,b1] and [a2,b2]
114125 * can be found as outputs of the Extended Euclidean Algorithm on inputs n and lambda).
115126 *
116- * The function below splits k into r1 and r2, such that
117- * - r1 + lambda * r2 == k (mod n)
118- * - either r1 < 2^128 or -r1 mod n < 2^128
119- * - either r2 < 2^128 or -r2 mod n < 2^128
120- *
121- * See proof below.
122127 */
123128static void secp256k1_scalar_split_lambda (secp256k1_scalar * r1 , secp256k1_scalar * r2 , const secp256k1_scalar * k ) {
124129 secp256k1_scalar c1 , c2 ;
@@ -140,6 +145,7 @@ static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar
140145 );
141146 VERIFY_CHECK (r1 != k );
142147 VERIFY_CHECK (r2 != k );
148+ VERIFY_CHECK (r1 != r2 );
143149 /* these _var calls are constant time since the shift amount is constant */
144150 secp256k1_scalar_mul_shift_var (& c1 , k , & g1 , 384 );
145151 secp256k1_scalar_mul_shift_var (& c2 , k , & g2 , 384 );
0 commit comments