Introduce new_ prefix for functions that allocate memory
This commit is contained in:
parent
06afe430a7
commit
d22bbcca62
|
@ -201,10 +201,10 @@ C_KZG_RET toeplitz_part_3(blst_p1 *out, const blst_p1 *h_ext_fft, uint64_t n2, c
|
|||
* @retval C_CZK_OK All is well
|
||||
* @retval C_CZK_MALLOC Memory allocation failed
|
||||
*/
|
||||
C_KZG_RET toeplitz_coeffs_step(poly *out, const poly *in) {
|
||||
C_KZG_RET new_toeplitz_coeffs_step(poly *out, const poly *in) {
|
||||
uint64_t n = in->length, n2 = n * 2;
|
||||
|
||||
ASSERT(init_poly(out, n2) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT(new_poly(out, n2) == C_KZG_OK, C_KZG_MALLOC);
|
||||
|
||||
out->coeffs[0] = in->coeffs[n - 1];
|
||||
for (uint64_t i = 1; i <= n + 1; i++) {
|
||||
|
@ -217,6 +217,15 @@ C_KZG_RET toeplitz_coeffs_step(poly *out, const poly *in) {
|
|||
return C_KZG_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recover memory allocated by #new_toeplitz_coeffs_step.
|
||||
*
|
||||
* @param p The coefficients to be freed
|
||||
*/
|
||||
void free_toeplitz_coeffs_step(poly *p) {
|
||||
free_poly(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimised version of the FK20 algorithm for use in data availability checks.
|
||||
*
|
||||
|
@ -245,7 +254,7 @@ C_KZG_RET fk20_single_da_opt(blst_p1 *out, const poly *p, const FK20SingleSettin
|
|||
ASSERT(n2 <= fk->ks->fs->max_width, C_KZG_BADARGS);
|
||||
ASSERT(is_power_of_two(n), C_KZG_BADARGS);
|
||||
|
||||
ASSERT(toeplitz_coeffs_step(&toeplitz_coeffs, p) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT(new_toeplitz_coeffs_step(&toeplitz_coeffs, p) == C_KZG_OK, C_KZG_MALLOC);
|
||||
|
||||
ASSERT(c_kzg_malloc((void **)&h_ext_fft, toeplitz_coeffs.length * sizeof *h_ext_fft) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT((ret = toeplitz_part_2(h_ext_fft, &toeplitz_coeffs, fk)) == C_KZG_OK,
|
||||
|
@ -258,7 +267,7 @@ C_KZG_RET fk20_single_da_opt(blst_p1 *out, const poly *p, const FK20SingleSettin
|
|||
|
||||
free(h);
|
||||
free(h_ext_fft);
|
||||
free_poly(&toeplitz_coeffs);
|
||||
free_toeplitz_coeffs_step(&toeplitz_coeffs);
|
||||
return C_KZG_OK;
|
||||
}
|
||||
|
||||
|
@ -376,7 +385,6 @@ C_KZG_RET new_fk20_multi_settings(FK20MultiSettings *fk, uint64_t n2, uint64_t c
|
|||
fk->ks = ks;
|
||||
fk->chunk_len = chunk_len;
|
||||
|
||||
// TODO check this!
|
||||
ASSERT(c_kzg_malloc((void **)&fk->x_ext_fft_files, chunk_len * sizeof *fk->x_ext_fft_files) == C_KZG_OK,
|
||||
C_KZG_MALLOC);
|
||||
|
||||
|
|
|
@ -67,6 +67,7 @@ C_KZG_RET reverse_bit_order(void *values, size_t size, uint64_t n);
|
|||
C_KZG_RET toeplitz_part_1(blst_p1 *out, const blst_p1 *x, uint64_t n, const FFTSettings *fs);
|
||||
C_KZG_RET toeplitz_part_2(blst_p1 *out, const poly *toeplitz_coeffs, const FK20SingleSettings *fk);
|
||||
C_KZG_RET toeplitz_part_3(blst_p1 *out, const blst_p1 *h_ext_fft, uint64_t n2, const FK20SingleSettings *fk);
|
||||
C_KZG_RET new_toeplitz_coeffs_step(poly *out, const poly *in);
|
||||
C_KZG_RET fk20_single_da_opt(blst_p1 *out, const poly *p, const FK20SingleSettings *fk);
|
||||
C_KZG_RET da_using_fk20_single(blst_p1 *out, const poly *p, const FK20SingleSettings *fk);
|
||||
C_KZG_RET new_fk20_single_settings(FK20SingleSettings *fk, uint64_t n2, const KZGSettings *ks);
|
||||
|
|
|
@ -112,7 +112,7 @@ void fk_single(void) {
|
|||
bool result;
|
||||
|
||||
TEST_CHECK(n_len >= 2 * poly_len);
|
||||
TEST_CHECK(init_poly(&p, poly_len) == C_KZG_OK);
|
||||
TEST_CHECK(new_poly(&p, poly_len) == C_KZG_OK);
|
||||
for (uint64_t i = 0; i < poly_len; i++) {
|
||||
fr_from_uint64(&p.coeffs[i], coeffs[i]);
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ void fk_single_strided(void) {
|
|||
bool result;
|
||||
|
||||
TEST_CHECK(n_len >= 2 * poly_len);
|
||||
TEST_CHECK(init_poly(&p, poly_len) == C_KZG_OK);
|
||||
TEST_CHECK(new_poly(&p, poly_len) == C_KZG_OK);
|
||||
for (uint64_t i = 0; i < poly_len; i++) {
|
||||
fr_from_uint64(&p.coeffs[i], coeffs[i]);
|
||||
}
|
||||
|
|
|
@ -104,7 +104,7 @@ C_KZG_RET compute_proof_multi(blst_p1 *out, const poly *p, const blst_fr *x0, ui
|
|||
ASSERT(is_power_of_two(n), C_KZG_BADARGS);
|
||||
|
||||
// Construct x^n - x0^n = (x - w^0)(x - w^1)...(x - w^(n-1))
|
||||
ASSERT(init_poly(&divisor, n + 1) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT(new_poly(&divisor, n + 1) == C_KZG_OK, C_KZG_MALLOC);
|
||||
|
||||
// -(x0^n)
|
||||
fr_pow(&x_pow_n, x0, n);
|
||||
|
@ -119,7 +119,7 @@ C_KZG_RET compute_proof_multi(blst_p1 *out, const poly *p, const blst_fr *x0, ui
|
|||
divisor.coeffs[n] = fr_one;
|
||||
|
||||
// Calculate q = p / (x^n - x0^n)
|
||||
ASSERT(poly_long_div(&q, p, &divisor) == C_KZG_OK, C_KZG_ERROR);
|
||||
ASSERT(new_poly_long_div(&q, p, &divisor) == C_KZG_OK, C_KZG_ERROR);
|
||||
|
||||
commit_to_poly(out, &q, ks);
|
||||
|
||||
|
@ -157,7 +157,7 @@ C_KZG_RET check_proof_multi(bool *out, const blst_p1 *commitment, const blst_p1
|
|||
ASSERT(is_power_of_two(n), C_KZG_BADARGS);
|
||||
|
||||
// Interpolate at a coset.
|
||||
ASSERT(init_poly(&interp, n) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT(new_poly(&interp, n) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT(fft_fr(interp.coeffs, ys, true, n, ks->fs) == C_KZG_OK, C_KZG_ERROR);
|
||||
|
||||
// Because it is a coset, not the subgroup, we have to multiply the polynomial coefficients by x^-i
|
||||
|
|
|
@ -35,7 +35,7 @@ void proof_single(void) {
|
|||
bool result;
|
||||
|
||||
// Create the polynomial
|
||||
init_poly(&p, poly_len);
|
||||
new_poly(&p, poly_len);
|
||||
for (int i = 0; i < poly_len; i++) {
|
||||
fr_from_uint64(&p.coeffs[i], coeffs[i]);
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ void proof_multi(void) {
|
|||
blst_p2 s2[secrets_len];
|
||||
|
||||
// Create the polynomial
|
||||
init_poly(&p, poly_len);
|
||||
new_poly(&p, poly_len);
|
||||
for (int i = 0; i < poly_len; i++) {
|
||||
fr_from_uint64(&p.coeffs[i], coeffs[i]);
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ void commit_to_nil_poly(void) {
|
|||
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, 4));
|
||||
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, s1, s2, secrets_len, &fs));
|
||||
|
||||
init_poly(&a, 0);
|
||||
new_poly(&a, 0);
|
||||
commit_to_poly(&result, &a, &ks);
|
||||
TEST_CHECK(blst_p1_is_equal(&g1_identity, &result));
|
||||
|
||||
|
|
42
src/poly.c
42
src/poly.c
|
@ -82,7 +82,7 @@ void eval_poly(blst_fr *out, const poly *p, const blst_fr *x) {
|
|||
* @retval C_CZK_BADARGS Invalid parameters were supplied
|
||||
* @retval C_CZK_MALLOC Memory allocation failed
|
||||
*/
|
||||
C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
|
||||
C_KZG_RET new_poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
|
||||
uint64_t a_pos = dividend->length - 1;
|
||||
uint64_t b_pos = divisor->length - 1;
|
||||
uint64_t diff = a_pos - b_pos;
|
||||
|
@ -92,7 +92,7 @@ C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
|
|||
ASSERT(divisor->length > 0, C_KZG_BADARGS);
|
||||
|
||||
// Initialise the output polynomial
|
||||
ASSERT(init_poly(out, poly_quotient_length(dividend, divisor)) == C_KZG_OK, C_KZG_MALLOC);
|
||||
ASSERT(new_poly(out, poly_quotient_length(dividend, divisor)) == C_KZG_OK, C_KZG_MALLOC);
|
||||
|
||||
// If the divisor is larger than the dividend, the result is zero-length
|
||||
if (out->length == 0) return C_KZG_OK;
|
||||
|
@ -117,6 +117,21 @@ C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
|
|||
return C_KZG_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise an empty polynomial of the given size.
|
||||
*
|
||||
* @remark This allocates space for the polynomial coefficients that must be later reclaimed by calling #free_poly.
|
||||
*
|
||||
* @param[out] out The initialised polynomial structure
|
||||
* @param[in] length The number of coefficients required, which is one more than the polynomial's degree
|
||||
* @retval C_CZK_OK All is well
|
||||
* @retval C_CZK_MALLOC Memory allocation failed
|
||||
*/
|
||||
C_KZG_RET new_poly(poly *out, uint64_t length) {
|
||||
out->length = length;
|
||||
return c_kzg_malloc((void **)&out->coeffs, length * sizeof *out->coeffs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise a polynomial of the given size with the given coefficients.
|
||||
*
|
||||
|
@ -128,34 +143,19 @@ C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor) {
|
|||
* @retval C_CZK_OK All is well
|
||||
* @retval C_CZK_MALLOC Memory allocation failed
|
||||
*/
|
||||
C_KZG_RET init_poly_with_coeffs(poly *out, const blst_fr *coeffs, uint64_t length) {
|
||||
ASSERT(init_poly(out, length) == C_KZG_OK, C_KZG_MALLOC);
|
||||
C_KZG_RET new_poly_with_coeffs(poly *out, const blst_fr *coeffs, uint64_t length) {
|
||||
ASSERT(new_poly(out, length) == C_KZG_OK, C_KZG_MALLOC);
|
||||
for (uint64_t i = 0; i < length; i++) {
|
||||
out->coeffs[i] = coeffs[i];
|
||||
}
|
||||
return C_KZG_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise an empty polynomial of the given size.
|
||||
*
|
||||
* @remark This allocates space for the polynomial coefficients that must be later reclaimed by calling #free_poly.
|
||||
*
|
||||
* @param[out] out The initialised polynomial structure
|
||||
* @param[in] length The number of coefficients required, which is one more than the polynomial's degree
|
||||
* @retval C_CZK_OK All is well
|
||||
* @retval C_CZK_MALLOC Memory allocation failed
|
||||
*/
|
||||
C_KZG_RET init_poly(poly *out, uint64_t length) {
|
||||
out->length = length;
|
||||
return c_kzg_malloc((void **)&out->coeffs, length * sizeof *out->coeffs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reclaim the memory used by a polynomial.
|
||||
*
|
||||
* @remark To avoid memory leaks, this must be called for polynomials initialised with #init_poly or
|
||||
* #init_poly_with_coeffs after use.
|
||||
* @remark To avoid memory leaks, this must be called for polynomials initialised with #new_poly or
|
||||
* #new_poly_with_coeffs after use.
|
||||
*
|
||||
* @param[in,out] p The polynomial
|
||||
*/
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
/**
|
||||
* Defines a polynomial whose coefficients are members of the finite field F_r.
|
||||
*
|
||||
* Initialise the storage with #init_poly. After use, free the storage with #free_poly.
|
||||
* Initialise the storage with #new_poly. After use, free the storage with #free_poly.
|
||||
*/
|
||||
typedef struct {
|
||||
blst_fr *coeffs; /**< `coeffs[i]` is the coefficient of the `x^i` term of the polynomial. */
|
||||
|
@ -30,7 +30,7 @@ typedef struct {
|
|||
} poly;
|
||||
|
||||
void eval_poly(blst_fr *out, const poly *p, const blst_fr *x);
|
||||
C_KZG_RET poly_long_div(poly *out, const poly *dividend, const poly *divisor);
|
||||
C_KZG_RET init_poly_with_coeffs(poly *out, const blst_fr *coeffs, uint64_t length);
|
||||
C_KZG_RET init_poly(poly *out, uint64_t length);
|
||||
C_KZG_RET new_poly_long_div(poly *out, const poly *dividend, const poly *divisor);
|
||||
C_KZG_RET new_poly(poly *out, uint64_t length);
|
||||
C_KZG_RET new_poly_with_coeffs(poly *out, const blst_fr *coeffs, uint64_t length);
|
||||
void free_poly(poly *p);
|
||||
|
|
|
@ -44,7 +44,7 @@ void poly_div_0(void) {
|
|||
fr_negate(&expected[0], &expected[0]);
|
||||
fr_from_uint64(&expected[1], 1);
|
||||
|
||||
TEST_CHECK(C_KZG_OK == poly_long_div(&actual, ÷nd, &divisor));
|
||||
TEST_CHECK(C_KZG_OK == new_poly_long_div(&actual, ÷nd, &divisor));
|
||||
TEST_CHECK(fr_equal(&expected[0], &actual.coeffs[0]));
|
||||
TEST_CHECK(fr_equal(&expected[1], &actual.coeffs[1]));
|
||||
|
||||
|
@ -78,7 +78,7 @@ void poly_div_1(void) {
|
|||
fr_negate(&expected[1], &expected[1]);
|
||||
fr_from_uint64(&expected[2], 3);
|
||||
|
||||
TEST_CHECK(C_KZG_OK == poly_long_div(&actual, ÷nd, &divisor));
|
||||
TEST_CHECK(C_KZG_OK == new_poly_long_div(&actual, ÷nd, &divisor));
|
||||
TEST_CHECK(fr_equal(&expected[0], &actual.coeffs[0]));
|
||||
TEST_CHECK(fr_equal(&expected[1], &actual.coeffs[1]));
|
||||
TEST_CHECK(fr_equal(&expected[2], &actual.coeffs[2]));
|
||||
|
@ -106,7 +106,7 @@ void poly_div_2(void) {
|
|||
divisor.length = 3;
|
||||
divisor.coeffs = b;
|
||||
|
||||
TEST_CHECK(C_KZG_OK == poly_long_div(&actual, ÷nd, &divisor));
|
||||
TEST_CHECK(C_KZG_OK == new_poly_long_div(&actual, ÷nd, &divisor));
|
||||
TEST_CHECK(NULL == actual.coeffs);
|
||||
|
||||
free_poly(&actual);
|
||||
|
@ -125,16 +125,16 @@ void poly_div_by_zero(void) {
|
|||
dividend.coeffs = a;
|
||||
|
||||
// Divisor
|
||||
init_poly(&divisor, 0);
|
||||
new_poly(&divisor, 0);
|
||||
|
||||
TEST_CHECK(C_KZG_BADARGS == poly_long_div(&dummy, ÷nd, &divisor));
|
||||
TEST_CHECK(C_KZG_BADARGS == new_poly_long_div(&dummy, ÷nd, &divisor));
|
||||
}
|
||||
|
||||
void poly_eval_check(void) {
|
||||
uint64_t n = 10;
|
||||
blst_fr actual, expected;
|
||||
poly p;
|
||||
init_poly(&p, n);
|
||||
new_poly(&p, n);
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
fr_from_uint64(&p.coeffs[i], i + 1);
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ void poly_eval_0_check(void) {
|
|||
uint64_t n = 7, a = 597;
|
||||
blst_fr actual, expected;
|
||||
poly p;
|
||||
init_poly(&p, n);
|
||||
new_poly(&p, n);
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
fr_from_uint64(&p.coeffs[i], i + a);
|
||||
}
|
||||
|
@ -168,7 +168,7 @@ void poly_eval_nil_check(void) {
|
|||
uint64_t n = 0;
|
||||
blst_fr actual;
|
||||
poly p;
|
||||
init_poly(&p, n);
|
||||
new_poly(&p, n);
|
||||
|
||||
eval_poly(&actual, &p, &fr_one);
|
||||
|
||||
|
|
Loading…
Reference in New Issue