Skip to content

Commit

Permalink
Remove unnecessary variables
Browse files Browse the repository at this point in the history
  • Loading branch information
b-wagn committed Oct 9, 2024
1 parent 447c5b2 commit 0651659
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
36 changes: 17 additions & 19 deletions src/eip7594/fk20.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void toeplitz_coeffs_stride(fr_t *out, const fr_t *in, size_t offset) {
*/
C_KZG_RET compute_fk20_cell_proofs(g1_t *out, const fr_t *p, const KZGSettings *s) {
C_KZG_RET ret;
size_t k, size_circ_domain;
size_t circulant_domain_size;

blst_scalar *scalars = NULL;
fr_t **coeffs = NULL;
Expand All @@ -71,23 +71,21 @@ C_KZG_RET compute_fk20_cell_proofs(g1_t *out, const fr_t *p, const KZGSettings *
limb_t *scratch = NULL;
bool precompute = s->wbits != 0;

/* Initialize length variables */
k = CELLS_PER_BLOB;
/*
* Note: this constant 2 is not related to `LOG_EXPANSION_FACTOR`.
* Instead, it is related to circulant matrices used in FK20, see
* Section 2.2 and 3.2 in https://eprint.iacr.org/2023/033.pdf.
*/
size_circ_domain = k * 2;
circulant_domain_size = CELLS_PER_BLOB * 2;

/* Do allocations */
ret = new_fr_array(&toeplitz_coeffs, size_circ_domain);
ret = new_fr_array(&toeplitz_coeffs, circulant_domain_size);
if (ret != C_KZG_OK) goto out;
ret = new_fr_array(&toeplitz_coeffs_fft, size_circ_domain);
ret = new_fr_array(&toeplitz_coeffs_fft, circulant_domain_size);
if (ret != C_KZG_OK) goto out;
ret = new_g1_array(&h_ext_fft, size_circ_domain);
ret = new_g1_array(&h_ext_fft, circulant_domain_size);
if (ret != C_KZG_OK) goto out;
ret = new_g1_array(&h, size_circ_domain);
ret = new_g1_array(&h, circulant_domain_size);
if (ret != C_KZG_OK) goto out;

if (precompute) {
Expand All @@ -99,30 +97,30 @@ C_KZG_RET compute_fk20_cell_proofs(g1_t *out, const fr_t *p, const KZGSettings *
}

/* Allocate 2d array for coefficients by column */
ret = c_kzg_calloc((void **)&coeffs, size_circ_domain, sizeof(void *));
ret = c_kzg_calloc((void **)&coeffs, circulant_domain_size, sizeof(void *));
if (ret != C_KZG_OK) goto out;
for (size_t i = 0; i < size_circ_domain; i++) {
ret = new_fr_array(&coeffs[i], k);
for (size_t i = 0; i < circulant_domain_size; i++) {
ret = new_fr_array(&coeffs[i], CELLS_PER_BLOB);
if (ret != C_KZG_OK) goto out;
}

/* Initialize values to zero */
for (size_t i = 0; i < size_circ_domain; i++) {
for (size_t i = 0; i < circulant_domain_size; i++) {
h_ext_fft[i] = G1_IDENTITY;
}

/* Compute toeplitz coefficients and organize by column */
for (size_t i = 0; i < FIELD_ELEMENTS_PER_CELL; i++) {
toeplitz_coeffs_stride(toeplitz_coeffs, p, i);
ret = fr_fft(toeplitz_coeffs_fft, toeplitz_coeffs, size_circ_domain, s);
ret = fr_fft(toeplitz_coeffs_fft, toeplitz_coeffs, circulant_domain_size, s);
if (ret != C_KZG_OK) goto out;
for (size_t j = 0; j < size_circ_domain; j++) {
for (size_t j = 0; j < circulant_domain_size; j++) {
coeffs[j][i] = toeplitz_coeffs_fft[j];
}
}

/* Compute h_ext_fft via MSM */
for (size_t i = 0; i < size_circ_domain; i++) {
for (size_t i = 0; i < circulant_domain_size; i++) {
if (precompute) {
/* Transform the field elements to 255-bit scalars */
for (size_t j = 0; j < FIELD_ELEMENTS_PER_CELL; j++) {
Expand All @@ -149,21 +147,21 @@ C_KZG_RET compute_fk20_cell_proofs(g1_t *out, const fr_t *p, const KZGSettings *
}
}

ret = g1_ifft(h, h_ext_fft, size_circ_domain, s);
ret = g1_ifft(h, h_ext_fft, circulant_domain_size, s);
if (ret != C_KZG_OK) goto out;

/* Zero the second half of h */
for (size_t i = k; i < size_circ_domain; i++) {
for (size_t i = CELLS_PER_BLOB; i < circulant_domain_size; i++) {
h[i] = G1_IDENTITY;
}

ret = g1_fft(out, h, size_circ_domain, s);
ret = g1_fft(out, h, circulant_domain_size, s);
if (ret != C_KZG_OK) goto out;

out:
c_kzg_free(scalars);
if (coeffs != NULL) {
for (size_t i = 0; i < size_circ_domain; i++) {
for (size_t i = 0; i < circulant_domain_size; i++) {
c_kzg_free(coeffs[i]);
}
c_kzg_free(coeffs);
Expand Down
29 changes: 14 additions & 15 deletions src/setup/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ void free_trusted_setup(KZGSettings *s) {
*/
static C_KZG_RET toeplitz_part_1(g1_t *out, const g1_t *x, size_t n, const KZGSettings *s) {
C_KZG_RET ret;

/*
* Note: this constant 2 is not related to `LOG_EXPANSION_FACTOR`.
* Instead, it is related to circulant matrices used in FK20, see
Expand Down Expand Up @@ -236,62 +237,60 @@ static C_KZG_RET toeplitz_part_1(g1_t *out, const g1_t *x, size_t n, const KZGSe
*/
static C_KZG_RET init_fk20_multi_settings(KZGSettings *s) {
C_KZG_RET ret;
size_t n, k, size_circ_domain;
size_t circulant_domain_size;
g1_t *x = NULL;
g1_t *points = NULL;
blst_p1_affine *p_affine = NULL;
bool precompute = s->wbits != 0;

n = FIELD_ELEMENTS_PER_BLOB;
k = CELLS_PER_BLOB;
/*
* Note: this constant 2 is not related to `LOG_EXPANSION_FACTOR`.
* Instead, it is related to circulant matrices used in FK20, see
* Section 2.2 and 3.2 in https://eprint.iacr.org/2023/033.pdf.
*/
size_circ_domain = 2 * k;
circulant_domain_size = 2 * CELLS_PER_BLOB;

if (FIELD_ELEMENTS_PER_CELL >= NUM_G2_POINTS) {
ret = C_KZG_BADARGS;
goto out;
}

/* Allocate space for arrays */
ret = new_g1_array(&x, k);
ret = new_g1_array(&x, CELLS_PER_BLOB);
if (ret != C_KZG_OK) goto out;
ret = new_g1_array(&points, size_circ_domain);
ret = new_g1_array(&points, circulant_domain_size);
if (ret != C_KZG_OK) goto out;

/* Allocate space for array of pointers, this is a 2D array */
ret = c_kzg_calloc((void **)&s->x_ext_fft_columns, size_circ_domain, sizeof(void *));
ret = c_kzg_calloc((void **)&s->x_ext_fft_columns, circulant_domain_size, sizeof(void *));
if (ret != C_KZG_OK) goto out;
for (size_t i = 0; i < size_circ_domain; i++) {
for (size_t i = 0; i < circulant_domain_size; i++) {
ret = new_g1_array(&s->x_ext_fft_columns[i], FIELD_ELEMENTS_PER_CELL);
if (ret != C_KZG_OK) goto out;
}

for (size_t offset = 0; offset < FIELD_ELEMENTS_PER_CELL; offset++) {
/* Compute x, sections of the g1 values */
size_t start = n - FIELD_ELEMENTS_PER_CELL - 1 - offset;
for (size_t i = 0; i < k - 1; i++) {
size_t start = FIELD_ELEMENTS_PER_BLOB - FIELD_ELEMENTS_PER_CELL - 1 - offset;
for (size_t i = 0; i < CELLS_PER_BLOB - 1; i++) {
size_t j = start - i * FIELD_ELEMENTS_PER_CELL;
x[i] = s->g1_values_monomial[j];
}
x[k - 1] = G1_IDENTITY;
x[CELLS_PER_BLOB - 1] = G1_IDENTITY;

/* Compute points, the fft of an extended x */
ret = toeplitz_part_1(points, x, k, s);
ret = toeplitz_part_1(points, x, CELLS_PER_BLOB, s);
if (ret != C_KZG_OK) goto out;

/* Reorganize from rows into columns */
for (size_t row = 0; row < size_circ_domain; row++) {
for (size_t row = 0; row < circulant_domain_size; row++) {
s->x_ext_fft_columns[row][offset] = points[row];
}
}

if (precompute) {
/* Allocate space for precomputed tables */
ret = c_kzg_calloc((void **)&s->tables, size_circ_domain, sizeof(void *));
ret = c_kzg_calloc((void **)&s->tables, circulant_domain_size, sizeof(void *));
if (ret != C_KZG_OK) goto out;

/* Allocate space for points in affine representation */
Expand All @@ -303,7 +302,7 @@ static C_KZG_RET init_fk20_multi_settings(KZGSettings *s) {
s->wbits, FIELD_ELEMENTS_PER_CELL
);

for (size_t i = 0; i < size_circ_domain; i++) {
for (size_t i = 0; i < circulant_domain_size; i++) {
/* Transform the points to affine representation */
const blst_p1 *p_arg[2] = {s->x_ext_fft_columns[i], NULL};
blst_p1s_to_affine(p_affine, p_arg, FIELD_ELEMENTS_PER_CELL);
Expand Down

0 comments on commit 0651659

Please sign in to comment.