Use downward gotos in g1_lincomb (#83)

* Use downward gotos in g1_lincomb

* Do it asn's way
This commit is contained in:
Justin Traglia 2023-01-24 19:15:16 +01:00 committed by GitHub
parent 7043128428
commit d849e626ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -772,7 +772,13 @@ static C_KZG_RET compute_challenges(fr_t *eval_challenge_out, fr_t *r_powers_out
* We do the second of these to save memory here. * We do the second of these to save memory here.
*/ */
static C_KZG_RET g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, const uint64_t len) { static C_KZG_RET g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, const uint64_t len) {
if (len < 8) { // Tunable parameter: must be at least 2 since Blst fails for 0 or 1 C_KZG_RET ret = C_KZG_MALLOC;
void *scratch = NULL;
blst_p1_affine *p_affine = NULL;
blst_scalar *scalars = NULL;
// Tunable parameter: must be at least 2 since Blst fails for 0 or 1
if (len < 8) {
// Direct approach // Direct approach
g1_t tmp; g1_t tmp;
*out = g1_identity; *out = g1_identity;
@ -782,19 +788,12 @@ static C_KZG_RET g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, const
} }
} else { } else {
// Blst's implementation of the Pippenger method // Blst's implementation of the Pippenger method
void *scratch = malloc(blst_p1s_mult_pippenger_scratch_sizeof(len)); scratch = malloc(blst_p1s_mult_pippenger_scratch_sizeof(len));
if (scratch == NULL) return C_KZG_MALLOC; if (scratch == NULL) goto out;
blst_p1_affine *p_affine = malloc(len * sizeof(blst_p1_affine)); p_affine = malloc(len * sizeof(blst_p1_affine));
if (p_affine == NULL) { if (p_affine == NULL) goto out;
free(scratch); scalars = malloc(len * sizeof(blst_scalar));
return C_KZG_MALLOC; if (scalars == NULL) goto out;
}
blst_scalar *scalars = malloc(len * sizeof(blst_scalar));
if (scalars == NULL) {
free(scratch);
free(p_affine);
return C_KZG_MALLOC;
}
// Transform the points to affine representation // Transform the points to affine representation
const blst_p1 *p_arg[2] = {p, NULL}; const blst_p1 *p_arg[2] = {p, NULL};
@ -809,13 +808,15 @@ static C_KZG_RET g1_lincomb(g1_t *out, const g1_t *p, const fr_t *coeffs, const
const byte *scalars_arg[2] = {(byte *)scalars, NULL}; const byte *scalars_arg[2] = {(byte *)scalars, NULL};
const blst_p1_affine *points_arg[2] = {p_affine, NULL}; const blst_p1_affine *points_arg[2] = {p_affine, NULL};
blst_p1s_mult_pippenger(out, points_arg, len, scalars_arg, 256, scratch); blst_p1s_mult_pippenger(out, points_arg, len, scalars_arg, 256, scratch);
}
// Tidy up ret = C_KZG_OK;
out:
free(scratch); free(scratch);
free(p_affine); free(p_affine);
free(scalars); free(scalars);
} return ret;
return C_KZG_OK;
} }
/** /**