Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/ecmult_impl.h
Show First 20 Lines • Show All 1,078 Lines • ▼ Show 20 Lines | #endif | ||||
* would choose that then the caller couldn't safely use any number | * would choose that then the caller couldn't safely use any number | ||||
* smaller than what this function returns */ | * smaller than what this function returns */ | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
return res; | return res; | ||||
} | } | ||||
/* Computes ecmult_multi by simply multiplying and adding each point. Does not | |||||
* require a scratch space */ | |||||
static int secp256k1_ecmult_multi_simple_var(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points) { | |||||
size_t point_idx; | |||||
secp256k1_scalar szero; | |||||
secp256k1_gej tmpj; | |||||
secp256k1_scalar_set_int(&szero, 0); | |||||
secp256k1_gej_set_infinity(r); | |||||
secp256k1_gej_set_infinity(&tmpj); | |||||
/* r = inp_g_sc*G */ | |||||
secp256k1_ecmult(ctx, r, &tmpj, &szero, inp_g_sc); | |||||
for (point_idx = 0; point_idx < n_points; point_idx++) { | |||||
secp256k1_ge point; | |||||
secp256k1_gej pointj; | |||||
secp256k1_scalar scalar; | |||||
if (!cb(&scalar, &point, point_idx, cbdata)) { | |||||
return 0; | |||||
} | |||||
/* r += scalar*point */ | |||||
secp256k1_gej_set_ge(&pointj, &point); | |||||
secp256k1_ecmult(ctx, &tmpj, &pointj, &scalar, NULL); | |||||
secp256k1_gej_add_var(r, r, &tmpj, NULL); | |||||
} | |||||
return 1; | |||||
} | |||||
typedef int (*secp256k1_ecmult_multi_func)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t); | typedef int (*secp256k1_ecmult_multi_func)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t); | ||||
static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) { | static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) { | ||||
size_t i; | size_t i; | ||||
int (*f)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t); | int (*f)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t); | ||||
size_t max_points; | size_t max_points; | ||||
size_t n_batches; | size_t n_batches; | ||||
size_t n_batch_points; | size_t n_batch_points; | ||||
secp256k1_gej_set_infinity(r); | secp256k1_gej_set_infinity(r); | ||||
if (inp_g_sc == NULL && n == 0) { | if (inp_g_sc == NULL && n == 0) { | ||||
return 1; | return 1; | ||||
} else if (n == 0) { | } else if (n == 0) { | ||||
secp256k1_scalar szero; | secp256k1_scalar szero; | ||||
secp256k1_scalar_set_int(&szero, 0); | secp256k1_scalar_set_int(&szero, 0); | ||||
secp256k1_ecmult(ctx, r, r, &szero, inp_g_sc); | secp256k1_ecmult(ctx, r, r, &szero, inp_g_sc); | ||||
return 1; | return 1; | ||||
} | } | ||||
if (scratch == NULL) { | |||||
return secp256k1_ecmult_multi_simple_var(ctx, r, inp_g_sc, cb, cbdata, n); | |||||
} | |||||
max_points = secp256k1_pippenger_max_points(scratch); | max_points = secp256k1_pippenger_max_points(scratch); | ||||
if (max_points == 0) { | if (max_points == 0) { | ||||
return 0; | return 0; | ||||
} else if (max_points > ECMULT_MAX_POINTS_PER_BATCH) { | } else if (max_points > ECMULT_MAX_POINTS_PER_BATCH) { | ||||
max_points = ECMULT_MAX_POINTS_PER_BATCH; | max_points = ECMULT_MAX_POINTS_PER_BATCH; | ||||
} | } | ||||
n_batches = (n+max_points-1)/max_points; | n_batches = (n+max_points-1)/max_points; | ||||
Show All 27 Lines |