Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/ecmult_impl.h
Show First 20 Lines • Show All 519 Lines • ▼ Show 20 Lines | static int secp256k1_ecmult_strauss_batch(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_points, size_t cb_offset) { | ||||
struct secp256k1_strauss_state state; | struct secp256k1_strauss_state state; | ||||
size_t i; | size_t i; | ||||
secp256k1_gej_set_infinity(r); | secp256k1_gej_set_infinity(r); | ||||
if (inp_g_sc == NULL && n_points == 0) { | if (inp_g_sc == NULL && n_points == 0) { | ||||
return 1; | return 1; | ||||
} | } | ||||
if (!secp256k1_scratch_resize(scratch, secp256k1_strauss_scratch_size(n_points), STRAUSS_SCRATCH_OBJECTS)) { | if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_strauss_scratch_size(n_points), STRAUSS_SCRATCH_OBJECTS)) { | ||||
return 0; | return 0; | ||||
} | } | ||||
secp256k1_scratch_reset(scratch); | |||||
points = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_gej)); | points = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_gej)); | ||||
scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_scalar)); | scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_scalar)); | ||||
state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej)); | state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej)); | ||||
state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe)); | state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe)); | ||||
#ifdef USE_ENDOMORPHISM | #ifdef USE_ENDOMORPHISM | ||||
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge)); | state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge)); | ||||
state.pre_a_lam = state.pre_a + n_points * ECMULT_TABLE_SIZE(WINDOW_A); | state.pre_a_lam = state.pre_a + n_points * ECMULT_TABLE_SIZE(WINDOW_A); | ||||
#else | #else | ||||
state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge)); | state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge)); | ||||
#endif | #endif | ||||
state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(scratch, n_points * sizeof(struct secp256k1_strauss_point_state)); | state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(scratch, n_points * sizeof(struct secp256k1_strauss_point_state)); | ||||
for (i = 0; i < n_points; i++) { | for (i = 0; i < n_points; i++) { | ||||
secp256k1_ge point; | secp256k1_ge point; | ||||
if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) return 0; | if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) { | ||||
secp256k1_scratch_deallocate_frame(scratch); | |||||
return 0; | |||||
} | |||||
secp256k1_gej_set_ge(&points[i], &point); | secp256k1_gej_set_ge(&points[i], &point); | ||||
} | } | ||||
secp256k1_ecmult_strauss_wnaf(ctx, &state, r, n_points, points, scalars, inp_g_sc); | secp256k1_ecmult_strauss_wnaf(ctx, &state, r, n_points, points, scalars, inp_g_sc); | ||||
secp256k1_scratch_deallocate_frame(scratch); | |||||
return 1; | return 1; | ||||
} | } | ||||
/* Wrapper for secp256k1_ecmult_multi_func interface */ | /* Wrapper for secp256k1_ecmult_multi_func interface */ | ||||
static int secp256k1_ecmult_strauss_batch_single(const secp256k1_ecmult_context *actx, 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_strauss_batch_single(const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) { | ||||
return secp256k1_ecmult_strauss_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0); | return secp256k1_ecmult_strauss_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 310 Lines • ▼ Show 20 Lines | #endif | ||||
(void)ctx; | (void)ctx; | ||||
secp256k1_gej_set_infinity(r); | secp256k1_gej_set_infinity(r); | ||||
if (inp_g_sc == NULL && n_points == 0) { | if (inp_g_sc == NULL && n_points == 0) { | ||||
return 1; | return 1; | ||||
} | } | ||||
bucket_window = secp256k1_pippenger_bucket_window(n_points); | bucket_window = secp256k1_pippenger_bucket_window(n_points); | ||||
if (!secp256k1_scratch_resize(scratch, secp256k1_pippenger_scratch_size(n_points, bucket_window), PIPPENGER_SCRATCH_OBJECTS)) { | if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_pippenger_scratch_size(n_points, bucket_window), PIPPENGER_SCRATCH_OBJECTS)) { | ||||
return 0; | return 0; | ||||
} | } | ||||
secp256k1_scratch_reset(scratch); | |||||
points = (secp256k1_ge *) secp256k1_scratch_alloc(scratch, entries * sizeof(*points)); | points = (secp256k1_ge *) secp256k1_scratch_alloc(scratch, entries * sizeof(*points)); | ||||
scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(scratch, entries * sizeof(*scalars)); | scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(scratch, entries * sizeof(*scalars)); | ||||
state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(scratch, sizeof(*state_space)); | state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(scratch, sizeof(*state_space)); | ||||
state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(scratch, entries * sizeof(*state_space->ps)); | state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(scratch, entries * sizeof(*state_space->ps)); | ||||
state_space->wnaf_na = (int *) secp256k1_scratch_alloc(scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int)); | state_space->wnaf_na = (int *) secp256k1_scratch_alloc(scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int)); | ||||
buckets = (secp256k1_gej *) secp256k1_scratch_alloc(scratch, (1<<bucket_window) * sizeof(*buckets)); | buckets = (secp256k1_gej *) secp256k1_scratch_alloc(scratch, (1<<bucket_window) * sizeof(*buckets)); | ||||
if (inp_g_sc != NULL) { | if (inp_g_sc != NULL) { | ||||
scalars[0] = *inp_g_sc; | scalars[0] = *inp_g_sc; | ||||
points[0] = secp256k1_ge_const_g; | points[0] = secp256k1_ge_const_g; | ||||
idx++; | idx++; | ||||
#ifdef USE_ENDOMORPHISM | #ifdef USE_ENDOMORPHISM | ||||
secp256k1_ecmult_endo_split(&scalars[0], &scalars[1], &points[0], &points[1]); | secp256k1_ecmult_endo_split(&scalars[0], &scalars[1], &points[0], &points[1]); | ||||
idx++; | idx++; | ||||
#endif | #endif | ||||
} | } | ||||
while (point_idx < n_points) { | while (point_idx < n_points) { | ||||
if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) { | if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) { | ||||
secp256k1_scratch_deallocate_frame(scratch); | |||||
return 0; | return 0; | ||||
} | } | ||||
idx++; | idx++; | ||||
#ifdef USE_ENDOMORPHISM | #ifdef USE_ENDOMORPHISM | ||||
secp256k1_ecmult_endo_split(&scalars[idx - 1], &scalars[idx], &points[idx - 1], &points[idx]); | secp256k1_ecmult_endo_split(&scalars[idx - 1], &scalars[idx], &points[idx - 1], &points[idx]); | ||||
idx++; | idx++; | ||||
#endif | #endif | ||||
point_idx++; | point_idx++; | ||||
} | } | ||||
secp256k1_ecmult_pippenger_wnaf(buckets, bucket_window, state_space, r, scalars, points, idx); | secp256k1_ecmult_pippenger_wnaf(buckets, bucket_window, state_space, r, scalars, points, idx); | ||||
/* Clear data */ | /* Clear data */ | ||||
for(i = 0; (size_t)i < idx; i++) { | for(i = 0; (size_t)i < idx; i++) { | ||||
secp256k1_scalar_clear(&scalars[i]); | secp256k1_scalar_clear(&scalars[i]); | ||||
state_space->ps[i].skew_na = 0; | state_space->ps[i].skew_na = 0; | ||||
for(j = 0; j < WNAF_SIZE(bucket_window+1); j++) { | for(j = 0; j < WNAF_SIZE(bucket_window+1); j++) { | ||||
state_space->wnaf_na[i * WNAF_SIZE(bucket_window+1) + j] = 0; | state_space->wnaf_na[i * WNAF_SIZE(bucket_window+1) + j] = 0; | ||||
} | } | ||||
} | } | ||||
for(i = 0; i < 1<<bucket_window; i++) { | for(i = 0; i < 1<<bucket_window; i++) { | ||||
secp256k1_gej_clear(&buckets[i]); | secp256k1_gej_clear(&buckets[i]); | ||||
} | } | ||||
secp256k1_scratch_deallocate_frame(scratch); | |||||
return 1; | return 1; | ||||
} | } | ||||
/* Wrapper for secp256k1_ecmult_multi_func interface */ | /* Wrapper for secp256k1_ecmult_multi_func interface */ | ||||
static int secp256k1_ecmult_pippenger_batch_single(const secp256k1_ecmult_context *actx, 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_pippenger_batch_single(const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) { | ||||
return secp256k1_ecmult_pippenger_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0); | return secp256k1_ecmult_pippenger_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 94 Lines • Show Last 20 Lines |