Skip to content

Commit

Permalink
Clean up toeplitz_coeffs_stride
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Sep 18, 2024
1 parent 7cf99f2 commit c9f6845
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/eip7594/cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
/** The number of bytes in a single cell. */
#define BYTES_PER_CELL (FIELD_ELEMENTS_PER_CELL * BYTES_PER_FIELD_ELEMENT)

/** The Reed-Solomon erasuring coding expansion factor. */
/** The Reed-Solomon erasure coding expansion factor. */
#define EXPANSION_FACTOR 2

/** The number of field elements in an extended blob */
#define FIELD_ELEMENTS_PER_EXT_BLOB (FIELD_ELEMENTS_PER_BLOB * EXPANSION_FACTOR)

/** The number of cells in a blob. */
#define CELLS_PER_BLOB (FIELD_ELEMENTS_PER_BLOB / FIELD_ELEMENTS_PER_CELL)

/** The number of cells in an extended blob. */
#define CELLS_PER_EXT_BLOB (FIELD_ELEMENTS_PER_EXT_BLOB / FIELD_ELEMENTS_PER_CELL)

Expand Down
2 changes: 1 addition & 1 deletion src/eip7594/fft.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "eip7594/fft.h"
#include "common/alloc.h"
#include "common/utils.h"
#include "eip4844/blob.h"
#include "eip7594/cell.h"
#include "eip7594/poly.h"

#include <string.h> /* For memcpy */
Expand Down
39 changes: 16 additions & 23 deletions src/eip7594/fk20.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,27 @@
/**
* Reorder and extend polynomial coefficients for the toeplitz method, strided version.
*
* @param[out] out The reordered polynomial, size `n * 2 / stride`
* @param[in] in The input polynomial, size `n`
* @param[in] n The size of the input polynomial
* @param[out] out The reordered polynomial, length `CELLS_PER_EXT_BLOB`
* @param[in] in The input polynomial, length `FIELD_ELEMENTS_PER_BLOB`
* @param[in] offset The offset
* @param[in] stride The stride
*/
static C_KZG_RET toeplitz_coeffs_stride(
fr_t *out, const fr_t *in, size_t n, size_t offset, size_t stride
) {
size_t k, k2;

if (stride == 0) return C_KZG_BADARGS;
static void toeplitz_coeffs_stride(fr_t *out, const fr_t *in, size_t offset) {
/* Calculate starting indices */
size_t out_start = CELLS_PER_BLOB + 2;
size_t in_start = CELLS_PER_EXT_BLOB - offset - 1;

k = n / stride;
k2 = k * 2;
/* Set the first element */
out[0] = in[FIELD_ELEMENTS_PER_BLOB - 1 - offset];

out[0] = in[n - 1 - offset];
for (size_t i = 1; i <= k + 1 && i < k2; i++) {
/* Initialize these elements to zero */
for (size_t i = 1; i < out_start; i++) {
out[i] = FR_ZERO;
}
for (size_t i = k + 2, j = 2 * stride - offset - 1; i < k2; i++, j += stride) {
out[i] = in[j];
}

return C_KZG_OK;
/* Copy elements with a fixed stride */
for (size_t i = 0; i < CELLS_PER_EXT_BLOB - out_start; i++) {
out[out_start + i] = in[in_start + i * FIELD_ELEMENTS_PER_CELL];
}
}

/**
Expand Down Expand Up @@ -77,7 +73,7 @@ C_KZG_RET compute_fk20_cell_proofs(g1_t *out, const fr_t *p, const KZGSettings *

/* Initialize length variables */
k = FIELD_ELEMENTS_PER_BLOB / FIELD_ELEMENTS_PER_CELL;
k2 = k * 2;
k2 = k * EXPANSION_FACTOR;

/* Do allocations */
ret = new_fr_array(&toeplitz_coeffs, k2);
Expand Down Expand Up @@ -112,10 +108,7 @@ C_KZG_RET compute_fk20_cell_proofs(g1_t *out, const fr_t *p, const KZGSettings *

/* Compute toeplitz coefficients and organize by column */
for (size_t i = 0; i < FIELD_ELEMENTS_PER_CELL; i++) {
ret = toeplitz_coeffs_stride(
toeplitz_coeffs, p, FIELD_ELEMENTS_PER_BLOB, i, FIELD_ELEMENTS_PER_CELL
);
if (ret != C_KZG_OK) goto out;
toeplitz_coeffs_stride(toeplitz_coeffs, p, i);
ret = fr_fft(toeplitz_coeffs_fft, toeplitz_coeffs, k2, s);
if (ret != C_KZG_OK) goto out;
for (size_t j = 0; j < k2; j++) {
Expand Down

0 comments on commit c9f6845

Please sign in to comment.