Skip to content

Commit

Permalink
Rename poly_degree to field_elements_per_blob
Browse files Browse the repository at this point in the history
  • Loading branch information
jtraglia committed Aug 24, 2023
1 parent d39c955 commit 2734dfd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
59 changes: 30 additions & 29 deletions src/c_kzg_4844.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ static C_KZG_RET blob_to_polynomial(
fr_t *p, const uint8_t *blob, const KZGSettings *s
) {
C_KZG_RET ret;
for (size_t i = 0; i < s->poly_degree; i++) {
for (size_t i = 0; i < s->field_elements_per_blob; i++) {
ret = bytes_to_bls_field(
&p[i], (Bytes32 *)&blob[i * BYTES_PER_FIELD_ELEMENT]
);
Expand Down Expand Up @@ -622,7 +622,8 @@ static C_KZG_RET compute_challenge(

/* Input size to the Fiat-Shamir challenge computation. */
size_t challenge_input_size = DOMAIN_STR_LENGTH + 16 +
(BYTES_PER_FIELD_ELEMENT * s->poly_degree) +
(BYTES_PER_FIELD_ELEMENT *
s->field_elements_per_blob) +
BYTES_PER_COMMITMENT;
ret = c_kzg_malloc((void **)&bytes, challenge_input_size);
if (ret != C_KZG_OK) goto out;
Expand All @@ -637,7 +638,7 @@ static C_KZG_RET compute_challenge(
/* Copy polynomial degree (16-bytes, big-endian) */
bytes_from_uint64(offset, 0);
offset += sizeof(uint64_t);
bytes_from_uint64(offset, s->poly_degree);
bytes_from_uint64(offset, s->field_elements_per_blob);
offset += sizeof(uint64_t);

/* Copy blob */
Expand Down Expand Up @@ -796,12 +797,12 @@ static C_KZG_RET evaluate_polynomial_in_evaluation_form(
uint64_t i;
const fr_t *roots_of_unity = s->roots_of_unity;

ret = new_fr_array(&inverses_in, s->poly_degree);
ret = new_fr_array(&inverses_in, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;
ret = new_fr_array(&inverses, s->poly_degree);
ret = new_fr_array(&inverses, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

for (i = 0; i < s->poly_degree; i++) {
for (i = 0; i < s->field_elements_per_blob; i++) {
/*
* If the point to evaluate at is one of the evaluation points by which
* the polynomial is given, we can just return the result directly.
Expand All @@ -816,18 +817,18 @@ static C_KZG_RET evaluate_polynomial_in_evaluation_form(
blst_fr_sub(&inverses_in[i], x, &roots_of_unity[i]);
}

ret = fr_batch_inv(inverses, inverses_in, s->poly_degree);
ret = fr_batch_inv(inverses, inverses_in, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

*out = FR_ZERO;
for (i = 0; i < s->poly_degree; i++) {
for (i = 0; i < s->field_elements_per_blob; i++) {
blst_fr_mul(&tmp, &inverses[i], &roots_of_unity[i]);
blst_fr_mul(&tmp, &tmp, &p[i]);
blst_fr_add(out, out, &tmp);
}
fr_from_uint64(&tmp, s->poly_degree);
fr_from_uint64(&tmp, s->field_elements_per_blob);
fr_div(out, out, &tmp);
fr_pow(&tmp, x, s->poly_degree);
fr_pow(&tmp, x, s->field_elements_per_blob);
blst_fr_sub(&tmp, &tmp, &FR_ONE);
blst_fr_mul(out, out, &tmp);

Expand All @@ -851,7 +852,7 @@ static C_KZG_RET evaluate_polynomial_in_evaluation_form(
static C_KZG_RET poly_to_kzg_commitment(
g1_t *out, const fr_t *p, const KZGSettings *s
) {
return g1_lincomb_fast(out, s->g1_values, p, s->poly_degree);
return g1_lincomb_fast(out, s->g1_values, p, s->field_elements_per_blob);
}

/**
Expand All @@ -868,7 +869,7 @@ C_KZG_RET blob_to_kzg_commitment(
fr_t *p = NULL;
g1_t commitment;

ret = new_fr_array(&p, s->poly_degree);
ret = new_fr_array(&p, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;
ret = blob_to_polynomial(p, blob, s);
if (ret != C_KZG_OK) goto out;
Expand Down Expand Up @@ -1001,7 +1002,7 @@ C_KZG_RET compute_kzg_proof(
fr_t *polynomial = NULL;
fr_t frz, fry;

ret = new_fr_array(&polynomial, s->poly_degree);
ret = new_fr_array(&polynomial, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;
ret = blob_to_polynomial(polynomial, blob, s);
if (ret != C_KZG_OK) goto out;
Expand Down Expand Up @@ -1039,7 +1040,7 @@ static C_KZG_RET compute_kzg_proof_impl(
fr_t *inverses = NULL;
fr_t *q = NULL;

ret = new_fr_array(&q, s->poly_degree);
ret = new_fr_array(&q, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

ret = evaluate_polynomial_in_evaluation_form(y_out, polynomial, z, s);
Expand All @@ -1051,12 +1052,12 @@ static C_KZG_RET compute_kzg_proof_impl(
/* m != 0 indicates that the evaluation point z equals root_of_unity[m-1] */
uint64_t m = 0;

ret = new_fr_array(&inverses_in, s->poly_degree);
ret = new_fr_array(&inverses_in, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;
ret = new_fr_array(&inverses, s->poly_degree);
ret = new_fr_array(&inverses, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

for (i = 0; i < s->poly_degree; i++) {
for (i = 0; i < s->field_elements_per_blob; i++) {
if (fr_equal(z, &roots_of_unity[i])) {
/* We are asked to compute a KZG proof inside the domain */
m = i + 1;
Expand All @@ -1068,26 +1069,26 @@ static C_KZG_RET compute_kzg_proof_impl(
blst_fr_sub(&inverses_in[i], &roots_of_unity[i], z);
}

ret = fr_batch_inv(inverses, inverses_in, s->poly_degree);
ret = fr_batch_inv(inverses, inverses_in, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

for (i = 0; i < s->poly_degree; i++) {
for (i = 0; i < s->field_elements_per_blob; i++) {
blst_fr_mul(&q[i], &q[i], &inverses[i]);
}

if (m != 0) { /* ω_{m-1} == z */
q[--m] = FR_ZERO;
for (i = 0; i < s->poly_degree; i++) {
for (i = 0; i < s->field_elements_per_blob; i++) {
if (i == m) continue;
/* Build denominator: z * (z - ω_i) */
blst_fr_sub(&tmp, z, &roots_of_unity[i]);
blst_fr_mul(&inverses_in[i], &tmp, z);
}

ret = fr_batch_inv(inverses, inverses_in, s->poly_degree);
ret = fr_batch_inv(inverses, inverses_in, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

for (i = 0; i < s->poly_degree; i++) {
for (i = 0; i < s->field_elements_per_blob; i++) {
if (i == m) continue;
/* Build numerator: ω_i * (p_i - y) */
blst_fr_sub(&tmp, &polynomial[i], y_out);
Expand All @@ -1099,7 +1100,7 @@ static C_KZG_RET compute_kzg_proof_impl(
}

g1_t out_g1;
ret = g1_lincomb_fast(&out_g1, s->g1_values, q, s->poly_degree);
ret = g1_lincomb_fast(&out_g1, s->g1_values, q, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

bytes_from_g1(proof_out, &out_g1);
Expand Down Expand Up @@ -1133,7 +1134,7 @@ C_KZG_RET compute_blob_kzg_proof(
fr_t *polynomial = NULL;
fr_t y;

ret = new_fr_array(&polynomial, s->poly_degree);
ret = new_fr_array(&polynomial, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

/* Do conversions first to fail fast, compute_challenge is expensive */
Expand Down Expand Up @@ -1182,7 +1183,7 @@ C_KZG_RET verify_blob_kzg_proof(
*ok = false;

/* Allocate a new polynomial */
ret = new_fr_array(&polynomial, s->poly_degree);
ret = new_fr_array(&polynomial, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

/* Do conversions first to fail fast, compute_challenge is expensive */
Expand Down Expand Up @@ -1251,7 +1252,7 @@ static C_KZG_RET compute_r_powers(
offset += DOMAIN_STR_LENGTH;

/* Copy degree of the polynomial */
bytes_from_uint64(offset, s->poly_degree);
bytes_from_uint64(offset, s->field_elements_per_blob);
offset += sizeof(uint64_t);

/* Copy number of commitments */
Expand Down Expand Up @@ -1427,7 +1428,7 @@ C_KZG_RET verify_blob_kzg_proof_batch(
if (ret != C_KZG_OK) goto out;
ret = new_fr_array(&ys_fr, n);
if (ret != C_KZG_OK) goto out;
ret = new_fr_array(&polynomial, s->poly_degree);
ret = new_fr_array(&polynomial, s->field_elements_per_blob);
if (ret != C_KZG_OK) goto out;

for (size_t i = 0; i < n; i++) {
Expand Down Expand Up @@ -1655,7 +1656,7 @@ static C_KZG_RET compute_roots_of_unity(
*/
void free_trusted_setup(KZGSettings *s) {
if (s == NULL) return;
s->poly_degree = 0;
s->field_elements_per_blob = 0;
s->bytes_per_blob = 0;
s->max_width = 0;
c_kzg_free(s->roots_of_unity);
Expand Down Expand Up @@ -1715,7 +1716,7 @@ C_KZG_RET load_trusted_setup(
while ((1ULL << max_scale) < n1)
max_scale++;

out->poly_degree = n1;
out->field_elements_per_blob = n1;
out->bytes_per_blob = n1 * BYTES_PER_FIELD_ELEMENT;
out->max_width = 1ULL << max_scale;
out->roots_of_unity = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/c_kzg_4844.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ typedef enum {
* Stores the setup and parameters needed for computing KZG proofs.
*/
typedef struct {
/** Number of terms in a polynomial. */
uint64_t poly_degree;
/** Number of field elements in a blob. */
uint64_t field_elements_per_blob;
/** Number of bytes in a blob. */
size_t bytes_per_blob;
/** The length of `roots_of_unity`, a power of 2. */
Expand Down
30 changes: 15 additions & 15 deletions src/test_c_kzg_4844.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void get_rand_fr(fr_t *out) {
}

static void get_rand_blob(uint8_t *out) {
for (uint64_t i = 0; i < s.poly_degree; i++) {
for (uint64_t i = 0; i < s.field_elements_per_blob; i++) {
get_rand_field_element((Bytes32 *)&out[i * 32]);
}
}
Expand Down Expand Up @@ -131,8 +131,8 @@ static void get_rand_uint32(uint32_t *out) {
}

static void eval_poly(fr_t *out, fr_t *poly_coefficients, fr_t *x) {
*out = poly_coefficients[s.poly_degree - 1];
for (size_t i = s.poly_degree - 1; i > 0; i--) {
*out = poly_coefficients[s.field_elements_per_blob - 1];
for (size_t i = s.field_elements_per_blob - 1; i > 0; i--) {
blst_fr_mul(out, out, x);
blst_fr_add(out, out, &poly_coefficients[i - 1]);
}
Expand Down Expand Up @@ -1013,13 +1013,13 @@ static void test_evaluate_polynomial_in_evaluation_form__constant_polynomial(
fr_t *p = NULL;
fr_t x, y, c;

ret = new_fr_array(&p, s.poly_degree);
ret = new_fr_array(&p, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);

get_rand_fr(&c);
get_rand_fr(&x);

for (size_t i = 0; i < s.poly_degree; i++) {
for (size_t i = 0; i < s.field_elements_per_blob; i++) {
p[i] = c;
}

Expand All @@ -1036,13 +1036,13 @@ test_evaluate_polynomial_in_evaluation_form__constant_polynomial_in_range(void
fr_t *p = NULL;
fr_t x, y, c;

ret = new_fr_array(&p, s.poly_degree);
ret = new_fr_array(&p, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);

get_rand_fr(&c);
x = s.roots_of_unity[123];

for (size_t i = 0; i < s.poly_degree; i++) {
for (size_t i = 0; i < s.field_elements_per_blob; i++) {
p[i] = c;
}

Expand All @@ -1055,18 +1055,18 @@ test_evaluate_polynomial_in_evaluation_form__constant_polynomial_in_range(void
static void test_evaluate_polynomial_in_evaluation_form__random_polynomial(void
) {
C_KZG_RET ret;
fr_t poly_coefficients[s.poly_degree];
fr_t poly_coefficients[s.field_elements_per_blob];
fr_t *p = NULL;
fr_t x, y, check;

ret = new_fr_array(&p, s.poly_degree);
ret = new_fr_array(&p, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);

for (size_t i = 0; i < s.poly_degree; i++) {
for (size_t i = 0; i < s.field_elements_per_blob; i++) {
get_rand_fr(&poly_coefficients[i]);
}

for (size_t i = 0; i < s.poly_degree; i++) {
for (size_t i = 0; i < s.field_elements_per_blob; i++) {
eval_poly(&p[i], poly_coefficients, &s.roots_of_unity[i]);
}

Expand Down Expand Up @@ -1134,7 +1134,7 @@ static void test_compute_kzg_proof__succeeds_expected_proof(void) {
Bytes48 proof, expected_proof;
int diff;

ret = new_fr_array(&poly, s.poly_degree);
ret = new_fr_array(&poly, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
ret = c_kzg_malloc((void **)&blob, s.bytes_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
Expand Down Expand Up @@ -1201,7 +1201,7 @@ static void test_compute_and_verify_kzg_proof__succeeds_round_trip(void) {
bool ok;
int diff;

ret = new_fr_array(&poly, s.poly_degree);
ret = new_fr_array(&poly, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
ret = c_kzg_malloc((void **)&blob, s.bytes_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
Expand Down Expand Up @@ -1257,7 +1257,7 @@ static void test_compute_and_verify_kzg_proof__succeeds_within_domain(void) {
bool ok;
int diff;

ret = new_fr_array(&poly, s.poly_degree);
ret = new_fr_array(&poly, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
ret = c_kzg_malloc((void **)&blob, s.bytes_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
Expand Down Expand Up @@ -1308,7 +1308,7 @@ static void test_compute_and_verify_kzg_proof__fails_incorrect_proof(void) {
fr_t y_fr, z_fr;
bool ok;

ret = new_fr_array(&poly, s.poly_degree);
ret = new_fr_array(&poly, s.field_elements_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
ret = c_kzg_malloc((void **)&blob, s.bytes_per_blob);
ASSERT_EQUALS(ret, C_KZG_OK);
Expand Down

0 comments on commit 2734dfd

Please sign in to comment.