Move some function arguments around

This commit is contained in:
Ben Edgington 2021-02-10 13:02:22 +00:00
parent 79d7868f2a
commit 5dc78d40f2
13 changed files with 49 additions and 49 deletions

View File

@ -52,7 +52,7 @@ void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr
}
// The main entry point for forward and reverse FFTs
C_KZG_RET fft_fr(blst_fr *out, const blst_fr *in, const FFTSettings *fs, bool inv, uint64_t n) {
C_KZG_RET fft_fr(blst_fr *out, const blst_fr *in, bool inv, uint64_t n, const FFTSettings *fs) {
uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS);
ASSERT(is_power_of_two(n), C_KZG_BADARGS);

View File

@ -21,4 +21,4 @@ void fft_fr_slow(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr
uint64_t l);
void fft_fr_fast(blst_fr *out, const blst_fr *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l);
C_KZG_RET fft_fr(blst_fr *out, const blst_fr *in, const FFTSettings *fs, bool inv, uint64_t n);
C_KZG_RET fft_fr(blst_fr *out, const blst_fr *in, bool inv, uint64_t n, const FFTSettings *fs);

View File

@ -41,7 +41,7 @@ long run_bench(int scale, int max_seconds) {
while (total_time < max_seconds * NANO) {
clock_gettime(CLOCK_REALTIME, &t0);
assert(C_KZG_OK == fft_fr(out, data, &fs, false, fs.max_width));
assert(C_KZG_OK == fft_fr(out, data, false, fs.max_width, &fs));
clock_gettime(CLOCK_REALTIME, &t1);
nits++;
total_time += tdiff(t0, t1);

View File

@ -71,8 +71,8 @@ void roundtrip_fft(void) {
}
// Forward and reverse FFT
TEST_CHECK(fft_fr(coeffs, data, &fs, false, fs.max_width) == C_KZG_OK);
TEST_CHECK(fft_fr(data, coeffs, &fs, true, fs.max_width) == C_KZG_OK);
TEST_CHECK(fft_fr(coeffs, data, false, fs.max_width, &fs) == C_KZG_OK);
TEST_CHECK(fft_fr(data, coeffs, true, fs.max_width, &fs) == C_KZG_OK);
// Verify that the result is still ascending values of i
for (int i = 0; i < fs.max_width; i++) {
@ -94,7 +94,7 @@ void inverse_fft(void) {
}
// Inverst FFT
TEST_CHECK(fft_fr(out, data, &fs, true, fs.max_width) == C_KZG_OK);
TEST_CHECK(fft_fr(out, data, true, fs.max_width, &fs) == C_KZG_OK);
// Verify against the known result, `inv_fft_expected`
int n = sizeof inv_fft_expected / sizeof inv_fft_expected[0];
@ -119,8 +119,8 @@ void stride_fft(void) {
fr_from_uint64(data + i, i);
}
TEST_CHECK(fft_fr(coeffs1, data, &fs1, false, width) == C_KZG_OK);
TEST_CHECK(fft_fr(coeffs2, data, &fs2, false, width) == C_KZG_OK);
TEST_CHECK(fft_fr(coeffs1, data, false, width, &fs1) == C_KZG_OK);
TEST_CHECK(fft_fr(coeffs2, data, false, width, &fs2) == C_KZG_OK);
for (int i = 0; i < width; i++) {
TEST_CHECK(fr_equal(coeffs1 + i, coeffs2 + i));

View File

@ -53,7 +53,7 @@ void fft_g1_fast(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr
}
// The main entry point for forward and reverse FFTs
C_KZG_RET fft_g1(blst_p1 *out, const blst_p1 *in, const FFTSettings *fs, bool inv, uint64_t n) {
C_KZG_RET fft_g1(blst_p1 *out, const blst_p1 *in, bool inv, uint64_t n, const FFTSettings *fs) {
uint64_t stride = fs->max_width / n;
ASSERT(n <= fs->max_width, C_KZG_BADARGS);
ASSERT(is_power_of_two(n), C_KZG_BADARGS);

View File

@ -21,4 +21,4 @@ void fft_g1_slow(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr
uint64_t l);
void fft_g1_fast(blst_p1 *out, const blst_p1 *in, uint64_t stride, const blst_fr *roots, uint64_t roots_stride,
uint64_t l);
C_KZG_RET fft_g1(blst_p1 *out, const blst_p1 *in, const FFTSettings *fs, bool inv, uint64_t n);
C_KZG_RET fft_g1(blst_p1 *out, const blst_p1 *in, bool inv, uint64_t n, const FFTSettings *fs);

View File

@ -41,7 +41,7 @@ long run_bench(int scale, int max_seconds) {
while (total_time < max_seconds * NANO) {
clock_gettime(CLOCK_REALTIME, &t0);
assert(C_KZG_OK == fft_g1(out, data, &fs, false, fs.max_width));
assert(C_KZG_OK == fft_g1(out, data, false, fs.max_width, &fs));
clock_gettime(CLOCK_REALTIME, &t1);
nits++;
total_time += tdiff(t0, t1);

View File

@ -58,8 +58,8 @@ void roundtrip_fft(void) {
make_data(data, fs.max_width);
// Forward and reverse FFT
TEST_CHECK(fft_g1(coeffs, data, &fs, false, fs.max_width) == C_KZG_OK);
TEST_CHECK(fft_g1(data, coeffs, &fs, true, fs.max_width) == C_KZG_OK);
TEST_CHECK(fft_g1(coeffs, data, false, fs.max_width, &fs) == C_KZG_OK);
TEST_CHECK(fft_g1(data, coeffs, true, fs.max_width, &fs) == C_KZG_OK);
// Verify that the result is still ascending values of i
for (int i = 0; i < fs.max_width; i++) {
@ -78,8 +78,8 @@ void stride_fft(void) {
blst_p1 data[width], coeffs1[width], coeffs2[width];
make_data(data, width);
TEST_CHECK(fft_g1(coeffs1, data, &fs1, false, width) == C_KZG_OK);
TEST_CHECK(fft_g1(coeffs2, data, &fs2, false, width) == C_KZG_OK);
TEST_CHECK(fft_g1(coeffs1, data, false, width, &fs1) == C_KZG_OK);
TEST_CHECK(fft_g1(coeffs2, data, false, width, &fs2) == C_KZG_OK);
for (int i = 0; i < width; i++) {
TEST_CHECK(blst_p1_is_equal(coeffs1 + i, coeffs2 + i));

View File

@ -81,7 +81,7 @@ C_KZG_RET toeplitz_part_1(blst_p1 *out, const blst_p1 *x, uint64_t n, KZGSetting
x_ext[i] = identity_g1;
}
ASSERT(fft_g1(out, x_ext, ks->fs, false, n2) == C_KZG_OK, C_KZG_ERROR);
ASSERT(fft_g1(out, x_ext, false, n2, ks->fs) == C_KZG_OK, C_KZG_ERROR);
free(x_ext);
return C_KZG_OK;
@ -96,7 +96,7 @@ C_KZG_RET toeplitz_part_2(blst_p1 *out, const poly *toeplitz_coeffs, const FK20S
C_KZG_OK,
C_KZG_MALLOC);
ASSERT(fft_fr(toeplitz_coeffs_fft, toeplitz_coeffs->coeffs, fk->ks->fs, false, toeplitz_coeffs->length) == C_KZG_OK,
ASSERT(fft_fr(toeplitz_coeffs_fft, toeplitz_coeffs->coeffs, false, toeplitz_coeffs->length, fk->ks->fs) == C_KZG_OK,
C_KZG_ERROR);
for (uint64_t i = 0; i < toeplitz_coeffs->length; i++) {
@ -109,7 +109,7 @@ C_KZG_RET toeplitz_part_2(blst_p1 *out, const poly *toeplitz_coeffs, const FK20S
// Transform back and return the first half of the vector
C_KZG_RET toeplitz_part_3(blst_p1 *out, const blst_p1 *h_ext_fft, uint64_t length, const FK20SingleSettings *fk) {
ASSERT(fft_g1(out, h_ext_fft, fk->ks->fs, true, length) == C_KZG_OK, C_KZG_ERROR);
ASSERT(fft_g1(out, h_ext_fft, true, length, fk->ks->fs) == C_KZG_OK, C_KZG_ERROR);
return C_KZG_OK;
}
@ -160,7 +160,7 @@ C_KZG_RET fk20_single_da_opt(blst_p1 *out, const poly *p, FK20SingleSettings *fk
h[i] = identity_g1;
}
ASSERT(fft_g1(out, h, fk->ks->fs, false, n2) == C_KZG_OK, C_KZG_ERROR);
ASSERT(fft_g1(out, h, false, n2, fk->ks->fs) == C_KZG_OK, C_KZG_ERROR);
free(h);
free(h_ext_fft);

View File

@ -117,7 +117,7 @@ void fk_single(void) {
// Initialise the secrets and data structures
generate_trusted_setup(&s1, &s2, &secret, secrets_len);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, n));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, &fs, s1, s2, secrets_len));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, s1, s2, secrets_len, &fs));
TEST_CHECK(C_KZG_OK == new_fk20_single_settings(&fk, 2 * poly_len, &ks));
// Generate the proofs
@ -130,7 +130,7 @@ void fk_single(void) {
eval_poly(&y, &p, &x);
proof = all_proofs[reverse_bits_limited(2 * poly_len, i)];
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &ks, &commitment, &proof, &x, &y));
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &commitment, &proof, &x, &y, &ks));
TEST_CHECK(true == result);
}
@ -169,7 +169,7 @@ void fk_single_strided(void) {
// Initialise the secrets and data structures
generate_trusted_setup(&s1, &s2, &secret, secrets_len);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, n));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, &fs, s1, s2, secrets_len));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, s1, s2, secrets_len, &fs));
TEST_CHECK(C_KZG_OK == new_fk20_single_settings(&fk, 2 * poly_len, &ks));
// Generate the proofs
@ -182,7 +182,7 @@ void fk_single_strided(void) {
eval_poly(&y, &p, &x);
proof = all_proofs[reverse_bits_limited(2 * poly_len, i)];
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &ks, &commitment, &proof, &x, &y));
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &commitment, &proof, &x, &y, &ks));
TEST_CHECK(true == result);
}

