Skip to content

Commit

Permalink
Precompute reverse-bits-limited cell indices
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Sep 16, 2024
1 parent 1c67090 commit 3187576
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/eip7594/eip7594.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@
/** The domain separator for verify_cell_kzg_proof_batch's random challenge. */
static const char *RANDOM_CHALLENGE_DOMAIN_VERIFY_CELL_KZG_PROOF_BATCH = "RCKZGCBATCH__V1_";

/**
* This is a precomputed map of cell index to reverse-bits-limited cell index.
*
* for (size_t i = 0; i < CELLS_PER_EXT_BLOB; i++)
* printf("%#04llx,\n", reverse_bits_limited(CELLS_PER_EXT_BLOB, i));
*/
static const uint64_t CELL_INDICES_RBL[CELLS_PER_EXT_BLOB] = {
0x00, 0x40, 0x20, 0x60, 0x10, 0x50, 0x30, 0x70, 0x08, 0x48, 0x28, 0x68, 0x18, 0x58, 0x38, 0x78,
0x04, 0x44, 0x24, 0x64, 0x14, 0x54, 0x34, 0x74, 0x0c, 0x4c, 0x2c, 0x6c, 0x1c, 0x5c, 0x3c, 0x7c,
0x02, 0x42, 0x22, 0x62, 0x12, 0x52, 0x32, 0x72, 0x0a, 0x4a, 0x2a, 0x6a, 0x1a, 0x5a, 0x3a, 0x7a,
0x06, 0x46, 0x26, 0x66, 0x16, 0x56, 0x36, 0x76, 0x0e, 0x4e, 0x2e, 0x6e, 0x1e, 0x5e, 0x3e, 0x7e,
0x01, 0x41, 0x21, 0x61, 0x11, 0x51, 0x31, 0x71, 0x09, 0x49, 0x29, 0x69, 0x19, 0x59, 0x39, 0x79,
0x05, 0x45, 0x25, 0x65, 0x15, 0x55, 0x35, 0x75, 0x0d, 0x4d, 0x2d, 0x6d, 0x1d, 0x5d, 0x3d, 0x7d,
0x03, 0x43, 0x23, 0x63, 0x13, 0x53, 0x33, 0x73, 0x0b, 0x4b, 0x2b, 0x6b, 0x1b, 0x5b, 0x3b, 0x7b,
0x07, 0x47, 0x27, 0x67, 0x17, 0x57, 0x37, 0x77, 0x0f, 0x4f, 0x2f, 0x6f, 0x1f, 0x5f, 0x3f, 0x7f,
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// Compute
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -57,7 +74,8 @@ static const char *RANDOM_CHALLENGE_DOMAIN_VERIFY_CELL_KZG_PROOF_BATCH = "RCKZGC
* @remark If proofs is NULL, they won't be computed.
* @remark Will return an error if both cells & proofs are NULL.
*/
C_KZG_RET compute_cells_and_kzg_proofs(
C_KZG_RET
compute_cells_and_kzg_proofs(
Cell *cells, KZGProof *proofs, const Blob *blob, const KZGSettings *s
) {
C_KZG_RET ret;
Expand Down Expand Up @@ -647,9 +665,8 @@ static C_KZG_RET compute_commitment_to_aggregated_interpolation_poly(
if (ret != C_KZG_OK) goto out;

/* Now divide by the coset shift factor */
uint64_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, i);
uint64_t idx = FIELD_ELEMENTS_PER_EXT_BLOB - pos % FIELD_ELEMENTS_PER_EXT_BLOB;
shift_poly(column_interpolation_poly, FIELD_ELEMENTS_PER_CELL, &s->roots_of_unity[idx]);
uint64_t pos = -CELL_INDICES_RBL[i] % FIELD_ELEMENTS_PER_EXT_BLOB;
shift_poly(column_interpolation_poly, FIELD_ELEMENTS_PER_CELL, &s->roots_of_unity[pos]);

/* Update the aggregated poly */
for (size_t k = 0; k < FIELD_ELEMENTS_PER_CELL; k++) {
Expand Down Expand Up @@ -707,9 +724,10 @@ static C_KZG_RET computed_weighted_sum_of_proofs(
if (ret != C_KZG_OK) goto out;

for (uint64_t i = 0; i < num_cells; i++) {
uint64_t pos = reverse_bits_limited(CELLS_PER_EXT_BLOB, cell_indices[i]);
/* Compute h_k^n, with h_k and n as in the spec */
uint64_t pos = CELL_INDICES_RBL[cell_indices[i]];
coset_factor_pow = s->roots_of_unity[pos * FIELD_ELEMENTS_PER_CELL];

/* Scale the power of r by h_k^n */
blst_fr_mul(&weighted_powers_of_r[i], &r_powers[i], &coset_factor_pow);
}
Expand All @@ -725,10 +743,10 @@ static C_KZG_RET computed_weighted_sum_of_proofs(
* Given some cells, verify that their proofs are valid.
*
* @param[out] ok True if the proofs are valid
* @param[in] commitments_bytes The commitments for the cells
* @param[in] cell_indices The cell indices for the cells
* @param[in] cells The cells to check
* @param[in] proofs_bytes The proofs for the cells
* @param[in] commitments_bytes The commitments for the cells, length `num_cells`
* @param[in] cell_indices The indices for the cells, length `num_cells`
* @param[in] cells The cells to check, length `num_cells`
* @param[in] proofs_bytes The proofs for the cells, length `num_cells`
* @param[in] num_cells The number of cells provided
* @param[in] s The trusted setup
*/
Expand Down Expand Up @@ -851,6 +869,7 @@ C_KZG_RET verify_cell_kzg_proof_batch(
);
if (ret != C_KZG_OK) goto out;

/* Subtract commitment from sum by adding the negated commitment */
blst_p1_cneg(&interpolation_poly_commit, true);
blst_p1_add(&final_g1_sum, &final_g1_sum, &interpolation_poly_commit);

Expand Down

0 comments on commit 3187576

Please sign in to comment.