MCPcopy Create free account
hub / github.com/ElementsProject/elements / test_ecmult_multi_random

Function test_ecmult_multi_random

src/secp256k1/src/tests.c:5009–5173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5007}
5008
5009static int test_ecmult_multi_random(secp256k1_scratch *scratch) {
5010 /* Large random test for ecmult_multi_* functions which exercises:
5011 * - Few or many inputs (0 up to 128, roughly exponentially distributed).
5012 * - Few or many 0*P or a*INF inputs (roughly uniformly distributed).
5013 * - Including or excluding an nonzero a*G term (or such a term at all).
5014 * - Final expected result equal to infinity or not (roughly 50%).
5015 * - ecmult_multi_var, ecmult_strauss_single_batch, ecmult_pippenger_single_batch
5016 */
5017
5018 /* These 4 variables define the eventual input to the ecmult_multi function.
5019 * g_scalar is the G scalar fed to it (or NULL, possibly, if g_scalar=0), and
5020 * scalars[0..filled-1] and gejs[0..filled-1] are the scalars and points
5021 * which form its normal inputs. */
5022 int filled = 0;
5023 secp256k1_scalar g_scalar = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
5024 secp256k1_scalar scalars[128];
5025 secp256k1_gej gejs[128];
5026 /* The expected result, and the computed result. */
5027 secp256k1_gej expected, computed;
5028 /* Temporaries. */
5029 secp256k1_scalar sc_tmp;
5030 secp256k1_ge ge_tmp;
5031 /* Variables needed for the actual input to ecmult_multi. */
5032 secp256k1_ge ges[128];
5033 ecmult_multi_data data;
5034
5035 int i;
5036 /* Which multiplication function to use */
5037 int fn = secp256k1_testrand_int(3);
5038 secp256k1_ecmult_multi_func ecmult_multi = fn == 0 ? secp256k1_ecmult_multi_var :
5039 fn == 1 ? secp256k1_ecmult_strauss_batch_single :
5040 secp256k1_ecmult_pippenger_batch_single;
5041 /* Simulate exponentially distributed num. */
5042 int num_bits = 2 + secp256k1_testrand_int(6);
5043 /* Number of (scalar, point) inputs (excluding g). */
5044 int num = secp256k1_testrand_int((1 << num_bits) + 1);
5045 /* Number of those which are nonzero. */
5046 int num_nonzero = secp256k1_testrand_int(num + 1);
5047 /* Whether we're aiming to create an input with nonzero expected result. */
5048 int nonzero_result = secp256k1_testrand_bits(1);
5049 /* Whether we will provide nonzero g multiplicand. In some cases our hand
5050 * is forced here based on num_nonzero and nonzero_result. */
5051 int g_nonzero = num_nonzero == 0 ? nonzero_result :
5052 num_nonzero == 1 && !nonzero_result ? 1 :
5053 (int)secp256k1_testrand_bits(1);
5054 /* Which g_scalar pointer to pass into ecmult_multi(). */
5055 const secp256k1_scalar* g_scalar_ptr = (g_nonzero || secp256k1_testrand_bits(1)) ? &g_scalar : NULL;
5056 /* How many EC multiplications were performed in this function. */
5057 int mults = 0;
5058 /* How many randomization steps to apply to the input list. */
5059 int rands = (int)secp256k1_testrand_bits(3);
5060 if (rands > num_nonzero) rands = num_nonzero;
5061
5062 secp256k1_gej_set_infinity(&expected);
5063 secp256k1_gej_set_infinity(&gejs[0]);
5064 secp256k1_scalar_set_int(&scalars[0], 0);
5065
5066 if (g_nonzero) {

Callers 1

run_ecmult_multi_testsFunction · 0.85

Calls 15

secp256k1_testrand_intFunction · 0.85
secp256k1_testrand_bitsFunction · 0.85
random_scalar_order_testFunction · 0.85
secp256k1_ecmult_genFunction · 0.85
secp256k1_scalar_set_intFunction · 0.70
secp256k1_scalar_mulFunction · 0.70
secp256k1_scalar_negateFunction · 0.70
secp256k1_gej_set_geFunction · 0.70
secp256k1_ecmultFunction · 0.70

Tested by

no test coverage detected