View File

@ -22,12 +22,12 @@ void commit_to_poly(blst_p1 *out, const poly *p, const KZGSettings *ks) {
}
// Compute KZG proof for polynomial at position x0
C_KZG_RET compute_proof_single(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0) {
return compute_proof_multi(out, ks, p, x0, 1);
C_KZG_RET compute_proof_single(blst_p1 *out, poly *p, const blst_fr *x0, const KZGSettings *ks) {
return compute_proof_multi(out, p, x0, 1, ks);
}
C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, blst_fr *y) {
C_KZG_RET check_proof_single(bool *out, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x, blst_fr *y,
const KZGSettings *ks) {
blst_p2 x_g2, s_minus_x;
blst_p1 y_g1, commitment_minus_y;
p2_mul(&x_g2, blst_p2_generator(), x);
@ -43,7 +43,7 @@ C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *co
// Compute KZG proof for polynomial in coefficient form at positions x * w^y where w is
// an n-th root of unity (this is the proof for one data availability sample, which consists
// of several polynomial evaluations)
C_KZG_RET compute_proof_multi(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0, uint64_t n) {
C_KZG_RET compute_proof_multi(blst_p1 *out, poly *p, const blst_fr *x0, uint64_t n, const KZGSettings *ks) {
poly divisor, q;
blst_fr x_pow_n;
@ -75,8 +75,8 @@ C_KZG_RET compute_proof_multi(blst_p1 *out, const KZGSettings *ks, poly *p, cons
// Check a proof for a KZG commitment for an evaluation f(x w^i) = y_i
// The ys must have a power of 2 length
C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, const blst_fr *ys, uint64_t n) {
C_KZG_RET check_proof_multi(bool *out, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x,
const blst_fr *ys, uint64_t n, const KZGSettings *ks) {
poly interp;
blst_fr inv_x, inv_x_pow, x_pow;
blst_p2 xn2, xn_minus_yn;
@ -84,7 +84,7 @@ C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *com
// Interpolate at a coset.
ASSERT(init_poly(&interp, n) == C_KZG_OK, C_KZG_MALLOC);
ASSERT(fft_fr(interp.coeffs, ys, ks->fs, true, n) == C_KZG_OK, C_KZG_BADARGS);
ASSERT(fft_fr(interp.coeffs, ys, true, n, ks->fs) == C_KZG_OK, C_KZG_BADARGS);
// Because it is a coset, not the subgroup, we have to multiply the polynomial coefficients by x^-i
blst_fr_eucl_inverse(&inv_x, x);
@ -114,7 +114,7 @@ C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *com
}
// We don't allocate space for the secrets s1 and s2 here as they are assumed to be pre-generated
C_KZG_RET new_kzg_settings(KZGSettings *ks, FFTSettings *fs, blst_p1 *secret_g1, blst_p2 *secret_g2, uint64_t length) {
C_KZG_RET new_kzg_settings(KZGSettings *ks, blst_p1 *secret_g1, blst_p2 *secret_g2, uint64_t length, FFTSettings *fs) {
ASSERT(length >= fs->max_width, C_KZG_BADARGS);
ks->fs = fs;

View File

@ -27,11 +27,11 @@ typedef struct {
} KZGSettings;
void commit_to_poly(blst_p1 *out, const poly *p, const KZGSettings *ks);
C_KZG_RET compute_proof_single(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0);
C_KZG_RET check_proof_single(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, blst_fr *y);
C_KZG_RET compute_proof_multi(blst_p1 *out, const KZGSettings *ks, poly *p, const blst_fr *x0, uint64_t n);
C_KZG_RET check_proof_multi(bool *out, const KZGSettings *ks, const blst_p1 *commitment, const blst_p1 *proof,
const blst_fr *x, const blst_fr *ys, uint64_t n);
C_KZG_RET new_kzg_settings(KZGSettings *ks, FFTSettings *fs, blst_p1 *secret_g1, blst_p2 *secret_g2, uint64_t length);
C_KZG_RET compute_proof_single(blst_p1 *out, poly *p, const blst_fr *x0, const KZGSettings *ks);
C_KZG_RET check_proof_single(bool *out, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x, blst_fr *y,
const KZGSettings *ks);
C_KZG_RET compute_proof_multi(blst_p1 *out, poly *p, const blst_fr *x0, uint64_t n, const KZGSettings *ks);
C_KZG_RET check_proof_multi(bool *out, const blst_p1 *commitment, const blst_p1 *proof, const blst_fr *x,
const blst_fr *ys, uint64_t n, const KZGSettings *ks);
C_KZG_RET new_kzg_settings(KZGSettings *ks, blst_p1 *secret_g1, blst_p2 *secret_g2, uint64_t length, FFTSettings *fs);
void free_kzg_settings(KZGSettings *ks);

View File

@ -43,22 +43,22 @@ void proof_single(void) {
// Initialise the secrets and data structures
generate_trusted_setup(&s1, &s2, &secret, secrets_len);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, 4)); // ln_2 of poly_len
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, &fs, s1, s2, secrets_len));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, s1, s2, secrets_len, &fs));
// Compute the proof for x = 25
fr_from_uint64(&x, 25);
commit_to_poly(&commitment, &p, &ks);
TEST_CHECK(C_KZG_OK == compute_proof_single(&proof, &ks, &p, &x));
TEST_CHECK(C_KZG_OK == compute_proof_single(&proof, &p, &x, &ks));
eval_poly(&value, &p, &x);
// Verify the proof that the (unknown) polynomial has y = value at x = 25
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &ks, &commitment, &proof, &x, &value));
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &commitment, &proof, &x, &value, &ks));
TEST_CHECK(true == result);
// Change the value and check that the proof fails
blst_fr_add(&value, &value, &fr_one);
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &ks, &commitment, &proof, &x, &value));
TEST_CHECK(C_KZG_OK == check_proof_single(&result, &commitment, &proof, &x, &value, &ks));
TEST_CHECK(false == result);
free_fft_settings(&fs);
@ -97,17 +97,17 @@ void proof_multi(void) {
// Initialise the secrets and data structures
generate_trusted_setup(&s1, &s2, &secret, secrets_len);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs1, 4)); // ln_2 of poly_len
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks1, &fs1, s1, s2, secrets_len));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks1, s1, s2, secrets_len, &fs1));
// Commit to the polynomial
commit_to_poly(&commitment, &p, &ks1);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs2, coset_scale));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks2, &fs2, s1, s2, secrets_len));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks2, s1, s2, secrets_len, &fs2));
// Compute proof at the points [x * root_i] 0 <= i < coset_len
fr_from_uint64(&x, 5431);
TEST_CHECK(C_KZG_OK == compute_proof_multi(&proof, &ks2, &p, &x, coset_len));
TEST_CHECK(C_KZG_OK == compute_proof_multi(&proof, &p, &x, coset_len, &ks2));
// y_i is the value of the polynomial at each x_i
for (int i = 0; i < coset_len; i++) {
@ -116,12 +116,12 @@ void proof_multi(void) {
}
// Verify the proof that the (unknown) polynomial has value y_i at x_i
TEST_CHECK(C_KZG_OK == check_proof_multi(&result, &ks2, &commitment, &proof, &x, y, coset_len));
TEST_CHECK(C_KZG_OK == check_proof_multi(&result, &commitment, &proof, &x, y, coset_len, &ks2));
TEST_CHECK(true == result);
// Change a value and check that the proof fails
blst_fr_add(y + coset_len / 2, y + coset_len / 2, &fr_one);
TEST_CHECK(C_KZG_OK == check_proof_multi(&result, &ks2, &commitment, &proof, &x, y, coset_len));
TEST_CHECK(C_KZG_OK == check_proof_multi(&result, &commitment, &proof, &x, y, coset_len, &ks2));
TEST_CHECK(false == result);
free_fft_settings(&fs1);
@ -146,7 +146,7 @@ void commit_to_nil_poly(void) {
// Initialise the (arbitrary) secrets and data structures
generate_trusted_setup(&s1, &s2, &secret, secrets_len);
TEST_CHECK(C_KZG_OK == new_fft_settings(&fs, 4));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, &fs, s1, s2, secrets_len));
TEST_CHECK(C_KZG_OK == new_kzg_settings(&ks, s1, s2, secrets_len, &fs));
init_poly(&a, 0);
commit_to_poly(&result, &a, &ks);