diff --git a/src/c_kzg_util.c b/src/c_kzg_util.c index d52368d..35a7346 100644 --- a/src/c_kzg_util.c +++ b/src/c_kzg_util.c @@ -49,7 +49,21 @@ C_KZG_RET c_kzg_malloc(void **x, size_t n) { * @retval C_CZK_OK All is well * @retval C_CZK_MALLOC Memory allocation failed */ -C_KZG_RET new_fr(fr_t **x, size_t n) { +C_KZG_RET new_fr_array(fr_t **x, size_t n) { + return c_kzg_malloc((void **)x, n * sizeof **x); +} + +/** + * Allocate memory for an array of arrays of field elements. + * + * @remark Free the space later using `free()`, after freeing each of the array's elements. + * + * @param[out] x Pointer to the allocated space + * @param[in] n The number of field element arrays to be allocated + * @retval C_CZK_OK All is well + * @retval C_CZK_MALLOC Memory allocation failed + */ +C_KZG_RET new_fr_array_2(fr_t ***x, size_t n) { return c_kzg_malloc((void **)x, n * sizeof **x); } @@ -63,7 +77,21 @@ C_KZG_RET new_fr(fr_t **x, size_t n) { * @retval C_CZK_OK All is well * @retval C_CZK_MALLOC Memory allocation failed */ -C_KZG_RET new_p1(g1_t **x, size_t n) { +C_KZG_RET new_g1_array(g1_t **x, size_t n) { + return c_kzg_malloc((void **)x, n * sizeof **x); +} + +/** + * Allocate memory for an array of arrays of G1 group elements. + * + * @remark Free the space later using `free()`, after freeing each of the array's elements. + * + * @param[out] x Pointer to the allocated space + * @param[in] n The number of G1 arrays to be allocated + * @retval C_CZK_OK All is well + * @retval C_CZK_MALLOC Memory allocation failed + */ +C_KZG_RET new_g1_array_2(g1_t ***x, size_t n) { return c_kzg_malloc((void **)x, n * sizeof **x); } @@ -77,6 +105,6 @@ C_KZG_RET new_p1(g1_t **x, size_t n) { * @retval C_CZK_OK All is well * @retval C_CZK_MALLOC Memory allocation failed */ -C_KZG_RET new_p2(g2_t **x, size_t n) { +C_KZG_RET new_g2_array(g2_t **x, size_t n) { return c_kzg_malloc((void **)x, n * sizeof **x); } diff --git a/src/c_kzg_util.h b/src/c_kzg_util.h index 6d11063..14b4411 100644 --- a/src/c_kzg_util.h +++ b/src/c_kzg_util.h @@ -20,6 +20,8 @@ #include "c_kzg.h" C_KZG_RET c_kzg_malloc(void **p, size_t n); -C_KZG_RET new_fr(fr_t **x, size_t n); -C_KZG_RET new_p1(g1_t **x, size_t n); -C_KZG_RET new_p2(g2_t **x, size_t n); +C_KZG_RET new_fr_array(fr_t **x, size_t n); +C_KZG_RET new_fr_array_2(fr_t ***x, size_t n); +C_KZG_RET new_g1_array(g1_t **x, size_t n); +C_KZG_RET new_g1_array_2(g1_t ***x, size_t n); +C_KZG_RET new_g2_array(g2_t **x, size_t n); diff --git a/src/das_extension_test.c b/src/das_extension_test.c index 82aaa36..4d8247b 100644 --- a/src/das_extension_test.c +++ b/src/das_extension_test.c @@ -38,7 +38,7 @@ void das_extension_test_known(void) { TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, 4)); half = fs.max_width / 2; - TEST_CHECK(C_KZG_OK == new_fr(&data, half)); + TEST_CHECK(C_KZG_OK == new_fr_array(&data, half)); for (uint64_t i = 0; i < half; i++) { fr_from_uint64(data + i, i); } @@ -62,10 +62,10 @@ void das_extension_test_random(void) { fr_t *even_data, *odd_data, *data, *coeffs; for (int scale = 4; scale < 10; scale++) { TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, scale)); - TEST_CHECK(C_KZG_OK == new_fr(&even_data, fs.max_width / 2)); - TEST_CHECK(C_KZG_OK == new_fr(&odd_data, fs.max_width / 2)); - TEST_CHECK(C_KZG_OK == new_fr(&data, fs.max_width)); - TEST_CHECK(C_KZG_OK == new_fr(&coeffs, fs.max_width)); + TEST_CHECK(C_KZG_OK == new_fr_array(&even_data, fs.max_width / 2)); + TEST_CHECK(C_KZG_OK == new_fr_array(&odd_data, fs.max_width / 2)); + TEST_CHECK(C_KZG_OK == new_fr_array(&data, fs.max_width)); + TEST_CHECK(C_KZG_OK == new_fr_array(&coeffs, fs.max_width)); for (int rep = 0; rep < 4; rep++) { diff --git a/src/fft_common.c b/src/fft_common.c index 94e971b..e0f0fee 100644 --- a/src/fft_common.c +++ b/src/fft_common.c @@ -75,8 +75,8 @@ C_KZG_RET new_fft_settings(FFTSettings *fs, unsigned int max_scale) { fr_from_uint64s(&fs->root_of_unity, scale2_root_of_unity[max_scale]); // Allocate space for the roots of unity - TRY(new_fr(&fs->expanded_roots_of_unity, fs->max_width + 1)); - TRY(new_fr(&fs->reverse_roots_of_unity, fs->max_width + 1)); + TRY(new_fr_array(&fs->expanded_roots_of_unity, fs->max_width + 1)); + TRY(new_fr_array(&fs->reverse_roots_of_unity, fs->max_width + 1)); // Populate the roots of unity TRY(expand_root_of_unity(fs->expanded_roots_of_unity, &fs->root_of_unity, fs->max_width)); diff --git a/src/fk20_proofs.c b/src/fk20_proofs.c index bba1b86..e2478fe 100644 --- a/src/fk20_proofs.c +++ b/src/fk20_proofs.c @@ -44,7 +44,7 @@ C_KZG_RET toeplitz_part_1(g1_t *out, const g1_t *x, uint64_t n, const FFTSetting uint64_t n2 = n * 2; g1_t *x_ext; - TRY(new_p1(&x_ext, n2)); + TRY(new_g1_array(&x_ext, n2)); for (uint64_t i = 0; i < n; i++) { x_ext[i] = x[i]; } @@ -75,7 +75,7 @@ C_KZG_RET toeplitz_part_2(g1_t *out, const poly *toeplitz_coeffs, const g1_t *x_ // CHECK(toeplitz_coeffs->length == fk->x_ext_fft_len); // TODO: how to implement? - TRY(new_fr(&toeplitz_coeffs_fft, toeplitz_coeffs->length)); + TRY(new_fr_array(&toeplitz_coeffs_fft, toeplitz_coeffs->length)); TRY(fft_fr(toeplitz_coeffs_fft, toeplitz_coeffs->coeffs, false, toeplitz_coeffs->length, fs)); for (uint64_t i = 0; i < toeplitz_coeffs->length; i++) { @@ -185,10 +185,10 @@ C_KZG_RET fk20_single_da_opt(g1_t *out, const poly *p, const FK20SingleSettings TRY(new_poly(&toeplitz_coeffs, 2 * p->length)); TRY(toeplitz_coeffs_step(&toeplitz_coeffs, p)); - TRY(new_p1(&h_ext_fft, toeplitz_coeffs.length)); + TRY(new_g1_array(&h_ext_fft, toeplitz_coeffs.length)); TRY(toeplitz_part_2(h_ext_fft, &toeplitz_coeffs, fk->x_ext_fft, fk->ks->fs)); - TRY(new_p1(&h, n2)); + TRY(new_g1_array(&h, n2)); TRY(toeplitz_part_3(h, h_ext_fft, n2, fk->ks->fs)); TRY(fft_g1(out, h, false, n2, fk->ks->fs)); @@ -253,13 +253,13 @@ C_KZG_RET fk20_compute_proof_multi(g1_t *out, const poly *p, const FK20MultiSett CHECK(fk->ks->fs->max_width >= n2); - TRY(new_p1(&h_ext_fft, n2)); + TRY(new_g1_array(&h_ext_fft, n2)); for (uint64_t i = 0; i < n2; i++) { h_ext_fft[i] = g1_identity; } TRY(new_poly(&toeplitz_coeffs, 2 * p->length)); - TRY(new_p1(&h_ext_fft_file, toeplitz_coeffs.length)); + TRY(new_g1_array(&h_ext_fft_file, toeplitz_coeffs.length)); for (uint64_t i = 0; i < fk->chunk_len; i++) { TRY(toeplitz_coeffs_step(&toeplitz_coeffs, p)); TRY(toeplitz_part_2(h_ext_fft_file, &toeplitz_coeffs, fk->x_ext_fft_files[i], fk->ks->fs)); @@ -270,7 +270,7 @@ C_KZG_RET fk20_compute_proof_multi(g1_t *out, const poly *p, const FK20MultiSett free_poly(&toeplitz_coeffs); free(h_ext_fft_file); - TRY(new_p1(&h, n2)); + TRY(new_g1_array(&h, n2)); TRY(toeplitz_part_3(h, h_ext_fft, n2, fk->ks->fs)); TRY(fft_g1(out, h, false, n2, fk->ks->fs)); @@ -303,13 +303,13 @@ C_KZG_RET fk20_multi_da_opt(g1_t *out, const poly *p, const FK20MultiSettings *f k = n / fk->chunk_len; k2 = k * 2; - TRY(new_p1(&h_ext_fft, k2)); + TRY(new_g1_array(&h_ext_fft, k2)); for (uint64_t i = 0; i < k2; i++) { h_ext_fft[i] = g1_identity; } TRY(new_poly(&toeplitz_coeffs, n2 / fk->chunk_len)); - TRY(new_p1(&h_ext_fft_file, toeplitz_coeffs.length)); + TRY(new_g1_array(&h_ext_fft_file, toeplitz_coeffs.length)); for (uint64_t i = 0; i < fk->chunk_len; i++) { TRY(toeplitz_coeffs_stride(&toeplitz_coeffs, p, i, fk->chunk_len)); TRY(toeplitz_part_2(h_ext_fft_file, &toeplitz_coeffs, fk->x_ext_fft_files[i], fk->ks->fs)); @@ -321,7 +321,7 @@ C_KZG_RET fk20_multi_da_opt(g1_t *out, const poly *p, const FK20MultiSettings *f free(h_ext_fft_file); // Calculate `h` - TRY(new_p1(&h, k2)); + TRY(new_g1_array(&h, k2)); TRY(toeplitz_part_3(h, h_ext_fft, k2, fk->ks->fs)); // Overwrite the second half of `h` with zero @@ -380,13 +380,13 @@ C_KZG_RET new_fk20_single_settings(FK20SingleSettings *fk, uint64_t n2, const KZ fk->ks = ks; fk->x_ext_fft_len = n2; - TRY(new_p1(&x, n)); + TRY(new_g1_array(&x, n)); for (uint64_t i = 0; i < n - 1; i++) { x[i] = ks->secret_g1[n - 2 - i]; } x[n - 1] = g1_identity; - TRY(new_p1(&fk->x_ext_fft, 2 * n)); + TRY(new_g1_array(&fk->x_ext_fft, 2 * n)); TRY(toeplitz_part_1(fk->x_ext_fft, x, n, ks->fs)); free(x); @@ -426,9 +426,9 @@ C_KZG_RET new_fk20_multi_settings(FK20MultiSettings *fk, uint64_t n2, uint64_t c fk->chunk_len = chunk_len; // `x_ext_fft_files` is two dimensional. Allocate space for pointers to the rows. - TRY(c_kzg_malloc((void **)&fk->x_ext_fft_files, chunk_len * sizeof *fk->x_ext_fft_files)); + TRY(new_g1_array_2(&fk->x_ext_fft_files, chunk_len * sizeof *fk->x_ext_fft_files)); - TRY(new_p1(&x, k)); + TRY(new_g1_array(&x, k)); for (uint64_t offset = 0; offset < chunk_len; offset++) { uint64_t start = n - chunk_len - 1 - offset; for (uint64_t i = 0, j = start; i + 1 < k; i++, j -= chunk_len) { @@ -436,7 +436,7 @@ C_KZG_RET new_fk20_multi_settings(FK20MultiSettings *fk, uint64_t n2, uint64_t c } x[k - 1] = g1_identity; - TRY(new_p1(&fk->x_ext_fft_files[offset], 2 * k)); + TRY(new_g1_array(&fk->x_ext_fft_files[offset], 2 * k)); TRY(toeplitz_part_1(fk->x_ext_fft_files[offset], x, k, ks->fs)); } diff --git a/src/fk20_proofs_test.c b/src/fk20_proofs_test.c index dd3e3f2..79cb868 100644 --- a/src/fk20_proofs_test.c +++ b/src/fk20_proofs_test.c @@ -190,8 +190,8 @@ void fk_multi_0(void) { n = chunk_len * chunk_count; secrets_len = 2 * n; - TEST_CHECK(C_KZG_OK == new_p1(&s1, secrets_len)); - TEST_CHECK(C_KZG_OK == new_p2(&s2, secrets_len)); + TEST_CHECK(C_KZG_OK == new_g1_array(&s1, secrets_len)); + TEST_CHECK(C_KZG_OK == new_g2_array(&s2, secrets_len)); generate_trusted_setup(s1, s2, &secret, secrets_len); TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, 4 + 5 + 1)); @@ -214,24 +214,24 @@ void fk_multi_0(void) { commit_to_poly(&commitment, &p, &ks); // Compute the multi proofs, assuming that the polynomial will be extended with zeros - TEST_CHECK(C_KZG_OK == new_p1(&all_proofs, 2 * chunk_count)); + TEST_CHECK(C_KZG_OK == new_g1_array(&all_proofs, 2 * chunk_count)); TEST_CHECK(C_KZG_OK == da_using_fk20_multi(all_proofs, &p, &fk)); // Now actually extend the polynomial with zeros - TEST_CHECK(C_KZG_OK == new_fr(&extended_coeffs, 2 * n)); + TEST_CHECK(C_KZG_OK == new_fr_array(&extended_coeffs, 2 * n)); for (uint64_t i = 0; i < n; i++) { extended_coeffs[i] = p.coeffs[i]; } for (uint64_t i = n; i < 2 * n; i++) { extended_coeffs[i] = fr_zero; } - TEST_CHECK(C_KZG_OK == new_fr(&extended_coeffs_fft, 2 * n)); + TEST_CHECK(C_KZG_OK == new_fr_array(&extended_coeffs_fft, 2 * n)); TEST_CHECK(C_KZG_OK == fft_fr(extended_coeffs_fft, extended_coeffs, false, 2 * n, &fs)); TEST_CHECK(C_KZG_OK == reverse_bit_order(extended_coeffs_fft, sizeof extended_coeffs_fft[0], 2 * n)); // Verify the proofs - TEST_CHECK(C_KZG_OK == new_fr(&ys, chunk_len)); - TEST_CHECK(C_KZG_OK == new_fr(&ys2, chunk_len)); + TEST_CHECK(C_KZG_OK == new_fr_array(&ys, chunk_len)); + TEST_CHECK(C_KZG_OK == new_fr_array(&ys2, chunk_len)); domain_stride = fs.max_width / (2 * n); for (uint64_t pos = 0; pos < 2 * chunk_count; pos++) { uint64_t domain_pos, stride; diff --git a/src/kzg_proofs.c b/src/kzg_proofs.c index c395a0f..67f452a 100644 --- a/src/kzg_proofs.c +++ b/src/kzg_proofs.c @@ -211,8 +211,8 @@ C_KZG_RET new_kzg_settings(KZGSettings *ks, const g1_t *secret_g1, const g2_t *s ks->length = length; // Allocate space for the secrets - TRY(new_p1(&ks->secret_g1, ks->length)); - TRY(new_p2(&ks->secret_g2, ks->length)); + TRY(new_g1_array(&ks->secret_g1, ks->length)); + TRY(new_g2_array(&ks->secret_g2, ks->length)); // Populate the secrets for (uint64_t i = 0; i < ks->length; i++) { diff --git a/src/poly.c b/src/poly.c index 016626b..0b1d902 100644 --- a/src/poly.c +++ b/src/poly.c @@ -128,7 +128,7 @@ 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) { out->length = length; - return new_fr(&out->coeffs, length); + return new_fr_array(&out->coeffs, length); } /**