From a9ef9a893aa5631b1432d8224f00d424440d4757 Mon Sep 17 00:00:00 2001 From: Ramana Kumar Date: Tue, 27 Sep 2022 18:25:43 +0100 Subject: [PATCH] Add a few more implementations --- min-src/README.md | 1 + min-src/c_kzg_4844.c | 83 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/min-src/README.md b/min-src/README.md index 5ba7914..964bbc5 100644 --- a/min-src/README.md +++ b/min-src/README.md @@ -8,6 +8,7 @@ This is a copy of C-KZG stripped down to support the [Polynomial Commitments](ht - `verify_kzg_proof` - `compute_kzg_proof` - `evaluate_polynomial_in_evaluation_form` + and `compute_powers` from the [Validator](https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/validator.md) API. We also provide `load_trusted_setup` and `free_trusted_setup` to load the diff --git a/min-src/c_kzg_4844.c b/min-src/c_kzg_4844.c index 8188c6e..94756f1 100644 --- a/min-src/c_kzg_4844.c +++ b/min-src/c_kzg_4844.c @@ -108,6 +108,9 @@ static int log_2_byte(byte b) { return r; } +/** The zero field element */ +static const fr_t fr_zero = {0L, 0L, 0L, 0L}; + /** This is 1 in Blst's `blst_fr` limb representation. Crazy but true. */ static const fr_t fr_one = {0x00000001fffffffeL, 0x5884b7fa00034802L, 0x998c4fefecbc4ff5L, 0x1824b159acc5056fL}; @@ -136,6 +139,17 @@ static bool fr_is_one(const fr_t *p) { return a[0] == 1 && a[1] == 0 && a[2] == 0 && a[3] == 0; } +/** + * Add two field elements. + * + * @param[out] out @p a plus @p b in the field + * @param[in] a Field element + * @param[in] b Field element + */ +static void fr_add(fr_t *out, const fr_t *a, const fr_t *b) { + blst_fr_add(out, a, b); +} + /** * Multiply two field elements. * @@ -147,6 +161,16 @@ static void fr_mul(fr_t *out, const fr_t *a, const fr_t *b) { blst_fr_mul(out, a, b); } +/** + * Square a field element. + * + * @param[out] out @p a squared + * @param[in] a A field element + */ +static void fr_sqr(fr_t *out, const fr_t *a) { + blst_fr_sqr(out, a); +} + /** * Create a field element from a single 64-bit unsigned integer. * @@ -599,13 +623,52 @@ void free_trusted_setup(KZGSettings *s) { free_kzg_settings(s); } -/* -void compute_powers(BLSFieldElement out[], const BLSFieldElement *x, uint64_t n) { fr_pow(out, x, n); } +/** + * Exponentiation of a field element. + * + * Uses square and multiply for log(@p n) performance. + * + * @remark A 64-bit exponent is sufficient for our needs here. + * + * @param[out] out @p a raised to the power of @p n + * @param[in] x The field element to be exponentiated + * @param[in] n The exponent + */ +void compute_powers(fr_t out[], const fr_t *x, uint64_t n) { + fr_t tmp = *x; + *out = fr_one; -void vector_lincomb(BLSFieldElement out[], const BLSFieldElement *vectors, const BLSFieldElement *scalars, uint64_t num_vectors, uint64_t vector_len) { - fr_vector_lincomb(out, vectors, scalars, num_vectors, vector_len); + while (true) { + if (n & 1) { + fr_mul(out, out, &tmp); + } + if ((n >>= 1) == 0) break; + fr_sqr(&tmp, &tmp); + } } +void bytes_to_bls_field(BLSFieldElement *out, const scalar_t *bytes) { + blst_fr_from_scalar(out, bytes); +} + +/** + * Compute linear combinations of a sequence of vectors with some scalars + */ +void vector_lincomb(fr_t out[], const fr_t *vectors, const fr_t *scalars, uint64_t n, uint64_t m) { + fr_t (*vectors_ptr)[n][m] = (fr_t (*)[n][m]) vectors; + fr_t tmp; + uint64_t i, j; + for (j = 0; j < m; j++) + out[j] = fr_zero; + for (i = 0; i < n; i++) { + for (j = 0; j < m; j++) { + fr_mul(&tmp, &scalars[i], &((*vectors_ptr)[i][j])); + fr_add(&out[j], &out[j], &tmp); + } + } +} + +/* void g1_lincomb(KZGCommitment *out, const KZGCommitment points[], const BLSFieldElement scalars[], uint64_t num_points) { g1_linear_combination(out, points, scalars, num_points); @@ -613,14 +676,6 @@ void blob_to_kzg_commitment(KZGCommitment *out, const BLSFieldElement blob[], co g1_linear_combination(out, s->secret_g1_l, blob, s->length); } -void bytes_to_bls_field(BLSFieldElement *out, const scalar_t *bytes) { - fr_from_scalar(out, bytes); -} - -C_KZG_RET evaluate_polynomial_in_evaluation_form(BLSFieldElement *out, const PolynomialEvalForm *polynomial, const BLSFieldElement *z, const KZGSettings *s) { - return eval_poly_l(out, polynomial, z, s->fs); -} - C_KZG_RET verify_kzg_proof(bool *out, const KZGCommitment *polynomial_kzg, const BLSFieldElement *z, const BLSFieldElement *y, const KZGProof *kzg_proof, const KZGSettings *s) { return check_proof_single(out, polynomial_kzg, kzg_proof, z, y, s); } @@ -630,4 +685,8 @@ C_KZG_RET compute_kzg_proof(KZGProof *out, const PolynomialEvalForm *polynomial, TRY(evaluate_polynomial_in_evaluation_form(&value, polynomial, z, s)); return compute_proof_single_l(out, polynomial, z, &value, s); } + +C_KZG_RET evaluate_polynomial_in_evaluation_form(BLSFieldElement *out, const PolynomialEvalForm *polynomial, const BLSFieldElement *z, const KZGSettings *s) { + return eval_poly_l(out, polynomial, z, s->fs); +} */